# https://hub.docker.com/r/library/python/tags/
image: "python:3.7"

stages:
- build
- test
- deploy

variables:
  GIT_DEPTH: 1
  LC_ALL: "C"
  GIT_STRATEGY: clone
  GIT_SUBMODULE_STRATEGY: recursive
  HIVEMIND_SOURCE_HIVED_URL: $HIVEMIND_SOURCE_HIVED_URL
  HIVEMIND_DB_NAME: "hive_$CI_COMMIT_REF_SLUG"
  HIVEMIND_HTTP_PORT: $HIVEMIND_HTTP_PORT
  # Configured at gitlab repository settings side
  POSTGRES_USER: $HIVEMIND_POSTGRES_USER
  POSTGRES_PASSWORD: $HIVEMIND_POSTGRES_PASSWORD
  POSTGRES_HOST_AUTH_METHOD: trust
  # official way to provide password to psql: http://www.postgresql.org/docs/9.3/static/libpq-envars.html
  PGPASSWORD: $HIVEMIND_POSTGRES_PASSWORD

  PYTHONUSERBASE: ./local-site

before_script:
  - echo "CI_NODE_TOTAL is $CI_NODE_TOTAL"
  - echo "CI_NODE_INDEX is $CI_NODE_INDEX"
  - echo "CI_CONCURRENT_ID is $CI_CONCURRENT_ID"
  - echo "CI_COMMIT_REF_SLUG is $CI_COMMIT_REF_SLUG"
  - python3 --version
  - python3 -m site --user-site
  - pip3 install --user --upgrade pip setuptools

hivemind_build:
  stage: build
  script:
    - git fetch --tags
    - git tag -f ci_implicit_tag
    - echo $PYTHONUSERBASE
    - "python3 setup.py bdist_egg"
    - ls -l dist/*
  artifacts:
    paths:
      - dist/
    expire_in: 1 week
  tags:
     - hivemind

hivemind_sync:
  stage: deploy
  environment: hive-4.pl.syncad.com
  needs:
    - job: hivemind_build
      artifacts: true
  variables:
    GIT_STRATEGY: none

  script:
    - scripts/ci_sync.sh "$HIVEMIND_DB_NAME" "$HIVEMIND_POSTGRESQL_CONNECTION_STRING" "$HIVEMIND_SOURCE_HIVED_URL" $HIVEMIND_MAX_BLOCK
    - scripts/ci_start_server.sh "$HIVEMIND_DB_NAME" "$HIVEMIND_POSTGRESQL_CONNECTION_STRING" "$HIVEMIND_SOURCE_HIVED_URL" $HIVEMIND_HTTP_PORT
  artifacts:
    paths:
      - hivemind-sync.log
      - hive_server.pid
    expire_in: 1 week

  tags:
     - hivemind

hivemind_start_server:
  stage: deploy
  environment: hive-4.pl.syncad.com
  needs:
    - job: hivemind_build
      artifacts: true
  variables:
    GIT_STRATEGY: none

  script:
    - scripts/ci_start_server.sh "$HIVEMIND_DB_NAME" "$HIVEMIND_POSTGRESQL_CONNECTION_STRING" "$HIVEMIND_SOURCE_HIVED_URL" $HIVEMIND_HTTP_PORT

  artifacts:
    paths:
      - hive_server.log
      - hive_server.pid
    expire_in: 1 week

  when: manual

  tags:
     - hivemind
