variables:
  DOCKER_TLS_CERTDIR: ""
  GIT_STRATEGY: clone
  GIT_SUBMODULE_STRATEGY: recursive
  REGISTRY_USER: chembl/eubopen
  APPLICATION: eubopen-web
  SERVER_IMAGE_TAG: ${CI_REGISTRY}/${REGISTRY_USER}/${APPLICATION}:${CI_COMMIT_SHORT_SHA}
  DOCKER_DRIVER: overlay
  CONFIGS_FOLDER: run_config
  # Config staging
  ENV_STAGING_FILE_PATH: ${CONFIGS_FOLDER}/staging/.env
  # INGRESS_STAGING_FILE_PATH: ${CONFIGS_FOLDER}/staging/ingress.yml
  # Config prod hx
  ENV_PROD_HX_FILE_PATH: ${CONFIGS_FOLDER}/prod_hx/.env
  # Config prod hx
  ENV_PROD_HH_FILE_PATH: ${CONFIGS_FOLDER}/prod_hh/.env

stages:
  - build
  - test_and_qa
  - prepare_config
  - deploy


# ----------------------------------------------------------------------------------------------------------------------
# build
# ----------------------------------------------------------------------------------------------------------------------
build:
  image:
    name: docker:latest
    entrypoint: ["/bin/sh", "-c"]
  stage: build
  variables:
    DOCKER_HOST: tcp://docker:2375
  services:
    - docker:dind
  tags:
    - dind
  script:
    - set -x
    - echo 'building!'
    - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
    - docker info
    - docker version # verify docker cli is there. Also prints server info
    - echo building ${SERVER_IMAGE_TAG}
    - docker build --tag ${SERVER_IMAGE_TAG} .
    - docker push ${SERVER_IMAGE_TAG}

# ----------------------------------------------------------------------------------------------------------------------
# test
# ----------------------------------------------------------------------------------------------------------------------
unit_tests:
  stage: test_and_qa
  image:
    name: $SERVER_IMAGE_TAG
    entrypoint: [""]
  tags:
    - docker
  script:
    - set -x
    - echo 'Implement these tests!'

# ----------------------------------------------------------------------------------------------------------------------
# Prepare Configuration
# ----------------------------------------------------------------------------------------------------------------------
prepare_config_staging:
  stage: prepare_config
  image:
    name: alpine/git
    entrypoint: ["/bin/sh", "-c"]
  only:
    - staging
  tags:
    - docker
  script:
    - set -x
    - rm -rf ${CONFIGS_FOLDER} || true
    - mkdir -p ${CONFIGS_FOLDER}
    - git clone https://${CONFIG_DEPLOY_GITLAB_USERNAME}:${CONFIG_DEPLOY_GITLAB_TOKEN}@${CONFIGURATIONS_REPO} ${CONFIGS_FOLDER}
  artifacts:
    paths:
      - ${ENV_STAGING_FILE_PATH}

# ----------------------------------------------------------------------------------------------------------------------
# Deploy to staging
# ----------------------------------------------------------------------------------------------------------------------
deploy_to_staging:
  stage: deploy
  image:
    name: lachlanevenson/k8s-kubectl:latest
    entrypoint: ["/bin/sh", "-c"]
  only:
    - staging
  tags:
    - docker
  script:
  - set -x
  - echo "$KUBE_CA_PEM_HX" > "$(pwd)/kube.ca.pem"
  - kubectl config set-cluster ${KUBE_CLUS_NAME_HX} --server="${KUBE_URL_HX}" --certificate-authority="$(pwd)/kube.ca.pem"
  - kubectl config set-credentials ${KUBE_USER_HX} --token="${KUBE_TOKEN_HX}"
  - kubectl config set-context ${CHEMBL_NS_STAGING} --cluster=${KUBE_CLUS_NAME_HX} --user=${KUBE_USER_HX}
  - kubectl config use-context ${CHEMBL_NS_STAGING}
  - echo ${CHEMBL_NS_STAGING} ${KUBE_URL_HX} ${KUBE_CLUS_NAME_HX} ${KUBE_USER_HX}
  - kubectl get pods -n ${CHEMBL_NS_STAGING}
  - kubectl create secret generic ${ENV_SECRET_NAME_STAGING} --from-file=.env=${ENV_STAGING_FILE_PATH} -o yaml -n ${CHEMBL_NS_STAGING} --dry-run=client | kubectl replace -f -
  - cat ${ENV_STAGING_FILE_PATH}
  - sed -i "s~<NAMESPACE>~${CHEMBL_NS_STAGING}~g" k8s-deployment.yaml
  - sed -i "s~<HTTP_PROXY>~${HTTP_PROXY_HX}~g" k8s-deployment.yaml
  - sed -i "s~<HTTPS_PROXY>~${HTTPS_PROXY_HX}~g" k8s-deployment.yaml
  - sed -i "s~<NO_PROXY>~${NO_PROXY_HX_DEFAULT}~g" k8s-deployment.yaml
  - sed -i "s~<SERVER_IMAGE_TAG>~${SERVER_IMAGE_TAG}~g" k8s-deployment.yaml
  - sed -i "s~<ENV_SECRET_NAME>~${ENV_SECRET_NAME_STAGING}~g" k8s-deployment.yaml
  - sed -i "s~<NODE_PORT>~${EUBOPEN_WEB_PORT_STAGING}~" k8s-deployment.yaml
  - cat k8s-deployment.yaml
  - kubectl apply -n ${CHEMBL_NS_STAGING} -f k8s-deployment.yaml
  - kubectl get pods -n ${CHEMBL_NS_STAGING}

# ----------------------------------------------------------------------------------------------------------------------
# Preprare configs for prod deployment
# ----------------------------------------------------------------------------------------------------------------------
prepare_config_hx_hh:
  stage: prepare_config
  image:
    name: alpine/git
    entrypoint: ["/bin/sh", "-c"]
  only:
    - master
  tags:
    - docker
  script:
    - set -x
    - rm -rf ${CONFIGS_FOLDER} || true
    - mkdir -p ${CONFIGS_FOLDER}
    - git clone https://${CONFIG_DEPLOY_GITLAB_USERNAME}:${CONFIG_DEPLOY_GITLAB_TOKEN}@${CONFIGURATIONS_REPO} ${CONFIGS_FOLDER}
  artifacts:
    paths:
      - ${ENV_PROD_HX_FILE_PATH}
      - ${ENV_PROD_HH_FILE_PATH}


# ----------------------------------------------------------------------------------------------------------------------
# Deploy to prod hx
# ----------------------------------------------------------------------------------------------------------------------
deploy_to_prod_hx:
  stage: deploy
  image:
    name: lachlanevenson/k8s-kubectl:latest
    entrypoint: ["/bin/sh", "-c"]
  only:
    - master
  tags:
    - docker
  script:
  - set -x
  - echo "$KUBE_CA_PEM_HX" > "$(pwd)/kube.ca.pem"
  - kubectl config set-cluster ${KUBE_CLUS_NAME_HX} --server="${KUBE_URL_HX}" --certificate-authority="$(pwd)/kube.ca.pem"
  - kubectl config set-credentials ${KUBE_USER_HX} --token="${KUBE_TOKEN_HX}"
  - kubectl config set-context ${CHEMBL_NS_PROD} --cluster=${KUBE_CLUS_NAME_HX} --user=${KUBE_USER_HX}
  - kubectl config use-context ${CHEMBL_NS_PROD}
  - echo ${CHEMBL_NS_PROD} ${KUBE_URL_HX} ${KUBE_CLUS_NAME_HX} ${KUBE_USER_HX}
  - kubectl get pods -n ${CHEMBL_NS_PROD}
  - kubectl create secret generic ${ENV_SECRET_NAME_PROD_HX} --from-file=.env=${ENV_PROD_HX_FILE_PATH} -o yaml -n ${CHEMBL_NS_PROD} --dry-run=client | kubectl replace -f -
  - cat ${ENV_PROD_HX_FILE_PATH}
  - sed -i "s~<NAMESPACE>~${CHEMBL_NS_PROD}~g" k8s-deployment.yaml
  - sed -i "s~<HTTP_PROXY>~${HTTP_PROXY_HX}~g" k8s-deployment.yaml
  - sed -i "s~<HTTPS_PROXY>~${HTTPS_PROXY_HX}~g" k8s-deployment.yaml
  - sed -i "s~<NO_PROXY>~${NO_PROXY_HX_DEFAULT}~g" k8s-deployment.yaml
  - sed -i "s~<SERVER_IMAGE_TAG>~${SERVER_IMAGE_TAG}~g" k8s-deployment.yaml
  - sed -i "s~<ENV_SECRET_NAME>~${ENV_SECRET_NAME_PROD_HX}~g" k8s-deployment.yaml
  - sed -i "s~<NODE_PORT>~${EUBOPEN_WEB_PORT_PROD_HX}~" k8s-deployment.yaml
  - cat k8s-deployment.yaml
  - kubectl apply -n ${CHEMBL_NS_PROD} -f k8s-deployment.yaml
  - kubectl get pods -n ${CHEMBL_NS_PROD}

# ----------------------------------------------------------------------------------------------------------------------
# Deploy to prod hh
# ----------------------------------------------------------------------------------------------------------------------
deploy_to_prod_hh:
  stage: deploy
  image:
    name: lachlanevenson/k8s-kubectl:latest
    entrypoint: ["/bin/sh", "-c"]
  only:
    - master
  tags:
    - docker
  script:
  - set -x
  - echo "$KUBE_CA_PEM_HH" > "$(pwd)/kube.ca.pem"
  - kubectl config set-cluster ${KUBE_CLUS_NAME_HH} --server="${KUBE_URL_HH}" --certificate-authority="$(pwd)/kube.ca.pem"
  - kubectl config set-credentials ${KUBE_USER_HH} --token="${KUBE_TOKEN_HH}"
  - kubectl config set-context ${CHEMBL_NS_PROD} --cluster=${KUBE_CLUS_NAME_HH} --user=${KUBE_USER_HH}
  - kubectl config use-context ${CHEMBL_NS_PROD}
  - echo ${CHEMBL_NS_PROD} ${KUBE_URL_HH} ${KUBE_CLUS_NAME_HH} ${KUBE_USER_HH}
  - kubectl get pods -n ${CHEMBL_NS_PROD}
  - kubectl create secret generic ${ENV_SECRET_NAME_PROD_HH} --from-file=.env=${ENV_PROD_HH_FILE_PATH} -o yaml -n ${CHEMBL_NS_PROD} --dry-run=client | kubectl apply -f -
  - cat ${ENV_PROD_HH_FILE_PATH}
  - sed -i "s~<NAMESPACE>~${CHEMBL_NS_PROD}~g" k8s-deployment.yaml
  - sed -i "s~<HTTP_PROXY>~${HTTP_PROXY_HH}~g" k8s-deployment.yaml
  - sed -i "s~<HTTPS_PROXY>~${HTTPS_PROXY_HH}~g" k8s-deployment.yaml
  - sed -i "s~<NO_PROXY>~${NO_PROXY_HH_DEFAULT}~g" k8s-deployment.yaml
  - sed -i "s~<SERVER_IMAGE_TAG>~${SERVER_IMAGE_TAG}~g" k8s-deployment.yaml
  - sed -i "s~<ENV_SECRET_NAME>~${ENV_SECRET_NAME_PROD_HH}~g" k8s-deployment.yaml
  - sed -i "s~<NODE_PORT>~${EUBOPEN_WEB_PORT_PROD_HH}~" k8s-deployment.yaml
  - cat k8s-deployment.yaml
  - kubectl apply -n ${CHEMBL_NS_PROD} -f k8s-deployment.yaml
  - kubectl get pods -n ${CHEMBL_NS_PROD}
