From 1edd6cba7df6ce6db208f8a573360ee97b556ad0 Mon Sep 17 00:00:00 2001 From: Lars Kellogg-Stedman Date: Fri, 19 Feb 2016 21:05:51 -0500 Subject: [PATCH] automatically cache undercloud images with this commit, once an image is fetched successfully and we validate the checksum we save the image using the md5 checksum as the name, and in subsequent runs we use the cached copy rather than fetching it again. this also means that we support arbitrarily named remote images (so you can fetch "undercloud-image-with-extra-sauce.qcow2" and it will work just fine). Change-Id: I1c7b1c4ba110f2cc730c8a694494772dd52ad487 --- .../setup/undercloud/defaults/main.yml | 2 + .../setup/undercloud/tasks/fetch_image.yml | 69 +++++++++++++++++++ .../libvirt/setup/undercloud/tasks/main.yml | 29 +------- 3 files changed, 72 insertions(+), 28 deletions(-) create mode 100644 playbooks/roles/libvirt/setup/undercloud/tasks/fetch_image.yml diff --git a/playbooks/roles/libvirt/setup/undercloud/defaults/main.yml b/playbooks/roles/libvirt/setup/undercloud/defaults/main.yml index a5ef38a59..40c13a9f1 100644 --- a/playbooks/roles/libvirt/setup/undercloud/defaults/main.yml +++ b/playbooks/roles/libvirt/setup/undercloud/defaults/main.yml @@ -2,3 +2,5 @@ qemu_executable: Fedora: /usr/bin/qemu-kvm CentOS: /usr/libexec/qemu-kvm 'Red Hat Enterprise Linux': /usr/libexec/qemu-kvm + +image_cache_directory: /usr/share/oooq_images/ diff --git a/playbooks/roles/libvirt/setup/undercloud/tasks/fetch_image.yml b/playbooks/roles/libvirt/setup/undercloud/tasks/fetch_image.yml new file mode 100644 index 000000000..04a2ba634 --- /dev/null +++ b/playbooks/roles/libvirt/setup/undercloud/tasks/fetch_image.yml @@ -0,0 +1,69 @@ +# Fetching the undercloud images can take a long time. This +# tasklist caches images in {{ image_cache_directory }} if an image is +# (a) downloaded successfully and (b) successfully verifies against +# the checksum. Images are cached using the checksum as the filename, +# and subsequent playbook runs will use the cached copy rather than +# trying to fetch the remote copy. + +- name: ensure image cache directory exists + file: + path: "{{ image_cache_directory }}" + state: directory + become: true + +- name: get undercloud image expected checksum + command: > + curl -sf {{url}}.md5 + register: undercloud_md5_expected + +- name: check for undercloud image in cache + command: > + test -f {{undercloud_md5_expected.stdout.split()[0]}}.qcow2 + args: + chdir: "{{ image_cache_directory }}" + ignore_errors: true + register: image_exists + changed_when: false + +- name: get undercloud image + command: > + curl -sf -C- -o _undercloud.qcow2 {{ url }} + args: + chdir: "{{ image_cache_directory }}" + register: curl_result + until: curl_result.rc not in [18, 56] + retries: 20 + delay: 5 + become: true + when: image_exists|failed + +- name: get actual md5 checksum of undercloud image + command: > + md5sum _undercloud.qcow2 + args: + chdir: "{{ image_cache_directory }}" + register: undercloud_md5_actual + when: image_exists|failed + +- name: verify undercloud image checksum + fail: + msg: undercloud image checksum does not match + when: > + image_exists|failed and ( + undercloud_md5_expected.stdout.split()[0] != + undercloud_md5_actual.stdout.split()[0]) + +- name: cache undercloud image by checksum + command: > + mv _undercloud.qcow2 {{ undercloud_md5_expected.stdout.split()[0] }}.qcow2 + args: + chdir: "{{ image_cache_directory }}" + when: image_exists|failed + become: true + +- name: get undercloud image from cache + command: + cp {{ image_cache_directory }}/{{undercloud_md5_expected.stdout.split()[0]}}.qcow2 + {{ working_dir }}/undercloud.qcow2 + become: true + become_user: "{{ non_root_user }}" diff --git a/playbooks/roles/libvirt/setup/undercloud/tasks/main.yml b/playbooks/roles/libvirt/setup/undercloud/tasks/main.yml index 2b9f9cb9c..fde040bc0 100644 --- a/playbooks/roles/libvirt/setup/undercloud/tasks/main.yml +++ b/playbooks/roles/libvirt/setup/undercloud/tasks/main.yml @@ -12,34 +12,7 @@ bridges: "{{ networks.bridges }}" register: undercloud_mac_map -- name: get undercloud appliance image - command: > - curl -sf -C- -o "{{item.dest}}" "{{item.url}}" - args: - chdir: "{{ working_dir }}" - register: curl_result - - # see https://curl.haxx.se/libcurl/c/libcurl-errors.html - # for a list of curl exit codes. we're retrying on - # 18 (CURLE_PARTIAL_FILE) and 56 (CURLE_RECV_ERROR). - until: curl_result.rc not in [18, 56] - retries: 20 - delay: 5 - with_items: - - url: "{{url}}" - dest: undercloud.qcow2 - - url: "{{url}}.md5" - dest: undercloud.qcow2.md5 - become: true - become_user: "{{non_root_user}}" - -- name: verify undercloud appliance image - command: > - md5sum -c undercloud.qcow2.md5 - args: - chdir: "{{ working_dir }}" - become: true - become_user: "{{non_root_user}}" +- include: fetch_image.yml - name: copy instackenv.json to appliance shell: |