32-quay-deploy.yml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. # TODO: recursive!
  139. - name: Ensure PG datadir is owned by the correct user.
  140. become_method: containers.podman.podman_unshare
  141. become: yes
  142. ansible.builtin.file:
  143. path: /local/quay-pg
  144. state: directory
  145. owner: 26
  146. mode: 0770
  147. - name: Start postgres container if necessary.
  148. containers.podman.podman_container:
  149. name: postgresql
  150. image: "{{ registry_host }}/rhel9/postgresql-15:latest"
  151. rm: yes
  152. detach: yes
  153. env:
  154. POSTGRESQL_USER: quay
  155. POSTGRESQL_PASSWORD: secret
  156. POSTGRESQL_DATABASE: quay
  157. POSTGRESQL_ADMIN_PASSWORD: verysecret
  158. network:
  159. - quay
  160. volumes:
  161. - /local/quay-pg:/var/lib/pgsql/data:Z
  162. state: started
  163. register: pg_started
  164. - name: Wait for the PostgreSQL container to become ready if it was changed in any way.
  165. containers.podman.podman_container_info:
  166. name: postgresql
  167. when: pg_started.changed
  168. register: pg_info
  169. until: pg_info.containers[0].State.Running
  170. retries: 12
  171. delay: 5
  172. - name: Create the trigram extension if necessary.
  173. containers.podman.podman_container_exec:
  174. name: postgresql
  175. command: 'psql -d quay -U postgres -c "CREATE EXTENSION IF NOT EXISTS pg_trgm"'
  176. register: pg_ext
  177. changed_when:
  178. - not "already exists" in pg_ext.stderr
  179. - name: If we started the PG container and created the extension, stop the container now.
  180. containers.podman.podman_container:
  181. name: postgresql
  182. state: stopped
  183. when:
  184. - pg_started.changed
  185. - pg_ext.changed
  186. - name: Create Quay config directory if necessary.
  187. ansible.builtin.file:
  188. path: "{{ ansible_facts['user_dir'] }}/config"
  189. state: directory
  190. mode: 0770
  191. - name: Publish Quay key on registry.
  192. ansible.builtin.copy:
  193. src: /home/student/ca/quay-key.pem
  194. dest: "{{ ansible_facts['user_dir'] }}/config/ssl.key"
  195. mode: 0440
  196. - name: Publish Quay cert on registry.
  197. ansible.builtin.copy:
  198. src: /home/student/ca/quay-cert.pem
  199. dest: "{{ ansible_facts['user_dir'] }}/config/ssl.cert"
  200. mode: 0440
  201. - name: Publish Quay config file.
  202. ansible.builtin.copy:
  203. dest: "{{ ansible_facts['user_dir'] }}/config/config.yaml"
  204. content: |
  205. BUILDLOGS_REDIS:
  206. host: redis
  207. password: verysecret
  208. port: 6379
  209. CREATE_NAMESPACE_ON_PUSH: true
  210. DATABASE_SECRET_KEY: 410c87de-8ad8-4f4c-9670-2ec25bc87191
  211. DB_URI: postgresql://quay:secret@postgresql:5432/quay
  212. DISTRIBUTED_STORAGE_CONFIG:
  213. default:
  214. - LocalStorage
  215. - storage_path: /registry
  216. DISTRIBUTED_STORAGE_DEFAULT_LOCATIONS: []
  217. DISTRIBUTED_STORAGE_PREFERENCE:
  218. - default
  219. FEATURE_MAILING: false
  220. SECRET_KEY: 7ce58d4d-b6f5-4400-ba6b-77b9f728a115
  221. SERVER_HOSTNAME: registry.ocp4.example.com
  222. PREFERRED_URL_SCHEME: https
  223. SETUP_COMPLETE: true
  224. SUPER_USERS:
  225. - admin
  226. TESTING: false
  227. USER_EVENTS_REDIS:
  228. host: redis
  229. password: verysecret
  230. port: 6379
  231. mode: 0660
  232. # TODO: recursive!
  233. - name: Ensure Quay data dirs are owned by the correct user.
  234. become_method: containers.podman.podman_unshare
  235. become: yes
  236. ansible.builtin.file:
  237. path: "{{ item }}"
  238. state: directory
  239. owner: 1001
  240. loop:
  241. - /local/quay
  242. - "{{ ansible_facts['user_dir'] }}/config"
  243. - name: Ensure systemd user dir is there.
  244. ansible.builtin.file:
  245. path: "{{ ansible_facts['user_dir'] }}/.config/systemd/user"
  246. state: directory
  247. - name: Deploy service units.
  248. ansible.builtin.template:
  249. dest: "{{ ansible_facts['user_dir'] }}/.config/systemd/user/{{ item }}"
  250. src: "templates/{{ item }}.j2"
  251. loop:
  252. - quay-pg.service
  253. - quay-redis.service
  254. - quay.service
  255. - name: Reload systemd.
  256. ansible.builtin.systemd_service:
  257. daemon_reload: yes
  258. scope: user
  259. - name: Enable services and start them.
  260. ansible.builtin.systemd_service:
  261. name: "{{ item }}"
  262. scope: user
  263. state: started
  264. enabled: yes
  265. loop:
  266. - quay-pg
  267. - quay-redis
  268. - quay
  269. register: startup
  270. - name: Wait a bit if the Quay container was just started.
  271. ansible.builtin.uri:
  272. method: GET
  273. url: https://registry.ocp4.example.com/
  274. headers:
  275. Accept: application/json
  276. Content-Type: application/json
  277. validate_certs: no
  278. status_code:
  279. - 200
  280. - 404
  281. - 502
  282. when: startup.results[2].changed
  283. register: startup_wait
  284. until: startup_wait.status == 200
  285. retries: 30
  286. delay: 5
  287. - name: Check if the admin user exists already.
  288. ansible.builtin.uri:
  289. method: GET
  290. url: https://registry.ocp4.example.com/api/v1/users/admin
  291. headers:
  292. Accept: application/json
  293. Content-Type: application/json
  294. validate_certs: no
  295. status_code:
  296. - 200
  297. - 404
  298. return_content: yes
  299. register: adminuser_is_there
  300. - name: Create an admin user if not yet there.
  301. block:
  302. - name: Obtain an encoded CSRF token.
  303. ansible.builtin.uri:
  304. method: GET
  305. url: https://registry.ocp4.example.com/
  306. headers:
  307. Accept: application/json
  308. Content-Type: application/json
  309. validate_certs: no
  310. return_content: yes
  311. ignore_errors: yes
  312. register: csrf_token_payload
  313. - ansible.builtin.assert:
  314. that:
  315. - csrf_token_payload.cookies['_csrf_token'] is defined
  316. fail_msg: "No CSRF token returned by registry. Can not proceed."
  317. success_msg: "Good, CSRF token found in response."
  318. # In case of issues, run with -v and this will show the raw cookie.
  319. - ansible.builtin.debug:
  320. var: csrf_token_payload.cookies
  321. verbosity: 1
  322. - name: Store the cookie as a new fact. We need it later.
  323. ansible.builtin.set_fact:
  324. csrf_cookie: "{{ csrf_token_payload.cookies['_csrf_token'] }}"
  325. # In case of issues, run with -v and this will show the cookie payload.
  326. - ansible.builtin.debug:
  327. var: csrf_cookie
  328. verbosity: 1
  329. # Must chop out the part of the token before the first dot (the rest is control shit).
  330. # Next, and pad it (==) at the end to have 112 characters (no checking done here).
  331. # Lastly, convert that from JSON to a dict and obtain the value of the token (_csrf_token).
  332. - name: Store CSRF token as a new fact.
  333. ansible.builtin.set_fact:
  334. csrf_token: "{{ (csrf_token_payload.cookies['_csrf_token'] | ansible.builtin.regex_replace('^(\\w+)\\..*$', '\\1==') | ansible.builtin.b64decode | ansible.builtin.from_json)['_csrf_token'] }}"
  335. # In case of issues, run with -v and this will show the decoded token.
  336. - ansible.builtin.debug:
  337. var: csrf_token
  338. verbosity: 1
  339. - name: Send a POST request to registry API to create the admin user.
  340. ansible.builtin.uri:
  341. method: POST
  342. url: https://registry.ocp4.example.com/api/v1/user/
  343. headers:
  344. Accept: application/json
  345. Content-Type: application/json
  346. Cookie: _csrf_token={{ csrf_cookie }}
  347. X-CSRF-Token: "{{ csrf_token }}"
  348. body: |
  349. {
  350. "username": "admin",
  351. "password": "redhat123",
  352. "repeatPassword": "redhat123",
  353. "email": "admin@example.com"
  354. }
  355. body_format: json
  356. validate_certs: no
  357. return_content: yes
  358. register: admin_user_response
  359. # In case of issues, run with -v and this will show the response.
  360. - ansible.builtin.debug:
  361. var: admin_user_response
  362. verbosity: 1
  363. when: adminuser_is_there.status == 404
  364. ...