From 7978a2aa7eae064d4520c9b4a6393c91108290d7 Mon Sep 17 00:00:00 2001 From: Dan Funk Date: Thu, 17 Sep 2020 11:34:45 -0400 Subject: [PATCH] Trying to set up travis and docker auto-deploy --- .travis.yml | 47 +++++++++++++++++++++++++++ Dockerfile | 10 ++++-- config/default.py | 1 + deploy.sh | 41 +++++++++++++++++++++++ tests/services/test_ivy_service.py | 6 ++-- tests/services/test_sample_service.py | 24 +++++++------- 6 files changed, 112 insertions(+), 17 deletions(-) create mode 100644 .travis.yml create mode 100755 deploy.sh diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..a4393b8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,47 @@ +language: python + +python: + - "3.7" + +services: + - postgresql + - docker + +addons: + chrome: stable + sonarcloud: + organization: "sartography" + +before_install: + - psql -c 'create database communicator-test;' -U postgres + +install: + - pipenv install --dev + +env: + global: + - TESTING=true + - PB_ENABLED=false + - SQLALCHEMY_DATABASE_URI="postgresql://postgres:@localhost:5432/communicator_test" + +script: + - pipenv run coverage run -m pytest + - pipenv run coverage xml -i + +after_success: + - sonar-scanner + +deploy: + provider: script + script: bash deploy.sh sartography/testing-communicator + skip_cleanup: true + on: + all_branches: true + condition: $TRAVIS_BRANCH =~ ^(dev|testing|demo|training|staging|master|rrt\/.*)$ + +notifications: + email: + on_success: change + on_failure: always + recipients: + - dan@sartography.com diff --git a/Dockerfile b/Dockerfile index bc602d0..dbc28da 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,16 @@ -FROM sartography/cr-connect-python-base +FROM python:3.8-slim WORKDIR /app COPY Pipfile Pipfile.lock /app/ RUN set -xe \ - && pipenv install --dev \ + && pip install pipenv \ + && apt-get update -q \ + && apt-get install -y -q \ + gcc python3-dev libssl-dev \ + curl postgresql-client git-core \ + gunicorn3 postgresql-client \ + && pipenv install --dev && apt-get remove -y gcc python3-dev libssl-dev \ && apt-get autoremove -y \ && apt-get clean -y \ diff --git a/config/default.py b/config/default.py index f0fb087..2b28a34 100644 --- a/config/default.py +++ b/config/default.py @@ -28,6 +28,7 @@ SQLALCHEMY_DATABASE_URI = environ.get( 'SQLALCHEMY_DATABASE_URI', default="postgresql://%s:%s@%s:%s/%s" % (DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME) ) +SQLALCHEMY_TRACK_MODIFICATIONS=False TOKEN_AUTH_TTL_HOURS = float(environ.get('TOKEN_AUTH_TTL_HOURS', default=24)) SECRET_KEY = environ.get('SECRET_KEY', default="Shhhh!!! This is secret! And better darn well not show up in prod.") SWAGGER_AUTH_KEY = environ.get('SWAGGER_AUTH_KEY', default="SWAGGER") diff --git a/deploy.sh b/deploy.sh new file mode 100755 index 0000000..5dc8261 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +######################################################################### +# Builds the Docker image for the current git branch on Travis CI and +# publishes it to Docker Hub. +# +# Parameters: +# $1: Docker Hub repository to publish to +# +# Required environment variables (place in Settings menu on Travis CI): +# $DOCKER_USERNAME: Docker Hub username +# $DOCKER_TOKEN: Docker Hub access token +######################################################################### + +echo 'Building Docker image...' +DOCKER_REPO="$1" + +function branch_to_tag () { + if [ "$1" == "master" ]; then echo "latest"; else echo "$1" ; fi +} + +function branch_to_deploy_group() { + echo "covid"; + # if [[ $1 =~ ^(rrt\/.*)$ ]]; then echo "rrt"; else echo "crconnect" ; fi +} + +DOCKER_TAG=$(branch_to_tag "$TRAVIS_BRANCH") + +DEPLOY_GROUP=$(branch_to_deploy_group "$TRAVIS_BRANCH") + +echo "DOCKER_REPO = $DOCKER_REPO" +echo "DOCKER_TAG = $DOCKER_TAG" + +echo "$DOCKER_TOKEN" | docker login -u "$DOCKER_USERNAME" --password-stdin || exit 1 +docker build -f Dockerfile -t "$DOCKER_REPO:$DOCKER_TAG" . || exit 1 + + +# Push Docker image to Docker Hub +echo "Publishing to Docker Hub..." +docker push "$DOCKER_REPO" || exit 1 +echo "Done." diff --git a/tests/services/test_ivy_service.py b/tests/services/test_ivy_service.py index eac2e48..8aca4d9 100644 --- a/tests/services/test_ivy_service.py +++ b/tests/services/test_ivy_service.py @@ -12,9 +12,9 @@ class IvyServiceTest(BaseTest): def test_read_file_and_build_records(self): records = IvyService.samples_from_ivy_file(self.ivy_file) - self.assertEquals("987654321", records[0].student_id) - self.assertEquals("testpositive@virginia.edu", records[1].email) - self.assertEquals("1142270225", records[2].result_code) + self.assertEqual("987654321", records[0].student_id) + self.assertEqual("testpositive@virginia.edu", records[1].email) + self.assertEqual("1142270225", records[2].result_code) def test_invalid_file(self): with self.assertRaises(CommError): diff --git a/tests/services/test_sample_service.py b/tests/services/test_sample_service.py index 9c1e2ff..ee2cf70 100644 --- a/tests/services/test_sample_service.py +++ b/tests/services/test_sample_service.py @@ -34,38 +34,38 @@ class IvyServiceTest(BaseTest): # Load firebase records service = SampleService() fb_samples = self.get_firebase_records() - self.assertEquals(0, len(db.session.query(Sample).all())) + self.assertEqual(0, len(db.session.query(Sample).all())) service.add_or_update_records(fb_samples) - self.assertEquals(4, len(db.session.query(Sample).all())) + self.assertEqual(4, len(db.session.query(Sample).all())) ivy_samples = IvyService.samples_from_ivy_file(self.ivy_file) service.add_or_update_records(ivy_samples) # There are 6 records in ivy, but three records that should match up, giving seven total - self.assertEquals(6, len(db.session.query(Sample).filter(Sample.in_ivy == True).all())) - self.assertEquals(4, len(db.session.query(Sample).filter(Sample.in_firebase == True).all())) - self.assertEquals(3, len(db.session.query(Sample).filter(Sample.in_firebase == True) + self.assertEqual(6, len(db.session.query(Sample).filter(Sample.in_ivy == True).all())) + self.assertEqual(4, len(db.session.query(Sample).filter(Sample.in_firebase == True).all())) + self.assertEqual(3, len(db.session.query(Sample).filter(Sample.in_firebase == True) .filter(Sample.in_ivy == True).all())) - self.assertEquals(7, len(db.session.query(Sample).all())) + self.assertEqual(7, len(db.session.query(Sample).all())) def test_correlate_samples_ivy_first(self): service = SampleService() - self.assertEquals(0, len(db.session.query(Sample).all())) + self.assertEqual(0, len(db.session.query(Sample).all())) ivy_samples = IvyService.samples_from_ivy_file(self.ivy_file) service.add_or_update_records(ivy_samples) - self.assertEquals(6, len(db.session.query(Sample).all())) + self.assertEqual(6, len(db.session.query(Sample).all())) fb_samples = self.get_firebase_records() service.add_or_update_records(fb_samples) - self.assertEquals(6, len(db.session.query(Sample).filter(Sample.in_ivy == True).all())) - self.assertEquals(4, len(db.session.query(Sample).filter(Sample.in_firebase == True).all())) - self.assertEquals(3, len(db.session.query(Sample).filter(Sample.in_firebase == True) + self.assertEqual(6, len(db.session.query(Sample).filter(Sample.in_ivy == True).all())) + self.assertEqual(4, len(db.session.query(Sample).filter(Sample.in_firebase == True).all())) + self.assertEqual(3, len(db.session.query(Sample).filter(Sample.in_firebase == True) .filter(Sample.in_ivy == True).all())) - self.assertEquals(7, len(db.session.query(Sample).all())) + self.assertEqual(7, len(db.session.query(Sample).all()))