name: 'Occlum HW composite action'
description: 'Occlum HW mode container run and source build/install'
inputs:
  container-name:
    description: 'Occlum container name'
    required: true
  build-envs:
    description: 'Occlum build environments'
    required: false
    default: 'OCCLUM_RELEASE_BUILD=1'
  os:
    description: 'Occlum docker image base OS'
    required: false
    default: 'ubuntu20.04'
  container-run-params:
    description: 'Extra container run parameters'
    required: false
    default: ''

runs:
  using: "composite"
  steps:
    - name: Print info
      run: echo Occlum build environment ${{ inputs.build-envs }}
      shell: bash

    - name: Get Occlum version
      run: |
        if ${{ contains(github.event.pull_request.labels.*.name, 'Pre-release-test') }}; then
          echo "OCCLUM_VERSION=$(grep 'Version =' src/pal/include/occlum_version.h | awk '{print $4}')-test" >> $GITHUB_ENV;
        else
          echo "OCCLUM_VERSION=$(grep 'Version =' src/pal/include/occlum_version.h | awk '{print $4}')" >> $GITHUB_ENV;
        fi;
      shell: bash

    - name: Set container name
      run: echo "CONTAINER_NAME=${{ inputs.container-name }}_$RANDOM" >> $GITHUB_ENV
      shell: bash

    - name: Create container
      run: |
        if [[ "${{ matrix.self_runner[1] }}" == "SGX1-HW" ]]; then
          docker run -itd --name=${{ env.CONTAINER_NAME }} ${{ inputs.container-run-params }} --privileged --rm --env CARGO_HTTP_MULTIPLEXING=false --device /dev/isgx -v $GITHUB_WORKSPACE:/root/occlum occlum/occlum:${{ env.OCCLUM_VERSION }}-${{ inputs.os }};
        elif [[ "${{ matrix.self_runner[1] }}" == "SGX2-HW" ]]; then
          docker run -itd --name=${{ env.CONTAINER_NAME }} ${{ inputs.container-run-params }} --privileged --rm --env CARGO_HTTP_MULTIPLEXING=false -v /dev/sgx:/dev/sgx -v $GITHUB_WORKSPACE:/root/occlum occlum/occlum:${{ env.OCCLUM_VERSION }}-${{ inputs.os }};
        else
          echo "Unsupported Hardware"
        fi;
      shell: bash

    # The pccs_url must be v3
    - name: Update PCCS server
      run: |
        docker exec ${{ env.CONTAINER_NAME }} bash -c "sed -r -i 's/\"pccs_url\": \"https:\/\/localhost:8081\/sgx\/certification\/v4\/\"/\"pccs_url\": \"https:\/\/sgx-dcap-server.cn-hongkong.aliyuncs.com\/sgx\/certification\/v3\/\"/g' /etc/sgx_default_qcnl.conf"
      shell: bash

    # - name: Change download source of crates.io
    #   run: |
    #     docker exec ${{ env.CONTAINER_NAME }} bash -c "cat <<- EOF >/root/.cargo/config
    #     [source.crates-io]
    #     registry = \"https://github.com/rust-lang/crates.io-index\"
    #     replace-with = 'ustc'
    #     [source.ustc]
    #     registry = \"git://mirrors.ustc.edu.cn/crates.io-index\"
    #     EOF"
    #   shell: bash

    # - name: Config git proxy
    #   run: |
    #     docker exec ${{ env.CONTAINER_NAME }} bash -c "git config --global http.proxy socks5://localhost:5432"
    #     docker exec ${{ env.CONTAINER_NAME }} bash -c "git config --global https.proxy socks5://localhost:5432"
    #   shell: bash

    - uses: ./.github/workflows/composite_action/prebuild
      with:
        container-name: ${{ env.CONTAINER_NAME }}

    - name: Build dependencies
      run: docker exec ${{ env.CONTAINER_NAME }} bash -c "cargo uninstall sccache || true; cd /root/occlum; make submodule"
      shell: bash

    - name: Build source
      run: docker exec ${{ env.CONTAINER_NAME }} bash -c "source /opt/intel/sgxsdk/environment; cd /root/occlum; ${{ inputs.build-envs}} make install"
      shell: bash

    # When there comes new features, the configuration should be enabled accordingly
    - name: Configure Occlum features
      run: |
        if [[ "${{ matrix.self_runner[2] }}" == "EDMM" ]]; then
          docker exec ${{ env.CONTAINER_NAME }} bash -c "jq '.feature.enable_posix_shm = true | .feature.enable_edmm = true' /opt/occlum/etc/template/Occlum.json > /tmp.json && mv /tmp.json /opt/occlum/etc/template/Occlum.json"
        fi;
      shell: bash
