From 6035092c133b0bb4ee813862a3e2bd6fe44edc1d Mon Sep 17 00:00:00 2001 From: Roman Dobosz Date: Mon, 16 Nov 2015 16:45:21 +0100 Subject: [PATCH] Inital import --- README.rst | 52 +++++++++++++++++++++++++++++++++++++++++++++ filter_vm_action.py | 45 +++++++++++++++++++++++++++++++++++++++ host-evacuate.yaml | 30 ++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 README.rst create mode 100644 filter_vm_action.py create mode 100644 host-evacuate.yaml diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..b854e1e --- /dev/null +++ b/README.rst @@ -0,0 +1,52 @@ +Mistral evacuate plugin +======================= + +This is a PoC for providing automatic evacuation for VMs in OpenStack cloud +using Mistral. + +Installation +------------ + +#. Copy ``filter_vm.py`` to the place reachable by python interpreter - see + ``PYTHONPATH`` or ``sys.path`` for reference. +#. Append line + + .. code:: ini + + [entry_points] + mistral.actions = + … + custom.filter_vms = evac_poc:FilterAndEvacuate + + to ``setup.cfg`` file under Mistral repository +#. Reinstall Mistral if it was installed in system (not in virtualenv). +#. Run db-sync tool via either + + .. code:: shell-session + + $ tools/sync_db.sh --config-file + + or + + .. code:: shell-session + + $ mistral-db-manage --config-file populate + +#. Register Mistral workflow: + + .. code:: shell-session + + $ mistral workflow-create evacuate-workflow.yaml + +#. Create JSON file with content similar to: + + .. code:: json + + { + "search_opts": { + "host": "compute-hostanme" + } + } + +#. Trigger the action via: + TBD diff --git a/filter_vm_action.py b/filter_vm_action.py new file mode 100644 index 0000000..d2662c9 --- /dev/null +++ b/filter_vm_action.py @@ -0,0 +1,45 @@ +""" +FilterVmAction - custom action. + +Simple action for filtering VM on the presence of metadata/extra spec +"evacuate" flag +""" +from mistral.actions.openstack.actions import NovaAction +from mistral.workflow.utils import Result + + +class FilterVmAction(NovaAction): + """ + Filter and return VMs whith the flag 'evacuate' either on vm metadtata + or flavor extra spec. + """ + + def __init__(self, metadata, flavor, uuid): + """init.""" + self._metadata = metadata + self._flavor = flavor + self._uuid = uuid + + def run(self): + """Entry point for the action execution.""" + client = self._get_client() + metadata = self._metadata + + if str(metadata.get('evacuate')).upper() == 'TRUE': + return Result(data={'status': 0, 'uuid': self._uuid}) + elif str(metadata.get('evacuate')).upper() == 'FALSE': + return Result(error='sie nie udalo!!!!1111jeden') + + # ether is no metadata for vm - check flavor + flavors = client.flavors.list() + try: + flavor = filter(lambda x: x.id == self._flavor, flavors)[0] + except: + raise Exception('Flavor not found') + + evacuate = flavor.get_keys().get('evacuation:evacuate') + + if str(evacuate).upper() == 'TRUE': + return Result(data={'status': 0, 'uuid': self._uuid}) + + return Result(data='1', error=True) diff --git a/host-evacuate.yaml b/host-evacuate.yaml new file mode 100644 index 0000000..eb5c938 --- /dev/null +++ b/host-evacuate.yaml @@ -0,0 +1,30 @@ +--- +version: '2.0' + +host-evacuate: + description: Evacuate VMs from given host + type: direct + + input: + - search_opts + + output: + vm_list: <% $.list_vms %> + + tasks: + list_vms: + action: nova.servers_list search_opts=<% $.search_opts %> + publish: + vms: <% $.list_vms %> + on-success: filter_vms + + filter_vms: + with-items: vm in <% $.vms %> + action: custom.filter_vms flavor=<% $.vm.flavor.id %> metadata=<% $.vm.metadata %> uuid=<% $.vm.id %> + publish: + filtered_vms: <% $.filter_vms.uuid %> + on-success: evacuate_vms + + evacuate_vms: + with-items: vm_uuid in <% $.filtered_vms %> + action: nova.servers_evacuate server=<% $.vm_uuid %> on_shared_storage=false