# Homepage recategorisation — ana-ml2 (10.250.50.54), 2 containers.
# Sibling of playbooks/homepage-regroup-ana-docker.yaml; rationale lives there.
#
#   scriberr  ->  AI - Studios       (a transcription UI you open, not an API seat)
#   dockge    ->  Compose Consoles
#
# ⚠ THE vLLM SEATS ON THIS HOST ARE DELIBERATELY NOT TOUCHED. Every one of them
# would need a recreate to change its `homepage.group`, and a recreate means a
# multi-minute model reload on a seat that peers reach through the gateway. The
# separation the operator asked for — UI up top, API endpoints out of the way —
# is achieved for those groups by ORDER and `initiallyCollapsed` in
# stacks/homepage/conf/settings.yaml, which costs nothing. Keep it that way: if
# a future pass wants to rename `AI - Inference`, weigh it against bouncing six
# model seats.
#
# Run:  scripts/elway infra-ops@10.250.50.54 --playbook playbooks/homepage-regroup-ana-ml2.yaml

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

  - 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 scriberr
    sudo: true
    shell: cd /opt/docker/compose/scriberr && docker compose up -d scriberr

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"}}'
      scriberr 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')"

  # Container age, not liveness — a seat may be legitimately stopped, so
  # "is it running" cannot answer "did I bounce it". See the same step in
  # playbooks/homepage-regroup-irv-ml1.yaml for how that distinction was found.
  - name: the vLLM seats were NOT recreated by this run
    sudo: true
    changed_when: "false"
    shell: >-
      for c in $(docker ps -a --filter 'name=vllm-' --filter 'name=llama-'
      --format '{{.Names}}'); 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: scriberr answers
    changed_when: "false"
    shell: >-
      for i in 1 2 3 4 5 6 7 8 9 10 11 12; do
      curl -sfL -o /dev/null -m 5 http://127.0.0.1:8080/ && exit 0;
      sleep 5; done; exit 1
