45-oc-mirror.yml 6.7 KB

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