Files
ilnmors-homelab/ansible/roles/app/tasks/services/set_mastodon.yaml
T
2026-07-05 23:25:42 +09:00

277 lines
11 KiB
YAML

---
- name: Set redis service name
ansible.builtin.set_fact:
redis_service: "mastodon"
- name: Create redis_mastodon directory
ansible.builtin.file:
path: "{{ node['home_path'] }}/{{ item }}"
state: "directory"
owner: "{{ services['redis']['subuid'] }}"
group: "{{ node['group'] }}"
mode: "0770"
loop:
- "containers/redis"
- "containers/redis/{{ redis_service }}"
- "containers/redis/{{ redis_service }}/data"
become: true
- name: Deploy redis config file
ansible.builtin.template:
src: "{{ hostvars['console']['node']['config_path'] }}/services/containers/app/redis/redis.conf.j2"
dest: "{{ node['home_path'] }}/containers/redis/{{ redis_service }}/redis.conf"
owner: "{{ ansible_user }}"
group: "{{ node['group'] }}"
mode: "0644"
register: "is_redis_conf"
- name: Deploy redis container file
ansible.builtin.template:
src: "{{ hostvars['console']['node']['config_path'] }}/services/containers/app/redis/redis.container.j2"
dest: "{{ node['home_path'] }}/.config/containers/systemd/redis_{{ redis_service }}.container"
owner: "{{ ansible_user }}"
group: "{{ node['group'] }}"
mode: "0644"
register: "is_redis_containerfile"
- name: Enable (Restart) redis service
ansible.builtin.systemd:
name: "redis_{{ redis_service }}.service"
state: "restarted"
enabled: true
daemon_reload: true
scope: "user"
when: is_redis_conf.changed or is_redis_containerfile.changed # noqa: no-handler
- name: Create mastodon directory
ansible.builtin.file:
path: "{{ node['home_path'] }}/{{ item }}"
state: "directory"
owner: "{{ services['mastodon']['subuid'] }}"
group: "{{ node['group'] }}"
mode: "0770"
loop:
- "data/containers/mastodon"
- "data/containers/mastodon/system"
- "containers/mastodon"
- "containers/mastodon/ssl"
become: true
- name: Check data directory empty
ansible.builtin.stat:
path: "{{ node['home_path'] }}/data/containers/mastodon/.init"
register: "is_mastodon_init"
- name: Check migrate file
ansible.builtin.stat:
path: "{{ node['home_path'] }}/data/containers/mastodon/.migrated_{{ version['containers']['mastodon'] }}"
register: "is_mastodon_migrated"
- name: Deploy root certificate
ansible.builtin.copy:
content: |
{{ hostvars['console']['ca']['root']['crt'] }}
dest: "{{ node['home_path'] }}/containers/mastodon/ssl/{{ root_cert_filename }}"
owner: "{{ services['mastodon']['subuid'] }}"
group: "{{ node['group'] }}"
mode: "0440"
become: true
notify: "notification_restart_mastodon"
no_log: true
- name: Register secret value to podman secret
containers.podman.podman_secret:
name: "{{ item.name }}"
data: "{{ item.value }}"
state: "present"
force: true
loop:
- name: "MASTODON_SECRET_KEY_BASE"
value: "{{ hostvars['console']['mastodon']['secret_key'] }}"
- name: "MASTODON_ENCRYPTION_PRIMARY_KEY"
value: "{{ hostvars['console']['mastodon']['encryption']['primary_key'] }}"
- name: "MASTODON_ENCRYPTION_DETERMINISTIC_KEY"
value: "{{ hostvars['console']['mastodon']['encryption']['deterministic_key'] }}"
- name: "MASTODON_ENCRYPTION_SALT"
value: "{{ hostvars['console']['mastodon']['encryption']['salt'] }}"
- name: "MASTODON_POSTGRES_PASSWORD"
value: "{{ hostvars['console']['postgresql']['password']['mastodon'] }}"
- name: "MASTODON_OIDC_CLIENT_SECRET"
value: "{{ hostvars['console']['mastodon']['oidc']['secret'] }}"
- name: "MASTODON_VAPID_PRIVATE_KEY"
value: "{{ hostvars['console']['mastodon']['vapid']['private_key'] }}"
- name: "MASTODON_VAPID_PUBLIC_KEY"
value: "{{ hostvars['console']['mastodon']['vapid']['public_key'] }}"
notify: "notification_restart_mastodon"
no_log: true
- name: Pull image
containers.podman.podman_image:
name: "{{ item }}"
tag: "{{ version['containers']['mastodon'] }}"
state: "present"
pull: true
loop:
- "ghcr.io/mastodon/mastodon"
- "ghcr.io/mastodon/mastodon-streaming"
- name: Initialize mastodon
when: not is_mastodon_init.stat.exists
block:
- name: Execute init command
containers.podman.podman_container:
name: "mastodon_init"
image: "ghcr.io/mastodon/mastodon:{{ version['containers']['mastodon'] }}"
command: "bundle exec rails db:prepare"
state: "started"
rm: true
detach: false
env:
LOCAL_DOMAIN: "{{ services['mastodon']['domain']['public'] }}.{{ domain['public'] }}"
SAFETY_ASSURED: "1"
DB_HOST: "{{ services['postgresql']['domain']['internal'] }}.{{ domain['internal'] }}"
DB_PORT: "{{ services['postgresql']['ports']['tcp'] }}"
DB_NAME: "mastodon_db"
DB_USER: "mastodon"
DB_PASS: "{{ hostvars['console']['postgresql']['password']['mastodon'] }}"
DB_SSLMODE: "verify-full"
PGSSLROOTCERT: "/etc/ssl/mastodon/{{ root_cert_filename }}"
REDIS_URL: "redis://host.containers.internal:{{ services['mastodon']['ports']['redis'] }}"
SECRET_KEY_BASE: "{{ hostvars['console']['mastodon']['secret_key'] }}"
ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY: "{{ hostvars['console']['mastodon']['encryption']['primary_key'] }}"
ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY: "{{ hostvars['console']['mastodon']['encryption']['deterministic_key'] }}"
ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT: "{{ hostvars['console']['mastodon']['encryption']['salt'] }}"
volume:
- "{{ node['home_path'] }}/containers/mastodon/ssl:/etc/ssl/mastodon:ro"
- "{{ node['home_path'] }}/data/containers/mastodon/system:/mastodon/public/system:rw"
no_log: true
- name: Create .init file
ansible.builtin.file:
path: "{{ node['home_path'] }}/data/containers/mastodon/.init"
state: "touch"
mode: "0644"
owner: "{{ ansible_user }}"
group: "{{ node['group'] }}"
- name: Create .migrated file
ansible.builtin.file:
path: "{{ node['home_path'] }}/data/containers/mastodon/.migrated_{{ version['containers']['mastodon'] }}"
state: "touch"
mode: "0644"
owner: "{{ ansible_user }}"
group: "{{ node['group'] }}"
- name: Migrate DB of mastodon
when: not is_mastodon_migrated.stat.exists and is_mastodon_init.stat.exists
block:
- name: Stop the services
ansible.builtin.systemd:
name: "{{ item }}"
state: "stopped"
enabled: true
daemon_reload: true
scope: "user"
loop:
- "mastodon-web.service"
- "mastodon-worker.service"
- "mastodon-streaming.service"
- name: Execute migrate command
containers.podman.podman_container:
name: "mastodon_migrate"
image: "ghcr.io/mastodon/mastodon:{{ version['containers']['mastodon'] }}"
command: "bundle exec rails db:migrate"
state: "started"
rm: true
detach: false
env:
LOCAL_DOMAIN: "{{ services['mastodon']['domain']['public'] }}.{{ domain['public'] }}"
DB_HOST: "{{ services['postgresql']['domain']['internal'] }}.{{ domain['internal'] }}"
DB_PORT: "{{ services['postgresql']['ports']['tcp'] }}"
DB_NAME: "mastodon_db"
DB_USER: "mastodon"
DB_PASS: "{{ hostvars['console']['postgresql']['password']['mastodon'] }}"
DB_SSLMODE: "verify-full"
PGSSLROOTCERT: "/etc/ssl/mastodon/{{ root_cert_filename }}"
REDIS_URL: "redis://host.containers.internal:{{ services['mastodon']['ports']['redis'] }}"
SECRET_KEY_BASE: "{{ hostvars['console']['mastodon']['secret_key'] }}"
ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY: "{{ hostvars['console']['mastodon']['encryption']['primary_key'] }}"
ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY: "{{ hostvars['console']['mastodon']['encryption']['deterministic_key'] }}"
ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT: "{{ hostvars['console']['mastodon']['encryption']['salt'] }}"
volume:
- "{{ node['home_path'] }}/containers/mastodon/ssl:/etc/ssl/mastodon:ro"
- "{{ node['home_path'] }}/data/containers/mastodon/system:/mastodon/public/system:rw"
no_log: true
- name: Create .migrated file
ansible.builtin.file:
path: "{{ node['home_path'] }}/data/containers/mastodon/.migrated_{{ version['containers']['mastodon'] }}"
state: "touch"
mode: "0644"
owner: "{{ ansible_user }}"
group: "{{ node['group'] }}"
- name: Deploy mastodon.container file
ansible.builtin.template:
src: "{{ hostvars['console']['node']['config_path'] }}/services/containers/app/mastodon/{{ item }}.j2"
dest: "{{ node['home_path'] }}/.config/containers/systemd/{{ item }}"
owner: "{{ ansible_user }}"
group: "{{ node['group'] }}"
mode: "0644"
loop:
- "mastodon-web.container"
- "mastodon-worker.container"
- "mastodon-streaming.container"
notify: "notification_restart_mastodon"
- name: Enable mastodon.service
ansible.builtin.systemd:
name: "{{ item }}"
state: "started"
enabled: true
daemon_reload: true
scope: "user"
loop:
- "mastodon-web.service"
- "mastodon-worker.service"
- "mastodon-streaming.service"
- name: Check container archive images
ansible.builtin.stat:
path: "{{ node['home_path'] }}/archives/containers/{{ item.file }}.tar"
loop:
- name: "redis"
image: "docker.io/library/redis:{{ version['containers']['redis'] }}"
file: "docker.io_library_redis_{{ version['containers']['redis'] }}"
- name: "mastodon"
image: "ghcr.io/mastodon/mastodon:{{ version['containers']['mastodon'] }}"
file: "ghcr.io_mastodon_mastodon_{{ version['containers']['mastodon'] }}"
- name: "mastodon-streaming"
image: "ghcr.io/mastodon/mastodon-streaming:{{ version['containers']['mastodon'] }}"
file: "ghcr.io_mastodon_mastodon-streaming_{{ version['containers']['mastodon'] }}"
loop_control:
label: "{{ item.name }}"
register: container_archive_images
- name: Save container archive images
containers.podman.podman_save:
image:
- "{{ item.item.image }}"
dest: "{{ node['home_path'] }}/archives/containers/{{ item.item.file }}.tar"
format: "oci-archive"
force: false
loop: "{{ container_archive_images.results }}"
loop_control:
label: "{{ item.item.file }}"
when: not item.stat.exists
- name: Fetch container archive images
ansible.builtin.fetch:
src: "{{ node['home_path'] }}/archives/containers/{{ item.item.file }}.tar"
dest: "{{ hostvars['console']['node']['data_path'] }}/images/containers/"
flat: true
loop: "{{ container_archive_images.results }}"
loop_control:
label: "{{ item.item.file }}"