Jesse Pretorius 993bac94f5 MNAIO: Extend image saving to include manifest
In order to more successfully reproduce an environment using
saved images, we include the VM XML definition files and the
output from 'pip freeze'. We capture the list of files, their
checksums and the SHA for the git repo into a json manifest
file.

Change-Id: Ia0bf74d509b4acb10b0dd832a4cfe1bb2afb2503
2018-08-15 19:25:12 +01:00

110 lines
4.0 KiB
YAML

---
# Copyright 2018, Rackspace US, Inc.
#
# 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 witing, 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.
- name: Save VM disk images for re-use
hosts: vm_hosts
gather_facts: "{{ gather_facts | default(true) }}"
environment: "{{ deployment_environment_variables | default({}) }}"
tags:
- save-vms
tasks:
- name: Gather variables for each operating system
include_vars: "{{ item }}"
with_first_found:
- "{{ playbook_dir }}/vars/{{ ansible_distribution | lower }}-{{ ansible_distribution_version | lower }}.yml"
- "{{ playbook_dir }}/vars/{{ ansible_distribution | lower }}-{{ ansible_distribution_major_version | lower }}.yml"
- "{{ playbook_dir }}/vars/{{ ansible_os_family | lower }}-{{ ansible_distribution_major_version | lower }}.yml"
- "{{ playbook_dir }}/vars/{{ ansible_distribution | lower }}.yml"
- "{{ playbook_dir }}/vars/{{ ansible_os_family | lower }}.yml"
tags:
- always
- name: Get info about the virt storage pools
virt_pool:
command: info
register: _virt_pools
tags:
- always
- name: Stop running VMs
virt:
name: "{{ hostvars[item]['server_hostname'] }}"
state: shutdown
when:
- hostvars[item]['server_vm'] | default(false) | bool
with_items: "{{ groups['pxe_servers'] }}"
- name: Save VM Disk Image
shell: |
if [[ -e {{ hostvars[item]['server_hostname'] }}.img ]]; then
if [[ -e {{ hostvars[item]['server_hostname'] }}-base.img ]]; then
qemu-img commit {{ hostvars[item]['server_hostname'] }}.img
else
mv {{ hostvars[item]['server_hostname'] }}.img {{ hostvars[item]['server_hostname'] }}-base.img
fi
exit 2
fi
args:
executable: /bin/bash
chdir: "{{ _virt_pools.pools.default.path | default('/data') }}"
when:
- hostvars[item]['server_vm'] | default(false) | bool
with_items: "{{ groups['pxe_servers'] }}"
register: _save_disk_image
changed_when: _save_disk_image.rc == 2
- name: Save VM definition
copy:
src: "/etc/libvirt/qemu/{{ hostvars[item]['server_hostname'] }}.xml"
dest: "{{ _virt_pools.pools.default.path | default('/data') }}/"
remote_src: yes
when:
- hostvars[item]['server_vm'] | default(false) | bool
with_items: "{{ groups['pxe_servers'] }}"
- name: Get the current SHA1 for the manifest
command: "git rev-parse HEAD"
args:
chdir: "{{ playbook_dir }}"
register: _repo_sha
changed_when: false
- name: Add pip freeze results to the data
shell: "pip --disable-pip-version-check freeze > pip-requirements.txt"
args:
executable: /bin/bash
chdir: "{{ _virt_pools.pools.default.path | default('/data') }}"
changed_when: false
- name: Find all the files for the manifest
find:
paths: "{{ _virt_pools.pools.default.path | default('/data') }}"
patterns:
- "*-base.img"
- "*.xml"
- "*.txt"
get_checksum: yes
register: _manifest_files
- name: Prepare the manifest file content
set_fact:
_manifest_content: >-
{ 'openstack-ansible-ops_SHA1': '{{ _repo_sha.stdout }}', 'files': {{ _manifest_files.files | json_query('[*].{path: path, checksum: checksum}') | sort(attribute='path') }} }
- name: Write out the manifest file
copy:
content: "{{ _manifest_content | to_nice_json }}"
dest: "{{ _virt_pools.pools.default.path | default('/data') }}/manifest.json"