From 7f68c69bf951bc8b6921e51a02cf87ef3c09a449 Mon Sep 17 00:00:00 2001 From: Lindsey Baron Date: Mon, 12 Aug 2019 13:53:01 -0700 Subject: [PATCH 1/7] ignore --- .dockerignore | 6 ++++++ .gitignore | 2 ++ 2 files changed, 8 insertions(+) create mode 100644 .dockerignore create mode 100644 .gitignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..0015c6b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +Dockerfile +README.md +*.pyc +*.pyo +*.pyd +__pycache__ \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b32d8aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +.idea \ No newline at end of file From e22294e0d44924533b56e2b7aaf429e6795656b4 Mon Sep 17 00:00:00 2001 From: Lindsey Baron Date: Mon, 12 Aug 2019 13:53:25 -0700 Subject: [PATCH 2/7] lint --- README.md | 14 +++++++------- app/app.py | 4 +++- tests/test_true.py | 5 ++--- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 2155e62..a2c81ef 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,16 @@ Your task should you choose to accept it is to: -* Create a Dockerfile to package this simple python app -* Create a script(s) for deploying the app to some cloud service that uses Docker images -* Submit a pull request with your work +* Create a Dockerfile to package this simple python app. +* Create a script(s) for deploying the app to some cloud service that uses Docker images. +* Submit a pull request with your work. Script(s) should include: -* one or more commands to initialize the cloud service resources needed before first-run -* a command that deploys the app/docker image after verifying the unit tests complete succesfully. +* one or more commands to initialize the cloud service resources needed before first-run. +* a command that deploys the app/docker image after verifying the unit tests complete successfully. -Use any language you like for the scripts (shell, python, or whatever) +Use any language you like for the scripts (shell, python, or whatever). -My recomendation for deployment platform is [Google Cloud Run](https://cloud.google.com/run/). +My recommendation for deployment platform is [Google Cloud Run](https://cloud.google.com/run/). Submit a pull request including all your changes when you are ready. \ No newline at end of file diff --git a/app/app.py b/app/app.py index de96c0c..5612de0 100644 --- a/app/app.py +++ b/app/app.py @@ -4,9 +4,11 @@ app = Flask(__name__) + @app.route('/') def hello_world(): return 'Hello Evite!\n' + if __name__ == "__main__": - app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 8080))) + app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 8080))) diff --git a/tests/test_true.py b/tests/test_true.py index 40c13cc..722303d 100644 --- a/tests/test_true.py +++ b/tests/test_true.py @@ -1,4 +1,3 @@ def test_true(): - foo = True - assert foo == True, "foo should be true" - + foo = True + assert foo == True, "foo should be true" From c5e763720b0063bb4e063cfce99661526cf5b106 Mon Sep 17 00:00:00 2001 From: Lindsey Baron Date: Mon, 12 Aug 2019 13:53:46 -0700 Subject: [PATCH 3/7] containerize and deploy --- Dockerfile | 19 +++++++++++++++++++ ship.sh | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 Dockerfile create mode 100755 ship.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..428a9a5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM python:3.7 + +COPY requirements.txt / +WORKDIR / + +RUN pip install --upgrade pip +RUN pip install -r requirements.txt +RUN pip install gunicorn +RUN pip install --upgrade pytest + +COPY app/ /app/ + +RUN pytest tests/ + +WORKDIR /app + +ENV FLASK_APP=$WORKDIR/app.py + +CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 app:app \ No newline at end of file diff --git a/ship.sh b/ship.sh new file mode 100755 index 0000000..bfa2f30 --- /dev/null +++ b/ship.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +export PROJECT_ID='devops-test1-1051' +export SERVICE_NAME='helloevite' +export GCLOUD_REGION=us-central1 + +echo 'Setting region...' +gcloud config set run/region $GCLOUD_REGION + +echo 'Setting project...' +gcloud config set project ${PROJECT_ID} + +echo 'Enabling API services...' +gcloud services enable run.googleapis.com +gcloud services enable cloudbuild.googleapis.com + + +echo 'Building...' +gcloud builds submit --tag gcr.io/${PROJECT_ID}/${SERVICE_NAME} + +echo 'Deploying...' +#gcloud beta run deploy ${PROJECT_ID} --image gcr.io/${PROJECT_ID}/app --platform managed --allow-unauthenticated +gcloud beta run deploy ${SERVICE_NAME} --image gcr.io/${PROJECT_ID}/${SERVICE_NAME} --platform managed --allow-unauthenticated \ No newline at end of file From 4d6364b223fadb5f16ef42054c316bba25f1addb Mon Sep 17 00:00:00 2001 From: Lindsey Baron Date: Mon, 12 Aug 2019 13:53:55 -0700 Subject: [PATCH 4/7] cloud build config --- cloudbuild.yaml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 cloudbuild.yaml diff --git a/cloudbuild.yaml b/cloudbuild.yaml new file mode 100644 index 0000000..4b02d9a --- /dev/null +++ b/cloudbuild.yaml @@ -0,0 +1,19 @@ +steps: + # Create docker image + - name: 'gcr.io/cloud-builders/docker' + args: ['build', '-t', 'gcr.io/$PROJECT_ID/$SERVICE_NAME', '.'] + + # Push the container image to Container Registry + - name: 'gcr.io/cloud-builders/docker' + args: ['push', 'gcr.io/$PROJECT_ID/$SERVICE_NAME'] + + # Deploy container image to Cloud Run + - name: 'gcr.io/cloud-builders/gcloud' + args: ['beta', 'run', 'deploy', + '$SERVICE_NAME', + '--image', 'gcr.io/$PROJECT_ID/$SERVICE_NAME', + '--region', '$GCLOUD_REGION', + '--platform', 'managed', + '--allow-unauthenticated',] +images: +- gcr.io/$PROJECT_ID/$SERVICE_NAME \ No newline at end of file From 7f44801ac9f90bcb2df88a04ac8a0d480d3041ce Mon Sep 17 00:00:00 2001 From: Lindsey Baron Date: Mon, 12 Aug 2019 14:14:29 -0700 Subject: [PATCH 5/7] handle tests --- Dockerfile | 1 + ship.sh | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 428a9a5..b32ba81 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,6 +9,7 @@ RUN pip install gunicorn RUN pip install --upgrade pytest COPY app/ /app/ +COPY tests/ /tests/ RUN pytest tests/ diff --git a/ship.sh b/ship.sh index bfa2f30..dc4c8e0 100755 --- a/ship.sh +++ b/ship.sh @@ -14,10 +14,17 @@ echo 'Enabling API services...' gcloud services enable run.googleapis.com gcloud services enable cloudbuild.googleapis.com - echo 'Building...' -gcloud builds submit --tag gcr.io/${PROJECT_ID}/${SERVICE_NAME} +status=$? +build="gcloud builds submit --tag gcr.io/${PROJECT_ID}/${SERVICE_NAME}" +status=$? -echo 'Deploying...' -#gcloud beta run deploy ${PROJECT_ID} --image gcr.io/${PROJECT_ID}/app --platform managed --allow-unauthenticated -gcloud beta run deploy ${SERVICE_NAME} --image gcr.io/${PROJECT_ID}/${SERVICE_NAME} --platform managed --allow-unauthenticated \ No newline at end of file +if [ $? -eq 0 ] +then + echo "Image built. Deploying..." + gcloud beta run deploy ${SERVICE_NAME} --image gcr.io/${PROJECT_ID}/${SERVICE_NAME} --platform managed --allow-unauthenticated + exit 0 +else + echo "Image failed to build." >&2 + exit 1 +fi \ No newline at end of file From 12f7e8fe7d71c5e6635a436f2ec1bc450d7a5af3 Mon Sep 17 00:00:00 2001 From: Lindsey Baron Date: Mon, 12 Aug 2019 14:33:04 -0700 Subject: [PATCH 6/7] handle tests --- cloudbuild.yaml | 19 ------------------- ship.sh | 20 +++++++++++--------- 2 files changed, 11 insertions(+), 28 deletions(-) delete mode 100644 cloudbuild.yaml diff --git a/cloudbuild.yaml b/cloudbuild.yaml deleted file mode 100644 index 4b02d9a..0000000 --- a/cloudbuild.yaml +++ /dev/null @@ -1,19 +0,0 @@ -steps: - # Create docker image - - name: 'gcr.io/cloud-builders/docker' - args: ['build', '-t', 'gcr.io/$PROJECT_ID/$SERVICE_NAME', '.'] - - # Push the container image to Container Registry - - name: 'gcr.io/cloud-builders/docker' - args: ['push', 'gcr.io/$PROJECT_ID/$SERVICE_NAME'] - - # Deploy container image to Cloud Run - - name: 'gcr.io/cloud-builders/gcloud' - args: ['beta', 'run', 'deploy', - '$SERVICE_NAME', - '--image', 'gcr.io/$PROJECT_ID/$SERVICE_NAME', - '--region', '$GCLOUD_REGION', - '--platform', 'managed', - '--allow-unauthenticated',] -images: -- gcr.io/$PROJECT_ID/$SERVICE_NAME \ No newline at end of file diff --git a/ship.sh b/ship.sh index dc4c8e0..d79d8ca 100755 --- a/ship.sh +++ b/ship.sh @@ -1,23 +1,25 @@ #!/bin/bash -export PROJECT_ID='devops-test1-1051' -export SERVICE_NAME='helloevite' -export GCLOUD_REGION=us-central1 +set -e -echo 'Setting region...' +export PROJECT_ID='devops-test1-1420' +export SERVICE_NAME='hello-evite' +export GCLOUD_REGION='us-central1' + +docker build -t ${SERVICE_NAME} . + +echo "Setting region to ${GCLOUD_REGION}..." gcloud config set run/region $GCLOUD_REGION -echo 'Setting project...' -gcloud config set project ${PROJECT_ID} +echo "Setting project ID to ${PROJECT_ID}..." +gcloud config set project $PROJECT_ID echo 'Enabling API services...' gcloud services enable run.googleapis.com gcloud services enable cloudbuild.googleapis.com echo 'Building...' -status=$? -build="gcloud builds submit --tag gcr.io/${PROJECT_ID}/${SERVICE_NAME}" -status=$? +gcloud builds submit --tag gcr.io/${PROJECT_ID}/${SERVICE_NAME} if [ $? -eq 0 ] then From 4881cd2d0719decde26af57d65d4c8f076043fe2 Mon Sep 17 00:00:00 2001 From: Lindsey Baron Date: Mon, 12 Aug 2019 14:50:57 -0700 Subject: [PATCH 7/7] remove mistake code --- ship.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/ship.sh b/ship.sh index d79d8ca..5c5b14a 100755 --- a/ship.sh +++ b/ship.sh @@ -6,8 +6,6 @@ export PROJECT_ID='devops-test1-1420' export SERVICE_NAME='hello-evite' export GCLOUD_REGION='us-central1' -docker build -t ${SERVICE_NAME} . - echo "Setting region to ${GCLOUD_REGION}..." gcloud config set run/region $GCLOUD_REGION