Fortune Speak is a Python sample app for Managed VMs that synthesize and display a random fortune sound everytime you load the page.
It extends a traditional Python App Engine application with new functionalities that are unlocked by Managed VMs
- Get more CPU and RAM by running your App Engine module on Google Compute Engine VMs
- set GCE machine type in
app.yaml
(view sources)
- set GCE machine type in
- Escape the sandbox by writing to files and launching subprocess
- cache wave file to local disk (view sources)
- launch the
fortune
executable (view sources)
- Customize your runtime by installing third party packages
- install the
fortune
package withapt-get
(view sources) - install
pyttsx
andflask
fromrequirements.txt
withpip
(view sources)
- install the
- Call into native Python C extensions
- import and call
_speak
pyttsx
driver (view sources)
- import and call
Below is a tutorial that will guide you on how to build, run and deploy this application step by step.
During this step you will
- create a new Managed VMs project
- provision your local development environments
- run the final application locally and deploy it to production
- Go to Google Developers Console and create a new project.
- Enable billing
- Open
https://preview.appengine.google.com/settings?&app_id=s~<project>
- Click
Setup Google APIs project for VM Runtime...
-
Install boot2docker
-
Setup and start boot2docker
boot2docker init boot2docker up
- Get and install the SDK preview release
- Setup the Managed VMs components: gcloud components update appengine-managed-vms gcloud auth login gcloud set project docker pull gcr.io/google_appengine/python-compat
-
Get the application code
git clone https://github.com/GoogleCloudPlatform/appengine-vm-fortunespeak-python cd appengine-vm-fortunespeak-python git fetch --all
-
Run the application locally
gcloud preview app run app.yaml
-
After seeing a request to
/_ah/start
in the logs open http://localhost:8080
-
Build and deploy the application image
gcloud preview app deploy app.yaml --server preview.appengine.google.com
-
After the command complete succesfully open
https://<project>.appspot.com
During this step you will:
- create a simple Python hello world application
-
First switch to
step0
branchgit checkout step0
-
Create a
app.yaml
file w/ the default App Engine configuration -
Create a
main.py
file usingwebapp2
w/ aRequestHandler
that printhello: <Country the HTTP request is coming from>
-
Run locally and deploy it to production.
-
Review the solution
-
Compare with your working directory
git diff step1 -R
-
If stuck, stash your working directory and switch to the solution branch
git stash git checkout step1
During this step you will:
- enable Managed VMs in your application configuration
- select a bigger instance class
- Modify
app.yaml
to addvm: true
- Modify
app.yaml
to select a bigger instance class - Run locally
- Notice that a Dockerfile has been created in your application directory
- Deploy to production
-
Review the solution
-
Compare with your working directory
git diff step2 -R
-
If stuck, stash your working directory and switch to the solution branch
git stash git checkout step2
During this step you will:
- perform system operation: write to the local filesystem
- Modify
main.py
to log the greetings to a file called 'messages.txt' - Modify
main.py
to add a newRequestHandler
that display the content of this file. - Run locally and deploy it to production
-
Review the solution
-
Compare with your working directory
git diff step3 -R
-
If stuck, stash your working directory and switch to the solution branch
git stash git checkout step3
During this step you will:
- add native dependencies to your application with
apt-get
- perform system operattion: launch an external process
- Modify
Dockerfile
toRUN apt-get install -y fortunes
- Modify
main.py
to launch/usr/games/fortunes
with thesubprocess
module in the mainRequestHandler
, capture the standard output and display it back in the response. - Run it locally and deploy to production
-
Review the solution
-
Compare with your working directory
git diff step4 -R
-
If stuck, stash your working directory and switch to the solution branch
git stash git checkout step4
During this step you will:
- managed your application dependencies with
pip
andrequirements.txt
- rewrite your web application handler using a modern python framework
Flask
- Create a
requirements.txt
withFlask
listed as a dependency - Modify
Dockerfile
toRUN pip install -r requirements.txt -t .
- Edit
main.py
to useflask.route
instead ofwebapp2.RequestHandler
- Run it locally and deploy to production
-
Review the solution
-
Compare with your working directory
git diff step5 -R
-
If stuck, stash your working directory and switch to the solution branch
git stash git checkout step5
During this step you will:
- add
pyttsx
C extensions as a dependency of your application - call into native code to perform text to speech.
-
Modify
requirements.txt
to listpyttsx
as a depdency -
Copy the file
synth.py
from the master branchgit checkout master synth.py
-
Call
synth.Say
from you main request handler and return the wavform data with theaudio-x/wav
content type -
Run it locally and deploy to production
-
Review the solution
-
Compare with your working directory
git diff step6 -R
-
If stuck, stash your working directory and switch to the solution branch
git stash git checkout step6