Mohammed Naser | 4b63004 | 2023-02-07 20:47:45 +0000 | [diff] [blame] | 1 | # Copyright (c) 2023 VEXXHOST, Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | - name: Check if image exists |
| 16 | run_once: true |
| 17 | openstack.cloud.image_info: |
| 18 | cloud: atmosphere |
| 19 | image: "{{ glance_image_name }}" |
| 20 | register: _image_info |
Mohammed Naser | 41f239c | 2023-02-18 17:34:21 +0000 | [diff] [blame] | 21 | # NOTE(mnaser): This often fails since the SSL certificates are not |
| 22 | # ready yet. We need to wait for them to be ready. |
| 23 | retries: 120 |
| 24 | delay: 1 |
| 25 | until: _image_info is not failed |
Mohammed Naser | 4b63004 | 2023-02-07 20:47:45 +0000 | [diff] [blame] | 26 | |
| 27 | - name: Download image and upload into Glance |
| 28 | run_once: true |
Mohammed Naser | 8ccabb6 | 2025-02-05 13:20:09 -0500 | [diff] [blame] | 29 | when: _image_info.images | length == 0 |
Mohammed Naser | 4b63004 | 2023-02-07 20:47:45 +0000 | [diff] [blame] | 30 | block: |
| 31 | - name: Generate temporary work directory |
| 32 | ansible.builtin.tempfile: |
Mohammed Naser | d0c8a59 | 2025-02-05 11:40:39 -0500 | [diff] [blame] | 33 | path: "{{ glance_image_tempfile_path | default(omit) }}" |
Mohammed Naser | 4b63004 | 2023-02-07 20:47:45 +0000 | [diff] [blame] | 34 | state: directory |
| 35 | register: _workdir |
| 36 | |
| 37 | - name: Download image |
| 38 | ansible.builtin.get_url: |
| 39 | url: "{{ glance_image_url }}" |
| 40 | dest: "{{ _workdir.path }}/{{ glance_image_url | basename }}" |
Tadas Sutkaitis | 4ace418 | 2023-02-27 04:31:52 +0200 | [diff] [blame] | 41 | mode: "0600" |
Mohammed Naser | d0c8a59 | 2025-02-05 11:40:39 -0500 | [diff] [blame] | 42 | tmp_dest: "{{ _workdir.path }}" |
Mohammed Naser | 4b63004 | 2023-02-07 20:47:45 +0000 | [diff] [blame] | 43 | register: _get_url |
Mohammed Naser | 2797116 | 2024-04-03 21:48:17 -0400 | [diff] [blame] | 44 | retries: 3 |
| 45 | delay: "{{ 15 | random + 3 }}" |
| 46 | until: _get_url is not failed |
Guilherme Steinmüller | b835cc2 | 2024-07-07 21:10:25 -0300 | [diff] [blame] | 47 | environment: |
| 48 | http_proxy: "{{ glance_image_http_proxy }}" |
| 49 | https_proxy: "{{ glance_image_https_proxy }}" |
| 50 | no_proxy: "{{ glance_image_no_proxy }}" |
Mohammed Naser | 4b63004 | 2023-02-07 20:47:45 +0000 | [diff] [blame] | 51 | |
| 52 | - name: Get image format |
| 53 | changed_when: false |
| 54 | ansible.builtin.shell: | |
| 55 | set -o pipefail |
| 56 | qemu-img info {{ _get_url.dest }} | grep -i "file format" | awk '{ print $3 }' |
| 57 | args: |
| 58 | executable: /bin/bash |
| 59 | register: _image_format |
| 60 | |
| 61 | - name: Convert file to target disk format |
Mohammed Naser | 306d1e4 | 2024-06-17 11:19:49 -0400 | [diff] [blame] | 62 | when: |
| 63 | - glance_image_disk_format not in ['aki', 'ari'] |
| 64 | - glance_image_disk_format != _image_format.stdout |
Tadas Sutkaitis | 4ace418 | 2023-02-27 04:31:52 +0200 | [diff] [blame] | 65 | changed_when: true |
Mohammed Naser | 4b63004 | 2023-02-07 20:47:45 +0000 | [diff] [blame] | 66 | ansible.builtin.command: |
| 67 | qemu-img convert -O {{ glance_image_disk_format }} {{ _get_url.dest }} {{ _get_url.dest }}.converted |
Mohammed Naser | 306d1e4 | 2024-06-17 11:19:49 -0400 | [diff] [blame] | 68 | register: glance_image_conversion |
Mohammed Naser | 4b63004 | 2023-02-07 20:47:45 +0000 | [diff] [blame] | 69 | |
| 70 | - name: Wait until image service ready |
| 71 | kubernetes.core.k8s_info: |
| 72 | api_version: apps/v1 |
| 73 | kind: Deployment |
| 74 | name: glance-api |
| 75 | namespace: openstack |
| 76 | wait_sleep: 1 |
| 77 | wait_timeout: 600 |
| 78 | wait: true |
| 79 | wait_condition: |
| 80 | type: Available |
| 81 | status: true |
| 82 | |
| 83 | - name: Upload image into Glance |
| 84 | openstack.cloud.image: |
| 85 | cloud: atmosphere |
| 86 | name: "{{ glance_image_name }}" |
Mohammed Naser | 306d1e4 | 2024-06-17 11:19:49 -0400 | [diff] [blame] | 87 | filename: "{{ _get_url.dest }}{% if glance_image_conversion is not skipped %}.converted{% endif %}" |
Mohammed Naser | 4b63004 | 2023-02-07 20:47:45 +0000 | [diff] [blame] | 88 | min_disk: "{{ glance_image_min_disk | default(omit) }}" |
| 89 | min_ram: "{{ glance_image_min_ram | default(omit) }}" |
| 90 | container_format: "{{ glance_image_container_format | default(omit) }}" |
| 91 | disk_format: "{{ glance_image_disk_format | default(omit) }}" |
| 92 | properties: "{{ glance_image_properties | default(omit) }}" |
| 93 | kernel: "{{ glance_image_kernel | default(omit) }}" |
| 94 | ramdisk: "{{ glance_image_ramdisk | default(omit) }}" |
| 95 | is_public: "{{ glance_image_is_public | default(omit) }}" |
| 96 | tags: "{{ glance_image_tags | default(omit) }}" |
| 97 | wait: true |
| 98 | timeout: 600 |
| 99 | # NOTE(mnaser): This often fails since the SSL certificates are not |
| 100 | # ready yet. We need to wait for them to be ready. |
Mohammed Naser | 306d1e4 | 2024-06-17 11:19:49 -0400 | [diff] [blame] | 101 | retries: "{{ glance_image_retries | default(60) }}" |
Mohammed Naser | 4b63004 | 2023-02-07 20:47:45 +0000 | [diff] [blame] | 102 | delay: 5 |
| 103 | register: _result |
| 104 | until: _result is not failed |
| 105 | always: |
| 106 | - name: Remove work directory |
| 107 | ansible.builtin.file: |
| 108 | path: "{{ _workdir.path }}" |
| 109 | state: absent |