# yaml-language-server: $schema=gitlab-ci
# yaml-language-server: $format.enable=false

stages:
  - environment
  - compilers
  - concretize
  - install
  - deploy

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      when: never
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

variables:
  TERM: ansi
  CI_DATA: "/work/scitas-ge/richart/ci"

.parallel_definition:
  parallel:
    matrix:
      # - environment: [jed]
      #   slurm_options: ["-c 72 -p jed -q jed"]
      #   stack: ["pinot-noir"]
      #   app_image: ["registry.c4science.ch/scitas-stack/rhel9-kuma"]
      # - environment: [helvetios]
      #   slurm_options: ["-c 36"]
      #   stack: ["pinot-noir"]
      #   app_image: ["registry.c4science.ch/scitas-stack/rhel9-kuma"]
      - environment: [kuma_h100]
        slurm_options: ["-c 64 --reservation scitas"]
        stack: ["pinot-noir-gcc"]
        apptainer_options: ['--nv']
        app_image: ["registry.c4science.ch/scitas-stack/rhel9-kuma"]
      - environment: [kuma_l40s]
        slurm_options: ["-c 64 --reservation scitas"]
        stack: ["pinot-noir-gcc"]
        apptainer_options: ['--nv']
        app_image: ["registry.c4science.ch/scitas-stack/rhel9-kuma"]
      # - environment: [izar]
      #   slurm_options: ['-c 40 --gpus 2']
      #   apptainer_options: ['-nv']
      #   stack: ["pinot-noir"]
  tags:
    - ${environment}
    - stack
  variables:
    environment: ${environment}
  image: ${app_image}
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
.squashfs_var:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      variables:
        SQUASH_ID: ${CI_MERGE_REQUEST_IID}
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      variables:
        SQUASH_ID: ${CI_DEFAULT_BRANCH}

.parallel_job:
  extends:
    - .parallel_definition
    - .squashfs_var
  variables:
    MOUNT_POINT: $(jq -Mrc .stack.mount_point ${CI_PROJECT_DIR}/stacks/${stack}/config.json)
    FAKEHOME: "/scratch/$(id -un)"
    COMMAND_OPTIONS_SBATCH: ${slurm_options}
    STACK_VERSION: $(jq -Mrc .stack.version ${CI_PROJECT_DIR}/stacks/${stack}/config.json)
    APPTAINER_EXEC_OPTIONS: >-
      ${apptainer_options}
         --cleanenv
         -H $(mktemp -d -p ${FAKEHOME}/):/home/$(id -un)
         --bind ${CI_DATA}/buildcache:${MOUNT_POINT}/buildcache
         --bind ${CI_DATA}/spack-mirror:${MOUNT_POINT}/spack-mirror
         --bind ${CI_DATA}/squashfs-cache/:/squashfs-cache \
         --bind ${CI_DATA}/overlayfs:/overlayfs \
         --fusemount "host:/usr/libexec/apptainer/bin/squashfuse_ll ${CI_DATA}/squashfs-cache/${stack}-${environment}-${SQUASHFS_ID}.sqfs /overlayfs/lower"
         --fusemount "container:fuse-overlayfs -o squash_to_uid=$(id -u) -o squash_to_gid=$(id -g) -o lowerdir=/overlayfs/lower -o upperdir=/overlayfs/upper -o workdir=/overlayfs/wd ${MOUNT_POINT}/${stack}/${environment}/${STACK_VERSION}"
  after_script:
    - if [ $CI_JOB_STATUS != "success" ]; then exit 0; fi
    - ./ci/update_squashfs.sh

.spack_cache:
  cache:
    key: ${stack}-${environment}-spack-cache
    policy: pull-push
    paths:
      - ${stack}

# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
spack:checkout:
  stage: .pre
  variables:
    APPTAINER_EXEC_OPTIONS: --cleanenv --bind ${CI_DATA} --bind ${CI_DATA}/squashfs-cache:/squashfs-cache
  extends:
    - .parallel_definition
  before_script:
    - git config --global --add --bool advice.detachedHead false
  script:
    - mkdir -p ${CI_DATA}/overlayfs/{lower,upper,wd}
    - mkdir -p ${CI_DATA}/squashfs-cache
    - mkdir -p ${CI_DATA}/buildcache
    - mkdir -p ${CI_DATA}/spack-mirror
    - ./ci/prepare_squashfs.sh
  timeout: 1h

spack:setup:
  stage: environment
  extends:
    - .parallel_job
    - .spack_cache
  script:
    - ci/setup_spack.sh
  needs:
    - job: spack:checkout

spack:install_compilers:
  stage: compilers
  extends:
    - .parallel_job
    - .spack_cache
  script:
    - ci/install_compilers.sh
    - source ci/stack_env.sh
    - ${STACK_LOCATION}/spack/bin/spack -e ${environment} config blame compilers
  artifacts:
    reports:
      junit: spack-install-*.xml
  needs:
    - job: spack:setup
  timeout: 5h

spack:concretize:
  stage: concretize
  extends:
    - .parallel_job
    - .spack_cache
  script:
    - source ci/stack_env.sh

    - ${STACK_LOCATION}/spack/bin/spack -e ${environment}
          config blame | tee config-${environment}-${stack}.log

    - ${STACK_LOCATION}/spack/bin/spack -e ${environment}
          concretize | tee concretize-${environment}-${stack}.log

    - cp ${SPACK_SYSTEM_CONFIG_PATH}/spack.lock spack-${environment}-${stack}.lock
  artifacts:
    paths:
      - config-*.log
      - concretize-*.log
      - spack-*.lock
  needs:
    - job: spack:install_compilers
  timeout: 24h

spack:install:
  stage: install
  extends:
    - .parallel_job
    - .spack_cache
  script:
    - source ci/stack_env.sh
    - echo "{}" > spack-install-${environment}.xml

    - ${STACK_LOCATION}/spack/bin/spack -e ${environment} install
          --log-file spack-install-${environment}.xml
          --log-format junit
          --only-concrete
          --fail-fast
          --show-log-on-error

    - ${STACK_LOCATION}/spack/bin/spack -e ${environment} find -vl | tee spack-find-${environment}-${stack}.log
    - ${STACK_LOCATION}/spack/bin/spack -e ${environment}
          buildcache create
          --update-index
          --key EDC904DCE3D2E84E
          ${MOUNT_POINT}/buildcache
  needs:
    - job: spack:concretize
  artifacts:
    paths:
      - spack-find-*.log
    reports:
      junit: spack-install-*.xml
  timeout: 72h

spack:mksquashfs:
  stage: deploy
  extends:
    - .parallel_job
    - .spack_cache
  script:
    - source ci/stack_env.sh

    - ${STACK_LOCATION}/spack/bin/spack
      -e ${environment} module
      lmod refresh --yes | tee spack-modules-${environment}-${stack}.log

    - echo "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/stack/${stack}/stack-${stack}-${environment}-${CI_COMMIT_REF_SLUG}.sqfs"
    - "mksquashfs ${STACK_LOCATION} stack-${stack}-${environment}-${CI_COMMIT_REF_SLUG}.sqfs"
    - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file stack-${stack}-${environment}-${CI_COMMIT_REF_SLUG}.sqfs "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/stack/${stack}/stack-${stack}-${environment}-${CI_COMMIT_REF_SLUG}.sqfs"'
  needs:
    - job: spack:install
  timeout: 10h
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
