From 491a9f5229375a665dd5b03026db8fc9087ae7ea Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Fri, 26 Feb 2016 11:05:51 -0800 Subject: [PATCH] Adding a sample for extending the compat runtime. --- .../extending_runtime_compat/.dockerignore | 5 ++++ .../extending_runtime_compat/Dockerfile | 9 ++++++ .../extending_runtime_compat/README.md | 17 +++++++++++ managed_vms/extending_runtime_compat/app.yaml | 8 +++++ managed_vms/extending_runtime_compat/main.py | 30 +++++++++++++++++++ .../extending_runtime_compat/main_test.py | 30 +++++++++++++++++++ .../extending_runtime_compat/requirements.txt | 1 + managed_vms/hello_world_compat/README.md | 6 ++-- managed_vms/hello_world_compat/app.yaml | 1 + 9 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 managed_vms/extending_runtime_compat/.dockerignore create mode 100644 managed_vms/extending_runtime_compat/Dockerfile create mode 100644 managed_vms/extending_runtime_compat/README.md create mode 100644 managed_vms/extending_runtime_compat/app.yaml create mode 100644 managed_vms/extending_runtime_compat/main.py create mode 100644 managed_vms/extending_runtime_compat/main_test.py create mode 100644 managed_vms/extending_runtime_compat/requirements.txt diff --git a/managed_vms/extending_runtime_compat/.dockerignore b/managed_vms/extending_runtime_compat/.dockerignore new file mode 100644 index 000000000000..5ce5abfa3fce --- /dev/null +++ b/managed_vms/extending_runtime_compat/.dockerignore @@ -0,0 +1,5 @@ +.dockerignore +Dockerfile +.git +.hg +.svn diff --git a/managed_vms/extending_runtime_compat/Dockerfile b/managed_vms/extending_runtime_compat/Dockerfile new file mode 100644 index 000000000000..7ca5c7f16575 --- /dev/null +++ b/managed_vms/extending_runtime_compat/Dockerfile @@ -0,0 +1,9 @@ +# Extend the Managed VMs python-compat runtime. +FROM gcr.io/google_appengine/python-compat-multicore + +# Install the fortunes binary from the debian repositories. +RUN apt-get update && apt-get install -y fortunes + +ADD . /app/ + +RUN if [ -s requirements.txt ]; then pip install -r requirements.txt; fi diff --git a/managed_vms/extending_runtime_compat/README.md b/managed_vms/extending_runtime_compat/README.md new file mode 100644 index 000000000000..47c545d9e70c --- /dev/null +++ b/managed_vms/extending_runtime_compat/README.md @@ -0,0 +1,17 @@ +## Google App Engine Managed VMs extending runtime python-compat + +This sample demonstrates how to extend the [python-compat](https://cloud.google.com/appengine/docs/managed-vms/python/migrating-an-existing-app) runtime on [Google App Engine Managed VMs](https://cloud.google.com/appengine/docs/python/managed-vms/hello-world) + +### Running & deploying the sample + +1. `requirements.txt` is automatically installed by the runtime when deploying, however, to run the sample locally you will need to install dependencies: + + $ pip install -r requirements.txt + +2. Run the sample on your development server: + + $ dev_appserver.py --runtime python-compat . + +3. Deploy the sample: + + $ gcloud preview app deploy diff --git a/managed_vms/extending_runtime_compat/app.yaml b/managed_vms/extending_runtime_compat/app.yaml new file mode 100644 index 000000000000..bd752d1259d1 --- /dev/null +++ b/managed_vms/extending_runtime_compat/app.yaml @@ -0,0 +1,8 @@ +runtime: custom +vm: true +threadsafe: true +api_version: 1 + +handlers: +- url: .* # This regex directs all routes to main.app + script: main.app diff --git a/managed_vms/extending_runtime_compat/main.py b/managed_vms/extending_runtime_compat/main.py new file mode 100644 index 000000000000..e9bc3eacfa1a --- /dev/null +++ b/managed_vms/extending_runtime_compat/main.py @@ -0,0 +1,30 @@ +# Copyright 2016 Google Inc. All Rights Reserved. +# +# 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. + +# [START app] +import subprocess + +from flask import Flask + + +app = Flask(__name__) + + +# [START example] +@app.route('/') +def fortune(): + output = subprocess.check_output('/usr/games/fortune') + return output, 200, {'Content-Type': 'text/plain; charset=utf-8'} +# [END example] +# [END app] diff --git a/managed_vms/extending_runtime_compat/main_test.py b/managed_vms/extending_runtime_compat/main_test.py new file mode 100644 index 000000000000..43e9a2be3e11 --- /dev/null +++ b/managed_vms/extending_runtime_compat/main_test.py @@ -0,0 +1,30 @@ +# Copyright 2016 Google Inc. All Rights Reserved. +# +# 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. + +import os + +import main +import pytest + + +@pytest.mark.skipif( + not os.path.exists('/usr/games/fortune'), + reason='Fortune executable is not installed.') +def test_index(): + main.app.testing = True + client = main.app.test_client() + + r = client.get('/') + assert r.status_code == 200 + assert len(r.data) diff --git a/managed_vms/extending_runtime_compat/requirements.txt b/managed_vms/extending_runtime_compat/requirements.txt new file mode 100644 index 000000000000..632a1efabce7 --- /dev/null +++ b/managed_vms/extending_runtime_compat/requirements.txt @@ -0,0 +1 @@ +Flask==0.10.1 diff --git a/managed_vms/hello_world_compat/README.md b/managed_vms/hello_world_compat/README.md index 51cbecc5363a..2b63f050a972 100644 --- a/managed_vms/hello_world_compat/README.md +++ b/managed_vms/hello_world_compat/README.md @@ -1,12 +1,12 @@ -## Google App Engine Managed VMs Python Hello World +## Google App Engine Managed VMs python-compat Hello World -This sample demonstrates using [Python on Google App Engine Managed VMs](https://cloud.google.com/appengine/docs/python/managed-vms/hello-world) +This sample demonstrates using the [python-compat](https://cloud.google.com/appengine/docs/managed-vms/python/migrating-an-existing-app) runtime on [Google App Engine Managed VMs](https://cloud.google.com/appengine/docs/python/managed-vms/hello-world) ### Running & deploying the sample 1. `requirements.txt` is automatically installed by the runtime when deploying, however, to run the sample locally you will need to install dependencies: - $ pip install -t lib -r requirements.txt + $ pip install -r requirements.txt 2. Run the sample on your development server: diff --git a/managed_vms/hello_world_compat/app.yaml b/managed_vms/hello_world_compat/app.yaml index 5d8928f3e234..99dd1f152294 100644 --- a/managed_vms/hello_world_compat/app.yaml +++ b/managed_vms/hello_world_compat/app.yaml @@ -1,6 +1,7 @@ runtime: python-compat vm: true threadsafe: true +api_version: 1 handlers: - url: .* # This regex directs all routes to main.app