name: Workflow for Gigabyte Ampere Server Cuttlefish Installer

on:
  pull_request:
    paths:
      - '.github/workflows/gigabyte-ampere-cuttlefish-installer.yaml'
      - 'gigabyte-ampere-cuttlefish-installer/**'
  push:
    branchs:
      - '**'

permissions:
  contents: read

jobs:
  check-install-gigabyte-package-deb-job:
    runs-on: ubuntu-22.04-arm
    container:
      image: debian@sha256:2c91e484d93f0830a7e05a2b9d92a7b102be7cab562198b984a84fdbc7806d91 # debian:trixie-20260202
    steps:
    - name: Prepare test environment
      run: |
        apt-get update
        apt-get upgrade -y
        apt-get install -y sudo
        apt-get install -y curl ca-certificates
    - name: Setup repository
      run: |
        curl -fsSL https://us-apt.pkg.dev/doc/repo-signing-key.gpg -o /etc/apt/trusted.gpg.d/artifact-registry.asc
        chmod a+r /etc/apt/trusted.gpg.d/artifact-registry.asc
        echo "deb https://us-apt.pkg.dev/projects/android-cuttlefish-artifacts android-cuttlefish main" | tee -a /etc/apt/sources.list.d/artifact-registry.list
        apt-get update
    - name: Install meta package
      run: apt -o Apt::Get::Assume-Yes=true -o APT::Color=0 -o DPkgPM::Progress-Fancy=0 install cuttlefish-integration-gigabyte-arm64
    - name: ulimit config test
      run: |
        test -e /etc/security/limits.d/95-cuttlefish-integration-gigabyte-arm64-nofile.conf
        ulimit -n
    - name: NTP config test
      run: test -e /etc/ntpsec/ntp.d/google-time-server.conf
    - name: Package purge test
      run: |
        apt -o Apt::Get::Assume-Yes=true -o APT::Color=0 -o DPkgPM::Progress-Fancy=0 purge cuttlefish-integration-gigabyte-arm64
        test ! -e /etc/ntpsec/ntp.d/google-time-server.conf

  check-preseed-after-install-script-gpu-job:
    runs-on: ubuntu-22.04-arm
    container:
      image: debian@sha256:2c91e484d93f0830a7e05a2b9d92a7b102be7cab562198b984a84fdbc7806d91 # debian:trixie-20260202
    defaults:
      run:
        working-directory: ./gigabyte-ampere-cuttlefish-installer
    strategy:
      matrix:
        amd_gpu: ["true", "false"]
        nvidia_gpu: ["true", "false"]
    steps:
    - name: Checkout repository
      uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
    - name: Check preseed after install script
      uses: ./.github/actions/gigabyte-ampere-cuttlefish-installer-check-preseed-after-install-script
      with:
        emulate-nvidia: ${{ matrix.nvidia_gpu }}
        emulate-amd: ${{ matrix.amd_gpu }}

  build-installer-iso-job:
    needs:
      - check-install-gigabyte-package-deb-job
      - check-preseed-after-install-script-gpu-job
    runs-on: ubuntu-22.04
    container:
      image: debian@sha256:13f29b6806e531c3ff3b565bb6eed73f2132506c8c9d41bb996065ca20fb27f2 # debian:trixie-20260223 (amd64)
    steps:
    - name: Checkout repository
      uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
    - name: Build iso installer
      uses: ./.github/actions/build-gigabyte-ampere-cuttlefish-installer
    - name: Upload artifacts
      uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
      with:
        name: gigabyte-ampere-cuttlefish-installer-artifacts
        path: gigabyte-ampere-cuttlefish-installer/preseed-mini.iso.xz

  test-iso-qemu-job:
    needs: [build-installer-iso-job]
    runs-on: ubuntu-22.04-arm
    container:
      image: debian@sha256:2c91e484d93f0830a7e05a2b9d92a7b102be7cab562198b984a84fdbc7806d91 # debian:trixie-20260202
    defaults:
      run:
        working-directory: ./gigabyte-ampere-cuttlefish-installer
    env:
      TEST_DISK_SIZE: "10G"
    steps:
    - name: Checkout repository
      uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
    - name: Download artifacts
      uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
      with:
        name: gigabyte-ampere-cuttlefish-installer-artifacts
    - name: Prepare test environment
      uses: ./.github/actions/gigabyte-ampere-cuttlefish-installer-prepare-testbed
    - name: Copy test scripts
      run: |
        cp -f tests/installer-iso-* .
    - name: Run installer on qemu
      run: |
        ./installer-iso-install.expect successful_install sheeFei2
    - name: Extract partitions
      run: |
        ./installer-iso-extract-partitions.sh
    - name: Check if installation is successful
      run: |
        e2cp rootfs.img:/home/vsoc-01/successful_install successful_install
        echo sheeFei2 > successful_install_compare
        cmp successful_install successful_install_compare
    - name: Check if kernel is installed
      run: |
        e2ls -l boot.img:/vmlinuz-*
        e2cp boot.img:/$(e2ls boot.img:/vmlinuz-* | tail -1) .
        test -e vmlinuz-*
    - name: Check if Cuttlefish Debian packages are installed
      run: |
        e2ls -l rootfs.img:/usr/bin/cvd_host_orchestrator
        e2ls -l rootfs.img:/usr/bin/cvd_host_orchestrator | grep cvd_host_orchestrator | grep root
    - name: Check we don't have home partition
      run: |
        test '!' -e home.img

  test-iso-multidisk-qemu-empty-disk-job:
    needs: [build-installer-iso-job]
    runs-on: ubuntu-22.04-arm
    container:
      image: debian@sha256:2c91e484d93f0830a7e05a2b9d92a7b102be7cab562198b984a84fdbc7806d91 # debian:trixie-20260202
    defaults:
      run:
        working-directory: ./gigabyte-ampere-cuttlefish-installer
    steps:
    - name: Checkout repository
      uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
    - name: Download artifacts
      uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
      with:
        name: gigabyte-ampere-cuttlefish-installer-artifacts
    - name: Prepare test environment
      uses: ./.github/actions/gigabyte-ampere-cuttlefish-installer-prepare-testbed
    - name: Copy test scripts
      run: |
        cp -f tests/installer-iso-* .
        cp -f tests/multidisk/installer-iso-* .
    - name: Create Empty 2 disks
      run: |
        ./tests/multidisk/installer-create-empty-disk2.sh
    - name: Run installer on qemu
      run: |
        ./installer-iso-install.expect successful_install eD4uvei1
    - name: Extract partitions
      run: |
        ./installer-iso-extract-partitions-multidisk.sh
    - name: Check if rootfs is larger than 8G.
      run: |
        test $(stat -c '%s' rootfs.img) '-gt' 8589934592
    - name: Check if installation is successful
      run: |
        e2cp rootfs.img:/home/vsoc-01/successful_install successful_install
        echo eD4uvei1 > successful_install_compare
        cmp successful_install successful_install_compare
    - name: Check if kernel is installed
      run: |
        e2ls -l boot.img:/vmlinuz-*
        e2cp boot.img:/$(e2ls boot.img:/vmlinuz-* | tail -1) .
        test -e vmlinuz-*
    - name: Check if Cuttlefish Debian packages are installed
      run: |
        e2ls -l rootfs.img:/usr/bin/cvd_host_orchestrator
        e2ls -l rootfs.img:/usr/bin/cvd_host_orchestrator | grep cvd_host_orchestrator | grep root

  test-iso-multidisk-qemu-disk2-with-data-job:
    needs: [build-installer-iso-job]
    runs-on: ubuntu-22.04-arm
    container:
      image: debian@sha256:2c91e484d93f0830a7e05a2b9d92a7b102be7cab562198b984a84fdbc7806d91 # debian:trixie-20260202
    defaults:
      run:
        working-directory: ./gigabyte-ampere-cuttlefish-installer
    steps:
    - name: Checkout repository
      uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
    - name: Download artifacts
      uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
      with:
        name: gigabyte-ampere-cuttlefish-installer-artifacts
    - name: Prepare test environment
      uses: ./.github/actions/gigabyte-ampere-cuttlefish-installer-prepare-testbed
    - name: Copy test scripts
      run: |
        cp -f tests/installer-iso-* .
        cp -f tests/multidisk/installer-iso-* .
    - name: Create 2 disks with preinstalled partitions.
      run: |
        ./tests/multidisk/installer-create-preinstalled-disk2.sh
    - name: Run installer on qemu
      run: |
        ./installer-iso-install.expect successful_install Daiyaik0
    - name: Extract partitions
      run: |
        ./installer-iso-extract-partitions-multidisk.sh
    - name: Check if rootfs is larger than 8G.
      run: |
        test $(stat -c '%s' rootfs.img) '-gt' 8589934592
    - name: Check if installation is successful
      run: |
        e2cp rootfs.img:/home/vsoc-01/successful_install successful_install
        echo Daiyaik0 > successful_install_compare
        cmp successful_install successful_install_compare
    - name: Check if kernel is installed
      run: |
        e2ls -l boot.img:/vmlinuz-*
        e2cp boot.img:/$(e2ls boot.img:/vmlinuz-* | tail -1) .
        test -e vmlinuz-*
    - name: Check if Cuttlefish Debian packages are installed
      run: |
        e2ls -l rootfs.img:/usr/bin/cvd_host_orchestrator
        e2ls -l rootfs.img:/usr/bin/cvd_host_orchestrator | grep cvd_host_orchestrator | grep root

  test-iso-multidisk-qemu-disk5-with-data-job:
    needs: [build-installer-iso-job]
    runs-on: ubuntu-22.04-arm
    container:
      image: debian@sha256:2c91e484d93f0830a7e05a2b9d92a7b102be7cab562198b984a84fdbc7806d91 # debian:trixie-20260202
    defaults:
      run:
        working-directory: ./gigabyte-ampere-cuttlefish-installer
    steps:
    - name: Checkout repository
      uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
    - name: Download artifacts
      uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
      with:
        name: gigabyte-ampere-cuttlefish-installer-artifacts
    - name: Prepare test environment
      uses: ./.github/actions/gigabyte-ampere-cuttlefish-installer-prepare-testbed
    - name: Copy test scripts
      run: |
        cp -f tests/installer-iso-* .
        cp -f tests/multidisk/installer-iso-* .
    - name: Create 5 disks with preinstalled partitions.
      run: |
        ./tests/multidisk/installer-create-preinstalled-disk5.sh
    - name: Run installer on qemu
      run: |
        ./installer-iso-install.expect successful_install AiT7eipi
    - name: Extract partitions
      run: |
        ./installer-iso-extract-partitions-multidisk.sh
    - name: Check if rootfs is larger than 8G.
      run: |
        test $(stat -c '%s' rootfs.img) '-gt' 8589934592
    - name: Check if installation is successful
      run: |
        e2cp rootfs.img:/home/vsoc-01/successful_install successful_install
        echo AiT7eipi > successful_install_compare
        cmp successful_install successful_install_compare
    - name: Check if kernel is installed
      run: |
        e2ls -l boot.img:/vmlinuz-*
        e2cp boot.img:/$(e2ls boot.img:/vmlinuz-* | tail -1) .
        test -e vmlinuz-*
    - name: Check if Cuttlefish Debian packages are installed
      run: |
        e2ls -l rootfs.img:/usr/bin/cvd_host_orchestrator
        e2ls -l rootfs.img:/usr/bin/cvd_host_orchestrator | grep cvd_host_orchestrator | grep root

  test-iso-cf-qemu-job:
    needs: [build-installer-iso-job]
    runs-on: ubuntu-22.04-arm
    container:
      image: debian@sha256:2c91e484d93f0830a7e05a2b9d92a7b102be7cab562198b984a84fdbc7806d91 # debian:trixie-20260202
    defaults:
      run:
        working-directory: ./gigabyte-ampere-cuttlefish-installer
    steps:
    - name: Checkout repository
      uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
    - name: Download artifacts
      uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
      with:
        name: gigabyte-ampere-cuttlefish-installer-artifacts
    - name: Prepare test environment
      uses: ./.github/actions/gigabyte-ampere-cuttlefish-installer-prepare-testbed
    - name: Copy test scripts
      run: |
        cp -f tests/installer-iso-* .
    - name: Run installer on qemu
      run: |
        ./installer-iso-install.expect
    - name: Download Cuttlefish
      run: |
        ./utils/download-ci-cf.sh
    - name: Start qemu again. Normal boot.
      run: |
        screen -d -m -L -Logfile console_001.log ./installer-iso-run-qemu.sh
        while ! egrep "[^[:space:]]+[[:space:]]login:" console_001.log; do sleep 30; done
        # The login prompt appears before the system is usable, so wait for the
        # boot to complete. "degraded" means booted with some unit failed.
        until sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -o "ConnectTimeout 60" -p 33322 vsoc-01@localhost 'systemctl is-system-running | grep -qE "running|degraded"'; do sleep 30; done
        cp -f console_001.log console_001_p1.log
        CONSOLELINES=$(cat console_001_p1.log | wc -l)
        cat console_001_p1.log
        grep "login:" console_001_p1.log > /dev/null
        echo "CONSOLELINES=${CONSOLELINES}" >> $GITHUB_ENV
    - name: Deploy cuttlefish
      run: |
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'echo cuttlefish | sudo -S -k apt-get install -y unzip'
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'mkdir cf'
        sshpass -p cuttlefish scp -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -P 33322 cvd-host_package.tar.gz vsoc-01@localhost:/home/vsoc-01/
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'cd cf; tar -xvf ../cvd-host_package.tar.gz'
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'rm -f cvd-host_package.tar.gz'
        sshpass -p cuttlefish scp -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -P 33322 aosp_cf_arm64*_phone-*.zip vsoc-01@localhost:/home/vsoc-01/
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'cd cf; unzip ../aosp_cf_arm64*_phone-*.zip'
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'rm -f aosp_cf_arm64*_phone-*.zip'
    - name: Run cuttlefish command line
      run: |
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'cd cf; HOME=$PWD ./bin/launch_cvd -help' || true
    - name: Run apt-cache policy
      run: |
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'apt-cache policy'
    - name: Test for lzop
      run: |
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'lzop -V'
    - name: Test for Google NTP server
      run: |
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'ntpq -p'
        CHECK_GOOGLE_TIME_SERVER=0
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'ntpq -p' | grep 'time1.google' && CHECK_GOOGLE_TIME_SERVER=1
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'ntpq -p' | grep '.GOOG.' && CHECK_GOOGLE_TIME_SERVER=1
        if [ x"${CHECK_GOOGLE_TIME_SERVER}" != x"1" ]; then echo "Google Time Servers not found!"; false; fi
    - name: Test for ulimit
      run: |
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'ulimit -a'
        test $(sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'ulimit -n') -ge 2048
    - name: Check iptables points to iptables-legacy
      run: |
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'update-alternatives --display iptables' | grep "link currently points to /usr/sbin/iptables-legacy"
    - name: Test Java version
      run: |
        #sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'java --version'
        #JAVA_VERSION=$(sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" vsoc-01@localhost 'java -version 2>&1' | sed -n ';s/.* version "\([^"]*\)".*/\1/p;')
        JAVA_REQUIRED_VERSION="21.0"
        #test "$(printf '%s\n%s' "${JAVA_REQUIRED_VERSION}" "${JAVA_VERSION}" | sort -V | head -n1)" = "${JAVA_REQUIRED_VERSION}"
    - name: Shutdown qemu
      run: |
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'echo cuttlefish | sudo -S -k shutdown -h 1'
        while ! grep "reboot: Power down" console_001.log; do sleep 30; done
        tail -n +"${CONSOLELINES}" console_001.log

  test-iso-cf-container-qemu-job:
    needs: [build-installer-iso-job]
    runs-on: ubuntu-22.04-arm
    container:
      image: debian@sha256:2c91e484d93f0830a7e05a2b9d92a7b102be7cab562198b984a84fdbc7806d91 # debian:trixie-20260202
    defaults:
      run:
        working-directory: ./gigabyte-ampere-cuttlefish-installer
    steps:
    - name: Checkout repository
      uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
    - name: Download artifacts
      uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
      with:
        name: gigabyte-ampere-cuttlefish-installer-artifacts
    - name: Prepare test environment
      uses: ./.github/actions/gigabyte-ampere-cuttlefish-installer-prepare-testbed
    - name: Copy test scripts
      run: |
        cp -f tests/installer-iso-* .
    - name: Run installer on qemu
      run: |
        ./installer-iso-install.expect
    - name: Download Cuttlefish
      run: |
        ./utils/download-ci-cf.sh
    - name: Start qemu again. Normal boot.
      run: |
        screen -d -m -L -Logfile console_001.log ./installer-iso-run-qemu.sh
        while ! egrep "[^[:space:]]+[[:space:]]login:" console_001.log; do sleep 30; done
        # The login prompt appears before the system is usable, so wait for the
        # boot to complete. "degraded" means booted with some unit failed.
        until sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -o "ConnectTimeout 60" -p 33322 vsoc-01@localhost 'systemctl is-system-running | grep -qE "running|degraded"'; do sleep 30; done
        cp -f console_001.log console_001_p1.log
        CONSOLELINES=$(cat console_001_p1.log | wc -l)
        cat console_001_p1.log
        grep "login:" console_001_p1.log > /dev/null
        echo "CONSOLELINES=${CONSOLELINES}" >> $GITHUB_ENV
    - name: Test whether Host Orchestrator in the docker instance is alive or not.
      run: |
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'echo cuttlefish | sudo -S -k docker pull us-docker.pkg.dev/android-cuttlefish-artifacts/cuttlefish-orchestration/cuttlefish-orchestration:nightly'
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'echo cuttlefish | sudo -S -k docker run -d --privileged -p 4080:2080 us-docker.pkg.dev/android-cuttlefish-artifacts/cuttlefish-orchestration/cuttlefish-orchestration:nightly'
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'curl http://localhost:4080/cvds -v --retry 6 --retry-all-errors --retry-delay 30'
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'curl http://localhost:4080/cvds -v' | tr -d '[:space:]' | grep -xq '{"cvds":\[\]}'
    - name: Shutdown qemu
      run: |
        sshpass -p cuttlefish ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -p 33322 vsoc-01@localhost 'echo cuttlefish | sudo -S -k shutdown -h 1'
        while ! grep "reboot: Power down" console_001.log; do sleep 30; done
        tail -n +"${CONSOLELINES}" console_001.log
