45-oc-mirror.yml 6.8 KB

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