Add kayobe seed vm deprovision command

This command will destroy the seed VM and all associated data volumes.
This commit is contained in:
Mark Goddard 2017-08-02 10:43:48 +00:00
parent f76f244a38
commit c01f5834cb
11 changed files with 138 additions and 5 deletions

View File

@ -1,4 +1,7 @@
--- ---
# State of the VM. May be 'present' or 'absent'.
libvirt_vm_state: present
# Name of the VM. # Name of the VM.
libvirt_vm_name: libvirt_vm_name:

View File

@ -0,0 +1,52 @@
#!/bin/bash
# Copyright (c) 2017 StackHPC Ltd.
#
# 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.
# Ensure that a libvirt volume does not exists.
# On success, output a JSON object with a 'changed' item.
if [[ $# -ne 2 ]]; then
echo "Usage: $0 <name> <pool>"
exit 1
fi
NAME=$1
POOL=$2
# Check whether a volume with this name exists.
output=$(virsh vol-info --pool $POOL --vol $NAME 2>&1)
result=$?
if [[ $result -ne 0 ]]; then
if echo "$output" | grep 'Storage volume not found' >/dev/null 2>&1; then
echo '{"changed": false}'
exit 0
else
echo "Unexpected error while getting volume info"
echo "$output"
exit $result
fi
fi
# Delete the volume.
output=$(virsh vol-delete --pool $POOL --vol $NAME 2>&1)
result=$?
if [[ $result -ne 0 ]]; then
echo "Failed to delete volume"
echo "$output"
exit $result
fi
echo '{"changed": true}'
exit 0

View File

@ -1,3 +1,5 @@
#!/bin/bash
# Copyright (c) 2017 StackHPC Ltd. # Copyright (c) 2017 StackHPC Ltd.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may # Licensed under the Apache License, Version 2.0 (the "License"); you may
@ -12,8 +14,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
#!/bin/bash
# Ensure that a libvirt volume exists, optionally uploading an image. # Ensure that a libvirt volume exists, optionally uploading an image.
# On success, output a JSON object with a 'changed' item. # On success, output a JSON object with a 'changed' item.

View File

@ -0,0 +1,20 @@
---
# The destroyed state does not seem to be idempotent, so check whether the VM
# exists before destroying it.
- name: Check the VM's status
virt:
name: "{{ libvirt_vm_name }}"
command: list_vms
register: result
- block:
- name: Ensure the VM is absent
virt:
name: "{{ libvirt_vm_name }}"
state: destroyed
- name: Ensure the VM is undefined
virt:
name: "{{ libvirt_vm_name }}"
command: undefine
when: libvirt_vm_name in result.list_vms

View File

@ -0,0 +1,11 @@
---
- name: Ensure the VM volumes do not exist
script: >
destroy_virt_volume.sh
{{ item.name }}
{{ item.pool }}
with_items: "{{ libvirt_vm_volumes }}"
register: volume_result
changed_when:
- "{{ volume_result | success }}"
- "{{ (volume_result.stdout | from_json).changed | default(True) }}"

View File

@ -1,3 +1,10 @@
--- ---
- include: volumes.yml - block:
- include: vm.yml - include: volumes.yml
- include: vm.yml
when: libvirt_vm_state == 'present'
- block:
- include: destroy-volumes.yml
- include: destroy-vm.yml
when: libvirt_vm_state == 'absent'

View File

@ -0,0 +1,15 @@
---
- name: Ensure that the seed VM is deprovisioned
hosts: seed-hypervisor
vars:
seed_host: "{{ groups['seed'][0] }}"
seed_hostvars: "{{ hostvars[seed_host] }}"
roles:
- role: libvirt-vm
seed_vm_configdrive_volume:
name: "{{ seed_hostvars.seed_vm_name }}-configdrive"
pool: "{{ seed_hostvars.seed_vm_pool }}"
libvirt_vm_name: "{{ seed_hostvars.seed_vm_name }}"
libvirt_vm_volumes: "{{ seed_hostvars.seed_vm_volumes + [seed_vm_configdrive_volume] }}"
libvirt_vm_state: "absent"
become: True

View File

@ -69,6 +69,17 @@ To deprovision the overcloud::
(kayobe-venv) $ kayobe overcloud deprovision (kayobe-venv) $ kayobe overcloud deprovision
Deprovisioning The Seed VM
==========================
.. note::
This step will destroy the seed VM and its data volumes.
To deprovision the seed VM::
(kayobe-venv) $ kayobe seed vm deprovision
Running Kayobe Playbooks on Demand Running Kayobe Playbooks on Demand
================================== ==================================

View File

@ -241,12 +241,25 @@ class SeedVMProvision(KollaAnsibleMixin, KayobeAnsibleMixin, VaultMixin,
self.app.LOG.debug("Provisioning seed VM") self.app.LOG.debug("Provisioning seed VM")
self.run_kayobe_playbook(parsed_args, "ansible/ip-allocation.yml", self.run_kayobe_playbook(parsed_args, "ansible/ip-allocation.yml",
limit="seed") limit="seed")
self.run_kayobe_playbook(parsed_args, "ansible/seed-vm.yml") self.run_kayobe_playbook(parsed_args, "ansible/seed-vm-provision.yml")
# Now populate the Kolla Ansible inventory. # Now populate the Kolla Ansible inventory.
self.run_kayobe_playbook(parsed_args, "ansible/kolla-ansible.yml", self.run_kayobe_playbook(parsed_args, "ansible/kolla-ansible.yml",
tags="config") tags="config")
class SeedVMDeprovision(KollaAnsibleMixin, KayobeAnsibleMixin, VaultMixin,
Command):
"""Deprovision the seed VM.
This will destroy the seed VM and all associated volumes.
"""
def take_action(self, parsed_args):
self.app.LOG.debug("Deprovisioning seed VM")
self.run_kayobe_playbook(parsed_args,
"ansible/seed-vm-deprovision.yml")
class SeedHostConfigure(KollaAnsibleMixin, KayobeAnsibleMixin, VaultMixin, class SeedHostConfigure(KollaAnsibleMixin, KayobeAnsibleMixin, VaultMixin,
Command): Command):
"""Configure the seed node host OS.""" """Configure the seed node host OS."""

View File

@ -74,6 +74,7 @@ setup(
'seed_host_configure = kayobe.cli.commands:SeedHostConfigure', 'seed_host_configure = kayobe.cli.commands:SeedHostConfigure',
'seed_hypervisor_host_configure = kayobe.cli.commands:SeedHypervisorHostConfigure', 'seed_hypervisor_host_configure = kayobe.cli.commands:SeedHypervisorHostConfigure',
'seed_service_deploy = kayobe.cli.commands:SeedServiceDeploy', 'seed_service_deploy = kayobe.cli.commands:SeedServiceDeploy',
'seed_vm_deprovision = kayobe.cli.commands:SeedVMDeprovision',
'seed_vm_provision = kayobe.cli.commands:SeedVMProvision', 'seed_vm_provision = kayobe.cli.commands:SeedVMProvision',
], ],
}, },