# TODO(Corne): Update this image to use our own registry once building
#              images is in place.
image: artefact.skao.int/ska-tango-images-tango-itango:9.3.7
variables:
  GIT_SUBMODULE_STRATEGY: recursive
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
cache:
  paths:
    - .cache/pip
stages:
  - building
  - linting
  - static-analysis
  - unit-tests
  - integration-tests
  - packaging
newline_at_eof:
  stage: linting
  before_script:
    - pip3 install -r tangostationcontrol/test-requirements.txt
  script:
#     TODO(Corne): Ignore shell files in submodules more cleanly
    - flake8 --filename *.sh,*.conf,*.md,*.yml --select=W292 --exclude docker-compose/tango-prometheus-exporter,.tox,.egg-info,docker
python_linting:
  stage: linting
  before_script:
    - sudo apt-get update
    - sudo apt-get install -y git
  script:
    - cd tangostationcontrol
    - tox -e pep8
bandit:
  stage: static-analysis
  before_script:
    - sudo apt-get update
    - sudo apt-get install -y git
  script:
    - cd tangostationcontrol
    - tox -e bandit
shellcheck:
  stage: static-analysis
  before_script:
    - sudo apt-get update
    - sudo apt-get install -y shellcheck
  script:
#     TODO(Corne): Ignore shell files in submodules
    - shellcheck **/*.sh
unit_test:
  stage: unit-tests
  before_script:
    - sudo apt-get update
    - sudo apt-get install -y git
  script:
    - cd tangostationcontrol
    - tox -e py37
integration_test_docker:
  stage: integration-tests
  image: docker:latest
  tags:
    - privileged
  services:
    - name: docker:dind
  variables:
    DOCKER_TLS_CERTDIR: "/certs"
  before_script:
    - apk add --update make bash docker-compose
    - apk add --update bind-tools
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - touch /root/.Xauthority
#    Hack BASH_SOURCE into sourced files, docker its sh shell won't set this
    - export BASH_SOURCE=$(pwd)/bootstrap/etc/lofar20rc.sh
#    Hack HOSTNAME env variable into host.docker.internal, set in docker-compose
    - export HOSTNAME=host.docker.internal
#    - export HOSTNAME=$(hostname -i)
#    - export HOSTNAME=$(cat /run/systemd/netif/leases/2 | grep ^ADDRESS= | awk -F'=' '{print $2}')
#    source the lofarrc file and mask its non zero exit code
    - . bootstrap/etc/lofar20rc.sh || true
#    TANGO_HOST must be unset our databaseds will be unreachable
    - unset TANGO_HOST
#    Allow integration test to execute
    - chmod u+x $CI_PROJECT_DIR/sbin/run_integration_test.sh
#    Do not remove 'bash' or statement will be ignored by primitive docker shell
    - bash $CI_PROJECT_DIR/sbin/run_integration_test.sh
wheel_packaging:
  stage: packaging
  artifacts:
    paths:
      - tangostationcontrol/dist/*.whl
  before_script:
    - sudo apt-get update
    - sudo apt-get install -y git
    - pip3 install -r tangostationcontrol/test-requirements.txt
    - pip3 install -r docker-compose/itango/lofar-requirements.txt
  script:
    - cd tangostationcontrol
    - python setup.py bdist_wheel