name: Update CGW01 OpenLAN Cloud Gateway

defaults:
  run:
    shell: bash

env:
  AWS_DEFAULT_OUTPUT: json
  AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
  AWS_ACCESS_KEY_ID: ${{ secrets.AWS_CLIENT_ID }}
  AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_CLIENT_KEY }}

  # https://stackoverflow.com/questions/59977364/github-actions-how-use-strategy-matrix-with-script
  # Required object fiels per environment:
  # - deploy_repo_version - wlan_cloud_ucentral_deploy repo version to use
  testbeds: '[
    {
      "deploy_repo_version": "main"
    }
  ]'

on:
  workflow_dispatch:
    inputs:
      cgw_version:
        default: 'main'
        description: 'openlan-cgw branch'
        required: false
      cgw_image:
        default: 'latest'
        description: 'Image tag'
        required: false
      config_name:
        default: 'cgw01'
        #other: 'default'
        description: 'Helmfile Config/environment name'
        required: true
      namespace:
        default: 'openlan-cgw01'
        #other: 'openwifi-devcgw'
        description: 'Namespace (normally openlan-CONFIGNAME)'
        required: false
      cluster:
        default: "tip-wlan-main"
        #other: "tip-wlan-qa"
        description: "AWS cluster name: tip-wlan-main or tip-wlan-qa"
        required: false
      region:
        default: "ap-south-1"
        #other: "us-east-1"
        description: 'AWS Region'
        required: false
      upgrade:
        default: 'true'
        description: 'Just deploy CGW component, set to false for full install'
        required: false
      id:
        description: 'run identifier'
        required: false

jobs:
  id:
    name: Workflow ID Provider
    runs-on: ubuntu-latest
    steps:
      - name: ${{ github.event.inputs.id }}
        run: echo run identifier ${{ inputs.id }}

  generate-matrix:
    name: Generate matrix for build
    runs-on: ubuntu-latest
    needs:
      - id
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - name: generate-matrix
        id: set-matrix
        run: |
          cat >> $GITHUB_OUTPUT << EOF
          matrix={"include":${{ env.testbeds }}}
          EOF

  deploy:
    name: Update OpenLAN Cloud Gateway instances
    runs-on: ubuntu-latest
    needs:
      - id
      - generate-matrix
    strategy:
      matrix: ${{ fromJson( needs.generate-matrix.outputs.matrix ) }}
      fail-fast: false
    steps:
      - name: Checkout deploy repo with Helm values
        uses: actions/checkout@v4
        with:
          repository: Telecominfraproject/wlan-cloud-ucentral-deploy
          path: wlan-cloud-ucentral-deploy
          ref: ${{ matrix.deploy_repo_version }}

      # helmfile has trouble with installing from git+https repos
      # so check out the relevant branch of cgw and reference the helm chart from there
      - name: Checkout cgw repo
        uses: actions/checkout@v4
        with:
          repository: Telecominfraproject/openlan-cgw
          path: openlan-cgw
          ref: ${{ github.event.inputs.cgw_version }}

      - name: Fetch kubeconfig
        env:
          AWS_DEFAULT_REGION: ${{ github.event.inputs.region }}
        run: |
          aws eks update-kubeconfig --name ${{ github.event.inputs.cluster }}

      - name: Install kubectl, helmfile and plugins
        run: |
          curl -s -LO "https://dl.k8s.io/release/v1.27.14/bin/linux/amd64/kubectl"
          sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
          curl -s -LO "https://github.com/helmfile/helmfile/releases/download/v0.165.0/helmfile_0.165.0_linux_amd64.tar.gz"
          tar xvzf helmfile_0.165.0_linux_amd64.tar.gz helmfile
          sudo install -o root -g root -m 0755 helmfile /usr/local/bin/helmfile
          helm plugin install https://github.com/aslafy-z/helm-git --version 0.16.0
          helm plugin install https://github.com/databus23/helm-diff

      - name: Create certificate file
        working-directory: wlan-cloud-ucentral-deploy/cgw
        run: |
          echo "${{ secrets.DIGICERT_CERT }}" | base64 -d > websocket-cert.pem
          echo "${{ secrets.DIGICERT_KEY }}" | base64 -d > websocket-key.pem
          ./mkcertconfig websocket-cert.pem websocket-key.pem > values/certs.device.yaml

      - name: Deploy OpenLAN Cloud Gateway
        working-directory: wlan-cloud-ucentral-deploy/cgw
        run: |
          helm ls -n ${{ github.event.inputs.namespace }}
          if [[ "${{ github.event.inputs.upgrade }}" == "false" ]] ; then
            echo "If already installed then it may not be possible to upgrade service components"
            helmfile --environment ${{ github.event.inputs.config_name }} \
                --state-values-set "cgw.tag=${{ github.event.inputs.cgw_image }}" \
                apply
          else
            helmfile --environment ${{ github.event.inputs.config_name }} -l group=apps \
                --state-values-set "cgw.tag=${{ github.event.inputs.cgw_image }}" \
                apply
          fi

      - name: Show resource state on deployment failure
        if: failure()
        run: |
          echo "Deploy/STS:"
          kubectl get deploy,sts --namespace ${{ github.event.inputs.namespace }} -o wide
          echo "Pods:"
          kubectl get pods --namespace ${{ github.event.inputs.namespace }} -o wide
          echo "Pod Descriptions:"
          kubectl describe pods --namespace ${{ github.event.inputs.namespace }}
          echo "Services:"
          kubectl get services --namespace ${{ github.event.inputs.namespace }} -o wide
          echo "Service Descriptions:"
          kubectl describe services --namespace ${{ github.event.inputs.namespace }}
