45-oc-mirror.yml 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.unarchive:
  119. src: /local/backups/quay-data.tar.bz2
  120. dest: /local
  121. remote_src: yes
  122. - name: Ensure quay service is started after this.
  123. ansible.builtin.systemd_service:
  124. name: quay
  125. scope: user
  126. state: started
  127. - name: wait for quay to become ready again
  128. ansible.builtin.uri:
  129. method: GET
  130. url: https://registry.ocp4.example.com/
  131. headers:
  132. Accept: application/json
  133. Content-Type: application/json
  134. validate_certs: no
  135. status_code:
  136. - 200
  137. - 404
  138. - 502
  139. register: startup_wait
  140. until: startup_wait.status == 200
  141. retries: 30
  142. delay: 5
  143. when:
  144. - quay_nmft < 200
  145. - name: Ensure "oc mirror" has completed (non-idempotent, but only downloads 5-10 images if anything).
  146. hosts: workstation.lab.example.com
  147. gather_subset: min
  148. become: no
  149. tasks:
  150. - name: Ensure working directory exists.
  151. ansible.builtin.file:
  152. path: "{{ ansible_facts['user_dir'] }}/mirror"
  153. state: directory
  154. mode: 0755
  155. owner: student
  156. group: student
  157. - name: Ensure image set config is correct.
  158. ansible.builtin.copy:
  159. dest: "{{ ansible_facts['user_dir'] }}/image-set-config.yaml"
  160. mode: 0644
  161. owner: student
  162. group: student
  163. content: |
  164. kind: ImageSetConfiguration
  165. apiVersion: mirror.openshift.io/v2alpha1
  166. mirror:
  167. platform:
  168. channels:
  169. - name: stable-4.18
  170. type: ocp
  171. minVersion: 4.18.6
  172. maxVersion: 4.18.6
  173. graph: true
  174. operators:
  175. - catalog: registry.redhat.io/redhat/redhat-operator-index:v4.18
  176. full: false
  177. packages:
  178. - name: node-maintenance-operator
  179. - catalog: registry.redhat.io/redhat/certified-operator-index:v4.18
  180. full: false
  181. packages:
  182. - name: crunchy-postgres-operator
  183. additionalImages:
  184. - name: registry.redhat.io/ubi9/ubi:latest
  185. - name: registry.redhat.io/ubi9/toolbox:latest
  186. - name: Kick off "oc mirror".
  187. ansible.builtin.command:
  188. 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
  189. register: mirror_output
  190. - name: Show what happened on stdout.
  191. ansible.builtin.debug:
  192. var: mirror_output.stdout_lines
  193. - name: Show what happened on stderr.
  194. ansible.builtin.debug:
  195. var: mirror_output.stderr_lines
  196. ...