def main(): """Main method for cleaning up network namespaces. This method will make two passes checking for namespaces to delete. The process will identify candidates, sleep, and call garbage collect. The garbage collection will re-verify that the namespace meets the criteria for deletion (ie it is empty). The period of sleep and the 2nd pass allow time for the namespace state to settle, so that the check prior deletion will re-confirm the namespace is empty. The utility is designed to clean-up after the forced or unexpected termination of Crd agents. The --force flag should only be used as part of the cleanup of a devstack installation as it will blindly purge namespaces and their devices. This option also kills any lingering DHCP instances. """ eventlet.monkey_patch() conf = setup_conf() conf() config.setup_logging(conf) root_helper = agent_config.get_root_helper(conf) # Identify namespaces that are candidates for deletion. candidates = [ns for ns in ip_lib.IPWrapper.get_namespaces(root_helper) if eligible_for_deletion(conf, ns, conf.force)] if candidates: eventlet.sleep(2) for namespace in candidates: destroy_namespace(conf, namespace, conf.force)
def unplug_device(conf, device): try: device.link.delete() except RuntimeError: root_helper = agent_config.get_root_helper(conf) # Maybe the device is OVS port, so try to delete bridge_name = ovs_lib.get_bridge_for_iface(root_helper, device.name) if bridge_name: bridge = ovs_lib.OVSBridge(bridge_name, root_helper) bridge.delete_port(device.name) else: LOG.debug(_('Unable to find bridge for device: %s'), device.name)
def eligible_for_deletion(conf, namespace, force=False): """Determine whether a namespace is eligible for deletion. Eligibility is determined by having only the lo device or if force is passed as a parameter. """ # filter out namespaces without UUID as the name if not re.match(NS_MANGLING_PATTERN, namespace): return False root_helper = agent_config.get_root_helper(conf) ip = ip_lib.IPWrapper(root_helper, namespace) return force or ip.namespace_is_empty()
def kill_dhcp(conf, namespace): """Disable DHCP for a network if DHCP is still active.""" root_helper = agent_config.get_root_helper(conf) network_id = namespace.replace(dhcp.NS_PREFIX, '') dhcp_driver = importutils.import_object( conf.dhcp_driver, conf=conf, network=dhcp.NetModel(conf.use_namespaces, {'id': network_id}), root_helper=root_helper, plugin=FakeDhcpPlugin()) if dhcp_driver.active: dhcp_driver.disable()
def __init__(self, host=None): super(DhcpAgent, self).__init__(host=host) self.needs_resync = False self.conf = cfg.CONF self.cache = NetworkCache() self.root_helper = config.get_root_helper(self.conf) self.dhcp_driver_cls = importutils.import_class(self.conf.dhcp_driver) ctx = context.get_admin_context_without_session() self.plugin_rpc = DhcpPluginApi(topics.PLUGIN, ctx, self.conf.use_namespaces) # create dhcp dir to store dhcp info dhcp_dir = os.path.dirname("/%s/dhcp/" % self.conf.state_path) if not os.path.isdir(dhcp_dir): os.makedirs(dhcp_dir, 0o755) self.dhcp_version = self.dhcp_driver_cls.check_version() self._populate_networks_cache()
def destroy_namespace(conf, namespace, force=False): """Destroy a given namespace. If force is True, then dhcp (if it exists) will be disabled and all devices will be forcibly removed. """ try: root_helper = agent_config.get_root_helper(conf) ip = ip_lib.IPWrapper(root_helper, namespace) if force: kill_dhcp(conf, namespace) # NOTE: The dhcp driver will remove the namespace if is it empty, # so a second check is required here. if ip.netns.exists(namespace): for device in ip.get_devices(exclude_loopback=True): unplug_device(conf, device) ip.garbage_collect_namespace() except Exception: LOG.exception(_('Error unable to destroy namespace: %s'), namespace)
def __init__(self, host, conf=None): if conf: self.conf = conf else: self.conf = cfg.CONF self.root_helper = config.get_root_helper(self.conf) self.router_info = {} self._check_config_params() try: self.driver = importutils.import_object( self.conf.interface_driver, self.conf ) except Exception: msg = _("Error importing interface driver " "'%s'") % self.conf.interface_driver LOG.error(msg) raise SystemExit(msg) self.context = context.get_admin_context_without_session() self.plugin_rpc = L3PluginApi(topics.L3PLUGIN, host) self.fullsync = True self.updated_routers = set() self.removed_routers = set() self.sync_progress = False self._delete_stale_namespaces = (self.conf.use_namespaces and self.conf.router_delete_namespaces) self.rpc_loop = loopingcall.FixedIntervalLoopingCall( self._rpc_loop) self.rpc_loop.start(interval=RPC_LOOP_INTERVAL) super(L3NATAgent, self).__init__(conf=self.conf) self.target_ex_net_id = None
def __init__(self, conf): self.conf = conf self.root_helper = config.get_root_helper(conf)