###############################################################################
# Global variables
###############################################################################
variables:
  DEBIAN_FRONTEND: "noninteractive"
  LC_ALL: "C.UTF-8"
  LANG: "C.UTF-8"
  SFTP_SERVER: "vnf-catalog.etsi.org"
  SFTP_PORT: "29419"
  SFTP_USER: "osmsupport"
  ONDEMAND_DESTINATION_FOLDER: "Stable"

###############################################################################
# Anchor definition with common commands
###############################################################################
.default-script: &default_script
  - apt update && apt install -y gcc git make python3 python3 python3-pip python3-pycurl python3-setuptools software-properties-common wget
  - git submodule init
  - git submodule update
  - git clone https://osm.etsi.org/gerrit/osm/osmclient.git
  - pip3 install -r osmclient/requirements.txt -r osmclient/requirements-dev.txt
  - pip3 install osmclient/
  - rm -rf osmclient/
  - ./generate-packages.sh
  - mkdir temp
  # - mv magma/* temp
  - mv *.tar.gz charm-packages/*.tar.gz temp
  - osm -vvv repo-index --origin temp --destination vnf-catalog
  - rm -rf temp

###############################################################################
# Job to build artifacts
###############################################################################
build-local-repository:
  # Builds the repository locally but does not push if this is a new MR that
  # is still in development
  image: python:3.10
  stage: build
  script: *default_script
  artifacts:
    paths:
      - vnf-catalog/
  # rules:
  #   - if: '$CI_COMMIT_REF_PROTECTED == "false"'
  #     when: on_success

###############################################################################
# Job to publish artifacts on successful merge (publication to Testing)
###############################################################################
publish-testing-repository:
  # Builds the repository and pushes it to ftp site when a MR is accepted
  image: ubuntu:22.04
  stage: deploy
  dependencies:
    - build-local-repository  # Reuses artifacts generated in build los artefactos generados en build
  script:
    - apt update && apt install -y sshpass
    - |
        set -e
        echo "Uploading new files to temporary folder Testing-new..."
        sshpass -p "$VNF_CATALOG_CREDENTIALS" sftp -o "StrictHostKeyChecking no" -P $SFTP_PORT $SFTP_USER@$SFTP_SERVER << EOF
          mkdir Testing-new
          put -r vnf-catalog/* Testing-new
        EOF
        
        echo "Switching directories..."
        sshpass -p "$VNF_CATALOG_CREDENTIALS" sftp -o "StrictHostKeyChecking no" -P $SFTP_PORT $SFTP_USER@$SFTP_SERVER << EOF
          rename Testing Testing-old
          rename Testing-new Testing
        EOF
        
        echo "Cleaning up temporary folder Testing-new..."
        sshpass -p "$VNF_CATALOG_CREDENTIALS" sftp -o "StrictHostKeyChecking no" -P $SFTP_PORT $SFTP_USER@$SFTP_SERVER << EOF
          rmdir Testing-new
        EOF
  rules:
    - if: '$CI_COMMIT_REF_NAME == "master"' # Only in master
      when: on_success
    # - if: '$CI_COMMIT_REF_PROTECTED == "true"'
    #   when: on_success

###############################################################################
# Job to publish artifacts on demand (publication to Testing)
###############################################################################
publish-ondemand-repository:
  image: ubuntu:22.04
  stage: deploy
  dependencies:
    - build-local-repository
  script:
    - apt update && apt install -y sshpass
    - |
        set -e
        DESTINATION_FOLDER="${ONDEMAND_DESTINATION_FOLDER:-Stable}"
        TEMP_FOLDER="${DESTINATION_FOLDER}-new"
        BACKUP_FOLDER="${DESTINATION_FOLDER}-old"
        
        echo "Uploading files to temporary folder $TEMP_FOLDER in FTP ..."
        sshpass -p "$VNF_CATALOG_CREDENTIALS" sftp -o "StrictHostKeyChecking no" -P $SFTP_PORT $SFTP_USER@$SFTP_SERVER << EOF
          mkdir $TEMP_FOLDER
          put -r vnf-catalog/* $TEMP_FOLDER
        EOF
        
        echo "Switching directories..."
        sshpass -p "$VNF_CATALOG_CREDENTIALS" sftp -o "StrictHostKeyChecking no" -P $SFTP_PORT $SFTP_USER@$SFTP_SERVER << EOF
          rename $DESTINATION_FOLDER $BACKUP_FOLDER
          rename $TEMP_FOLDER $DESTINATION_FOLDER
        EOF
        
        echo "Cleaning up temporary folder $TEMP_FOLDER..."
        sshpass -p "$VNF_CATALOG_CREDENTIALS" sftp -o "StrictHostKeyChecking no" -P $SFTP_PORT $SFTP_USER@$SFTP_SERVER << EOF
          rmdir $TEMP_FOLDER
        EOF
  when: manual  # This job is run on demand
  allow_failure: true
  environment:
    name: production
    url: "ftp://$SFTP_SERVER/$DESTINATION_FOLDER"