Skip to content
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

Adding new Managed VMs samples. #156

Merged
merged 1 commit into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions managed_vms/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/
68 changes: 68 additions & 0 deletions managed_vms/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
## Google App Engine Managed VMs Python Samples

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).

See our other [Google Cloud Platform github repos](https://github.com/GoogleCloudPlatform) for sample applications and
scaffolding for other frameworks and use cases.

## Run Locally

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.

In general, the samples typically require:

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).

2. Setup the gcloud tool. This provides authentication to Google Cloud APIs and services.

```
gcloud init
```

3. Clone this repo.

```
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
cd python-docs-samples/managed_vms
```

4. Open a sample folder, create a virtualenv, install dependencies, and run the sample:

```
cd hello-world
virtualenv env
source env/bin/activate
pip install -r requirements.txt
python main.py
```

5. Visit the application at [http://localhost:8080](http://localhost:8080).


## Deploying

Some samples in this repositories may have special deployment instructions. Refer to the readme in the sample directory.

1. Use the [Google Developers Console](https://console.developer.google.com) to create a project/app id. (App id and project id are identical)

2. Setup the gcloud tool, if you haven't already.

```
gcloud init
```

3. Use gcloud to deploy your app.

```
gcloud preview app deploy app.yaml
```

4. Congratulations! Your application is now live at `your-app-id.appspot.com`

## Contributing changes

* See [CONTRIBUTING.md](../CONTRIBUTING.md)

## Licensing

* See [LICENSE](../LICENSE)
8 changes: 8 additions & 0 deletions managed_vms/analytics/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.dockerignore
Dockerfile
.git
.hg
.svn
env
*.pyc
__pycache__
15 changes: 15 additions & 0 deletions managed_vms/analytics/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM gcr.io/google_appengine/python

# Change the -p argument to use Python 2.7 if desired.
RUN virtualenv /env -p python3.4

# Set virtualenv environment variables. This is equivalent to running
# source /env/bin/activate.
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH

ADD requirements.txt /app/
RUN pip install -r requirements.txt
ADD . /app/

CMD gunicorn -b :$PORT main:app
20 changes: 20 additions & 0 deletions managed_vms/analytics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Google Analytics Measurement Protocol sample for Google App Engine Managed VMs

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).

## Setup

Before you can run or deploy the sample, you will need to do the following:

1. Create a Google Analytics Property and obtain the Tracking ID.

2. Update the environment variables in in ``app.yaml`` with your Tracking ID.

## Running locally

Refer to the [top-level README](../README.md) for instructions on running and deploying.

You will need to set the following environment variables via your shell before running the sample:

$ export GA_TRACKING_ID=[your Tracking ID]
$ python main.py
7 changes: 7 additions & 0 deletions managed_vms/analytics/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
runtime: custom
vm: true

#[START env]
env_variables:
GA_TRACKING_ID: your-tracking-id
#[END env]
64 changes: 64 additions & 0 deletions managed_vms/analytics/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright 2015 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 os

from flask import Flask
import requests


app = Flask(__name__)


# Environment variables are defined in app.yaml.
GA_TRACKING_ID = os.environ['GA_TRACKING_ID']


def track_event(category, action, label=None, value=None):
data = {
'v': '1', # API Version.
'tid': GA_TRACKING_ID, # Tracking ID / Property ID.
# Anonymous Client Identifier. Ideally, this should be a UUID that
# is associated with particular user, device, or browser instance.
'cid': '555',
't': 'event', # Event hit type.
'ec': category, # Event category.
'ea': action, # Event action.
'el': label, # Event label.
'ev': value, # Event valueself.
}

response = requests.post(
'http://www.google-analytics.com/collect', data=data)

# If the request fails, this will raise a RequestException. Depending
# on your application's needs, this may be a non-error and can be caught
# by the caller.
response.raise_for_status()


@app.route('/')
def track_example():
track_event(
category='Example',
action='test action')
return 'Event tracked.'


if __name__ == '__main__':
# This is used when running locally. Gunicorn is used to run the
# application on Google App Engine. See CMD in Dockerfile.
app.run(host='127.0.0.1', port=8080, debug=True)
# [END app]
3 changes: 3 additions & 0 deletions managed_vms/analytics/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Flask==0.10.1
gunicorn==19.4.5
requests[security]==2.9.1
8 changes: 8 additions & 0 deletions managed_vms/cloudsql/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.dockerignore
Dockerfile
.git
.hg
.svn
env
*.pyc
__pycache__
15 changes: 15 additions & 0 deletions managed_vms/cloudsql/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM gcr.io/google_appengine/python

# Change the -p argument to use Python 2.7 if desired.
RUN virtualenv /env -p python3.4

# Set virtualenv environment variables. This is equivalent to running
# source /env/bin/activate.
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH

ADD requirements.txt /app/
RUN pip install -r requirements.txt
ADD . /app/

CMD gunicorn -b :$PORT main:app
29 changes: 29 additions & 0 deletions managed_vms/cloudsql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Python Google Cloud SQL sample for Google App Engine Managed VMs

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).

## Setup

Before you can run or deploy the sample, you will need to do the following:

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:

$ gcloud sql instances create [your-instance-name] \
--assign-ip \
--authorized-networks 0.0.0.0/0 \
--tier D0

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`.

3. Update the connection string in ``app.yaml`` with your instance values.

4. Finally, run ``create_tables.py`` to ensure that the database is properly configured and to create the tables needed for the sample.

## Running locally

Refer to the [top-level README](../README.md) for instructions on running and deploying.

You will need to set the following environment variables via your shell before running the sample:

$ export SQLALCHEMY_DATABASE_URI=[your connection string]
$ python main.py
13 changes: 13 additions & 0 deletions managed_vms/cloudsql/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
runtime: custom
vm: true

#[START env]
env_variables:
# Replace user, password, and host with the values obtained when
# configuring your Cloud SQL instance.
SQLALCHEMY_DATABASE_URI: mysql+pymysql://user:password@host/db
# If you are connecting over SSL, you can specify your certificates by
# using a connection string such as:
# > mysql+pymysql://user:password@host/db?
# ssl_key=client-key.pem?ssl_cert=client-cert.pem?ssl_ca=server-ca.pem
#[END env]
25 changes: 25 additions & 0 deletions managed_vms/cloudsql/create_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#! /usr/bin/env python
# Copyright 2015 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 all]

from main import db


if __name__ == '__main__':
print('Creating all database tables...')
db.create_all()
print('Done!')
# [END all]
Loading