/usr/lib/python2.7/dist-packages/neutron_lbaas/agent/agent_manager.py is in python-neutron-lbaas 2:8.0.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | # Copyright 2013 New Dream Network, LLC (DreamHost)
# Copyright 2015 Rackspace
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from neutron.agent import rpc as agent_rpc
from neutron import context as ncontext
from neutron.plugins.common import constants
from neutron.services import provider_configuration as provconfig
from neutron_lib import exceptions as n_exc
from oslo_config import cfg
from oslo_log import log as logging
import oslo_messaging
from oslo_service import loopingcall
from oslo_service import periodic_task
from oslo_utils import importutils
from neutron_lbaas._i18n import _, _LE, _LI
from neutron_lbaas.agent import agent_api
from neutron_lbaas.drivers.common import agent_driver_base
from neutron_lbaas.services.loadbalancer import constants as lb_const
from neutron_lbaas.services.loadbalancer import data_models
LOG = logging.getLogger(__name__)
DEVICE_DRIVERS = 'device_drivers'
OPTS = [
cfg.MultiStrOpt(
'device_driver',
default=['neutron_lbaas.drivers.haproxy.'
'namespace_driver.HaproxyNSDriver'],
help=_('Drivers used to manage loadbalancing devices'),
),
]
class DeviceNotFoundOnAgent(n_exc.NotFound):
message = _('Unknown device with loadbalancer_id %(loadbalancer_id)s')
class LbaasAgentManager(periodic_task.PeriodicTasks):
# history
# 1.0 Initial version
target = oslo_messaging.Target(version='1.0')
def __init__(self, conf):
super(LbaasAgentManager, self).__init__(conf)
self.conf = conf
self.context = ncontext.get_admin_context_without_session()
self.serializer = agent_driver_base.DataModelSerializer()
self.plugin_rpc = agent_api.LbaasAgentApi(
lb_const.LOADBALANCER_PLUGINV2,
self.context,
self.conf.host
)
self._load_drivers()
self.agent_state = {
'binary': 'neutron-lbaasv2-agent',
'host': conf.host,
'topic': lb_const.LOADBALANCER_AGENTV2,
'configurations': {'device_drivers': self.device_drivers.keys()},
'agent_type': lb_const.AGENT_TYPE_LOADBALANCERV2,
'start_flag': True}
self.admin_state_up = True
self._setup_state_rpc()
self.needs_resync = False
# pool_id->device_driver_name mapping used to store known instances
self.instance_mapping = {}
def _load_drivers(self):
self.device_drivers = {}
for driver in self.conf.device_driver:
driver = provconfig.get_provider_driver_class(driver,
DEVICE_DRIVERS)
try:
driver_inst = importutils.import_object(
driver,
self.conf,
self.plugin_rpc
)
except ImportError:
msg = _('Error importing loadbalancer device driver: %s')
raise SystemExit(msg % driver)
driver_name = driver_inst.get_name()
if driver_name not in self.device_drivers:
self.device_drivers[driver_name] = driver_inst
else:
msg = _('Multiple device drivers with the same name found: %s')
raise SystemExit(msg % driver_name)
def _setup_state_rpc(self):
self.state_rpc = agent_rpc.PluginReportStateAPI(
lb_const.LOADBALANCER_PLUGINV2)
report_interval = self.conf.AGENT.report_interval
if report_interval:
heartbeat = loopingcall.FixedIntervalLoopingCall(
self._report_state)
heartbeat.start(interval=report_interval)
def _report_state(self):
try:
instance_count = len(self.instance_mapping)
self.agent_state['configurations']['instances'] = instance_count
self.state_rpc.report_state(self.context, self.agent_state)
self.agent_state.pop('start_flag', None)
except Exception:
LOG.exception(_LE("Failed reporting state!"))
def initialize_service_hook(self, started_by):
self.sync_state()
@periodic_task.periodic_task
def periodic_resync(self, context):
if self.needs_resync:
self.needs_resync = False
self.sync_state()
@periodic_task.periodic_task(spacing=6)
def collect_stats(self, context):
for loadbalancer_id, driver_name in self.instance_mapping.items():
driver = self.device_drivers[driver_name]
try:
stats = driver.loadbalancer.get_stats(loadbalancer_id)
if stats:
self.plugin_rpc.update_loadbalancer_stats(
loadbalancer_id, stats)
except Exception:
LOG.exception(_LE('Error updating statistics on loadbalancer'
' %s'),
loadbalancer_id)
self.needs_resync = True
def sync_state(self):
known_instances = set(self.instance_mapping.keys())
try:
ready_instances = set(self.plugin_rpc.get_ready_devices())
for deleted_id in known_instances - ready_instances:
self._destroy_loadbalancer(deleted_id)
for loadbalancer_id in ready_instances:
self._reload_loadbalancer(loadbalancer_id)
except Exception:
LOG.exception(_LE('Unable to retrieve ready devices'))
self.needs_resync = True
self.remove_orphans()
def _get_driver(self, loadbalancer_id):
if loadbalancer_id not in self.instance_mapping:
raise DeviceNotFoundOnAgent(loadbalancer_id=loadbalancer_id)
driver_name = self.instance_mapping[loadbalancer_id]
return self.device_drivers[driver_name]
def _reload_loadbalancer(self, loadbalancer_id):
try:
loadbalancer_dict = self.plugin_rpc.get_loadbalancer(
loadbalancer_id)
loadbalancer = data_models.LoadBalancer.from_dict(
loadbalancer_dict)
driver_name = loadbalancer.provider.device_driver
if driver_name not in self.device_drivers:
LOG.error(_LE('No device driver on agent: %s.'), driver_name)
self.plugin_rpc.update_status(
'loadbalancer', loadbalancer_id, constants.ERROR)
return
self.device_drivers[driver_name].deploy_instance(loadbalancer)
self.instance_mapping[loadbalancer_id] = driver_name
self.plugin_rpc.loadbalancer_deployed(loadbalancer_id)
except Exception:
LOG.exception(_LE('Unable to deploy instance for '
'loadbalancer: %s'),
loadbalancer_id)
self.needs_resync = True
def _destroy_loadbalancer(self, lb_id):
driver = self._get_driver(lb_id)
try:
driver.undeploy_instance(lb_id, delete_namespace=True)
del self.instance_mapping[lb_id]
self.plugin_rpc.loadbalancer_destroyed(lb_id)
except Exception:
LOG.exception(_LE('Unable to destroy device for loadbalancer: %s'),
lb_id)
self.needs_resync = True
def remove_orphans(self):
for driver_name in self.device_drivers:
lb_ids = [lb_id for lb_id in self.instance_mapping
if self.instance_mapping[lb_id] == driver_name]
try:
self.device_drivers[driver_name].remove_orphans(lb_ids)
except NotImplementedError:
pass # Not all drivers will support this
def _handle_failed_driver_call(self, operation, obj, driver):
obj_type = obj.__class__.__name__.lower()
LOG.exception(_LE('%(operation)s %(obj)s %(id)s failed on device '
'driver %(driver)s'),
{'operation': operation.capitalize(), 'obj': obj_type,
'id': obj.id, 'driver': driver})
self._update_statuses(obj, error=True)
def agent_updated(self, context, payload):
"""Handle the agent_updated notification event."""
if payload['admin_state_up'] != self.admin_state_up:
self.admin_state_up = payload['admin_state_up']
if self.admin_state_up:
self.needs_resync = True
else:
for loadbalancer_id in self.instance_mapping.keys():
LOG.info(_LI("Destroying loadbalancer %s due to agent "
"disabling"), loadbalancer_id)
self._destroy_loadbalancer(loadbalancer_id)
LOG.info(_LI("Agent_updated by server side %s!"), payload)
def _update_statuses(self, obj, error=False):
lb_p_status = constants.ACTIVE
lb_o_status = None
obj_type = obj.__class__.__name__.lower()
obj_p_status = constants.ACTIVE
obj_o_status = lb_const.ONLINE
if error:
obj_p_status = constants.ERROR
obj_o_status = lb_const.OFFLINE
if isinstance(obj, data_models.HealthMonitor):
obj_o_status = None
if isinstance(obj, data_models.LoadBalancer):
lb_o_status = lb_const.ONLINE
if error:
lb_p_status = constants.ERROR
lb_o_status = lb_const.OFFLINE
lb = obj
else:
lb = obj.root_loadbalancer
self.plugin_rpc.update_status(obj_type, obj.id,
provisioning_status=obj_p_status,
operating_status=obj_o_status)
self.plugin_rpc.update_status('loadbalancer', lb.id,
provisioning_status=lb_p_status,
operating_status=lb_o_status)
def create_loadbalancer(self, context, loadbalancer, driver_name):
loadbalancer = data_models.LoadBalancer.from_dict(loadbalancer)
if driver_name not in self.device_drivers:
LOG.error(_LE('No device driver on agent: %s.'), driver_name)
self.plugin_rpc.update_status('loadbalancer', loadbalancer.id,
provisioning_status=constants.ERROR)
return
driver = self.device_drivers[driver_name]
try:
driver.loadbalancer.create(loadbalancer)
except Exception:
self._handle_failed_driver_call('create', loadbalancer,
driver.get_name())
else:
self.instance_mapping[loadbalancer.id] = driver_name
self._update_statuses(loadbalancer)
def update_loadbalancer(self, context, old_loadbalancer, loadbalancer):
loadbalancer = data_models.LoadBalancer.from_dict(loadbalancer)
old_loadbalancer = data_models.LoadBalancer.from_dict(old_loadbalancer)
driver = self._get_driver(loadbalancer.id)
try:
driver.loadbalancer.update(old_loadbalancer, loadbalancer)
except Exception:
self._handle_failed_driver_call('update', loadbalancer,
driver.get_name())
else:
self._update_statuses(loadbalancer)
def delete_loadbalancer(self, context, loadbalancer):
loadbalancer = data_models.LoadBalancer.from_dict(loadbalancer)
driver = self._get_driver(loadbalancer.id)
driver.loadbalancer.delete(loadbalancer)
del self.instance_mapping[loadbalancer.id]
def create_listener(self, context, listener):
listener = data_models.Listener.from_dict(listener)
driver = self._get_driver(listener.loadbalancer.id)
try:
driver.listener.create(listener)
except Exception:
self._handle_failed_driver_call('create', listener,
driver.get_name())
else:
self._update_statuses(listener)
def update_listener(self, context, old_listener, listener):
listener = data_models.Listener.from_dict(listener)
old_listener = data_models.Listener.from_dict(old_listener)
driver = self._get_driver(listener.loadbalancer.id)
try:
driver.listener.update(old_listener, listener)
except Exception:
self._handle_failed_driver_call('update', listener,
driver.get_name())
else:
self._update_statuses(listener)
def delete_listener(self, context, listener):
listener = data_models.Listener.from_dict(listener)
driver = self._get_driver(listener.loadbalancer.id)
driver.listener.delete(listener)
def create_pool(self, context, pool):
pool = data_models.Pool.from_dict(pool)
driver = self._get_driver(pool.loadbalancer.id)
try:
driver.pool.create(pool)
except Exception:
self._handle_failed_driver_call('create', pool, driver.get_name())
else:
self._update_statuses(pool)
def update_pool(self, context, old_pool, pool):
pool = data_models.Pool.from_dict(pool)
old_pool = data_models.Pool.from_dict(old_pool)
driver = self._get_driver(pool.loadbalancer.id)
try:
driver.pool.update(old_pool, pool)
except Exception:
self._handle_failed_driver_call('create', pool, driver.get_name())
else:
self._update_statuses(pool)
def delete_pool(self, context, pool):
pool = data_models.Pool.from_dict(pool)
driver = self._get_driver(pool.loadbalancer.id)
driver.pool.delete(pool)
def create_member(self, context, member):
member = data_models.Member.from_dict(member)
driver = self._get_driver(member.pool.loadbalancer.id)
try:
driver.member.create(member)
except Exception:
self._handle_failed_driver_call('create', member,
driver.get_name())
else:
self._update_statuses(member)
def update_member(self, context, old_member, member):
member = data_models.Member.from_dict(member)
old_member = data_models.Member.from_dict(old_member)
driver = self._get_driver(member.pool.loadbalancer.id)
try:
driver.member.update(old_member, member)
except Exception:
self._handle_failed_driver_call('create', member,
driver.get_name())
else:
self._update_statuses(member)
def delete_member(self, context, member):
member = data_models.Member.from_dict(member)
driver = self._get_driver(member.pool.loadbalancer.id)
driver.member.delete(member)
def create_healthmonitor(self, context, healthmonitor):
healthmonitor = data_models.HealthMonitor.from_dict(healthmonitor)
driver = self._get_driver(healthmonitor.pool.loadbalancer.id)
try:
driver.healthmonitor.create(healthmonitor)
except Exception:
self._handle_failed_driver_call('create', healthmonitor,
driver.get_name())
else:
self._update_statuses(healthmonitor)
def update_healthmonitor(self, context, old_healthmonitor,
healthmonitor):
healthmonitor = data_models.HealthMonitor.from_dict(healthmonitor)
old_healthmonitor = data_models.HealthMonitor.from_dict(
old_healthmonitor)
driver = self._get_driver(healthmonitor.pool.loadbalancer.id)
try:
driver.healthmonitor.update(old_healthmonitor, healthmonitor)
except Exception:
self._handle_failed_driver_call('create', healthmonitor,
driver.get_name())
else:
self._update_statuses(healthmonitor)
def delete_healthmonitor(self, context, healthmonitor):
healthmonitor = data_models.HealthMonitor.from_dict(healthmonitor)
driver = self._get_driver(healthmonitor.pool.loadbalancer.id)
driver.healthmonitor.delete(healthmonitor)
|