stages:
  - build
  - deploy

build:
  stage: build
  image: docker:19.03.0
  services:
    - docker:19.03.0-dind
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: "/certs"


  script:
    - echo $DOCKERHUBUSER
    - echo $DOCKERHUBPASSWORD
    - docker login -u $DOCKERHUBUSER -p $DOCKERHUBPASSWORD docker.io
    - docker build --tag=l1ll1/strudel2_backend:$CI_COMMIT_BRANCH .
    - docker push l1ll1/strudel2_backend:$CI_COMMIT_BRANCH
  tags:
    - build_strudel2_backend

deploy_test:
  stage: deploy
  only:
    - test@hpc-team/strudel2_backend
  tags: 
    - deploy_test
  variables:
    DOCKER_HOST: unix:///var/run/docker.sock
  script:
    - echo "deploying test"
    - cp ./nginx_snippets/* /etc/nginx/snippets/
    - mkdir -p /opt/strudel2/errors/
    - cp ./html/* /opt/strudel2/errors/
    - docker login -u $DOCKERHUBUSER -p $DOCKERHUBPASSWORD docker.io
    - docker stop strudel2_backend || echo "Failed to stop existing container"
    - docker ps -a -q | xargs -I{} docker rm  {} || echo "No containers to remove"
    - docker images -q | xargs -I{} docker rmi {} || echo "No images to remove"
    - docker pull l1ll1/strudel2_backend:$CI_COMMIT_BRANCH
    - docker run --mount type=bind,source=/messages,target=/messages --mount type=bind,source=/var/log/strudel2,target=/var/log/strudel2 --restart=always  -d -p 8080:8080 -p 8090:8090 --name strudel2_backend l1ll1/strudel2_backend:$CI_COMMIT_BRANCH || echo "Unable to start image ... possibly the old wsgi process is still running?"
  dependencies:
    - build

deploy_dev:
  stage: deploy
  only:
    - dev@hpc-team/strudel2_backend
  tags: 
    - deploy_dev
  variables:
    DOCKER_HOST: unix:///var/run/docker.sock
  script:
    - echo "deploying dev"
    - sudo apt -y install bc
    - seconds=$(echo "scale=0 ; (( `date -d "16:12:00" +%s` - `date +%s` ) + 86400) % 86400 "| bc -l) ; sleep $seconds
    - cp ./nginx_snippets/* /etc/nginx/snippets/
    - mkdir -p /opt/strudel2/errors/
    - cp ./html/* /opt/strudel2/errors/
    - docker login -u $DOCKERHUBUSER -p $DOCKERHUBPASSWORD docker.io
    - docker stop strudel2_backend || echo "Failed to stop existing container"
    - docker ps -a -q | xargs -I{} docker rm  {} || echo "No containers to remove"
    - docker images -q | xargs -I{} docker rmi {} || echo "No images to remove"
    - docker pull l1ll1/strudel2_backend:$CI_COMMIT_BRANCH
    - docker run --mount type=bind,source=/messages,target=/messages --mount type=bind,source=/var/log/strudel2,target=/var/log/strudel2 --restart=always  -d -p 8080:8080 -p 8090:8090 --name strudel2_backend l1ll1/strudel2_backend:$CI_COMMIT_BRANCH || echo "Unable to start image ... possibly the old wsgi process is still running?"
  dependencies:
    - build
    
deploy_prod:
  stage: deploy
  only:
    - prod@hpc-team/strudel2_backend
  tags: 
    - deploy_prod
  variables:
    DOCKER_HOST: unix:///var/run/docker.sock
  script:
    - echo "deploying prod"
    - sudo apt -y install bc
    - seconds=$(echo "scale=0 ; (( `date -d "23:59:00" +%s` - `date +%s` ) + 86400) % 86400 "| bc -l) ; sleep $seconds
    - cp ./nginx_snippets/* /etc/nginx/snippets/
    - mkdir -p /opt/strudel2/errors/
    - cp ./html/* /opt/strudel2/errors/
    - docker login -u $DOCKERHUBUSER -p $DOCKERHUBPASSWORD docker.io
    - docker stop strudel2_backend || echo "Failed to stop existing container"
    - docker ps -a -q | xargs -I{} docker rm  {} || echo "No containers to remove"
    - docker images -q | xargs -I{} docker rmi {} || echo "No images to remove"
    - docker pull l1ll1/strudel2_backend:$CI_COMMIT_BRANCH
    - docker run --mount type=bind,source=/messages,target=/messages --mount type=bind,source=/var/log/strudel2,target=/var/log/strudel2 --restart=always  -d -p 8080:8080 -p 8090:8090 --name strudel2_backend l1ll1/strudel2_backend:$CI_COMMIT_BRANCH || echo "Unable to start image ... possibly the old wsgi process is still running?"
  dependencies:
    - build
    
