45-oc-mirror.yml 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. ---
  2. # Create image set config if necessary, start "oc mirror".
  3. - name: Ensure backup file is owned by quay user.
  4. hosts: registry.ocp4.example.com
  5. gather_subset: min
  6. become: yes
  7. tasks:
  8. - name: Ensure database backup file is owned by quay user.
  9. ansible.builtin.file:
  10. path: /local/backups/quay-db.backup
  11. owner: quay
  12. group: quay
  13. mode: 0644
  14. - name: Ensure registry has a default (pre-mirrored) set of images.
  15. hosts: registry.ocp4.example.com
  16. gather_subset: min
  17. become: no
  18. remote_user: quay
  19. tasks:
  20. - name: Verify that the image manifests exist.
  21. containers.podman.podman_container_exec:
  22. name: postgresql
  23. command: psql -d quay -U postgres -t -c 'SELECT COUNT(id) FROM manifest'
  24. ignore_errors: yes
  25. register: quay_mft
  26. - name: Remember the number of image manifests in quay.
  27. ansible.builtin.set_fact:
  28. quay_nmft: "{{ quay_mft.stdout_lines[0] | default(0) | trim | int }}"
  29. - name: Import quay backup if manifests seem to be missing.
  30. block:
  31. - name: Ensure quay service is stopped.
  32. ansible.builtin.systemd_service:
  33. name: quay
  34. scope: user
  35. state: stopped
  36. - name: Ensure quay container is stopped.
  37. containers.podman.podman_container:
  38. name: quay
  39. state: stopped
  40. timeout: 60
  41. - name: Create the database if necessary.
  42. containers.podman.podman_container_exec:
  43. name: postgresql
  44. command: psql -d postgres -U postgres -t -c 'CREATE DATABASE quay OWNER quay'
  45. when:
  46. - (quay_mft.stderr | ansible.builtin.regex_search('FATAL: database "quay" does not exist')) is defined
  47. - name: Create a temporary pgpass file
  48. ansible.builtin.copy:
  49. dest: /tmp/pgpass
  50. owner: quay
  51. group: quay
  52. mode: 0600
  53. content: |
  54. postgresql:5432:quay:postgres:verysecret
  55. - name: Ensure the pgpass file is owned by postgres user of the container.
  56. become_method: containers.podman.podman_unshare
  57. become: yes
  58. ansible.builtin.file:
  59. path: /tmp/pgpass
  60. state: file
  61. owner: 26
  62. - name: Run pg_restore in a quay_import container.
  63. containers.podman.podman_container:
  64. name: quay_import
  65. image: "{{ registry_host }}/rhel9/postgresql-15:latest"
  66. rm: yes
  67. detach: no
  68. network:
  69. - quay
  70. volumes:
  71. - /local/backups/quay-db.backup:/quay-db.backup:Z
  72. - /tmp/pgpass:/var/lib/pgsql/.pgpass:Z
  73. command:
  74. - pg_restore
  75. - -dquay
  76. - -Upostgres
  77. - -hpostgresql
  78. - -c
  79. - /quay-db.backup
  80. state: started
  81. register: quay_import
  82. ignore_errors: yes
  83. failed_when:
  84. - (quay_import.stderr | regex_search('FATAL')) is defined
  85. - debug: var=quay_import
  86. - name: Restore the ownership of the file.
  87. become_method: containers.podman.podman_unshare
  88. become: yes
  89. ansible.builtin.file:
  90. path: /tmp/pgpass
  91. state: file
  92. owner: 0
  93. - name: Remove the pgpass file
  94. ansible.builtin.file:
  95. path: /tmp/pgpass
  96. state: absent
  97. - name: Remove the current Quay data directory.
  98. remote_user: lab
  99. become: yes
  100. ansible.builtin.file:
  101. path: /local/quay/{{ item }}
  102. state: absent
  103. loop:
  104. - sha256
  105. - uploads
  106. - name: Extract the latest Quay data directory backup.
  107. remote_user: lab
  108. become: yes
  109. ansible.builtin.unarchive:
  110. src: /local/backups/quay-data.tar.bz2
  111. dest: /local
  112. remote_src: yes
  113. - name: Ensure quay service is started after this.
  114. ansible.builtin.systemd_service:
  115. name: quay
  116. scope: user
  117. state: started
  118. - name: wait for quay to become ready again
  119. ansible.builtin.uri:
  120. method: GET
  121. url: https://registry.ocp4.example.com/
  122. headers:
  123. Accept: application/json
  124. Content-Type: application/json
  125. validate_certs: no
  126. status_code:
  127. - 200
  128. - 404
  129. - 502
  130. register: startup_wait
  131. until: startup_wait.status == 200
  132. retries: 30
  133. delay: 5
  134. when:
  135. - quay_nmft < 200
  136. - name: Ensure "oc mirror" has completed. (NON-IDEMPOTENT!)
  137. hosts: workstation.lab.example.com
  138. gather_subset: min
  139. become: no
  140. tasks:
  141. - name: Ensure working directory exists.
  142. ansible.builtin.file:
  143. path: "{{ ansible_facts['user_dir'] }}/mirror"
  144. state: directory
  145. mode: 0755
  146. owner: student
  147. group: student
  148. - name: Ensure image set config is correct.
  149. ansible.builtin.copy:
  150. dest: "{{ ansible_facts['user_dir'] }}/image-set-config.yaml"
  151. mode: 0644
  152. owner: student
  153. group: student
  154. content: |
  155. kind: ImageSetConfiguration
  156. apiVersion: mirror.openshift.io/v2alpha1
  157. mirror:
  158. platform:
  159. channels:
  160. - name: stable-4.18
  161. type: ocp
  162. minVersion: 4.18.6
  163. maxVersion: 4.18.6
  164. graph: true
  165. operators:
  166. - catalog: registry.redhat.io/redhat/redhat-operator-index:v4.18
  167. full: false
  168. packages:
  169. - name: node-maintenance-operator
  170. - catalog: registry.redhat.io/redhat/certified-operator-index:v4.18
  171. full: false
  172. packages:
  173. - name: crunchy-postgres-operator
  174. additionalImages:
  175. - name: registry.redhat.io/ubi9/ubi:latest
  176. - name: registry.redhat.io/ubi9/toolbox:latest
  177. - name: Kick off "oc mirror".
  178. ansible.builtin.command:
  179. cmd: oc mirror --v2 -c {{ ansible_facts['user_dir'] }}/image-set-config.yaml --workspace file://{{ ansible_facts['user_dir'] }}/mirror/ docker://registry.ocp4.example.com
  180. register: mirror_output
  181. - name: Show what happened on stdout.
  182. ansible.builtin.debug:
  183. var: mirror_output.stdout_lines
  184. - name: Show what happened on stderr.
  185. ansible.builtin.debug:
  186. var: mirror_output.stderr_lines
  187. ...