- name: "Create workspace directories"
  ansible.builtin.file:
    path: "{{ hotpocket_app.deployment_directory }}/{{ item }}"
    state: "directory"
  loop:
    - "etc"
    - "etc/backend"
    - "etc/backend/entrypoint.d"
    - "lib"
    - "lib/backend"
    - "log"
    - "run"
    - "run/backend-admin"
    - "run/backend-celery-beat"
    - "run/backend-celery-worker"
    - "run/backend-webapp"
    - "run/uploads"
- name: "Install docker-compose.yml"
  ansible.builtin.template:
    src: "templates/{{ hotpocket_app.mode }}/docker-compose.yaml.jinja2"
    dest: "{{ hotpocket_app.deployment_directory }}/docker-compose.yaml"
    owner: "{{ hotpocket_app.owner }}"
    group: "{{ hotpocket_app.group }}"
- name: "Install env files"
  ansible.builtin.template:
    src: "templates/{{ hotpocket_app.mode }}/{{ item }}.jinja2"
    dest: "{{ hotpocket_app.deployment_directory }}/etc/{{ item }}"
    owner: "{{ hotpocket_app.owner }}"
    group: "{{ hotpocket_app.group }}"
  loop: "{{ hotpocket_app_role.env_files[hotpocket_app.mode] }}"
- name: "Upload customization files"
  ansible.builtin.copy:
    src: "{{ item.src }}"
    dest: "{{ hotpocket_app.deployment_directory }}/{{ item.dest }}"
    owner: "{{ hotpocket_app.owner }}"
    group: "{{ hotpocket_app.group }}"
    mode: "{{ item.mode|default('644') }}"
  loop: "{{ hotpocket_app.customization }}"
  when: "hotpocket_app.customization is defined"
- name: "Install hotpocket_app.service unit"
  ansible.builtin.template:
    src: "templates/{{ hotpocket_app_role.services[hotpocket_app.mode].src }}.jinja2"
    dest: "{{ hotpocket_app.deployment_directory }}/etc/{{ hotpocket_app_role.services[hotpocket_app.mode].dest }}"
    owner: "{{ hotpocket_app.owner }}"
    group: "{{ hotpocket_app.group }}"
- name: "Stop the stack"
  ansible.builtin.command:
    argv:
      - "docker"
      - "compose"
      - "down"
    chdir: "{{ hotpocket_app.deployment_directory }}"
- name: "Run backend migrations"
  ansible.builtin.command:
    argv:
      - "docker"
      - "compose"
      - "run"
      - "--rm"
      - "backend-webapp"
      - "./manage.py"
      - "migrate"
    chdir: "{{ hotpocket_app.deployment_directory }}"
  when: "hotpocket_app.mode == 'fullstack' and is_manual_run is not defined"
- name: "Start the stack"
  ansible.builtin.command:
    argv:
      - "docker"
      - "compose"
      - "up"
      - "-d"
    chdir: "{{ hotpocket_app.deployment_directory }}"
  when: "is_manual_run is not defined"
