From 05dfcc8199d0eea448d7ea810f9f152b49ed202b Mon Sep 17 00:00:00 2001 From: Will Szumski Date: Thu, 7 Jun 2018 11:50:38 +0100 Subject: [PATCH] Adds new command to rename baremetal compute nodes Story: 2002176 Task: 20042 Change-Id: I2174a4719aaff63fff24e35ce62eab57b369b457 --- ansible/baremetal-compute-rename.yml | 58 +++++++++++++++++++ doc/source/administration.rst | 12 ++++ kayobe/cli/commands.py | 9 +++ kayobe/tests/unit/cli/test_commands.py | 18 ++++++ ...utomatic-node-rename-fc5fbe0f7c38151c.yaml | 5 ++ setup.cfg | 1 + 6 files changed, 103 insertions(+) create mode 100644 ansible/baremetal-compute-rename.yml create mode 100644 releasenotes/notes/feature-automatic-node-rename-fc5fbe0f7c38151c.yaml diff --git a/ansible/baremetal-compute-rename.yml b/ansible/baremetal-compute-rename.yml new file mode 100644 index 000000000..5fdc4958e --- /dev/null +++ b/ansible/baremetal-compute-rename.yml @@ -0,0 +1,58 @@ +--- +# This playbook will ensure that all baremetal compute nodes are named after +# their inventory host names. It matches the ipmi address in the inventory to +# the one gathered from inspection + +- name: Rename baremetal compute nodes + hosts: controllers[0] + gather_facts: False + vars: + venv: "{{ virtualenv_path }}/openstack-cli" + pre_tasks: + - name: Set up openstack cli virtualenv + pip: + virtualenv: "{{ venv }}" + name: + - python-openstackclient + - python-ironicclient + +- name: Rename baremetal compute nodes + hosts: baremetal-compute + gather_facts: False + vars: + venv: "{{ virtualenv_path }}/openstack-cli" + controller_host: "{{ groups['controllers'][0] }}" + tasks: + - name: Fail if ipmi host variable not set + vars: + ipmi_address: "{{ hostvars[inventory_hostname].ipmi_address }}" + fail: + msg: > + The host variable, ipmi_address is not defined for {{ inventory_hostname }}. This variable is required + to run this playbook. + when: ipmi_address is not defined or not ipmi_address + - name: Get list of nodes + command: > + {{ venv }}/bin/openstack baremetal node list -f json --fields uuid name driver_info + register: nodes + delegate_to: "{{ controller_host }}" + environment: "{{ openstack_auth_env }}" + run_once: true + changed_when: false + vars: + # NOTE: Without this, the controller's ansible_host variable will not + # be respected when using delegate_to. + ansible_host: "{{ hostvars[controller_host].ansible_host | default(controller_host) }}" + + - name: Rename baremetal compute nodes + command: > + {{ venv }}/bin/openstack baremetal node set --name "{{ inventory_hostname }}" "{{ node['UUID'] }}" + delegate_to: "{{ controller_host }}" + environment: "{{ openstack_auth_env }}" + vars: + # NOTE: Without this, the controller's ansible_host variable will not + # be respected when using delegate_to. + ansible_host: "{{ hostvars[controller_host].ansible_host | default(controller_host) }}" + ipmi_address: "{{ hostvars[inventory_hostname].ipmi_address }}" + node: "{{ (nodes.stdout | from_json) | selectattr('Driver Info.ipmi_address', 'equalto', ipmi_address) | first }}" + when: node['Name'] != inventory_hostname diff --git a/doc/source/administration.rst b/doc/source/administration.rst index 461931406..436a91b64 100644 --- a/doc/source/administration.rst +++ b/doc/source/administration.rst @@ -169,6 +169,18 @@ compute nodes:: (kayobe) $ kayobe baremetal compute inspect +Rename +------ + +Once nodes have been discovered, it is helpful to associate them with a name +to make them easier to work with. If you would like the nodes to be named +according to their inventory host names, you can run the following command: + + (kayobe) $ kayobe baremetal compute rename + +This command will use the ``ipmi_address`` host variable from the inventory +to map the inventory host name to the correct node. + Running Kayobe Playbooks on Demand ================================== diff --git a/kayobe/cli/commands.py b/kayobe/cli/commands.py index d2c766a7b..c1515bd6e 100644 --- a/kayobe/cli/commands.py +++ b/kayobe/cli/commands.py @@ -1137,3 +1137,12 @@ class BaremetalComputeProvide(KayobeAnsibleMixin, VaultMixin, Command): self.app.LOG.debug("Making baremetal compute nodes available") playbooks = _build_playbook_list("baremetal-compute-provide") self.run_kayobe_playbooks(parsed_args, playbooks) + + +class BaremetalComputeRename(KayobeAnsibleMixin, VaultMixin, Command): + """Rename baremetal compute nodes to match inventory hostname""" + + def take_action(self, parsed_args): + self.app.LOG.debug("Renaming baremetal compute nodes") + playbooks = _build_playbook_list("baremetal-compute-rename") + self.run_kayobe_playbooks(parsed_args, playbooks) diff --git a/kayobe/tests/unit/cli/test_commands.py b/kayobe/tests/unit/cli/test_commands.py index 1dde429ea..d3ee8350d 100644 --- a/kayobe/tests/unit/cli/test_commands.py +++ b/kayobe/tests/unit/cli/test_commands.py @@ -749,3 +749,21 @@ class TestCase(unittest.TestCase): ), ] self.assertEqual(expected_calls, mock_run.call_args_list) + + @mock.patch.object(commands.KayobeAnsibleMixin, + "run_kayobe_playbooks") + def test_baremetal_compute_rename(self, mock_run): + command = commands.BaremetalComputeRename(TestApp(), []) + parser = command.get_parser("test") + parsed_args = parser.parse_args([]) + result = command.run(parsed_args) + self.assertEqual(0, result) + expected_calls = [ + mock.call( + mock.ANY, + [ + "ansible/baremetal-compute-rename.yml", + ], + ), + ] + self.assertEqual(expected_calls, mock_run.call_args_list) diff --git a/releasenotes/notes/feature-automatic-node-rename-fc5fbe0f7c38151c.yaml b/releasenotes/notes/feature-automatic-node-rename-fc5fbe0f7c38151c.yaml new file mode 100644 index 000000000..9a9a416c4 --- /dev/null +++ b/releasenotes/notes/feature-automatic-node-rename-fc5fbe0f7c38151c.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Adds a command to rename baremetal compute nodes to match + their inventory host name - ``kayobe baremetal compute rename`` diff --git a/setup.cfg b/setup.cfg index 7de5acaff..fe627c993 100644 --- a/setup.cfg +++ b/setup.cfg @@ -32,6 +32,7 @@ kayobe.cli= baremetal_compute_inspect = kayobe.cli.commands:BaremetalComputeInspect baremetal_compute_manage = kayobe.cli.commands:BaremetalComputeManage baremetal_compute_provide = kayobe.cli.commands:BaremetalComputeProvide + baremetal_compute_rename = kayobe.cli.commands:BaremetalComputeRename control_host_bootstrap = kayobe.cli.commands:ControlHostBootstrap control_host_upgrade = kayobe.cli.commands:ControlHostUpgrade configuration_dump = kayobe.cli.commands:ConfigurationDump