Skip to content

Commit b00f1dc

Browse files
author
Jonathan Wayne Parrott
committed
Merge pull request #156 from GoogleCloudPlatform/managedvms-samples
Adding new Managed VMs samples.
2 parents 839a22a + d2fa9ec commit b00f1dc

File tree

142 files changed

+2653
-36
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+2653
-36
lines changed

managed_vms/.gitignore

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
env/
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
27+
# PyInstaller
28+
# Usually these files are written by a python script from a template
29+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
30+
*.manifest
31+
*.spec
32+
33+
# Installer logs
34+
pip-log.txt
35+
pip-delete-this-directory.txt
36+
37+
# Unit test / coverage reports
38+
htmlcov/
39+
.tox/
40+
.coverage
41+
.coverage.*
42+
.cache
43+
nosetests.xml
44+
coverage.xml
45+
*,cover
46+
47+
# Django stuff:
48+
*.log
49+
50+
# Sphinx documentation
51+
docs/_build/
52+
53+
# PyBuilder
54+
target/

managed_vms/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
## Google App Engine Managed VMs Python Samples
2+
3+
These are samples for using Python on Google App Engine Managed VMs. These samples are typically referenced from the [docs](https://cloud.google.com/appengine/docs).
4+
5+
See our other [Google Cloud Platform github repos](https://github.com/GoogleCloudPlatform) for sample applications and
6+
scaffolding for other frameworks and use cases.
7+
8+
## Run Locally
9+
10+
Some samples have specific instructions. If there is a README in the sample folder, pleaese refer to it for any additional steps required to run the sample.
11+
12+
In general, the samples typically require:
13+
14+
1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/), including the [gcloud tool](https://cloud.google.com/sdk/gcloud/), and [gcloud app component](https://cloud.google.com/sdk/gcloud-app).
15+
16+
2. Setup the gcloud tool. This provides authentication to Google Cloud APIs and services.
17+
18+
```
19+
gcloud init
20+
```
21+
22+
3. Clone this repo.
23+
24+
```
25+
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
26+
cd python-docs-samples/managed_vms
27+
```
28+
29+
4. Open a sample folder, create a virtualenv, install dependencies, and run the sample:
30+
31+
```
32+
cd hello-world
33+
virtualenv env
34+
source env/bin/activate
35+
pip install -r requirements.txt
36+
python main.py
37+
```
38+
39+
5. Visit the application at [http://localhost:8080](http://localhost:8080).
40+
41+
42+
## Deploying
43+
44+
Some samples in this repositories may have special deployment instructions. Refer to the readme in the sample directory.
45+
46+
1. Use the [Google Developers Console](https://console.developer.google.com) to create a project/app id. (App id and project id are identical)
47+
48+
2. Setup the gcloud tool, if you haven't already.
49+
50+
```
51+
gcloud init
52+
```
53+
54+
3. Use gcloud to deploy your app.
55+
56+
```
57+
gcloud preview app deploy app.yaml
58+
```
59+
60+
4. Congratulations! Your application is now live at `your-app-id.appspot.com`
61+
62+
## Contributing changes
63+
64+
* See [CONTRIBUTING.md](../CONTRIBUTING.md)
65+
66+
## Licensing
67+
68+
* See [LICENSE](../LICENSE)

managed_vms/analytics/.dockerignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.dockerignore
2+
Dockerfile
3+
.git
4+
.hg
5+
.svn
6+
env
7+
*.pyc
8+
__pycache__

managed_vms/analytics/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM gcr.io/google_appengine/python
2+
3+
# Change the -p argument to use Python 2.7 if desired.
4+
RUN virtualenv /env -p python3.4
5+
6+
# Set virtualenv environment variables. This is equivalent to running
7+
# source /env/bin/activate.
8+
ENV VIRTUAL_ENV /env
9+
ENV PATH /env/bin:$PATH
10+
11+
ADD requirements.txt /app/
12+
RUN pip install -r requirements.txt
13+
ADD . /app/
14+
15+
CMD gunicorn -b :$PORT main:app

managed_vms/analytics/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Google Analytics Measurement Protocol sample for Google App Engine Managed VMs
2+
3+
This sample demonstrates how to use the [Google Analytics Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/v1/) (or any other SQL server) on [Google App Engine Managed VMs](https://cloud.google.com/appengine).
4+
5+
## Setup
6+
7+
Before you can run or deploy the sample, you will need to do the following:
8+
9+
1. Create a Google Analytics Property and obtain the Tracking ID.
10+
11+
2. Update the environment variables in in ``app.yaml`` with your Tracking ID.
12+
13+
## Running locally
14+
15+
Refer to the [top-level README](../README.md) for instructions on running and deploying.
16+
17+
You will need to set the following environment variables via your shell before running the sample:
18+
19+
$ export GA_TRACKING_ID=[your Tracking ID]
20+
$ python main.py

managed_vms/analytics/app.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
runtime: custom
2+
vm: true
3+
4+
#[START env]
5+
env_variables:
6+
GA_TRACKING_ID: your-tracking-id
7+
#[END env]

managed_vms/analytics/main.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright 2015 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START app]
16+
import os
17+
18+
from flask import Flask
19+
import requests
20+
21+
22+
app = Flask(__name__)
23+
24+
25+
# Environment variables are defined in app.yaml.
26+
GA_TRACKING_ID = os.environ['GA_TRACKING_ID']
27+
28+
29+
def track_event(category, action, label=None, value=None):
30+
data = {
31+
'v': '1', # API Version.
32+
'tid': GA_TRACKING_ID, # Tracking ID / Property ID.
33+
# Anonymous Client Identifier. Ideally, this should be a UUID that
34+
# is associated with particular user, device, or browser instance.
35+
'cid': '555',
36+
't': 'event', # Event hit type.
37+
'ec': category, # Event category.
38+
'ea': action, # Event action.
39+
'el': label, # Event label.
40+
'ev': value, # Event valueself.
41+
}
42+
43+
response = requests.post(
44+
'http://www.google-analytics.com/collect', data=data)
45+
46+
# If the request fails, this will raise a RequestException. Depending
47+
# on your application's needs, this may be a non-error and can be caught
48+
# by the caller.
49+
response.raise_for_status()
50+
51+
52+
@app.route('/')
53+
def track_example():
54+
track_event(
55+
category='Example',
56+
action='test action')
57+
return 'Event tracked.'
58+
59+
60+
if __name__ == '__main__':
61+
# This is used when running locally. Gunicorn is used to run the
62+
# application on Google App Engine. See CMD in Dockerfile.
63+
app.run(host='127.0.0.1', port=8080, debug=True)
64+
# [END app]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Flask==0.10.1
2+
gunicorn==19.4.5
3+
requests[security]==2.9.1

managed_vms/cloudsql/.dockerignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.dockerignore
2+
Dockerfile
3+
.git
4+
.hg
5+
.svn
6+
env
7+
*.pyc
8+
__pycache__

managed_vms/cloudsql/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM gcr.io/google_appengine/python
2+
3+
# Change the -p argument to use Python 2.7 if desired.
4+
RUN virtualenv /env -p python3.4
5+
6+
# Set virtualenv environment variables. This is equivalent to running
7+
# source /env/bin/activate.
8+
ENV VIRTUAL_ENV /env
9+
ENV PATH /env/bin:$PATH
10+
11+
ADD requirements.txt /app/
12+
RUN pip install -r requirements.txt
13+
ADD . /app/
14+
15+
CMD gunicorn -b :$PORT main:app

managed_vms/cloudsql/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Python Google Cloud SQL sample for Google App Engine Managed VMs
2+
3+
This sample demonstrates how to use [Google Cloud SQL](https://cloud.google.com/sql/) (or any other SQL server) on [Google App Engine Managed VMs](https://cloud.google.com/appengine).
4+
5+
## Setup
6+
7+
Before you can run or deploy the sample, you will need to do the following:
8+
9+
1. Create a Cloud SQL instance. You can do this from the [Google Developers Console](https://console.developers.google.com) or via the [Cloud SDK](https://cloud.google.com/sdk). To create it via the SDK use the following command:
10+
11+
$ gcloud sql instances create [your-instance-name] \
12+
--assign-ip \
13+
--authorized-networks 0.0.0.0/0 \
14+
--tier D0
15+
16+
2. Create a new user and database for the application. The easiest way to do this is via the [Google Developers Console](https://console.developers.google.com/project/_/sql/instances/example-instance2/access-control/users). Alternatively, you can use MySQL tools such as the command line client or workbench, but you will need to set a root password for your database using `gcloud sql instances set-root-password`.
17+
18+
3. Update the connection string in ``app.yaml`` with your instance values.
19+
20+
4. Finally, run ``create_tables.py`` to ensure that the database is properly configured and to create the tables needed for the sample.
21+
22+
## Running locally
23+
24+
Refer to the [top-level README](../README.md) for instructions on running and deploying.
25+
26+
You will need to set the following environment variables via your shell before running the sample:
27+
28+
$ export SQLALCHEMY_DATABASE_URI=[your connection string]
29+
$ python main.py

managed_vms/cloudsql/app.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
runtime: custom
2+
vm: true
3+
4+
#[START env]
5+
env_variables:
6+
# Replace user, password, and host with the values obtained when
7+
# configuring your Cloud SQL instance.
8+
SQLALCHEMY_DATABASE_URI: mysql+pymysql://user:password@host/db
9+
# If you are connecting over SSL, you can specify your certificates by
10+
# using a connection string such as:
11+
# > mysql+pymysql://user:password@host/db?
12+
# ssl_key=client-key.pem?ssl_cert=client-cert.pem?ssl_ca=server-ca.pem
13+
#[END env]

managed_vms/cloudsql/create_tables.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#! /usr/bin/env python
2+
# Copyright 2015 Google Inc. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# [START all]
17+
18+
from main import db
19+
20+
21+
if __name__ == '__main__':
22+
print('Creating all database tables...')
23+
db.create_all()
24+
print('Done!')
25+
# [END all]

0 commit comments

Comments
 (0)