Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions roles/nova_compute_drain/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---

nova_compute_drain_venv: "{{ virtualenv_path }}/openstack"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

virtualenv_path is defined by kayobe. Should I add some default here? Something in the home directory maybe?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that would be a better default

nova_compute_drain_delegate_host: "{{ groups['controllers'][0] }}"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default doesn't make sense outside kayobe

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

localhost as a reasonable default?

40 changes: 40 additions & 0 deletions roles/nova_compute_drain/tasks/cold-migrate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---

- block:
- name: "Cold migrate instance: {{ instance_uuid }}" # noqa no-changed-when

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add changed_when: true, remove noqa

command: >
{{ nova_compute_drain_venv }}/bin/openstack
--os-compute-api-version 2.25
server migrate
{{ instance_uuid }}
--wait
register: result

- name: "Wait for VERIFY_RESIZE: {{ instance_uuid }}"
command: >
{{ nova_compute_drain_venv }}/bin/openstack server show {{ instance_uuid }} -f value -c status
register: result
until: result.stdout == 'VERIFY_RESIZE' or result.stdout == 'SHUTOFF'
changed_when: false
retries: 10
delay: 30

- name: "Confirm resize: {{ instance_uuid }}" # noqa no-changed-when

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add changed_when: true, remove noqa

command: >
{{ nova_compute_drain_venv }}/bin/openstack server migrate confirm {{ instance_uuid }}
when: result.stdout == 'VERIFY_RESIZE'

- name: "Wait for SHUTOFF: {{ instance_uuid }}"
command: >
{{ nova_compute_drain_venv }}/bin/openstack server show {{ instance_uuid }} -f value -c status
register: result
until: result.stdout == 'SHUTOFF'
retries: 10
delay: 30
changed_when: false
environment: "{{ openstack_auth_env }}"
delegate_to: "{{ nova_compute_drain_delegate_host }}"
vars:
ansible_host: "{{ hostvars[nova_compute_drain_delegate_host].ansible_host }}"
rescue:
- meta: noop # noqa unnamed-task

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use a comment to explain why we're ignoring errors here.

17 changes: 17 additions & 0 deletions roles/nova_compute_drain/tasks/instance-info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
- name: Query instances
command: >
{{ nova_compute_drain_venv }}/bin/openstack
server list --host {{ ansible_facts.nodename }}
--all-projects
--format json
register: instances

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
register: instances
register: nova_compute_drain_instances

delegate_to: "{{ nova_compute_drain_delegate_host }}"
environment: "{{ openstack_auth_env }}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
environment: "{{ openstack_auth_env }}"
environment: "{{ nova_compute_drain_openstack_auth_env }}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and add to defaults

changed_when: false
vars:
ansible_host: "{{ hostvars[nova_compute_drain_delegate_host].ansible_host }}"

- name: Set fact containing list of instances
set_fact:
nova_compute_drain_instance_info: "{{ instances.stdout | from_json }}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
nova_compute_drain_instance_info: "{{ instances.stdout | from_json }}"
nova_compute_drain_instance_info: "{{ nova_compute_drain_instances.stdout | from_json }}"

17 changes: 17 additions & 0 deletions roles/nova_compute_drain/tasks/live-migrate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to ignore errors here, or is it an async API that does not generally fail?

- name: "Live migrate instance: {{ instance_uuid }}" # noqa no-changed-when

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add changed_when: true, remove noqa

command: >
{{ nova_compute_drain_venv }}/bin/openstack
--os-compute-api-version 2.25
server migrate
{{ instance_uuid }}
--live-migration
--wait
delegate_to: "{{ nova_compute_drain_delegate_host }}"
environment: "{{ openstack_auth_env }}"
vars:
ansible_host: "{{ hostvars[nova_compute_drain_delegate_host].ansible_host }}"
register: result
failed_when:
- result is failed
28 changes: 28 additions & 0 deletions roles/nova_compute_drain/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
- include_tasks: setup.yml

- include_tasks: instance-info.yml

- include_tasks: live-migrate.yml
loop: "{{ nova_compute_drain_instance_info | selectattr('Status', 'equalto', 'ACTIVE') | list }}"
loop_control:
label: "{{ item.ID | default }}"
vars:
instance_uuid: "{{ item.ID | default }}"

- include_tasks: cold-migrate.yml
loop: "{{ nova_compute_drain_instance_info | selectattr('Status', 'equalto', 'SHUTOFF') | list }}"
loop_control:
label: "{{ item.ID | default }}"
vars:
instance_uuid: "{{ item.ID | default }}"

- include_tasks: instance-info.yml

- name: Fail if there are instances still on the host

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What can we do about instances in states other than ACTIVE or SHUTOFF?

fail:
msg: >
Instances still on {{ inventory_hostname }}: {{ instances.stdout | from_json }}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Instances still on {{ inventory_hostname }}: {{ instances.stdout | from_json }}
Instances still on {{ inventory_hostname }}: {{ nova_compute_drain_instance_info }}

when:
- nova_compute_migration_fatal | bool

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add to defaults

- nova_compute_drain_instance_info | length > 0
27 changes: 27 additions & 0 deletions roles/nova_compute_drain/tasks/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---

- name: Initiate openstack cli virtualenv # noqa package-latest
pip:
virtualenv: "{{ nova_compute_drain_venv }}"
name:
- pip
- setuptools
state: latest
virtualenv_command: /usr/bin/python3.6 -m venv

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
virtualenv_command: /usr/bin/python3.6 -m venv
virtualenv_command: /usr/bin/python3 -m venv

delegate_to: "{{ nova_compute_drain_delegate_host }}"
vars:
# NOTE: Without this, the delegate ansible_host variable will not
# be respected when using delegate_to.
ansible_host: "{{ hostvars[nova_compute_drain_delegate_host].ansible_host | default(nova_compute_drain_delegate_host) }}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run_once: true?


- name: Install openstack CLI tools in virtualenv
pip:
virtualenv: "{{ nova_compute_drain_venv }}"
name:
- python-openstackclient
extra_args: "{% if pip_upper_constraints_file %}-c {{ pip_upper_constraints_file }}{% endif %}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
extra_args: "{% if pip_upper_constraints_file %}-c {{ pip_upper_constraints_file }}{% endif %}"
extra_args: "{% if nova_compute_drain_upper_constraints_file %}-c {{ nova_compute_drain_upper_constraints_file }}{% endif %}"

And add to defaults?

delegate_to: "{{ nova_compute_drain_delegate_host }}"
vars:
# NOTE: Without this, the delegate's ansible_host variable will not
# be respected when using delegate_to.
ansible_host: "{{ hostvars[nova_compute_drain_delegate_host].ansible_host | default(nova_compute_drain_delegate_host) }}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run_once: true?