tripleo-ansible/tripleo_ansible/roles/octavia-undercloud/tasks/image_mgmt.yml
Brent Eagles 2d1a48b505 Octavia: handle empty amphora image search result
In some situations, openstack image list queries on name return a 0
exit code even though there is no result.

Note: this differs slightly from the original commit because the linters
are catching a new rule "comparison against empty strings" since the
original commit merged.

Change-Id: I20f63ea45e52181810654f7afab11499fef9baa2
Related-Bug: #1843059
(cherry picked from commit 5fccdd3c2c)
2020-06-23 16:42:45 -02:30

138 lines
4.2 KiB
YAML

---
- name: check if name is a symlink
stat:
path: "{{ image_filename }}"
register: symlnk_check
- name: bypass image naming logic if image name is provided (backwards-compatibility)
set_fact:
amphora_image: "{{ amphora_image_name }}"
when:
- amphora_image_name is defined
- not ((amphora_image_name | length) < 1)
- name: set the actual glance image name if it is a symlink
set_fact:
amphora_image: "{{ (symlnk_check.stat.lnk_target | basename | splitext)[0] }}"
when:
- amphora_image is undefined
- symlnk_check is defined and (symlnk_check.stat.islnk | bool)
- name: set the actual glance image name if it is not a symlink
set_fact:
amphora_image: "{{ (image_file_result.stat.path | basename | splitext)[0] }}"
when:
- amphora_image is undefined
- (image_file_result.stat.exists | bool) and (not (symlnk_check.stat.islnk | bool))
- name: gather facts about the service project
shell: |
openstack project show "{{ auth_project_name }}" -c id -f value
register: project_id_result
- name: check there's an image in glance already
shell: |
openstack image list --property owner={{ project_id_result.stdout }} --private --name {{ amphora_image }} -c ID -f value
environment:
OS_USERNAME: "{{ auth_username }}"
OS_PASSWORD: "{{ auth_password }}"
OS_PROJECT_NAME: "{{ auth_project_name }}"
register: glance_id_result
failed_when: false
- name: set image id fact
set_fact:
image_id: "{{ glance_id_result.stdout }}"
when:
- glance_id_result.rc is defined
- glance_id_result.rc == 0
- (glance_id_result.stdout | length) > 0
- name: get checksum if there's an image in glance already
shell: |
openstack image show {{ glance_id_result.stdout }} -c checksum -f value
environment:
OS_USERNAME: "{{ auth_username }}"
OS_PASSWORD: "{{ auth_password }}"
OS_PROJECT_NAME: "{{ auth_project_name }}"
when:
- image_id is defined
register: glance_results
failed_when: false
- name: set current_md5 fact from glance if image already exists there
set_fact:
current_md5: "{{ glance_results.stdout }}"
when:
- glance_results.rc is defined
- glance_results.rc == 0
- name: determine if the image needs to be replaced
set_fact:
replace_image: "{{ current_md5 != image_file_result.stat.md5 }}"
when:
- current_md5 is defined
- name: move existing image if the names match and the md5s are not the same
shell: |
ts=`openstack image show {{ image_id }} -f value -c created_at`
ts=${ts//:/}
ts=${ts//-/}
openstack image set {{ image_id }} --name "{{ amphora_image }}_$ts"
environment:
OS_USERNAME: "{{ auth_username }}"
OS_PASSWORD: "{{ auth_password }}"
OS_PROJECT_NAME: "{{ auth_project_name }}"
when:
- replace_image is defined and replace_image | bool
- name: decide whether to upload new image
set_fact:
upload_image: true
when:
- (current_md5 is not defined) or (replace_image is defined and replace_image | bool)
- block:
- name: create temporary directory
tempfile:
state: directory
register: amp_tmp_dir
- name: set RAW file name
set_fact:
raw_filename: "{{ amp_tmp_dir.path }}/{{ image_filename|splitext|first|basename }}.img"
- name: convert image from qcow2 to raw
shell: |
qemu-img convert -f qcow2 -O raw {{ image_filename }} {{ raw_filename }}
- name: setting amphora format to raw
set_fact:
raw_format: raw
when:
- amp_to_raw | bool
- name: upload image to glance
shell: |
openstack image create --disk-format {{ raw_format|default('qcow2') }} \
--container-format bare --tag {{ amp_image_tag }} \
--file {{ raw_filename|default(image_filename) }} \
--property hw_architecture={{ amp_hw_arch }} \
--private {{ amphora_image }}
environment:
OS_USERNAME: "{{ auth_username }}"
OS_PASSWORD: "{{ auth_password }}"
OS_PROJECT_NAME: "{{ auth_project_name }}"
register: image_result
changed_when: (image_result.stdout | length) < 1
when:
- image_file_result.stat.exists | bool
- upload_image is defined
- name: delete converted raw image
file:
path: "{{ amp_tmp_dir.path }}"
state: absent
when:
- amp_to_raw | bool