From 21c77b27d26bc1c98ebd59e705ba26831456f393 Mon Sep 17 00:00:00 2001 From: Nolan Brubaker Date: Mon, 24 Oct 2016 11:44:55 -0400 Subject: [PATCH] Add command to remove IPs from inventory This change adds support for removing container IP information from inventory. This might be useful in situations where an environment is deployed, but network information has changed, or there was a mistake in subnetting (such as an expanded used_ips range). Updating configs, such as LXC interface files and HAProxy or other load balancers is not handled via this command. Change-Id: Ibad9d587e14c41ecd486ed8668f9f815e348a94a --- doc/source/developer-docs/inventory.rst | 21 +++++++++++- lib/manage.py | 33 +++++++++++++++++++ tests/test_manage.py | 43 +++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) diff --git a/doc/source/developer-docs/inventory.rst b/doc/source/developer-docs/inventory.rst index 634ea3369c..cfa8df9946 100644 --- a/doc/source/developer-docs/inventory.rst +++ b/doc/source/developer-docs/inventory.rst @@ -147,7 +147,13 @@ Inspecting and Managing the Inventory The file ``scripts/inventory-manage.py`` is used to produce human readable output based on the ``/etc/openstack_deploy/openstack_inventory.json`` file. -The same script can be used to safely remove hosts from the inventory. +The same script can be used to safely remove hosts from the inventory, export +the inventory based on hosts, and clear IP addresses from containers within +the inventory files. + +Operations taken by this script only affect the +``/etc/opentstack_deploy/openstack_inventory.json`` file; any new or removed +information must be set by running playbooks. Viewing the Inventory ^^^^^^^^^^^^^^^^^^^^^ @@ -184,6 +190,19 @@ This JSON output has two top-level keys: ``hosts`` and ``all``. ``all`` contains global network information such as the load balancer IPs and provider network metadata. +Clearing Existing Container IP Addresses +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The ``--clear-ips`` parameter can be used to remove all container IP address +information from the ``openstack_inventory.json`` file. Baremetal hosts will +not be changed. + +This will *not* change the LXC configuration until the associated playbooks +are run and the containers restarted, which will result in API downtime. + +Any changes to the containers must also be reflected in the deployment's load +balancer. + The lxc_hosts Group ------------------- diff --git a/lib/manage.py b/lib/manage.py index f996cb2db3..6859c64a27 100644 --- a/lib/manage.py +++ b/lib/manage.py @@ -120,6 +120,15 @@ def args(): default=False ) + exclusive_action.add_argument( + '--clear-ips', + help=('Clears IPs from the existing inventory, but leaves ', + 'all other information intact. LXC interface files and ' + 'load balancers will *not* be modified.'), + action='store_true', + default=False + ) + return vars(parser.parse_args()) @@ -305,6 +314,25 @@ def export_host_info(inventory): return export_info +def remove_ip_addresses(inventory): + """Removes container IP address information from the inventory dictionary + + All container_networks information for containers will be deleted. + """ + hostvars = inventory['_meta']['hostvars'] + + for host, variables in hostvars.items(): + if variables.get('is_metal', False): + continue + + ip_vars = ['container_networks', 'container_address', + 'ansible_host', 'ansible_ssh_host'] + + # Don't raise a KeyError if the entries have already been removed. + for ip_var in ip_vars: + variables.pop(ip_var, None) + + def main(): """Run the main application.""" # Parse user args @@ -329,6 +357,11 @@ def main(): print(print_containers_per_group(inventory)) elif user_args['export'] is True: print(json.dumps(export_host_info(inventory), indent=2)) + elif user_args['clear_ips'] is True: + remove_ip_addresses(inventory) + with open(environment_file, 'wb') as f_handle: + f_handle.write(json.dumps(inventory, indent=2)) + print('Success. . .') else: recursive_dict_removal(inventory, user_args['remove_item']) with open(environment_file, 'wb') as f_handle: diff --git a/tests/test_manage.py b/tests/test_manage.py index d5ffabaaef..8a0eb5f81b 100644 --- a/tests/test_manage.py +++ b/tests/test_manage.py @@ -57,5 +57,48 @@ class TestExportFunction(unittest.TestCase): all_info['internal_lb_vip_address']) +class TestRemoveIpfunction(unittest.TestCase): + def setUp(self): + self.inv = test_inventory.get_inventory() + + def tearDown(self): + test_inventory.cleanup() + + def test_ips_removed(self): + mi.remove_ip_addresses(self.inv) + hostvars = self.inv['_meta']['hostvars'] + + for host, variables in hostvars.items(): + has_networks = 'container_networks' in variables + if variables.get('is_metal', False): + continue + self.assertFalse(has_networks) + + def test_metal_ips_kept(self): + mi.remove_ip_addresses(self.inv) + hostvars = self.inv['_meta']['hostvars'] + + for host, variables in hostvars.items(): + has_networks = 'container_networks' in variables + if not variables.get('is_metal', False): + continue + self.assertTrue(has_networks) + + def test_ansible_host_vars_removed(self): + mi.remove_ip_addresses(self.inv) + hostvars = self.inv['_meta']['hostvars'] + + for host, variables in hostvars.items(): + has_host = 'ansible_host' in variables + if variables.get('is_metal', False): + continue + self.assertFalse(has_host) + + def test_multiple_calls(self): + """Removal should fail silently if keys are absent.""" + mi.remove_ip_addresses(self.inv) + mi.remove_ip_addresses(self.inv) + + if __name__ == '__main__': unittest.main()