-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Adding Cloud run system package sample #2554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
e169f81
Adding Cloud run system package sample
dinagraves fee0179
Adding comments and clarifications
dinagraves 9895838
Fixing nits
dinagraves 508808d
Fixing lint
dinagraves 9f8de06
Clarifying comments on graphviz
dinagraves 5289ab0
Clarifying comments on graphviz
dinagraves 8864e81
Updating tests to use locally installed graphviz
dinagraves 7695c4c
Replacing full filepath with local 'dot'
dinagraves ce603e6
Trigger tests
dinagraves 4d1b09a
Updating to be python3.6 compatible
dinagraves 4e8ee04
Trigger tests
dinagraves 3d4682a
Merge branch 'master' into run-system-package
kurtisvg 3178866
Black formatting
dinagraves 5875146
Merge branch 'run-system-package' of github.com:GoogleCloudPlatform/p…
dinagraves 30a0fd8
Trigger tests
dinagraves 3ec076d
Merge branch 'master' of github.com:GoogleCloudPlatform/python-docs-s…
dinagraves 783778c
Trigger tests
dinagraves d0b5779
Stronger assertion in test
dinagraves e7ce431
Merge branch 'master' into run-system-package
busunkim96 5a341cb
String Formatting
dinagraves ec8f834
String formatting
dinagraves 7d1864c
fix lint in noxfile and only run sample in Py 3
busunkim96 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Dockerfile | ||
| .dockerignore | ||
| __pycache__ | ||
| .pytest_cache |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # Copyright 2019 Google, LLC. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # Use the official Python image. | ||
| # https://hub.docker.com/_/python | ||
| FROM python:3.7 | ||
|
|
||
| # [START run_system_package_ubuntu] | ||
| RUN apt-get update -y && apt-get install -y \ | ||
| graphviz \ | ||
| && apt-get clean | ||
| # [END run_system_package_ubuntu] | ||
|
|
||
| # Copy application dependency manifests to the container image. | ||
| # Copying this separately prevents re-running pip install on every code change. | ||
| COPY requirements.txt . | ||
|
|
||
| # Install production dependencies. | ||
| RUN pip install -r requirements.txt | ||
|
|
||
| # Copy local code to the container image. | ||
| ENV APP_HOME /app | ||
| WORKDIR $APP_HOME | ||
| COPY . . | ||
|
|
||
| # Run the web service on container startup. | ||
| # Use gunicorn webserver with one worker process and 8 threads. | ||
| # For environments with multiple CPU cores, increase the number of workers | ||
| # to be equal to the cores available. | ||
| CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Cloud Run System Package Sample | ||
|
|
||
| This sample shows how to use a CLI tool installed as a system package as part of a web service. | ||
|
|
||
| Use it with the [Using system packages tutorial](https://cloud.google.com/run/docs/tutorials/system-packages). | ||
|
|
||
|
|
||
| [![Run in Google Cloud][run_img]][run_link] | ||
|
|
||
| [run_img]: https://storage.googleapis.com/cloudrun/button.svg | ||
| [run_link]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&cloudshell_working_dir=run/system-package | ||
|
|
||
| ## Build | ||
|
|
||
| ``` | ||
| docker build --tag graphviz:python . | ||
| ``` | ||
|
|
||
| ## Run Locally | ||
|
|
||
| ``` | ||
| docker run --rm -p 9090:8080 graphviz:python | ||
| ``` | ||
|
|
||
| ## Test | ||
|
|
||
| ``` | ||
| pytest | ||
| ``` | ||
|
|
||
| _Note: you may need to install `pytest` using `pip install pytest`._ | ||
|
|
||
| ## Deploy | ||
|
|
||
| ```sh | ||
| # Set an environment variable with your GCP Project ID | ||
| export GOOGLE_CLOUD_PROJECT=<PROJECT_ID> | ||
|
|
||
| # Submit a build using Google Cloud Build | ||
| gcloud builds submit --tag gcr.io/${GOOGLE_CLOUD_PROJECT}/graphviz | ||
|
|
||
| # Deploy to Cloud Run | ||
| gcloud beta run deploy graphviz --image gcr.io/${GOOGLE_CLOUD_PROJECT}/graphviz | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # Copyright 2019 Google, LLC. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from flask import Flask, make_response, request | ||
| import os | ||
| import subprocess | ||
| import sys | ||
|
|
||
|
|
||
| app = Flask(__name__) | ||
dinagraves marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| # [START run_system_package_handler] | ||
| @app.route("/diagram.png", methods=["GET"]) | ||
| def index(): | ||
dinagraves marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Takes an HTTP GET request with query param dot and | ||
| # returns a png with the rendered DOT diagram in a HTTP response. | ||
| try: | ||
| image = create_diagram(request.args.get("dot")) | ||
| response = make_response(image) | ||
| response.headers.set("Content-Type", "image/png") | ||
| return response | ||
|
|
||
| except Exception as e: | ||
| print("error: {}".format(e)) | ||
|
|
||
| # Flush the stdout to avoid log buffering. | ||
| sys.stdout.flush() | ||
|
|
||
| # If no graphviz definition or bad graphviz def, return 400 | ||
| if "syntax" in str(e): | ||
| return "Bad Request: {}".format(e), 400 | ||
|
|
||
| return "Internal Server Error", 500 | ||
|
|
||
|
|
||
| # [END run_system_package_handler] | ||
|
|
||
|
|
||
| # [START run_system_package_exec] | ||
| def create_diagram(dot): | ||
dinagraves marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Generates a diagram based on a graphviz DOT diagram description. | ||
| if not dot: | ||
| raise Exception("syntax: no graphviz definition provided") | ||
|
|
||
| dot_args = [ # These args add a watermark to the dot graphic. | ||
| "-Glabel=Made on Cloud Run", | ||
| "-Gfontsize=10", | ||
| "-Glabeljust=right", | ||
| "-Glabelloc=bottom", | ||
| "-Gfontcolor=gray", | ||
| "-Tpng", | ||
| ] | ||
|
|
||
| # Uses local `dot` binary from Graphviz: | ||
| # https://graphviz.gitlab.io | ||
| image = subprocess.run( | ||
| ["dot"] + dot_args, input=dot.encode("utf-8"), stdout=subprocess.PIPE | ||
| ).stdout | ||
|
|
||
| if not image: | ||
| raise Exception("syntax: bad graphviz definition provided") | ||
| return image | ||
|
|
||
|
|
||
| # [END run_system_package_exec] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| PORT = int(os.getenv("PORT")) if os.getenv("PORT") else 8080 | ||
|
|
||
| # This is used when running locally. Gunicorn is used to run the | ||
| # application on Cloud Run. See entrypoint in Dockerfile. | ||
| app.run(host="127.0.0.1", port=PORT, debug=True) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # Copyright 2019 Google, LLC. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # NOTE: | ||
| # To pass these tests locally, run `brew install graphviz` | ||
|
|
||
|
|
||
| import main | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def client(): | ||
| main.app.testing = True | ||
| return main.app.test_client() | ||
|
|
||
|
|
||
| def test_empty_query_string(client): | ||
| r = client.get("/diagram.png") | ||
| assert r.status_code == 400 | ||
|
|
||
|
|
||
| def test_empty_dot_parameter(client): | ||
| r = client.get("/diagram.png?dot=") | ||
| assert r.status_code == 400 | ||
|
|
||
|
|
||
| def test_bad_dot_parameter(client): | ||
| r = client.get("/diagram.png?dot=digraph") | ||
| assert r.status_code == 400 | ||
|
|
||
|
|
||
| def test_good_dot_parameter(client): | ||
| r = client.get("/diagram.png?dot=digraph G { A -> {B, C, D} -> {F} }") | ||
| assert r.content_type == "image/png" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Flask==1.1.1 | ||
| pytest==5.1.3 | ||
| gunicorn==19.9.0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.