#cloud-config
# Ubuntu Desktop Autoinstall Configuration
# This replaces the legacy preseed format for Ubuntu 20.04+

autoinstall:
  version: 1

  # Locale and keyboard
  locale: en_US.UTF-8
  keyboard:
    layout: us
    variant: ''

  # CRITICAL: Network configuration during installation
  # This provides DHCP networking for the installation process
  # Cloud-init will be configured to NOT manage networking post-install
  network:
    version: 2
    ethernets:
      ens18:
        dhcp4: true
        dhcp6: false
      # Fallback for different interface names
      eth0:
        dhcp4: true
        dhcp6: false
      enp6s18:
        dhcp4: true
        dhcp6: false

  # Source configuration - specify desktop installation
  source:
    id: ubuntu-desktop-minimal
    search_drivers: false

  # Storage configuration
  storage:
    layout:
      name: direct
      # Use entire disk, no LVM
      match:
        size: largest
    swap:
      size: 1G  # 1GB swap partition

  # User configuration for Packer SSH access
  # This user will be removed by cloud-init during template preparation
  identity:
    hostname: packer-build
    username: packer
    password: '$6$IPycR11qeNxw62Lb$.3Vpe1L6G09dT1JnpUBJZY2TXlP8dH6AUu9C1jxJXb/fv80gzIS/nFxQ4UidZCOc2VbEBiXf3dzWVjxtDCnrq.'

  # SSH configuration
  ssh:
    install-server: true
    allow-pw: true
    authorized-keys: []

  # Package configuration - Additional tools (desktop comes from source)
  packages:
    - qemu-guest-agent
    - cloud-init
    - sudo
    - openssh-server
    - curl
    - vim
    - net-tools
    - spice-vdagent
    - spice-webdavd
    - xserver-xorg-video-qxl

  # Disable automatic updates during installation
  apt:
    disable_components: []
    geoip: true
    preserve_sources_list: false
    primary:
      - arches: [amd64]
        uri: http://archive.ubuntu.com/ubuntu
    security:
      - arches: [amd64]
        uri: http://security.ubuntu.com/ubuntu

  # No user interaction
  user-data:
    disable_root: false
    timezone: UTC

  # Commands to run late in the installation
  late-commands:
    # Configure sudo for packer user (must be 0440 permissions)
    - echo 'packer ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/packer
    - chmod 0440 /target/etc/sudoers.d/packer
    - chown root:root /target/etc/sudoers.d/packer
    # Configure SSH - remove any existing lines and add explicit settings
    - sed -i '/PasswordAuthentication/d' /target/etc/ssh/sshd_config
    - sed -i '/PermitRootLogin/d' /target/etc/ssh/sshd_config
    - sed -i '/PubkeyAuthentication/d' /target/etc/ssh/sshd_config
    - "echo 'PasswordAuthentication yes' >> /target/etc/ssh/sshd_config"
    - "echo 'PermitRootLogin yes' >> /target/etc/ssh/sshd_config"
    - "echo 'PubkeyAuthentication yes' >> /target/etc/ssh/sshd_config"
    # Configure cloud-init to enable SSH password authentication
    - "echo 'ssh_pwauth: true' >> /target/etc/cloud/cloud.cfg"
    # Create cloud-init config to enable SSH password auth
    - mkdir -p /target/etc/cloud/cloud.cfg.d
    - "echo 'ssh_pwauth: true' > /target/etc/cloud/cloud.cfg.d/99-enable-ssh-pwauth.cfg"
    # Disable firewall (ufw)
    - curtin in-target --target=/target -- systemctl disable ufw || true
    - curtin in-target --target=/target -- systemctl stop ufw || true
    # Enable GDM (GNOME Display Manager) for desktop login
    - curtin in-target --target=/target -- systemctl enable gdm
    # Ensure services are enabled
    - curtin in-target --target=/target -- systemctl enable qemu-guest-agent
    - curtin in-target --target=/target -- systemctl enable ssh
    - curtin in-target --target=/target -- systemctl enable cloud-init
    - curtin in-target --target=/target -- systemctl enable cloud-init-local
    - curtin in-target --target=/target -- systemctl enable cloud-config
    - curtin in-target --target=/target -- systemctl enable cloud-final
    # Enable SPICE vdagent for better desktop experience
    - curtin in-target --target=/target -- systemctl enable spice-vdagent
