32-quay-deploy.yml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. ---
  2. # Tasks required by 10-quay-deploy.adoc.
  3. - name: Issue a new Cert for Quay if necessary.
  4. hosts: workstation.lab.example.com
  5. gather_subset: min
  6. tasks:
  7. - name: Check if Quay key exists to save time
  8. ansible.builtin.stat:
  9. path: "{{ ansible_facts['user_dir'] }}/ca/quay-key.pem"
  10. register: qkey_file
  11. - name: Check if Quay cert exists to save time
  12. ansible.builtin.stat:
  13. path: "{{ ansible_facts['user_dir'] }}/ca/quay-cert.pem"
  14. register: qcert_file
  15. - name: Create a new private key for Quay, if it does not exist yet.
  16. community.crypto.openssl_privatekey:
  17. path: "{{ ansible_facts['user_dir'] }}/ca/quay-key.pem"
  18. type: RSA
  19. size: 4096
  20. mode: 0600
  21. when: qkey_file.stat.exists == false
  22. - name: Create a CSR for Quay
  23. community.crypto.openssl_csr:
  24. path: "{{ ansible_facts['user_dir'] }}/ca/quay-csr.pem"
  25. privatekey_path: "{{ ansible_facts['user_dir'] }}/ca/quay-key.pem"
  26. subject:
  27. C: US
  28. ST: North Carolina
  29. L: Raleigh
  30. O: Red Hat
  31. OU: RHT
  32. CN: registry.ocp4.example.com
  33. use_common_name_for_san: yes
  34. mode: 0600
  35. when: qcert_file.stat.exists == false
  36. - name: Issue a certificate for Quay if one isn't there yet.
  37. ansible.builtin.command:
  38. cmd: openssl ca -config {{ ansible_facts['user_dir'] }}/ca/openssl.cnf -passin pass:verysecret -in {{ ansible_facts['user_dir'] }}/ca/quay-csr.pem -out {{ ansible_facts['user_dir'] }}/ca/quay-cert.pem -batch -notext
  39. creates: "{{ ansible_facts['user_dir'] }}/ca/quay-cert.pem"
  40. - name: Load CA cert and Quay cert.
  41. ansible.builtin.set_fact:
  42. ca_cert: "{{ lookup('file', ansible_facts['user_dir'] + '/ca/ca-cert.pem') }}"
  43. quay_cert: "{{ lookup('file', ansible_facts['user_dir'] + '/ca/lab-ca/newcerts/00.pem') }}"
  44. - name: Concatenate Quay and CA certs.
  45. ansible.builtin.copy:
  46. dest: "{{ ansible_facts['user_dir'] }}/ca/quay-cert.pem"
  47. content: |
  48. {{ quay_cert }}
  49. {{ ca_cert }}
  50. - name: Prepare registry VM to run Quay services.
  51. hosts: registry.ocp4.example.com
  52. gather_subset: min
  53. tasks:
  54. - name: Ensure firewall allows HTTP/HTTPS.
  55. become: yes
  56. ansible.posix.firewalld:
  57. immediate: yes
  58. permanent: yes
  59. zone: public
  60. service: "{{ item }}"
  61. state: enabled
  62. loop:
  63. - http
  64. - https
  65. - name: Ensure unpriv users can open ports from 80 onwards.
  66. become: yes
  67. ansible.posix.sysctl:
  68. name: net.ipv4.ip_unprivileged_port_start
  69. value: "80"
  70. state: present
  71. sysctl_file: /etc/sysctl.d/quay-low-ports.conf
  72. reload: yes
  73. - name: Ensure user quay exists.
  74. become: yes
  75. ansible.builtin.user:
  76. name: quay
  77. create_home: yes
  78. state: present
  79. - name: Have the quay user accept student's SSH key.
  80. become: yes
  81. ansible.posix.authorized_key:
  82. key: "{{ lookup('ansible.builtin.file', '/home/student/.ssh/lab_rsa.pub') }}"
  83. user: quay
  84. state: present
  85. - name: Ensure user quay will linger.
  86. become: yes
  87. ansible.builtin.command:
  88. cmd: loginctl enable-linger quay
  89. creates: /var/lib/systemd/linger/quay
  90. - name: Ensure data directories are there.
  91. become: yes
  92. ansible.builtin.file:
  93. path: "{{ item }}"
  94. mode: 0770
  95. owner: quay
  96. group: quay
  97. state: directory
  98. loop:
  99. - /local/quay-pg
  100. - /local/quay
  101. - name: Ensure .docker directory is there
  102. become: yes
  103. ansible.builtin.file:
  104. path: "/home/quay/.docker"
  105. mode: 0700
  106. owner: quay
  107. group: quay
  108. state: directory
  109. # TODO: figure out how to customise this with registry host changes
  110. - name: Ensure podman will be able to log into the upstream registry
  111. become: yes
  112. ansible.builtin.copy:
  113. dest: "/home/quay/.docker/config.json"
  114. content: |
  115. {"auths":{"registry.redhat.io":{"auth":"fHVoYy1wb29sLTlmMDA1Mzc2LTM2YTItNDJhMS1hNTQwLTA0NzNkYzg3MzYzMzpleUpoYkdjaU9pSlNVelV4TWlKOS5leUp6ZFdJaU9pSTVPRGc1WVdFeFl6Qm1PV0kwWmpVM1lqazNObUk1WldFeU16SXdaalUwTUNKOS5zWmQ5VE1RbzBXREc2NUc5Qk1ObmtuYlBjRkIzNmhyRFhkMThfdTNLeHFaczdlOG1hQ19QeEFReGpwdVk0YVM2VERIbkxDNWpGYjRRNXFYVEpWbjJCOGE4cDFuY08tM24ySG5QdDg3NmktVUFDU3lldWtpb3k4aHI0V3d1ZkhReFVYMmxxWFhYdjN6blE3am1URUNBc25rWkNRSFU1dFNpRnNUZHhFZGZkeU42Z20xN3VqY2thZG5NbFBZcTZfU1I2bUtLaUpUdFQ3SFlDWXJBVk5zZ0tfNGFkZ2MtRXBlbEtHbGNERWkzNGhYbzFqbEIzRERyUWkxSUxCV0UwZkdXb1czZy1ZUzFGMFlEXzc0bm1XSU5mUE1jM25UOERaQWl0OEw0VlFPTnZnUE51YnVfTVVGUGhqX29VUjF3VUR0a1BRNktJdm82UWYyRkdwMndLM1B6YnRBRFFzRVZTZDlITzQ3a0RKdGFobk95YTFmRmdqZVk1bFNxLW1vT2RqUldCZ3U2XzNIX25lZExJR1lQRHRBZnp5cGJ1eHZ1cEd1M2hYWnVzeWN0aURtR203SkR5RW5KdjF1RFZmYVduU2EzSV9NcFRSVVcyZWU1RF9CanJleTdlU2I0bEpGcmp1eC1nY2JVaHFsWGJZc2l6azdXWHpvRmtrVFlMdXFDQ1FvS1J0OFdSN1UzTmh3c3Q2ckV3eEFOaWJFTlNzUVB3MGg4X0NDRm5qTHFSTl82cWpTc0tpeWRGT2tHVFliT0taTktaSVVhYkZFTjRhYVRVYmlYTVdPS2Eyak1xLUhwazBMNEowUmtOM2JkQVVqWmtERHE0ZFY1ZVFjdXNIeV9LY29nd1VKSjZ4MDNObnM4b0xBdjRJZ3RKeXlxcmE1YUJHSkxReHNjRXVSNzQwWQ=="}}}
  116. mode: 0600
  117. owner: quay
  118. group: quay
  119. - name: Configure containers and their environment on registry VM.
  120. hosts: registry.ocp4.example.com
  121. gather_subset: min
  122. remote_user: quay
  123. tasks:
  124. - name: Create a podman network, if necessary.
  125. containers.podman.podman_network:
  126. name: quay
  127. state: present
  128. - name: Pull all the images if necessary.
  129. containers.podman.podman_image:
  130. name: "{{ registry_host }}/{{ item }}"
  131. pull: yes
  132. state: present
  133. loop:
  134. - rhel9/postgresql-15:latest
  135. - rhel9/redis-7:latest
  136. - quay/quay-rhel8:v{{ quay_version }}
  137. - quay/clair-rhel8:v{{ quay_version }}
  138. - name: Ensure PG datadir is owned by the correct user.
  139. become_method: containers.podman.podman_unshare
  140. become: yes
  141. ansible.builtin.file:
  142. path: /local/quay-pg
  143. state: directory
  144. owner: 26
  145. mode: 0770
  146. - name: Start postgres container if necessary.
  147. containers.podman.podman_container:
  148. name: postgresql
  149. image: "{{ registry_host }}/rhel9/postgresql-15:latest"
  150. rm: yes
  151. detach: yes
  152. env:
  153. POSTGRESQL_USER: quay
  154. POSTGRESQL_PASSWORD: secret
  155. POSTGRESQL_DATABASE: quay
  156. POSTGRESQL_ADMIN_PASSWORD: verysecret
  157. network:
  158. - quay
  159. volumes:
  160. - /local/quay-pg:/var/lib/pgsql/data:Z
  161. state: started
  162. register: pg_started
  163. - name: Wait a bit if the PG container was just started.
  164. ansible.builtin.pause:
  165. prompt: Waiting for PostgreSQL container to start.
  166. seconds: 5
  167. when: pg_started.changed
  168. - name: Create the trigram extension if necessary.
  169. containers.podman.podman_container_exec:
  170. name: postgresql
  171. command: 'psql -d quay -U postgres -c "CREATE EXTENSION IF NOT EXISTS pg_trgm"'
  172. register: pg_ext
  173. changed_when:
  174. - not "already exists" in pg_ext.stderr
  175. - name: If we started the PG container and created the extension, stop the container now.
  176. containers.podman.podman_container:
  177. name: postgresql
  178. state: stopped
  179. when:
  180. - pg_started.changed
  181. - pg_ext.changed
  182. - name: Create Quay config directory if necessary.
  183. ansible.builtin.file:
  184. path: "{{ ansible_facts['user_dir'] }}/config"
  185. state: directory
  186. mode: 0770
  187. - name: Publish Quay key on registry.
  188. ansible.builtin.copy:
  189. src: /home/student/ca/quay-key.pem
  190. dest: "{{ ansible_facts['user_dir'] }}/config/ssl.key"
  191. - name: Publish Quay cert on registry.
  192. ansible.builtin.copy:
  193. src: /home/student/ca/quay-cert.pem
  194. dest: "{{ ansible_facts['user_dir'] }}/config/ssl.cert"
  195. - name: Publish Quay config file.
  196. ansible.builtin.copy:
  197. dest: "{{ ansible_facts['user_dir'] }}/config/config.yaml"
  198. content: |
  199. BUILDLOGS_REDIS:
  200. host: redis
  201. password: verysecret
  202. port: 6379
  203. CREATE_NAMESPACE_ON_PUSH: true
  204. DATABASE_SECRET_KEY: 410c87de-8ad8-4f4c-9670-2ec25bc87191
  205. DB_URI: postgresql://quay:secret@postgresql:5432/quay
  206. DISTRIBUTED_STORAGE_CONFIG:
  207. default:
  208. - LocalStorage
  209. - storage_path: /registry
  210. DISTRIBUTED_STORAGE_DEFAULT_LOCATIONS: []
  211. DISTRIBUTED_STORAGE_PREFERENCE:
  212. - default
  213. FEATURE_MAILING: false
  214. SECRET_KEY: 7ce58d4d-b6f5-4400-ba6b-77b9f728a115
  215. SERVER_HOSTNAME: registry.ocp4.example.com
  216. PREFERRED_URL_SCHEME: https
  217. SETUP_COMPLETE: true
  218. SUPER_USERS:
  219. - admin
  220. TESTING: false
  221. USER_EVENTS_REDIS:
  222. host: redis
  223. password: verysecret
  224. port: 6379
  225. - name: Ensure Quay data dirs are owned by the correct user.
  226. become_method: containers.podman.podman_unshare
  227. become: yes
  228. ansible.builtin.file:
  229. path: "{{ item }}"
  230. state: directory
  231. owner: 1001
  232. loop:
  233. - /local/quay
  234. - "{{ ansible_facts['user_dir'] }}/config"
  235. - name: Ensure systemd user dir is there.
  236. ansible.builtin.file:
  237. path: "{{ ansible_facts['user_dir'] }}/.config/systemd/user"
  238. state: directory
  239. - name: Deploy service units.
  240. ansible.builtin.template:
  241. dest: "{{ ansible_facts['user_dir'] }}/.config/systemd/user/{{ item }}"
  242. src: "templates/{{ item }}.j2"
  243. loop:
  244. - quay-pg.service
  245. - quay-redis.service
  246. - quay.service
  247. - name: Reload systemd.
  248. ansible.builtin.systemd_service:
  249. daemon_reload: yes
  250. scope: user
  251. - name: Enable services and start them.
  252. ansible.builtin.systemd_service:
  253. name: "{{ item }}"
  254. scope: user
  255. state: started
  256. enabled: yes
  257. loop:
  258. - quay-pg
  259. - quay-redis
  260. - quay
  261. # TODO: create a new "admin" user via API:
  262. #
  263. # 1. send a GET request to registry
  264. # 2. extract _csrf_token value
  265. # 3. b64dec
  266. # 4. POST headers must include:
  267. # Cookie: _csrf_token=ORIG_B64ENC_VALUE
  268. # X-CSRF-Token: B64DEC_VALUE OF _csrf_token ATTRIBUTE
  269. #
  270. # 5. POST /api/v1/users/
  271. # Cookie: _csrf_token=...
  272. # X-CSRF-Token: ....
  273. # Accept: application/json
  274. # Content-Type: application/json
  275. # {
  276. # "email": "admin@example.com",
  277. # "username": "admin",
  278. # "password": "redhat123",
  279. # "repeatPassword": "redhat123"
  280. # }
  281. # 6. Response:
  282. # {"anonymous": false, "username": "admin", "avatar": {"name": "admin", "hash": "258d8dc916db8cea2cafb6c3cd0cb0246efe061421dbd83ec3a350428cabda4f", "color": "#98df8a", "kind": "user"}, "can_create_repo": true, "is_me": true, "verified": true, "email": "admin@example.com", "logins": [], "invoice_email": false, "invoice_email_address": null, "preferred_namespace": false, "tag_expiration_s": 1209600.0, "prompts": [], "company": null, "family_name": null, "given_name": null, "location": null, "is_free_account": true, "has_password_set": true, "organizations": [], "super_user": false}
  283. # TODO: Clair?
  284. ...