---
sudo: required
services:
  - docker
language: python
python:
  - "2.7"
  - "3.4"
env:
  - OS_NAME=centos OS_VERSION=7
  - OS_NAME=ubuntu OS_VERSION=trusty
  - OS_NAME=ubuntu OS_VERSION=xenial
before_install:
  - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  - sudo apt-get update
  - sudo apt-get install -y docker-ce
  - docker --version
install:
  - wget http://archive.ubuntu.com/ubuntu/pool/universe/r/rpmlint/rpmlint_1.7-1_all.deb
  - sudo apt-get install -y ./rpmlint_1.7-1_all.deb lintian
  - gem install mdl
  - pip install tox
script:
  # First syntax, linting and pre-test stages
  - mdl -s relaxed README.md
  - rpmlint rpm/
  - tox -e pep8,bandit
  # Then unit tests
  - |
    if [  $TRAVIS_PYTHON_VERSION = '2.7' ]; then
      tox -e py27
    elif [  $TRAVIS_PYTHON_VERSION = '3.4' ]; then
      tox -e py34
    fi
  # with test coverage
  - tox -e cover,cobertura
  # Extract build version
  - VERSION=$(python setup.py --version)
  # Create a source tarball
  - python setup.py sdist
  # Now, start the build container
  - docker run --name build_container -d -ti -v $PWD:/src -w /src $OS_NAME:$OS_VERSION /bin/sh -c 'while true ; do sleep 1000 ; done'
  - |
    if [ $OS_NAME = 'centos' ]; then
      # Install prerequisites for the build
      docker exec -i -w /root build_container yum install -y rpm-build rpmlint
      # and ensure the build paths are present
      docker exec -i -w /root build_container sh -c 'mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}'
      # Install EPEL or OpensStack repositories to get python-pbr
      docker exec -i -w /root build_container yum install -y centos-release-openstack-queens
      docker exec -i -w /root build_container yum install -y python-pbr python-setuptools
      # Copy source tarball to the place rpmbuild expects it
      docker exec -i -w /src build_container sh -c 'cp -v dist/cloud_info_provider*tar.gz ~/rpmbuild/SOURCES'
      docker exec -i -w /src build_container sh -c 'cp -v rpm/*.spec ~/rpmbuild/SPECS'
      # Fix the RPM version when doing an inter-release build, to fix source taball name
      docker exec -i -w /root/rpmbuild/SPECS build_container sed -i "s/^\(Version.\).*/\\1 ${VERSION}/" cloud-info-provider.spec
      # Eventually build the RPMs
      docker exec -i -w /root/rpmbuild/SPECS build_container rpmbuild -ba cloud-info-provider.spec
      docker exec -i -w /root/rpmbuild/SPECS build_container rpmbuild -ba cloud-info-provider-openstack.spec
      docker exec -i -w /root/rpmbuild/SPECS build_container rpmbuild -ba cloud-info-provider-opennebula.spec
      # lint the RPM
      docker exec -i -w /root/rpmbuild build_container sh -c "rpmlint RPMS/"
      # if it works, get the RPMs we just built to the shared volume
      docker exec -i build_container sh -c 'cp -v /root/rpmbuild/RPMS/noarch/*.rpm /src'
    fi
  - |
    if [ $OS_NAME = 'ubuntu' ]; then
      docker exec -i -w /root build_container apt-get update
      docker exec -i -w /root build_container apt-get install -y devscripts debhelper git
      # Required only for cloud-info-provider package to build python package
      docker exec -i -w /root build_container apt-get install -y python-all-dev python-pbr python-setuptools
      # Build the various packages
      docker exec -i -w /src build_container debuild --no-tgz-check clean binary
      docker exec -i -w /src/debs/cloud-info-provider-openstack build_container debuild --no-tgz-check clean binary
      docker exec -i -w /src/debs/cloud-info-provider-opennebula build_container debuild --no-tgz-check clean binary
      # if it works, put all the debs in a dedicated directory
      docker exec -i -w /src build_container mkdir $OS_NAME-$OS_VERSION
      docker exec -i -w /src build_container sh -c "cp -v debs/*.deb ../*.deb /src/$OS_NAME-$OS_VERSION/"
      # Finally lint built packages
      lintian $OS_NAME-$OS_VERSION/*.deb
    fi
  # Now, start the test containers
  - docker run --name openstack_container -d -ti -v $PWD:/src -w /src $OS_NAME:$OS_VERSION /bin/sh -c 'while true ; do sleep 1000 ; done'
  - docker run --name opennebula_container -d -ti -v $PWD:/src -w /src $OS_NAME:$OS_VERSION /bin/sh -c 'while true ; do sleep 1000 ; done'
  - |
    if [ $OS_NAME = 'centos' ]; then
      # and install packages in dedicated clean containers
      docker exec -i -w /root openstack_container sh -c "yum install -y centos-release-openstack-queens"
      docker exec -i -w /src openstack_container sh -c "yum localinstall -y cloud-info-provider-openstack-*.rpm cloud-info-provider-$VERSION-*.rpm"
      # EPEL is needed for defusedxml
      docker exec -i -w /root opennebula_container sh -c "yum install -y epel-release"
      docker exec -i -w /src opennebula_container sh -c "yum localinstall -y cloud-info-provider-opennebula-*.rpm cloud-info-provider-$VERSION-*.rpm"
    fi
  - |
    if [ $OS_NAME = 'ubuntu' ]; then
      # and install packages in dedicated clean containers
      docker exec -i -w /src/$OS_NAME-$OS_VERSION openstack_container apt-get update
      docker exec -i -w /src/$OS_NAME-$OS_VERSION opennebula_container apt-get update
      if [ $OS_VERSION = 'trusty' ]; then
        # apt-get/apt is too old on trusty, so fallbaack to gdebi
        docker exec -i -w /src/$OS_NAME-$OS_VERSION openstack_container sh -c "apt-get install -y gdebi"
        docker exec -i -w /src/$OS_NAME-$OS_VERSION openstack_container sh -c "gdebi -n ./python-cloud-info-provider_*.deb ./python-cloud-info-provider-openstack_*.deb"
        docker exec -i -w /src/$OS_NAME-$OS_VERSION opennebula_container sh -c "apt-get install -y gdebi"
        docker exec -i -w /src/$OS_NAME-$OS_VERSION opennebula_container sh -c "gdebi -n ./python-cloud-info-provider_*.deb ./python-cloud-info-provider-opennebula_*.deb"
      else
        docker exec -i -w /src/$OS_NAME-$OS_VERSION openstack_container sh -c "apt-get install -y ./python-cloud-info-provider-openstack_*.deb ./python-cloud-info-provider_*.deb"
        docker exec -i -w /src/$OS_NAME-$OS_VERSION opennebula_container sh -c "apt-get install -y ./python-cloud-info-provider-opennebula_*.deb ./python-cloud-info-provider_*.deb"
      fi
    fi
deploy:
  provider: releases
  api_key:
    secure: eoZ239iUlxzG05pdHJlnfebI2dKCEikjT95MM+2iYmLBKz4upEP98nlqwjtJA1Y0wckpSRdp/iKS3z2uhubmVyKc1Z7hj3iqCq2TuMqQFDuiuXyDGImhQ7XNF8BB+6YAH25BQmwbFN5F6EkTY3mx6nXF6hQOKo9UdEs0XylcG1Y=
  file_glob: true
  file:
    - ./*.rpm
    - ./*/*.deb
  skip_cleanup: true
  on:
    tags: true
