# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

# Pip's cache doesn't store the python packages
# https://pip.pypa.io/en/stable/reference/pip_install/#caching
#
# If you want to also cache the installed packages, you have to install
# them in a virtualenv and cache it as well.
cache:
  paths:
    - .cache/pip
    - venv/

before_script:
  # Provides credentials to pip to access private GitLab PyPi index.
  - echo "machine gitlab.pik-potsdam.de" > ~/.netrc
  - echo "login gitlab-ci-token" >> ~/.netrc
  - echo "password ${CI_JOB_TOKEN}" >> ~/.netrc

stages:
  - build
  - test
  - deploy

build-job:
  stage: build
  image: python:3.10.9
  script:
    - python -V
    - pip install virtualenv
    - virtualenv venv
    - source venv/bin/activate
    - pwd
    - ls -lisah
    - git config --global url."https://gitlab.pik-potsdam.de/".insteadOf "ssh://git@gitlab.pik-potsdam.de/"
    - pip install -r requirements.txt
    - pip install -r requirements_dev.txt

test-job1:
  stage: test
  image: python:3.10.9
  script:
    - source venv/bin/activate
    - flake8 --config .flake8

test-job2:
  stage: test
  image: python:3.10.9
  script:
    - source venv/bin/activate
    - which python
    - pip freeze
    - python -m mypy --config-file=pyproject.toml server

deploy-to-production:
  stage: deploy
  tags:
    - bare-metal
  script:
    - echo "Current working directory and user"
    - pwd
    - whoami
    - groups
    - echo $HOME
    - echo "Reset git config"
    - rm /home/gitlab-runner/.gitconfig
    - git config --global url."https://gitlab.pik-potsdam.de/".insteadOf "ssh://git@gitlab.pik-potsdam.de/"
    - git config --global user.name gitlab-runner
    - git config --global user.email gitlab-runner@gitlab.pik-potsdam.de
    - git config --global -l --show-origin
    - echo "Go to deployment location"
    - cd /home/nacsos/nacsos-core
    - ls -lisah
    - sudo chown -R gitlab-runner /home/nacsos/nacsos-core
    - sudo chgrp -R gitlab-runner /home/nacsos/nacsos-core
    - ls -lisah
    - echo "Stopping NACSOS-core service"
    - sudo systemctl stop nacsos-core.service
    - echo "Dropping virtual environment"
    - rm -rf venv
    - echo "Fetching updated source"
    - git stash  # "reset" softly by stashing (in case files changed)
    - git pull origin production  # pull from origin (production branch)
    - echo "Creating new virtual environment"
    - python3.10 -m venv venv
    - source venv/bin/activate
    - which python
    - python -V
    - echo "Installing requirements"
    - pip install -r requirements.txt
    - echo "Handling migrations"
    - pip install alembic
    - cd /home/nacsos/nacsos-core/venv/src/nacsos-data/
    - alembic upgrade head
    - sudo chown -R nacsos /home/nacsos/nacsos-core
    - sudo chgrp -R nacsos /home/nacsos/nacsos-core
    - echo "Starting NACSOS-core service"
    - sudo systemctl start nacsos-core.service
# DEPRECATED
#    - echo "Run as nacsos user"
#    - sudo -u nacsos bash ./deploy.sh $CI_JOB_TOKEN
#    - echo "Becoming 'nacsos' user"
#    - sudo -u nacsos -s
  when: manual
  only:
    - production
