32-quay-deploy.yml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. get_attributes: no
  11. get_checksum: no
  12. get_mime: no
  13. register: qkey_file
  14. - name: Check if Quay cert exists to save time
  15. ansible.builtin.stat:
  16. path: "{{ ansible_facts['user_dir'] }}/ca/quay-cert.pem"
  17. get_attributes: no
  18. get_checksum: no
  19. get_mime: no
  20. register: qcert_file
  21. - name: Create a new private key for Quay, if it does not exist yet.
  22. community.crypto.openssl_privatekey:
  23. path: "{{ ansible_facts['user_dir'] }}/ca/quay-key.pem"
  24. type: RSA
  25. size: 4096
  26. mode: 0600
  27. when: qkey_file.stat.exists == false
  28. - name: Create a CSR for Quay
  29. community.crypto.openssl_csr:
  30. path: "{{ ansible_facts['user_dir'] }}/ca/quay-csr.pem"
  31. privatekey_path: "{{ ansible_facts['user_dir'] }}/ca/quay-key.pem"
  32. subject:
  33. C: US
  34. ST: North Carolina
  35. L: Raleigh
  36. O: Red Hat
  37. OU: RHT
  38. CN: registry.ocp4.example.com
  39. use_common_name_for_san: yes
  40. mode: 0600
  41. when: qcert_file.stat.exists == false
  42. - name: Issue a certificate for Quay if one isn't there yet.
  43. ansible.builtin.command:
  44. 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
  45. creates: "{{ ansible_facts['user_dir'] }}/ca/quay-cert.pem"
  46. - name: Load CA cert and Quay cert.
  47. ansible.builtin.set_fact:
  48. ca_cert: "{{ lookup('file', ansible_facts['user_dir'] + '/ca/ca-cert.pem') }}"
  49. quay_cert: "{{ lookup('file', ansible_facts['user_dir'] + '/ca/lab-ca/newcerts/00.pem') }}"
  50. - name: Concatenate Quay and CA certs.
  51. ansible.builtin.copy:
  52. dest: "{{ ansible_facts['user_dir'] }}/ca/quay-cert.pem"
  53. content: |
  54. {{ quay_cert }}
  55. {{ ca_cert }}
  56. - name: Prepare registry VM to run Quay services.
  57. hosts: registry.ocp4.example.com
  58. gather_subset: min
  59. tasks:
  60. - name: Ensure firewall allows HTTP/HTTPS.
  61. become: yes
  62. ansible.posix.firewalld:
  63. immediate: yes
  64. permanent: yes
  65. zone: public
  66. service: "{{ item }}"
  67. state: enabled
  68. loop:
  69. - http
  70. - https
  71. - name: Ensure unpriv users can open ports from 80 onwards.
  72. become: yes
  73. ansible.posix.sysctl:
  74. name: net.ipv4.ip_unprivileged_port_start
  75. value: "80"
  76. state: present
  77. sysctl_file: /etc/sysctl.d/quay-low-ports.conf
  78. reload: yes
  79. - name: Ensure user quay exists.
  80. become: yes
  81. ansible.builtin.user:
  82. name: quay
  83. create_home: yes
  84. state: present
  85. - name: Have the quay user accept student's SSH key.
  86. become: yes
  87. ansible.posix.authorized_key:
  88. key: "{{ lookup('ansible.builtin.file', '/home/student/.ssh/lab_rsa.pub') }}"
  89. user: quay
  90. state: present
  91. - name: Ensure user quay will linger.
  92. become: yes
  93. ansible.builtin.command:
  94. cmd: loginctl enable-linger quay
  95. creates: /var/lib/systemd/linger/quay
  96. - name: Ensure data directories are there.
  97. become: yes
  98. ansible.builtin.file:
  99. path: "{{ item }}"
  100. mode: 0770
  101. owner: quay
  102. group: quay
  103. state: directory
  104. loop:
  105. - /local/quay-pg
  106. - /local/quay
  107. - name: Ensure .docker directory is there
  108. become: yes
  109. ansible.builtin.file:
  110. path: "/home/quay/.docker"
  111. mode: 0700
  112. owner: quay
  113. group: quay
  114. state: directory
  115. # TODO: figure out how to customise this with registry host changes
  116. - name: Ensure podman will be able to log into the upstream registry
  117. become: yes
  118. ansible.builtin.copy:
  119. dest: "/home/quay/.docker/config.json"
  120. content: |
  121. {"auths":{"registry.redhat.io":{"auth":"fHVoYy1wb29sLTlmMDA1Mzc2LTM2YTItNDJhMS1hNTQwLTA0NzNkYzg3MzYzMzpleUpoYkdjaU9pSlNVelV4TWlKOS5leUp6ZFdJaU9pSTVPRGc1WVdFeFl6Qm1PV0kwWmpVM1lqazNObUk1WldFeU16SXdaalUwTUNKOS5zWmQ5VE1RbzBXREc2NUc5Qk1ObmtuYlBjRkIzNmhyRFhkMThfdTNLeHFaczdlOG1hQ19QeEFReGpwdVk0YVM2VERIbkxDNWpGYjRRNXFYVEpWbjJCOGE4cDFuY08tM24ySG5QdDg3NmktVUFDU3lldWtpb3k4aHI0V3d1ZkhReFVYMmxxWFhYdjN6blE3am1URUNBc25rWkNRSFU1dFNpRnNUZHhFZGZkeU42Z20xN3VqY2thZG5NbFBZcTZfU1I2bUtLaUpUdFQ3SFlDWXJBVk5zZ0tfNGFkZ2MtRXBlbEtHbGNERWkzNGhYbzFqbEIzRERyUWkxSUxCV0UwZkdXb1czZy1ZUzFGMFlEXzc0bm1XSU5mUE1jM25UOERaQWl0OEw0VlFPTnZnUE51YnVfTVVGUGhqX29VUjF3VUR0a1BRNktJdm82UWYyRkdwMndLM1B6YnRBRFFzRVZTZDlITzQ3a0RKdGFobk95YTFmRmdqZVk1bFNxLW1vT2RqUldCZ3U2XzNIX25lZExJR1lQRHRBZnp5cGJ1eHZ1cEd1M2hYWnVzeWN0aURtR203SkR5RW5KdjF1RFZmYVduU2EzSV9NcFRSVVcyZWU1RF9CanJleTdlU2I0bEpGcmp1eC1nY2JVaHFsWGJZc2l6azdXWHpvRmtrVFlMdXFDQ1FvS1J0OFdSN1UzTmh3c3Q2ckV3eEFOaWJFTlNzUVB3MGg4X0NDRm5qTHFSTl82cWpTc0tpeWRGT2tHVFliT0taTktaSVVhYkZFTjRhYVRVYmlYTVdPS2Eyak1xLUhwazBMNEowUmtOM2JkQVVqWmtERHE0ZFY1ZVFjdXNIeV9LY29nd1VKSjZ4MDNObnM4b0xBdjRJZ3RKeXlxcmE1YUJHSkxReHNjRXVSNzQwWQ=="}}}
  122. mode: 0600
  123. owner: quay
  124. group: quay
  125. - name: Configure containers and their environment on registry VM.
  126. hosts: registry.ocp4.example.com
  127. gather_subset: min
  128. remote_user: quay
  129. tasks:
  130. - name: Create a podman network, if necessary.
  131. containers.podman.podman_network:
  132. name: quay
  133. state: present
  134. - name: Pull all the images if necessary.
  135. containers.podman.podman_image:
  136. name: "{{ registry_host }}/{{ item }}"
  137. pull: yes
  138. state: present
  139. loop:
  140. - rhel9/postgresql-15:latest
  141. - rhel9/redis-7:latest
  142. - quay/quay-rhel8:v{{ quay_version }}
  143. - quay/clair-rhel8:v{{ quay_version }}
  144. # TODO: recursive!
  145. - name: Ensure PG datadir is owned by the correct user.
  146. become_method: containers.podman.podman_unshare
  147. become: yes
  148. ansible.builtin.file:
  149. path: /local/quay-pg
  150. state: directory
  151. owner: 26
  152. mode: 0770
  153. - name: Start postgres container if necessary.
  154. containers.podman.podman_container:
  155. name: postgresql
  156. image: "{{ registry_host }}/rhel9/postgresql-15:latest"
  157. rm: yes
  158. detach: yes
  159. env:
  160. POSTGRESQL_USER: quay
  161. POSTGRESQL_PASSWORD: secret
  162. POSTGRESQL_DATABASE: quay
  163. POSTGRESQL_ADMIN_PASSWORD: verysecret
  164. network:
  165. - quay
  166. volumes:
  167. - /local/quay-pg:/var/lib/pgsql/data:Z
  168. state: started
  169. register: pg_started
  170. - name: Wait for the PostgreSQL container to become ready if it was changed in any way.
  171. containers.podman.podman_container_info:
  172. name: postgresql
  173. when: pg_started.changed
  174. register: pg_info
  175. until: pg_info.containers[0].State.Running
  176. retries: 12
  177. delay: 5
  178. - name: Create the trigram extension if necessary.
  179. containers.podman.podman_container_exec:
  180. name: postgresql
  181. command: 'psql -d quay -U postgres -c "CREATE EXTENSION IF NOT EXISTS pg_trgm"'
  182. register: pg_ext
  183. changed_when:
  184. - not "already exists" in pg_ext.stderr
  185. - name: If we started the PG container and created the extension, stop the container now.
  186. containers.podman.podman_container:
  187. name: postgresql
  188. state: stopped
  189. when:
  190. - pg_started.changed
  191. - pg_ext.changed
  192. - name: Create Quay config directory if necessary.
  193. ansible.builtin.file:
  194. path: "{{ ansible_facts['user_dir'] }}/config"
  195. state: directory
  196. mode: 0770
  197. - name: Publish Quay key on registry.
  198. ansible.builtin.copy:
  199. src: /home/student/ca/quay-key.pem
  200. dest: "{{ ansible_facts['user_dir'] }}/config/ssl.key"
  201. mode: 0440
  202. - name: Publish Quay cert on registry.
  203. ansible.builtin.copy:
  204. src: /home/student/ca/quay-cert.pem
  205. dest: "{{ ansible_facts['user_dir'] }}/config/ssl.cert"
  206. mode: 0440
  207. - name: Publish Quay config file.
  208. ansible.builtin.copy:
  209. dest: "{{ ansible_facts['user_dir'] }}/config/config.yaml"
  210. content: |
  211. BUILDLOGS_REDIS:
  212. host: redis
  213. password: verysecret
  214. port: 6379
  215. CREATE_NAMESPACE_ON_PUSH: true
  216. DATABASE_SECRET_KEY: 410c87de-8ad8-4f4c-9670-2ec25bc87191
  217. DB_URI: postgresql://quay:secret@postgresql:5432/quay
  218. DISTRIBUTED_STORAGE_CONFIG:
  219. default:
  220. - LocalStorage
  221. - storage_path: /registry
  222. DISTRIBUTED_STORAGE_DEFAULT_LOCATIONS: []
  223. DISTRIBUTED_STORAGE_PREFERENCE:
  224. - default
  225. FEATURE_MAILING: false
  226. SECRET_KEY: 7ce58d4d-b6f5-4400-ba6b-77b9f728a115
  227. SERVER_HOSTNAME: registry.ocp4.example.com
  228. PREFERRED_URL_SCHEME: https
  229. SETUP_COMPLETE: true
  230. SUPER_USERS:
  231. - admin
  232. TESTING: false
  233. USER_EVENTS_REDIS:
  234. host: redis
  235. password: verysecret
  236. port: 6379
  237. mode: 0660
  238. # TODO: recursive!
  239. - name: Ensure Quay data dirs are owned by the correct user.
  240. become_method: containers.podman.podman_unshare
  241. become: yes
  242. ansible.builtin.file:
  243. path: "{{ item }}"
  244. state: directory
  245. owner: 1001
  246. loop:
  247. - /local/quay
  248. - "{{ ansible_facts['user_dir'] }}/config"
  249. - name: Ensure systemd user dir is there.
  250. ansible.builtin.file:
  251. path: "{{ ansible_facts['user_dir'] }}/.config/systemd/user"
  252. state: directory
  253. - name: Deploy service units.
  254. ansible.builtin.template:
  255. dest: "{{ ansible_facts['user_dir'] }}/.config/systemd/user/{{ item }}"
  256. src: "templates/{{ item }}.j2"
  257. loop:
  258. - quay-pg.service
  259. - quay-redis.service
  260. - quay.service
  261. - name: Reload systemd.
  262. ansible.builtin.systemd_service:
  263. daemon_reload: yes
  264. scope: user
  265. - name: Enable services and start them.
  266. ansible.builtin.systemd_service:
  267. name: "{{ item }}"
  268. scope: user
  269. state: started
  270. enabled: yes
  271. loop:
  272. - quay-pg
  273. - quay-redis
  274. - quay
  275. register: startup
  276. - name: Wait a bit if the Quay container was just started.
  277. ansible.builtin.uri:
  278. method: GET
  279. url: https://registry.ocp4.example.com/
  280. headers:
  281. Accept: application/json
  282. Content-Type: application/json
  283. validate_certs: no
  284. status_code:
  285. - 200
  286. - 404
  287. - 502
  288. when: startup.results[2].changed
  289. register: startup_wait
  290. until: startup_wait.status == 200
  291. retries: 30
  292. delay: 5
  293. - name: Check if the admin user exists already.
  294. ansible.builtin.uri:
  295. method: GET
  296. url: https://registry.ocp4.example.com/api/v1/users/admin
  297. headers:
  298. Accept: application/json
  299. Content-Type: application/json
  300. validate_certs: no
  301. status_code:
  302. - 200
  303. - 404
  304. return_content: yes
  305. register: adminuser_is_there
  306. - name: Create an admin user if not yet there.
  307. block:
  308. - name: Obtain an encoded CSRF token.
  309. ansible.builtin.uri:
  310. method: GET
  311. url: https://registry.ocp4.example.com/
  312. headers:
  313. Accept: application/json
  314. Content-Type: application/json
  315. validate_certs: no
  316. return_content: yes
  317. ignore_errors: yes
  318. register: csrf_token_payload
  319. - ansible.builtin.assert:
  320. that:
  321. - csrf_token_payload.cookies['_csrf_token'] is defined
  322. fail_msg: "No CSRF token returned by registry. Can not proceed."
  323. success_msg: "Good, CSRF token found in response."
  324. # In case of issues, run with -v and this will show the raw cookie.
  325. - ansible.builtin.debug:
  326. var: csrf_token_payload.cookies
  327. verbosity: 1
  328. - name: Store the cookie as a new fact. We need it later.
  329. ansible.builtin.set_fact:
  330. csrf_cookie: "{{ csrf_token_payload.cookies['_csrf_token'] }}"
  331. # In case of issues, run with -v and this will show the cookie payload.
  332. - ansible.builtin.debug:
  333. var: csrf_cookie
  334. verbosity: 1
  335. # Must chop out the part of the token before the first dot (the rest is control shit).
  336. # Next, and pad it (==) at the end to have 112 characters (no checking done here).
  337. # Lastly, convert that from JSON to a dict and obtain the value of the token (_csrf_token).
  338. - name: Store CSRF token as a new fact.
  339. ansible.builtin.set_fact:
  340. csrf_token: "{{ (csrf_token_payload.cookies['_csrf_token'] | ansible.builtin.regex_replace('^(\\w+)\\..*$', '\\1==') | ansible.builtin.b64decode | ansible.builtin.from_json)['_csrf_token'] }}"
  341. # In case of issues, run with -v and this will show the decoded token.
  342. - ansible.builtin.debug:
  343. var: csrf_token
  344. verbosity: 1
  345. - name: Send a POST request to registry API to create the admin user.
  346. ansible.builtin.uri:
  347. method: POST
  348. url: https://registry.ocp4.example.com/api/v1/user/
  349. headers:
  350. Accept: application/json
  351. Content-Type: application/json
  352. Cookie: _csrf_token={{ csrf_cookie }}
  353. X-CSRF-Token: "{{ csrf_token }}"
  354. body: |
  355. {
  356. "username": "admin",
  357. "password": "redhat123",
  358. "repeatPassword": "redhat123",
  359. "email": "admin@example.com"
  360. }
  361. body_format: json
  362. validate_certs: no
  363. return_content: yes
  364. register: admin_user_response
  365. # In case of issues, run with -v and this will show the response.
  366. - ansible.builtin.debug:
  367. var: admin_user_response
  368. verbosity: 1
  369. when: adminuser_is_there.status == 404
  370. ...