name: Centreon collect packaging

on:
  workflow_call:
    inputs:
      major_version:
        required: true
        type: string
      minor_version:
        required: true
        type: string
      cma_major_version:
        required: true
        type: string
      cma_minor_version:
        required: true
        type: string
      img_version:
        required: true
        type: string
      release:
        required: true
        type: string
      commit_hash:
        required: true
        type: string
      stability:
        required: true
        type: string
      legacy_engine:
        required: true
        type: string
      build_debug_packages:
        required: true
        type: boolean
      packages_in_artifact:
        required: true
        type: boolean
      image:
        required: true
        type: string
      distrib:
        required: true
        type: string
      arch:
        required: true
        type: string
      is_nightly:
        required: false
        type: string
      only_robot_cma:
        required: false
        type: boolean
        default: false
    secrets:
      collect_s3_access_key:
        required: true
      collect_s3_secret_key:
        required: true
      registry_username:
        required: true
      registry_password:
        required: true
      rpm_gpg_key:
        required: true
      rpm_gpg_signing_key_id:
        required: true
      rpm_gpg_signing_passphrase:
        required: true
      jira_base_url:
        required: true
      jira_user_email:
        required: true
      jira_api_token:
        required: true
jobs:
  package:
    runs-on: ${{ inputs.arch == 'arm64' && 'centreon-collect-arm64' || 'centreon-collect' }}

    env:
      SCCACHE_PATH: "/usr/bin/sccache"
      SCCACHE_BUCKET: "centreon-github-sccache"
      SCCACHE_REGION: "eu-west-1"
      AWS_ACCESS_KEY_ID: ${{ secrets.collect_s3_access_key }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.collect_s3_secret_key }}
      ONLY_ROBOT_CMA: ${{ inputs.only_robot_cma && 'ON' || 'OFF' }}

    container:
      image: ${{ vars.DOCKER_INTERNAL_REGISTRY_URL }}/${{ inputs.image }}:${{ inputs.img_version }}
      credentials:
        username: ${{ secrets.registry_username }}
        password: ${{ secrets.registry_password }}
      options: --cpus 4

    name: package

    steps:
      - name: Checkout sources
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

      - name: Parse distrib name
        id: parse-distrib
        uses: ./.github/actions/parse-distrib
        with:
          distrib: ${{ inputs.distrib }}

      - name: Install sccache
        run: |
          if [ "${{ inputs.arch }}" = "amd64" ]; then
            wget https://github.com/mozilla/sccache/releases/download/v0.12.0/sccache-v0.12.0-x86_64-unknown-linux-musl.tar.gz
            tar xzf sccache-v0.12.0-x86_64-unknown-linux-musl.tar.gz
            mv sccache-v0.12.0-x86_64-unknown-linux-musl/sccache /usr/bin/
          elif [ "${{ inputs.arch }}" = "arm64" ]; then
            wget https://github.com/mozilla/sccache/releases/download/v0.12.0/sccache-v0.12.0-aarch64-unknown-linux-musl.tar.gz
            tar xzf sccache-v0.12.0-aarch64-unknown-linux-musl.tar.gz
            mv sccache-v0.12.0-aarch64-unknown-linux-musl/sccache /usr/bin/
          fi
          ${SCCACHE_PATH} --start-server

      - name: Generate selinux binaries
        if: ${{ steps.parse-distrib.outputs.package_extension == 'rpm' }}
        run: |
          if [[ "${{ inputs.only_robot_cma }}" == "true" ]]; then
            MODULES=("centreon-monitoring-agent")
          else
            MODULES=("centreon-engine" "centreon-broker")
          fi

          cd selinux
          for MODULE in "${MODULES[@]}"; do
            cd $MODULE
            sed -i "s/@VERSION@/${{ inputs.major_version }}.${{ inputs.minor_version }}/g" $MODULE.te
            make -f /usr/share/selinux/devel/Makefile
            cd -
          done
        shell: bash

      - name: Remove selinux packaging files on debian
        if: ${{ steps.parse-distrib.outputs.package_extension == 'deb' }}
        run: rm -f packaging/centreon-*/*-selinux.yaml
        shell: bash

      - name: Compile sources
        run: |
          CMAKE="cmake3"
          if [ "${{ steps.parse-distrib.outputs.package_extension }}" = "deb" ]; then
            CMAKE="cmake"
          fi

          if [ "${{ inputs.arch }}" = "arm64" ]; then
            export VCPKG_FORCE_SYSTEM_BINARIES=1
            export TRIPLET=arm64-linux-release
          else
            export TRIPLET=x64-linux-release
          fi

          export VCPKG_ROOT="/vcpkg"
          export PATH="$VCPKG_ROOT:$PATH"

          mv /root/.cache /github/home/

          $CMAKE \
                  -B build \
                  -DVCPKG_OVERLAY_TRIPLETS=/custom-triplets \
                  -DVCPKG_TARGET_TRIPLET=$TRIPLET \
                  -DVCPKG_OVERLAY_PORTS=/overlays \
                  -GNinja \
                  -DDEBUG_ROBOT=OFF \
                  -DWITH_TESTING=OFF \
                  -DWITH_BENCH=ON \
                  -DWITH_MODULE_SIMU=OFF \
                  -DCMAKE_INSTALL_PREFIX=/usr \
                  -DWITH_STARTUP_SCRIPT=systemd \
                  -DWITH_ENGINE_LOGROTATE_SCRIPT=ON \
                  -DWITH_USER_BROKER=centreon-broker \
                  -DWITH_GROUP_BROKER=centreon-broker \
                  -DWITH_USER_ENGINE=centreon-engine \
                  -DWITH_GROUP_ENGINE=centreon-engine \
                  -DWITH_VAR_DIR=/var/log/centreon-engine \
                  -DWITH_DAEMONS=ON \
                  -DWITH_CREATE_FILES=OFF \
                  -DWITH_CONFIG_FILES=ON \
                  -DCMAKE_BUILD_TYPE=RelWithDebInfo \
                  -DCMAKE_C_COMPILER_LAUNCHER=${SCCACHE_PATH} \
                  -DCMAKE_CXX_COMPILER_LAUNCHER=${SCCACHE_PATH} \
                  -DLEGACY_ENGINE=${{ inputs.legacy_engine }} \
                  -DONLY_ROBOT_CMA=$ONLY_ROBOT_CMA \
                  -S .

          ninja -Cbuild
        shell: bash

      - name: Cache statistics
        run: ${SCCACHE_PATH} --show-stats
        shell: bash

      - name: Stop sccache server
        run: ${SCCACHE_PATH} --stop-server
        shell: bash

      - name: Generate debug files
        if: ${{ ! inputs.only_robot_cma }}
        run: |
          for file in $(find build/{broker,engine,clib,connectors} -name '*.so' -type f); do
            echo "Making a debug file of $file"
            objcopy --merge-notes --only-keep-debug $file $file.debug
            objcopy --strip-debug $file
            objcopy --add-gnu-debuglink $file.debug $file
          done
          exe=("build/broker/cbd"
               "build/broker/watchdog/cbwd"
               "build/engine/centengine"
               "build/engine/centenginestats"
               "build/connectors/perl/centreon_connector_perl"
               "build/connectors/ssh/centreon_connector_ssh"
               "build/ccc/ccc"
               "build/agent/centagent")
          for file in "${exe[@]}"; do
            echo "Making a debug file of $file"
            objcopy --merge-notes --only-keep-debug $file $file.debug
            objcopy --strip-debug $file
            objcopy --add-gnu-debuglink $file.debug $file
          done
        shell: bash

      - name: Generate Mandatory CMA debug files
        if: ${{ inputs.only_robot_cma }}
        run: |
          for file in $(find build/{broker,engine,clib,connectors} -name '*.so' -type f); do
            echo "Making a debug file of $file"
            objcopy --merge-notes --only-keep-debug $file $file.debug
            objcopy --strip-debug $file
            objcopy --add-gnu-debuglink $file.debug $file
          done
          exe=("build/broker/cbd"
               "build/engine/centengine"
               "build/engine/centenginestats"
               "build/ccc/ccc"
               "build/agent/centagent")
          for file in "${exe[@]}"; do
            echo "Making a debug file of $file"
            objcopy --merge-notes --only-keep-debug $file $file.debug
            objcopy --strip-debug $file
            objcopy --add-gnu-debuglink $file.debug $file
          done
        shell: bash

      - if: ${{ inputs.build_debug_packages == false }}
        run: rm -f packaging/centreon-{collect,monitoring-agent}/*debuginfo*.yaml
        shell: bash

      - name: Package centreon-monitoring-agent
        if: ${{ inputs.only_robot_cma }}
        uses: ./.github/actions/package
        with:
          nfpm_file_pattern: "packaging/centreon-monitoring-agent/*.yaml"
          module_name: "centreon-monitoring-agent"
          distrib: ${{ inputs.distrib }}
          package_extension: ${{ steps.parse-distrib.outputs.package_extension }}
          major_version: ${{ inputs.cma_major_version }}
          minor_version: ${{ inputs.cma_minor_version }}
          release: ${{ inputs.release }}
          arch: ${{ inputs.arch }}
          commit_hash: ${{ inputs.commit_hash }}
          cache_key: ${{ github.run_id }}-${{ github.sha }}-${{ steps.parse-distrib.outputs.package_extension }}-centreon-monitoring-agent-${{ steps.parse-distrib.outputs.repo_distrib_name }}-${{ inputs.arch }}-${{ github.head_ref || github.ref_name }}
          rpm_gpg_key: ${{ secrets.rpm_gpg_key }}
          rpm_gpg_signing_key_id: ${{ secrets.rpm_gpg_signing_key_id }}
          rpm_gpg_signing_passphrase: ${{ secrets.rpm_gpg_signing_passphrase }}
          stability: ${{ inputs.stability }}
          upload_artifacts: ${{ inputs.packages_in_artifact && 'true' || 'false' }}

      - name: Delete packaging files not needed by cma
        if: ${{ inputs.only_robot_cma }}
        run: |
          rm -f packaging/centreon-collect/centreon-{connector,*-selinux*,collect,broker-graphite.,broker-influxdb.,broker-victoria-metrics.,engine-bench.,broker*debuginfo,clib-debuginfo,engine*debuginfo}*
        shell: bash

      - name: Package centreon-collect
        uses: ./.github/actions/package
        with:
          nfpm_file_pattern: "packaging/centreon-collect/*.yaml"
          module_name: "centreon-collect"
          distrib: ${{ inputs.distrib }}
          package_extension: ${{ steps.parse-distrib.outputs.package_extension }}
          major_version: ${{ inputs.major_version }}
          minor_version: ${{ inputs.minor_version }}
          release: ${{ inputs.release }}
          arch: ${{ inputs.arch }}
          commit_hash: ${{ inputs.commit_hash }}
          cache_key: ${{ github.run_id }}-${{ github.sha }}-${{ steps.parse-distrib.outputs.package_extension }}-centreon-collect-${{ steps.parse-distrib.outputs.repo_distrib_name }}-${{ inputs.arch }}-${{ github.head_ref || github.ref_name }}
          rpm_gpg_key: ${{ secrets.rpm_gpg_key }}
          rpm_gpg_signing_key_id: ${{ secrets.rpm_gpg_signing_key_id }}
          rpm_gpg_signing_passphrase: ${{ secrets.rpm_gpg_signing_passphrase }}
          stability: ${{ inputs.stability }}
          upload_artifacts: ${{ inputs.packages_in_artifact && 'true' || 'false' }}

      - name: Cleaning not needed packages
        run: rm -rf *-debuginfo*.${{ steps.parse-distrib.outputs.package_extension }}
        shell: bash
