---
- hosts: pfservers
  name: localdev pre run_tests setup
  become: True

  vars:
    docker_images:
      - pfdebian
      - radiusd
      - pfbuild-debian-bullseye

  tasks:
    - name: Install python3-docker for images
      ansible.builtin.package:
        name: python3-docker
        state: present

    - name: Pull images
      community.docker.docker_image:
        name: "ghcr.io/inverse-inc/packetfence/{{ item }}"
        tag: devel
        source: pull
      loop: "{{ docker_images }}"

    - name: Tag and push images
      community.docker.docker_image:
        name: "ghcr.io/inverse-inc/packetfence/{{ item }}"
        repository: "packetfence/{{ item }}"
        tag: devel
        source: local
      loop: "{{ docker_images }}"

    - name: Create containers/.local_env, set LOCAL_DEV, HTML_MOUNT (w/ localdev)
      copy:
        dest: "/usr/local/pf/containers/.local_env"
        content: |
          LOCAL_DEV=true
          HTML_MOUNT=/usr/local/pf/html

    - name: Synchronize local pfappserver SPA, expose hosts' html/pfappserver/root/dist (w/ localdev)
      ansible.posix.synchronize:
        src: "{{ lookup('env', 'VENOM_ROOT_DIR') }}/../../html/pfappserver/root/dist"
        dest: '/usr/local/pf/html/pfappserver/root/'
        archive: yes
        delete: yes

    - name: Synchronize /docs for --build httpd.admin_dispatcher (w/ localdev)
      ansible.posix.synchronize:
        src: "{{ lookup('env', 'VENOM_ROOT_DIR') }}/../../docs"
        dest: '/usr/local/pf/'
        archive: yes
        delete: yes

    - name: Restart packetfence-httpd.admin_dispatcher (w/ localdev)
      ansible.builtin.service:
        name: packetfence-httpd.admin_dispatcher
        state: restarted

    - name: Wait for build httpd.admin_dispatcher watching :8890 (limit <= 15m)
      wait_for:
        host: "localhost"
        port: 8890
        timeout: 900