02_bootstrap.ymlΒΆ

---
# Keep this wait at the top of the flow: every later artisan command assumes it.

- name: "⏳ Wait until Pixelfed application container finished entrypoint bootstrap"
  ansible.builtin.command:
    argv:
      - container
      - exec
      - "{{ PIXELFED_LOCAL_CID }}" # nocheck: container-exec-resolver  PIXELFED_LOCAL_CID resolved at delegate_to runtime
      - sh
      - -lc
      - 'ps -ef | grep -v grep | grep -q "frankenphp run"'
  register: pixelfed_entrypoint_bootstrap
  changed_when: false
  retries: 120
  delay: 5
  until: pixelfed_entrypoint_bootstrap.rc == 0
  delegate_to: "{{ PIXELFED_HOST_NODE }}"

- name: "πŸ”Ž Check whether the Pixelfed bootstrap marker exists"
  ansible.builtin.command:
    argv:
      - container
      - exec
      - "{{ PIXELFED_LOCAL_CID }}" # nocheck: container-exec-resolver  PIXELFED_LOCAL_CID resolved at delegate_to runtime
      - sh
      - -lc
      - test -e {{ PIXELFED_INIT_MARKER }}
  register: pixelfed_bootstrap_marker_exists
  changed_when: false
  failed_when: false
  delegate_to: "{{ PIXELFED_HOST_NODE }}"

- name: "πŸ”Ž Check whether the Pixelfed users table exists"
  database_query:
    config: "{{ lookup('database', application_id) }}"
    query_file: "{{ role_path }}/files/sql/select/users-table.sql"
    named_args:
      db_name: "{{ PIXELFED_DB_NAME }}"
    expect_rows: true
    mask_values: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
  register: pixelfed_bootstrap_users_table
  changed_when: false
  retries: 20
  delay: 3
  until: pixelfed_bootstrap_users_table is success
  no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"

- name: "πŸ‘₯ Count existing Pixelfed users when the users table already exists"
  database_query:
    config: "{{ lookup('database', application_id) }}"
    query_file: "{{ role_path }}/files/sql/count-users.sql"
    expect_rows: true
    mask_values: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
  when: pixelfed_bootstrap_users_table.rows | length > 0
  register: pixelfed_bootstrap_user_count
  changed_when: false
  retries: 20
  delay: 3
  until: pixelfed_bootstrap_user_count is success
  no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"

- name: "🚚 Choose Pixelfed migration strategy"
  ansible.builtin.set_fact:
    pixelfed_bootstrap_requires_reset: >-
      {{
        (pixelfed_bootstrap_marker_exists.rc | default(1) | int) != 0
        and ((pixelfed_bootstrap_user_count.rows[0][0] | default('0')) | int) == 0
      }}
    pixelfed_migrate_argv: >-
      {{
        ['container', 'exec', '-u', PIXELFED_APP_USER, PIXELFED_LOCAL_CID, 'php', 'artisan']
        + (
          ['migrate:fresh', '--force']
          if (
            (pixelfed_bootstrap_marker_exists.rc | default(1) | int) != 0
            and ((pixelfed_bootstrap_user_count.rows[0][0] | default('0')) | int) == 0
          )
          else ['migrate', '--force', '--isolated']
        )
      }}

- name: "▢️ Run Pixelfed migrations"
  ansible.builtin.command:
    argv: "{{ pixelfed_migrate_argv }}"
  register: pixelfed_migrate
  changed_when: "'Nothing to migrate' not in pixelfed_migrate.stdout"
  retries: 60
  delay: 5
  until: pixelfed_migrate.rc == 0
  delegate_to: "{{ PIXELFED_HOST_NODE }}"

- name: "βœ… Ensure Pixelfed Passport keys exist"
  ansible.builtin.command:
    argv:
      - container
      - exec
      - -u
      - "{{ PIXELFED_APP_USER }}"
      - "{{ PIXELFED_LOCAL_CID }}" # nocheck: container-exec-resolver  PIXELFED_LOCAL_CID resolved at delegate_to runtime
      - sh
      - -lc
      - |
        if [ ! -f {{ PIXELFED_STORAGE_DIR }}/oauth-private.key ] || [ ! -f {{ PIXELFED_STORAGE_DIR }}/oauth-public.key ]; then
          php artisan passport:keys --force
        fi
  register: pixelfed_passport_keys
  changed_when: false
  retries: 20
  delay: 5
  until: pixelfed_passport_keys.rc == 0
  delegate_to: "{{ PIXELFED_HOST_NODE }}"

- name: "βœ… Ensure the Pixelfed public storage symlink exists"
  ansible.builtin.command:
    argv:
      - container
      - exec
      - -u
      - "{{ PIXELFED_APP_USER }}"
      - "{{ PIXELFED_LOCAL_CID }}" # nocheck: container-exec-resolver  PIXELFED_LOCAL_CID resolved at delegate_to runtime
      - sh
      - -lc
      - |
        mkdir -p {{ PIXELFED_PUBLIC_STORAGE_SOURCE }}
        if [ ! -L {{ PIXELFED_PUBLIC_STORAGE_LINK }} ]; then
          php artisan storage:link
        fi
  register: pixelfed_storage_link
  changed_when: false
  retries: 20
  delay: 5
  until: pixelfed_storage_link.rc == 0
  delegate_to: "{{ PIXELFED_HOST_NODE }}"

- name: "🎬 Initialize the Pixelfed instance actor"
  ansible.builtin.command:
    argv:
      - container
      - exec
      - -u
      - "{{ PIXELFED_APP_USER }}"
      - "{{ PIXELFED_LOCAL_CID }}" # nocheck: container-exec-resolver  PIXELFED_LOCAL_CID resolved at delegate_to runtime
      - php
      - artisan
      - instance:actor
  register: pixelfed_instance_actor
  changed_when: false
  retries: 20
  delay: 5
  until: pixelfed_instance_actor.rc == 0
  delegate_to: "{{ PIXELFED_HOST_NODE }}"

- name: "🏷️ Mark Pixelfed bootstrap as complete for the worker container"
  ansible.builtin.command:
    argv:
      - container
      - exec
      - -u
      - "{{ PIXELFED_APP_USER }}"
      - "{{ PIXELFED_LOCAL_CID }}" # nocheck: container-exec-resolver  PIXELFED_LOCAL_CID resolved at delegate_to runtime
      - sh
      - -lc
      - |
        if [ ! -e {{ PIXELFED_INIT_MARKER }} ]; then
          touch {{ PIXELFED_INIT_MARKER }}
        fi
  register: pixelfed_bootstrap_marker
  changed_when: false
  retries: 20
  delay: 5
  until: pixelfed_bootstrap_marker.rc == 0
  delegate_to: "{{ PIXELFED_HOST_NODE }}"