45-oc-mirror.yml 7.1 KB

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