Skip to content

Commit bc18466

Browse files
scrungusmarkgoddardMoteHue
authored
os_images changes (#20)
* changes * Apply suggestions from code review Co-authored-by: Mark Goddard <[email protected]> * remove galaxy_info * linting fixes * Update roles/os_images/tasks/promote.yml Co-authored-by: Matt Crees <[email protected]> * Update README.md * readability * readability * Update roles/os_images/README.md Co-authored-by: Matt Crees <[email protected]> * allow images to be retired separately * linting fix * openstack_images has changed to images * still run for only retire * change display info: * Update roles/os_images/defaults/main.yml Co-authored-by: Mark Goddard <[email protected]> * remove visibility default redundancy --------- Co-authored-by: Mark Goddard <[email protected]> Co-authored-by: Matt Crees <[email protected]>
1 parent 05155dc commit bc18466

File tree

6 files changed

+91
-33
lines changed

6 files changed

+91
-33
lines changed

roles/os_images/README.md

+11-4
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ mutually exclusive where each contain:
6666
be built (even if an existing image that name has been built before). The images on glance
6767
will be replaced if `os_images_upload` is set to `True`. This defaults to
6868
`os_images_force_rebuild`if left unset.
69-
* `is_public`: (optional) whether the image should be set as visible to all
70-
projects or kept private.
69+
* `is_public`: (optional) (deprecated - use `visibility`) whether the image should be set as visible to all
70+
projects or kept private. Note that if both `is_public` and `visibility` are provided, `is_public` will
71+
be preferred.
72+
* `visibility`: (optional) Allowed values are 'public', 'private', 'shared'
73+
or 'community'. Default is 'public'
7174
* `owner`: (optional) ID of the project that should own the uploaded image.
7275

7376
`os_images_common`: A set of elements to include in every image listed.
@@ -97,13 +100,17 @@ following parameters:
97100
will be replaced with the newly built image if `os_images_upload` is set to `True`. Defaults to
98101
`False`.
99102

100-
`os_images_public`: Whether uploaded images are public. Defaults to `True` - note this requires admin permissions.
103+
`os_images_public`: (Deprecated - use `os_images_visibility`) Whether uploaded images are public. Defaults to `True` - note this requires admin permissions.
104+
105+
`os_images_visibility`: The visibility of images uploaded. One of `community`,`public` or `private`. If unset, defaults to `os_images_public` (requires admin permissions for anything other than `private`)
101106

102107
`os_images_venv`: Path to virtualenv in which to install python dependencies to upload images.
103108

104109
`os_images_dib_venv`: Path to virtualenv in which to install DIB to build images.
105110

106-
`os_images_promote`: Whether or not to retire old and promote new images. Defaults to `False`.
111+
`os_images_promote`: Whether or not to promote new images. Defaults to `False`.
112+
113+
`os_images_retire`: Whether or not to retire old images. Defaults to `os_image_promote`. May be necessary to set separately if you are promoting a new candidate image for which there is no existing one to retire, for example.
107114

108115
`os_images_build`: Whether or not to build the images.
109116

roles/os_images/defaults/main.yml

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ os_images_dib_venv: "{{ os_images_venv }}"
99
# State of python dependencies.
1010
os_images_package_state: present
1111

12-
# Upper constraints file for installation of python dependencies to upload
13-
# images.
14-
#
1512
# Use Antelope upper constraints as openstacksdk 1.0.1 and openstackclient 6.2.0 are required
1613
os_images_upper_constraints_file: https://releases.openstack.org/constraints/upper/2023.1
1714

@@ -85,13 +82,20 @@ os_images_force_rebuild: false
8582
# Whether images should be public (requires admin rights)
8683
os_images_public: true
8784

88-
# Whether or not should old images be retired and new images be promoted
85+
# Whether or not should new images be promoted
8986
os_images_promote: false
9087

88+
# Whether or not should old images be retired
89+
os_images_retire: "{{ os_images_promote }}"
90+
9191
# Whether or not to build the images
9292
os_images_build: true
9393

9494
# Image suffix which would be removed during image promotion for exmple: -rc, -dev, -test
9595
os_images_name_suffix:
96+
9697
# Whether or not to hide the images in Glance list
9798
os_images_hide: false
99+
100+
# Visibility of images ('public' requires admin rights.)
101+
os_images_visibility: "{{ 'public' if os_images_public | bool else 'private' }}"

roles/os_images/tasks/images.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# The rpm-distro element executes 'semanage' during its cleanup phase.
1111
- name: Ensure diskimage-builder SELinux dependencies are installed
1212
vars:
13-
package_name: "python3-policycoreutils"
13+
package_name: python3-policycoreutils
1414
ansible.builtin.package:
1515
name: "{{ package_name }}"
1616
state: present

roles/os_images/tasks/main.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@
1717
ansible.builtin.import_tasks: promote.yml
1818
vars:
1919
ansible_python_interpreter: "{{ os_images_venv ~ '/bin/python' if os_images_venv != None else old_ansible_python_interpreter }}"
20-
when: os_images_promote | bool
20+
when: >
21+
((os_images_list | selectattr('rename_image', 'defined')) + (os_images_list | selectattr('hide_image', 'defined'))) | list | length > 0
22+
or (os_images_promote | bool)
23+
or (os_images_retire | bool)
24+
or (os_images_hide | bool)

roles/os_images/tasks/promote.yml

+58-20
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,64 @@
44
msg: os_images_name_suffix is empty please provide it.
55
when: os_images_name_suffix is defined and os_images_name_suffix | length == 0
66

7-
- name: Gather retire candidates info
7+
- name: Gather candidates info
88
openstack.cloud.image_info:
99
auth_type: "{{ os_images_auth_type }}"
1010
auth: "{{ os_images_auth }}"
1111
cacert: "{{ os_images_cacert | default(omit) }}"
1212
interface: "{{ os_images_interface | default(omit, true) }}"
1313
region_name: "{{ os_images_region | default(omit) }}"
14-
register: retire_list
15-
when: item.rename_image | default(os_images_promote) | bool
14+
register: retire_or_promote_list
15+
when: >
16+
(os_images_list | selectattr('rename_image', 'defined') | list | length > 0)
17+
or (os_images_promote | bool)
18+
or (os_images_retire | bool)
1619
1720
- name: Ensure images for retirement exist
1821
ansible.builtin.assert:
1922
that:
20-
- item.name in retire_list.openstack_images | map(attribute='name') | list
23+
- promotion_name in retire_or_promote_list.images | map(attribute='name') | list
2124
fail_msg: "The image {{ item.name[: -(os_images_name_suffix | length | int)] }} does not exist."
25+
vars:
26+
promotion_name: "{{ item.name[: -(os_images_name_suffix | length | int)] }}"
2227
loop: "{{ os_images_list | list }}"
23-
when: item.rename_image | default(os_images_promote) | bool
24-
25-
- name: Gather promote candidates info
26-
openstack.cloud.image_info:
27-
auth_type: "{{ os_images_auth_type }}"
28-
auth: "{{ os_images_auth }}"
29-
cacert: "{{ os_images_cacert | default(omit) }}"
30-
interface: "{{ os_images_interface | default(omit, true) }}"
31-
region_name: "{{ os_images_region | default(omit) }}"
32-
register: promote_list
33-
when: item.rename_image | default(os_images_promote) | bool
28+
when: item.rename_image | default(os_images_retire) | bool
3429

3530
- name: Ensure images for promotion exist
3631
ansible.builtin.assert:
3732
that:
38-
- item.name in promote_list.images | map(attribute='name') | list
33+
- item.name in retire_or_promote_list.images | map(attribute='name') | list
3934
fail_msg: The image {{ item.name }} does not exist.
4035
loop: "{{ os_images_list | list }}"
4136
when: item.rename_image | default(os_images_promote) | bool
4237

38+
- name: Check if image suffix is provided
39+
ansible.builtin.set_fact:
40+
image_suffix_provided: "{{ os_images_name_suffix is defined and os_images_name_suffix is not none and (os_images_name_suffix | length > 0) }}"
41+
42+
- name: Display info
43+
ansible.builtin.debug:
44+
msg: |
45+
Warning: no image suffix is provided so retired images are not renamed and candidate images are not promoted.
46+
Images with the `hide_image` attribute will still be hidden.
47+
when: not (image_suffix_provided | bool)
48+
4349
- name: Hide retire candidate images
4450
ansible.builtin.command: "{{ os_images_venv }}/bin/openstack image set --hidden {{ promotion_name }}"
4551
vars:
46-
promotion_name: "{{ item.name[: -(os_images_name_suffix | length | int)] }}"
52+
promotion_name: "{{ item.name[: -(os_images_name_suffix | length | int)] if image_suffix_provided else item.name }}"
4753
with_items: "{{ os_images_list | list }}"
4854
changed_when: true
49-
when: (item.rename_image | default(os_images_promote) | bool) and (item.hide_image | default(os_images_hide) | bool)
55+
when: (item.hide_image | default(os_images_hide) | bool)
5056

5157
- name: Ensure old images are retired
5258
ansible.builtin.command: "{{ os_images_venv }}/bin/openstack image set {{ promotion_name }} --name {{ promotion_name }}.{{ date_suffix }}"
5359
vars:
5460
date_suffix: "{{ ansible_date_time.date }}"
5561
promotion_name: "{{ item.name[: -(os_images_name_suffix | length | int)] }}"
5662
loop: "{{ os_images_list | list }}"
57-
when: item.rename_image | default(os_images_promote) | bool
5863
changed_when: true
64+
when: image_suffix_provided and (item.rename_image | default(os_images_retire) | bool)
5965
environment: "{{ os_images_venv }}"
6066

6167
- name: Ensure new images are promoted
@@ -64,5 +70,37 @@
6470
promotion_name: "{{ item.name[: -(os_images_name_suffix | length | int)] }}"
6571
loop: "{{ os_images_list | list }}"
6672
changed_when: true
67-
when: item.rename_image | default(os_images_promote) | bool
73+
when: image_suffix_provided and (item.rename_image | default(os_images_promote) | bool)
6874
environment: "{{ os_images_venv }}"
75+
76+
- name: Discover retired images
77+
ansible.builtin.set_fact:
78+
retire_message: |
79+
The following images have been retired:
80+
{% for item in images %}
81+
{{ item.name[: -(os_images_name_suffix | length | int)] }} > {{ item.name[: -(os_images_name_suffix | length | int)] }}.{{ ansible_date_time.date }}
82+
{% endfor %}
83+
vars:
84+
images: "{{ os_images_list | list }}"
85+
when: image_suffix_provided and (item.rename_image | default(os_images_retire) | bool)
86+
87+
- name: Display info
88+
ansible.builtin.debug:
89+
var: retire_message
90+
when: image_suffix_provided and (item.rename_image | default(os_images_retire) | bool)
91+
92+
- name: Discover promoted images
93+
ansible.builtin.set_fact:
94+
promote_message: |
95+
The following images have been promoted:
96+
{% for item in images %}
97+
{{ item.name }} > {{ item.name[: -(os_images_name_suffix | length | int)] }}
98+
{% endfor %}
99+
vars:
100+
images: "{{ os_images_list | list }}"
101+
when: image_suffix_provided and (item.rename_image | default(os_images_promote) | bool)
102+
103+
- name: Display info
104+
ansible.builtin.debug:
105+
var: promote_message
106+
when: image_suffix_provided and (item.rename_image | default(os_images_promote) | bool)

roles/os_images/tasks/upload.yml

+8-3
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
region_name: "{{ os_images_region | default(omit) }}"
2727
name: "{{ item.name ~ '-kernel' }}"
2828
state: present
29-
is_public: "{{ item.is_public | default(os_images_public) | bool }}"
29+
visibility: "{{ visibility }}"
3030
container_format: aki
3131
disk_format: aki
3232
filename: "{{ os_images_cache }}/{{ item.name }}/{{ item.name }}.vmlinuz"
3333
with_items: "{{ os_images_list | list }}"
34+
vars:
35+
visibility: "{{ item.visibility | default(item.is_public | ternary('public', 'private') if item.is_public is defined else os_images_visibility) }}"
3436
loop_control:
3537
label: "{{ item.name }}"
3638
when:
@@ -65,11 +67,13 @@
6567
region_name: "{{ os_images_region | default(omit) }}"
6668
name: "{{ item.name ~ '-ramdisk' }}"
6769
state: present
68-
is_public: "{{ item.is_public | default(os_images_public) | bool }}"
70+
visibility: "{{ visibility }}"
6971
container_format: ari
7072
disk_format: ari
7173
filename: "{{ os_images_cache }}/{{ item.name }}/{{ item.name }}.initrd"
7274
with_items: "{{ os_images_list | list }}"
75+
vars:
76+
visibility: "{{ item.visibility | default(item.is_public | ternary('public', 'private') if item.is_public is defined else os_images_visibility) }}"
7377
loop_control:
7478
label: "{{ item.name }}"
7579
when:
@@ -101,7 +105,7 @@
101105
region_name: "{{ os_images_region | default(omit) }}"
102106
name: "{{ item.0.name }}"
103107
state: present
104-
is_public: "{{ item.0.is_public | default(os_images_public) | bool }}"
108+
visibility: "{{ visibility }}"
105109
owner: "{{ item.0.owner | default(omit) }}"
106110
container_format: bare
107111
disk_format: "{{ item.0.type | default('qcow2') }}"
@@ -111,6 +115,7 @@
111115
ramdisk: "{{ item.2.id if is_baremetal else omit }}"
112116
vars:
113117
is_baremetal: "{{ item.0.elements is defined and 'baremetal' in item.0.elements }}"
118+
visibility: "{{ item.0.visibility | default(item.0.is_public | ternary('public', 'private') if item.0.is_public is defined else os_images_visibility) }}"
114119
with_together:
115120
- "{{ os_images_list | list }}"
116121
- "{{ kernel_result.results }}"

0 commit comments

Comments
 (0)