# Homepage recategorisation — nh3-docker (10.100.50.40), 2 containers.
# Sibling of playbooks/homepage-regroup-ana-docker.yaml; rationale lives there.
#
#   adguardhome  ->  DNS & Filtering     (⚠ uses docker-compose.yml)
#   dockge       ->  Compose Consoles
#
# Run:  scripts/elway infra-ops@10.100.50.40 --playbook playbooks/homepage-regroup-nh3-docker.yaml

steps:
  - name: adguardhome -> DNS & Filtering
    sudo: true
    shell: >-
      sed -i 's|homepage.group=Service Networking$|homepage.group=DNS \& Filtering|'
      /opt/docker/compose/adguard/docker-compose.yml
    when: grep -q 'homepage.group=Service Networking$' /opt/docker/compose/adguard/docker-compose.yml

  - name: dockge -> Compose Consoles
    sudo: true
    shell: >-
      sed -i 's|homepage.group=Service Networking$|homepage.group=Compose Consoles|'
      /opt/docker/compose/dockge/compose.yaml
    when: grep -q 'homepage.group=Service Networking$' /opt/docker/compose/dockge/compose.yaml

  - name: recreate dockge
    sudo: true
    shell: cd /opt/docker/compose/dockge && docker compose up -d dockge

  - name: recreate adguardhome
    sudo: true
    shell: cd /opt/docker/compose/adguard && docker compose up -d adguardhome

verify:
  - name: every relabelled container now carries its new group
    sudo: true
    changed_when: "false"
    shell: >-
      docker inspect -f '{{.Name}} {{index .Config.Labels "homepage.group"}}'
      adguardhome dockge

  - name: no container is left in the retired Service Networking group
    sudo: true
    changed_when: "false"
    shell: >-
      test -z "$(docker ps -q --filter 'label=homepage.group=Service Networking')"

  - name: adguard is back
    changed_when: "false"
    shell: >-
      for i in 1 2 3 4 5 6 7 8 9 10; do
      curl -sfL -o /dev/null -m 5 http://127.0.0.1:8080/ && exit 0;
      sleep 3; done; exit 1

  - name: everything is running
    sudo: true
    changed_when: "false"
    shell: >-
      test "$(docker inspect -f '{{.State.Running}}' adguardhome dockge
      | sort -u)" = "true"
