--- - name: Deploy bloom hosts: bloom gather_facts: false vars: repo: git@tangled.org:proteus.works/bloom branch: main app_dir: /opt/apps/bloom database: bloom_prod data_dir: /opt/apps/bloom-data local_taxonomy: "{{ playbook_dir }}/../data/taxonomy" taxonomy_db: "{{ playbook_dir }}/../data/taxonomy/taxonomy.sqlite" compose: >- docker compose --project-directory /opt/apps -p apps -f /opt/apps/docker-compose.yml -f /opt/apps/bloom/docker-compose.yml tasks: - name: Check the server has been provisioned ansible.builtin.stat: path: /opt/apps/bloom-secrets/age.txt register: age_key - name: Fail if the age identity is missing ansible.builtin.assert: that: age_key.stat.exists fail_msg: >- /opt/apps/bloom-secrets/age.txt is missing, so the container cannot decrypt fnox.toml. Run `task provision` first. success_msg: server has an age identity - name: Pull the latest code ansible.builtin.git: repo: "{{ repo }}" dest: "{{ app_dir }}" version: "{{ branch }}" force: true register: pull - name: Report the deployed revision ansible.builtin.debug: msg: "{{ app_dir }} is at {{ pull.after[:7] }}" - name: Check the taxonomy data is staged locally ansible.builtin.stat: path: "{{ taxonomy_db }}" delegate_to: localhost register: staged tags: [data] - name: Fail if the taxonomy data has not been staged ansible.builtin.assert: that: staged.stat.exists | default(false) fail_msg: "No {{ taxonomy_db }} -- run `task data:sync` first." success_msg: taxonomy data staged tags: [data] - name: Create the data directory ansible.builtin.file: path: "{{ data_dir }}/taxonomy" state: directory mode: "0755" tags: [data] # owner/group off because -a over ssh preserves the *local* uid, which is # nobody here and deploy cannot chown. Not --inplace, so the swap is atomic. - name: Ship the taxonomy data ansible.posix.synchronize: src: "{{ local_taxonomy }}/" dest: "{{ data_dir }}/taxonomy/" archive: true delete: true owner: false group: false rsync_opts: - "--include=*.sqlite" - "--exclude=*" notify: restart bloom tags: [data] - name: Generate Umami's secrets ansible.builtin.shell: cmd: >- umask 077 && printf 'APP_SECRET=%s\nTWO_FACTOR_ENCRYPTION_KEY=%s\n' "$(openssl rand -hex 32)" "$(openssl rand -hex 32)" > /opt/apps/bloom-secrets/umami.env creates: /opt/apps/bloom-secrets/umami.env - name: Build the image on the server ansible.builtin.command: cmd: "{{ compose }} build bloom" changed_when: true - name: Create the databases if they are missing ansible.builtin.shell: cmd: >- {{ compose }} exec -T postgres psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname='{{ item }}'" | grep -q 1 || {{ compose }} exec -T postgres createdb -U postgres {{ item }} loop: ["{{ database }}", umami] changed_when: true - name: Run migrations ansible.builtin.command: cmd: "{{ compose }} run --rm --no-deps bloom fnox exec -- /app/bin/migrate" changed_when: true - name: Start the server ansible.builtin.command: cmd: "{{ compose }} up -d --no-build --pull never --no-deps bloom" changed_when: true - name: Start VictoriaMetrics and Umami ansible.builtin.command: cmd: "{{ compose }} up -d --no-deps victoriametrics umami" changed_when: true # Caddy imports /opt/apps/caddy-sites/*.caddy, so bloom's site blocks live # here rather than in the infra repo. A bad snippet fails the reload and # leaves the running config untouched. - name: Create the Caddy site directory ansible.builtin.file: path: /opt/apps/caddy-sites state: directory mode: "0755" - name: Install the Caddy site config ansible.builtin.copy: src: "{{ playbook_dir }}/caddy/bloom.caddy" dest: /opt/apps/caddy-sites/bloom.caddy mode: "0644" register: caddy_site - name: Reload Caddy ansible.builtin.command: cmd: "{{ compose }} exec -T -w /etc/caddy caddy caddy reload --config /etc/caddy/Caddyfile" when: caddy_site.changed changed_when: true - name: Read back the container state ansible.builtin.command: cmd: "{{ compose }} ps bloom" changed_when: false register: status - name: Show the container state ansible.builtin.debug: msg: "{{ status.stdout_lines }}" handlers: # Pooled connections hold open handles, so after rsync renames the file # they still read the old inode. Only fires when the data actually changed. - name: restart bloom ansible.builtin.command: cmd: "{{ compose }} restart bloom" changed_when: true