#cloud-config
# ─────────────────────────────────────────────────────────────
# Nexus One AI — Workstation Autoinstall
# Hardware target: personal AI appliance (Mini-ITX / SFF / DGX Spark class)
#   GPU:     NVIDIA DGX Spark, RTX 5090 Workstation, or RTX PRO 6000
#            Workstation (1 GPU)
#   RAM:     64 GB DDR5
#   Storage: 1× 2 TB NVMe SSD (single drive — simple LVM)
#   Network: 2.5 GbE (single interface)
#   Users:   1-3 active, 5 max — not a shared server, no HA
#
# This reuses the same compact single-GPU install profile as the Server S
# ("starter") image below — same hardware class, same Ansible playbook
# (ansible/starter.yml) — but is a distinct, standalone product category
# (Nexus One AI Workstation), not part of the Server S/M/L/Max ladder.
# See cezen_license.WORKSTATION_TIER / TIER_MATRIX["workstation"].
# ─────────────────────────────────────────────────────────────
autoinstall:
  version: 1

  # ── Locale & keyboard ──────────────────────────
  locale: en_IN.UTF-8
  keyboard:
    layout: us

  # ── Network: DHCP during install; static config applied post-install ──
  network:
    network:
      version: 2
      ethernets:
        any-en:
          dhcp4: true
          match:
            name: "en*"
        any-eth:
          dhcp4: true
          match:
            name: "eth*"

  # ── Storage: single 2 TB NVMe, simple LVM ─────
  # Workstations have one drive — no RAID needed.
  storage:
    layout:
      name: lvm
      match:
        size: largest

  # ── Identity ──────────────────────────────────
  identity:
    hostname: cezenai-workstation
    username: cezen
    # Default password: cezen@123  (change via first-boot wizard)
    password: "$6$I5VA.42G1xTeVhCv$KCLzqIKg/kbNHZyiTEMAY4FZsJMDDwoS90k6Ffb9VEwmcK.wuzlJNe3ceiEfLrzYzXEvqjYsLc7klAbeGPGab."

  # ── SSH ───────────────────────────────────────
  ssh:
    install-server: true
    allow-pw: true

  # ── Base packages ─────────────────────────────
  packages:
    - git
    - curl
    - wget
    - python3
    - whiptail
    - openssh-server
    - nvme-cli          # NVMe health / SMART monitoring

  # ── Late commands ─────────────────────────────
  late-commands:
    # Expand LVM to fill the full 2 TB NVMe
    - lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv || true
    - resize2fs /dev/ubuntu-vg/ubuntu-lv || true

    # Passwordless sudo for cezen (needed by install.sh + first-boot wizard)
    - echo "cezen ALL=(ALL) NOPASSWD:ALL" > /target/etc/sudoers.d/cezen
    - chmod 440 /target/etc/sudoers.d/cezen

    # Replace installer netplan with simple DHCP target config.
    # The first-boot wizard will switch to static if desired.
    - rm -f /target/etc/netplan/50-cloud-init.yaml /target/etc/netplan/00-installer-config.yaml || true
    - |
      cat > /target/etc/netplan/99-cezen-dhcp.yaml << 'EOF'
      network:
        version: 2
        ethernets:
          any-en:
            dhcp4: true
            match:
              name: "en*"
          any-eth:
            dhcp4: true
            match:
              name: "eth*"
      EOF

    # Disable cdrom APT source
    - sed -i 's/^deb cdrom:/# deb cdrom:/' /target/etc/apt/sources.list || true

    # Pull the Nexus One AI installer from cgit. The ISO intentionally does not
    # bundle the full package, keeping the image small and the installed code
    # current at deployment time.
    - mkdir -p /target/opt/aipackage
    - git clone https://cgit.cezentech.com/jinojose/aipackage.git /target/opt/aipackage

    # Write tier marker — used by install.sh (as a fallback default only;
    # the first-boot wizard always passes --tier=starter explicitly for
    # Ansible routing) and by the portal branding/entitlement system, which
    # reads "workstation" here to show Nexus One AI Workstation branding and
    # the workstation feature set (TIER_MATRIX["workstation"]) instead of a
    # Server tier. install.sh never writes to this file, so it persists
    # unchanged through Phase 1 and Phase 2.
    - mkdir -p /target/opt/cezen
    - echo "workstation" > /target/opt/cezen/tier

    # Deploy first-boot TUI wizard
    - cp /target/opt/aipackage/autoinstall/firstboot-setup.sh /target/opt/cezen/firstboot-setup.sh
    - chmod +x /target/opt/cezen/firstboot-setup.sh

    # Set hostname
    - echo "cezenai-workstation" > /target/etc/hostname
    - sed -i 's/aiserver/cezenai-workstation/g' /target/etc/hosts || true

    # Systemd service: run first-boot wizard on tty1 once.
    # CEZEN_TIER=workstation tells firstboot-setup.sh to skip the Server
    # tier-selection menu (Step 3) — Workstation is never offered there.
    - |
      cat > /target/etc/systemd/system/cezen-setup.service << 'EOF'
      [Unit]
      Description=Nexus One AI — Console Setup Wizard (Workstation)
      After=cloud-final.service cloud-init.target network-online.target
      Wants=cloud-init.target network-online.target
      ConditionPathExists=!/opt/cezen/.setup-done
      OnFailure=getty@tty1.service

      [Service]
      Type=oneshot
      WorkingDirectory=/opt/cezen
      ExecStartPre=-/bin/systemctl stop getty@tty1.service
      ExecStartPre=-/usr/bin/chvt 1
      ExecStart=/bin/bash -lc 'clear >/dev/tty1 2>/dev/null || true; /usr/bin/openvt -c 1 -f -w -- env TERM=linux CEZEN_TIER=workstation /opt/cezen/firstboot-setup.sh'
      ExecStartPost=-/bin/systemctl start getty@tty1.service
      StandardOutput=journal+console
      StandardError=journal+console
      Restart=no

      [Install]
      WantedBy=cloud-init.target
      EOF

    - curtin in-target -- systemctl enable ssh
    - curtin in-target -- systemctl enable cezen-setup.service

  user-data:
    disable_root: false
