swarm.ymlΒΆ

---
- name: "πŸ“ Akaunting setup | Scale akaunting service to 0 replicas"
  ansible.builtin.shell: >
    container service update --detach --replicas 0
    {{ AKAUNTING_SERVICE | quote }}
  changed_when: true

- name: "⏳ Akaunting setup | Wait until no running task remains"
  ansible.builtin.shell: |
    container service ps {{ AKAUNTING_SERVICE | quote }} \
      --filter desired-state=running \
      --format '{{ "{{.CurrentState}}" }}' | wc -l | grep -qE '^\s*0\s*$'
  register: akaunting_swarm_drained
  changed_when: false
  retries: 60
  delay: 5
  until: akaunting_swarm_drained.rc == 0

# Exception: the service runs the image built from the akaunting/docker repository, whose code lands in the shared volume; the upstream tag carries an older PHP that cannot run that code.
- name: "πŸ” Akaunting setup | Read the image the akaunting service runs"
  ansible.builtin.command: >-
    container service inspect {{ AKAUNTING_SERVICE }}
    --format {{ '{{.Spec.TaskTemplate.ContainerSpec.Image}}' }}
  register: akaunting_service_image
  changed_when: false
  failed_when: akaunting_service_image.rc != 0 or (akaunting_service_image.stdout | trim | length == 0)

- name: "▢️ Akaunting setup | Run one-shot install"
  ansible.builtin.shell: |
    set -euo pipefail
    JOB={{ AKAUNTING_SETUP_JOB | quote }}
    container service rm "$JOB" 2>/dev/null || true
    container service create --detach --restart-condition on-failure --restart-max-attempts 3 --name "$JOB" \
      --constraint node.role==manager \
      --env-file {{ lookup('container', application_id, 'files.env') | quote }} \
      --env AKAUNTING_SETUP=true \
      --mount type=volume,source={{ AKAUNTING_VOLUME | quote }},target={{ lookup('volume', 'web-app-akaunting', 'data').target }} \
      --network {{ application_id | entity_name | quote }} \
      {% if lookup('database', application_id, 'shared') | bool %}--network {{ lookup('database', application_id, 'network') | quote }} {% endif %}\
      {{ akaunting_service_image.stdout | trim | quote }} \
      {{ AKAUNTING_ENTRYPOINT }}
  changed_when: true

- name: "⏳ Akaunting setup | Wait for install job to complete"
  ansible.builtin.script: "{{ lookup('path_absolute', 'roles/sys-svc-compose/files/shell/swarm/job_state.sh') }}"
  environment:
    JOB: "{{ AKAUNTING_SETUP_JOB }}"
  register: akaunting_setup_done
  changed_when: false
  retries: 60
  delay: 5
  until: akaunting_setup_done.rc != 1
  failed_when: akaunting_setup_done.rc != 0

- name: "πŸ—‘οΈ Akaunting setup | Remove one-shot install service"
  ansible.builtin.shell: >
    container service rm
    {{ AKAUNTING_SETUP_JOB }}
  changed_when: true
  failed_when: false

- name: "πŸ“ Akaunting setup | Scale akaunting back to nominal replicas"
  ansible.builtin.shell: >
    container service update --detach --replicas {{ AKAUNTING_REPLICAS }}
    {{ AKAUNTING_SERVICE | quote }}
  changed_when: true

- name: "⏳ Akaunting setup | Wait until replicas converge X/X"
  ansible.builtin.shell: |
    set -euo pipefail
    SVC={{ AKAUNTING_SERVICE | quote }}
    WANT={{ AKAUNTING_REPLICAS }}/{{ AKAUNTING_REPLICAS }}
    got="$(container service ls --filter name="$SVC" --format '{{ "{{.Replicas}}" }}' | head -1)"
    test "$got" = "$WANT"
  register: akaunting_replicas_converged
  changed_when: false
  retries: 60
  delay: 5
  until: akaunting_replicas_converged.rc == 0