netns_cleanup_util.py

清理无用的网络名字空间。当neutron的agent非正常退出时可以通过该工具来清理环境。

主过程十分简单,第一步是获取可能的无用名字空间,第二步是sleep后清除这些名字空间。

  1. def main():
  2. """Main method for cleaning up network namespaces.
  3. This method will make two passes checking for namespaces to delete. The
  4. process will identify candidates, sleep, and call garbage collect. The
  5. garbage collection will re-verify that the namespace meets the criteria for
  6. deletion (ie it is empty). The period of sleep and the 2nd pass allow
  7. time for the namespace state to settle, so that the check prior deletion
  8. will re-confirm the namespace is empty.
  9. The utility is designed to clean-up after the forced or unexpected
  10. termination of Neutron agents.
  11. The --force flag should only be used as part of the cleanup of a devstack
  12. installation as it will blindly purge namespaces and their devices. This
  13. option also kills any lingering DHCP instances.
  14. """
  15. conf = setup_conf()
  16. conf()
  17. config.setup_logging()
  18. root_helper = agent_config.get_root_helper(conf)
  19. # Identify namespaces that are candidates for deletion.
  20. candidates = [ns for ns in
  21. ip_lib.IPWrapper.get_namespaces(root_helper)
  22. if eligible_for_deletion(conf, ns, conf.force)]
  23. if candidates:
  24. eventlet.sleep(2)
  25. for namespace in candidates:
  26. destroy_namespace(conf, namespace, conf.force)