name: 'scale-up'
description: 'Scale Up Private GitHub Runners'

inputs:
  max_parallel:
    description: 'Number of private runners to add'
    required: true
  verbose:
    description: 'Verbosity indicator'
    required: false
    default: 'false'
  GCP_CRED_JSON:
    description: 'GCP_CRED_JSON'
    required: false
  TMC_API_TOKEN:
    description: 'TMC_API_TOKEN'
    required: false
  GH_ARC_APP_ID:
    description: 'GH_ARC_APP_ID'
    required: false
  GH_ARC_INSTALLATION_ID:
    description: 'GH_ARC_INSTALLATION_ID'
    required: false
  GH_ARC_PRIVATE_KEY:
    description: 'GH_ARC_PRIVATE_KEY'
    required: false
  GH_ARC_PAT:
    description: 'GH_ARC_PAT'
    required: false

outputs:
  runner-type:
    description: 'Type runner deployment. gke or tmc'
    value: ${{ steps.configure-runner-type.outputs.runner-type }}

runs:
  using: 'composite'
  steps:
    - name: 'Action: wait for cluster - stream-apps-gh-runners'
      shell: bash
      env:
        VERBOSE: ${{ inputs.verbose && '--verbose' || '' }}
      run: ./scripts/wait-for-cluster-${RUNNER_TYPE}.sh stream-apps-gh-runners
    - name: 'Configure: Cluster Region'
      if: ${{ env.PROVIDER == 'gke' }}
      shell: bash
      run: |
        set +e
        REGION=$(gcloud container clusters list | grep -F "stream-apps-gh-runners" | awk '{print $2}')
        if [ "$REGION" == "" ]; then
          echo "CREATE_CLUSTER=true" >> $GITHUB_ENV
        else
          REG_MT=$(./scripts/determine-default.sh stream-apps-gh-runners "machine_type")
          export REGION
          CUR_MT=$(./scripts/determine-machine-type.sh)
          if [ "$REQ_MT" != "$CUR_MT" ]; then
            echo "::notice ::Current machinetype is $CUR_MT and required is $REQ_MT"
            echo "CREATE_CLUSTER=true" >> $GITHUB_ENV
          fi
        fi
        echo "REGION=${{ inputs.region }}" >> $GITHUB_ENV
    - name: 'Action: Re/Create SCDF PRO Runners'
      if: ${{ env.CREATE_CLUSTER == 'true' }}
      shell: bash
      env:
        VERBOSE: ${{ inputs.verbose && '--verbose' || '' }}
        GH_ARC_APP_ID: ${{ inputs.GH_ARC_APP_ID }}
        GH_ARC_INSTALLATION_ID: ${{ inputs.GH_ARC_INSTALLATION_ID }}
        GH_ARC_PRIVATE_KEY: ${{ inputs.GH_ARC_PRIVATE_KEY }}
        GH_ARC_PAT: ${{ inputs.GH_ARC_PAT }}
      run: |
        set +e
        ./scripts/delete-runners-${RUNNER_TYPE}.sh
        set -e
        ./scripts/create-runners-cluster-${RUNNER_TYPE}.sh
    - name: 'Action: scale cluster - stream-apps-gh-runners for ${{ inputs.max_parallel }} runners'
      shell: bash
      env:
        CLUSTER_NAME: 'stream-apps-gh-runners'
        VERBOSE: ${{ inputs.verbose && '--verbose' || '' }}
      run: |
        echo "::notice ::Scaling stream-apps-gh-runners to ${{ inputs.max_parallel }} pods"
        source ./scripts/kubeconfig-runners.sh
        IDLE=$(./scripts/count-runners-idle.sh runners-stream-ci)
        INC=$((11 * ${{ inputs.max_parallel }} / 10))
        if ((IDLE < INC)); then
          REQUEST=$((INC - IDLE))
          echo "::info Requesting $REQUEST runners with $IDLE idle"
          ./scripts/scale-cluster-pods.sh stream-apps-gh-runners $REQUEST --add 1
        else
          echo "::info Sufficient runners idle:$IDLE"
        fi
    - name: 'Check: Wait for cluster nodes: stream-apps-gh-runners'
      shell: bash
      env:
        VERBOSE: ${{ inputs.verbose && '--verbose' || '' }}
      run: |
        echo "::notice ::Waiting for cluster stream-apps-gh-runners and it's nodes"
        ./scripts/wait-for-cluster-${RUNNER_TYPE}.sh stream-apps-gh-runners --nodes
    - name: 'Action: Ensure large runner'
      shell: bash
      run: ./scripts/large-runners.sh
    - name: 'Action: Increase runners with ${{ inputs.max_parallel }}'
      uses: ./.github/actions/increase-runners
      with:
        inc: ${{ inputs.max_parallel }}
