# Homepage recategorisation — irv-ml1 (10.100.79.3), 5 containers.
# Sibling of playbooks/homepage-regroup-ana-docker.yaml; rationale lives there.
#
#   arbo, comfyui, waterland-studio  ->  AI - Studios      (was AI - Image & Media)
#   yt-voice-clipper                 ->  AI - Studios      (was AI - Audio Tools)
#   dockge                           ->  Compose Consoles
#
# `AI - Studios` is the "you open this and do work in it" group; the ASR and
# TTS API seats stay where they are and get collapsed by settings.yaml instead,
# which is what keeps the GPU seats out of this playbook entirely.
#
# ⚠ yt-voice-clipper carries its homepage labels in `docker-compose.override.yml`,
# not in `docker-compose.yml`, and its compose dir is a git checkout of the
# project — the override is the deploy-local layer, which is the right place
# for it.
#
# ⚠ This host is reached over the WireGuard tunnel. If the run cannot connect,
# check the tunnel before assuming the host is down.
#
# Run:  scripts/elway infra-ops@10.100.79.3 --playbook playbooks/homepage-regroup-irv-ml1.yaml

steps:
  - name: arbo -> AI - Studios
    sudo: true
    shell: >-
      sed -i 's|homepage.group=AI - Image . Media$|homepage.group=AI - Studios|'
      /opt/docker/compose/arbo/compose.yaml
    when: grep -q 'homepage.group=AI - Image . Media$' /opt/docker/compose/arbo/compose.yaml

  - name: comfyui -> AI - Studios
    sudo: true
    shell: >-
      sed -i 's|homepage.group=AI - Image . Media$|homepage.group=AI - Studios|'
      /opt/docker/compose/comfyui/compose.yaml
    when: grep -q 'homepage.group=AI - Image . Media$' /opt/docker/compose/comfyui/compose.yaml

  - name: waterland-studio -> AI - Studios
    sudo: true
    shell: >-
      sed -i 's|homepage.group=AI - Image . Media$|homepage.group=AI - Studios|'
      /opt/docker/compose/waterland-studio/compose.yaml
    when: grep -q 'homepage.group=AI - Image . Media$' /opt/docker/compose/waterland-studio/compose.yaml

  - name: yt-voice-clipper -> AI - Studios
    sudo: true
    shell: >-
      sed -i 's|homepage.group=AI - Audio Tools$|homepage.group=AI - Studios|'
      /opt/docker/compose/yt-voice-clipper/docker-compose.override.yml
    when: grep -q 'homepage.group=AI - Audio Tools$' /opt/docker/compose/yt-voice-clipper/docker-compose.override.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

  # ---- recreates ---------------------------------------------------------
  - name: recreate arbo
    sudo: true
    shell: cd /opt/docker/compose/arbo && docker compose up -d engine

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

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

  - name: recreate yt-voice-clipper
    sudo: true
    shell: cd /opt/docker/compose/yt-voice-clipper && docker compose up -d api

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

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"}}'
      arbo comfyui waterland-studio yt-voice-clipper-api-1 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')"

  # Assert NOT-RECREATED, not RUNNING. The first version of this checked that
  # all five seats were up and failed — because chatterbox-fast has been down
  # since 2026-08-10 and speaches since earlier the same morning, both long
  # before this playbook existed. "Is it running" is the wrong question: a seat
  # can be legitimately stopped. The question this step is actually asking is
  # "did I bounce a model seat to relabel a dashboard", and container age
  # answers it directly.
  - name: the TTS and ASR seats were NOT recreated by this run
    sudo: true
    changed_when: "false"
    shell: >-
      for c in kokoro dots-tts chatterbox-fast tts-gateway parakeet speaches; do
      created=$(docker inspect -f '{{.Created}}' "$c" 2>/dev/null) || continue;
      age=$(( $(date +%s) - $(date -d "$created" +%s) ));
      if [ "$age" -lt 600 ]; then echo "$c was recreated ${age}s ago"; exit 1; fi;
      done

  - name: everything relabelled is running
    sudo: true
    changed_when: "false"
    shell: >-
      test "$(docker inspect -f '{{.State.Running}}' arbo comfyui
      waterland-studio yt-voice-clipper-api-1 dockge | sort -u)" = "true"
