Skip to content

Commit 0494d09

Browse files
committed
Merge pull request #152 from GoogleCloudPlatform/django_mvms
Add Managed VMs Django Example
2 parents 24d4c70 + 9352154 commit 0494d09

19 files changed

+495
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ coverage.xml
77
nosetests.xml
88
python-docs-samples.json
99
__pycache__
10+
*db\.sqlite3
11+
managed_vms/django_tutorial/static/*
12+
**/migrations/*
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.dockerignore
2+
Dockerfile
3+
db.sqlite3
4+
__pycache__
5+
*.pyc
6+
*.pyo
7+
*.pyd
8+
.Python
9+
env
10+
pip-log.txt
11+
pip-delete-this-directory.txt
12+
.tox
13+
.coverage
14+
.coverage.*
15+
.cache
16+
nosetests.xml
17+
coverage.xml
18+
*,cover
19+
*.log
20+
.git
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2015, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
#
14+
15+
# [START docker]
16+
17+
# The Google App Engine python runtime is Debian Jessie with Python installed
18+
# and various os-level packages to allow installation of popular Python
19+
# libraries. The source is on github at:
20+
# https://github.com/GoogleCloudPlatform/python-docker
21+
FROM gcr.io/google_appengine/python
22+
23+
# Create a virtualenv for the application dependencies.
24+
# # If you want to use Python 3, add the -p python3.4 flag.
25+
RUN virtualenv /env
26+
27+
# Set virtualenv environment variables. This is equivalent to running
28+
# source /env/bin/activate. This ensures the application is executed within
29+
# the context of the virtualenv and will have access to its dependencies.
30+
ENV VIRTUAL_ENV /env
31+
ENV PATH /env/bin:$PATH
32+
33+
# Install dependencies.
34+
ADD requirements.txt /app/requirements.txt
35+
RUN /env/bin/pip install -r /app/requirements.txt
36+
37+
# Add Application code
38+
ADD . /app
39+
40+
CMD gunicorn -b :$PORT mysite.wsgi
41+
# [END docker]

managed_vms/django_tutorial/README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Getting started with Django on Google Cloud Platform
2+
3+
This repository is an example of how to run a [Django](https://www.djangoproject.com/)
4+
app on Google Managed VMs. It uses the [Writing your first Django app](https://docs.djangoproject.com/en/1.9/intro/tutorial01/) as the example app to deploy.
5+
6+
7+
## Setup the database
8+
9+
This tutorial assumes you are setting up Django using a SQL database, which is the easiest way to run Django. If you have an existing SQL database running, you can use that, but if not, these are the instructions for creating a managed MySQL instance using CloudSQL.
10+
11+
12+
* Create a [CloudSQL instance](https://console.cloud.google.com/project/_/sql/create)
13+
14+
* In the instances list, click your Cloud SQL instance.
15+
16+
* Click Access Control.
17+
18+
* In the IP address subsection, click Request an IPv4 address to enable access to the Cloud SQL instance through an
19+
IPv4 address. It will take a moment to initialize the new IP address.
20+
21+
* Also under Access Control, in the Authorization subsection, under Allowed Networks, click the add (+) button .
22+
23+
* In the Networks field, enter 0.0.0.0/0. This value allows access by all IP addresses.
24+
25+
* Click Save.
26+
27+
Note: setting allowed networks to 0.0.0.0/0 opens your SQL instance to traffic from any computer. For production databases, it's highly recommended to limit the authorized networks to only IP ranges that need access.
28+
29+
* Alternatively, the instance can be created with the gcloud command line tool as follows, substituting `your-root-pw
30+
with a strong, unique password.
31+
32+
`gcloud sql instances create <instance_name> --assign-ip --authorized-networks=0.0.0.0/0 set-root-password=your-root-pw`
33+
34+
* Create a Database And User
35+
36+
* Using the root password created in the last step to create a new database, user, and password using your preferred MySQL client. Alternatively, follow these instructions to create the database and user from the console.
37+
* From the CloudSQL instance in the console, click New user.
38+
* Enter a username and password for the application. For example, name the user "pythonapp" and give it a randomly
39+
generated password.
40+
* Click Add.
41+
* Click Databases and then click New database.
42+
* For Name, enter the name of your database (for example, "polls"), and click Add.
43+
44+
Once you have a SQL host, configuring mysite/settings.py to point to your database. Change `your-database-name`,
45+
`your-database-user`, `your-database-host` , and `your-database-password` to match the settings created above. Note the
46+
instance name is not used in this configuration, and the host name is the IP address you created.
47+
48+
## Running locally
49+
50+
First make sure you have Django installed. It's recommended you do so in a
51+
[virtualenv](https://virtualenv.pypa.io/en/latest/). The requirements.txt
52+
contains just the Django dependency.
53+
54+
`pip install -r requirements.txt`
55+
56+
Once the database is setup, run the migrations.
57+
58+
`python manage.py migrate`
59+
60+
If you'd like to use the admin console, create a superuser.
61+
62+
`python manage.py createsuperuser`
63+
64+
The app can be run locally the same way as any other Django app.
65+
66+
`python manage.py runserver`
67+
68+
Now you can view the admin panel of your local site at
69+
70+
`http://localhost:8080/admin`
71+
72+
## Deploying
73+
74+
Since the Django development server is not built for production, our container uses the Gunicorn server. Since Gunicorn doesn't serve static content,
75+
the static content is instead served from Google Cloud Storage.
76+
77+
First, make a bucket and make it publically readable, replacing <your-gcs-bucket> with a bucket name, such as your project id:
78+
79+
`gsutil mb gs://<your-gcs-bucket>`
80+
`gsutil defacl set public-read gs://<your-gcs-bucket>`
81+
82+
Next, gather all the static content locally into one folder using the Django `collectstatic` command
83+
84+
`python manage.py collectstatic`
85+
86+
Then upload it to CloudStorage using the `gsutil rsync` command
87+
88+
`gsutil rsync -R static/ gs://<your-gcs-bucket>/static`
89+
90+
Now your static content can be served from the following URL:
91+
92+
`http://storage.googleapis.com/<your-gcs-bucket/static/`
93+
94+
Make sure to replace <your-cloud-bucket> within `mysite/settings.py` to set STATIC_URL to the correct value to serve static content from, and
95+
uncomment the STATIC_URL to point to the new URL.
96+
97+
The app can be deployed by running
98+
99+
`gcloud preview app deploy app.yaml --set-default --version=1 --promote`
100+
101+
which deploys to version 1, and `promote` makes version 1 the default version.
102+
103+
Now you can view the admin panel of your deployed site at
104+
105+
`https://<your-app-id>.appspot.com/admin`
106+
107+
## Contributing changes
108+
109+
* See [CONTRIBUTING.md](CONTRIBUTING.md)
110+
111+
112+
## Licensing
113+
114+
* See [LICENSE](LICENSE)

managed_vms/django_tutorial/__init__.py

Whitespace-only changes.

managed_vms/django_tutorial/app.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This file specifies your Python application's runtime configuration
2+
# including URL routing, versions, static file uploads, etc. See
3+
# https://developers.google.com/appengine/docs/python/config/appconfig
4+
# for details.
5+
6+
# TODO: Enter your application id below. If you have signed up
7+
# using cloud.google.com/console use the "project id" for your application
8+
# id.
9+
# [START runtime]
10+
runtime: custom
11+
vm: true
12+
13+
# [END runtime]
14+

managed_vms/django_tutorial/manage.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
# Copyright 2015 Google Inc.
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+
import os
17+
import sys
18+
19+
if __name__ == "__main__":
20+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
21+
22+
from django.core.management import execute_from_command_line
23+
24+
execute_from_command_line(sys.argv)

managed_vms/django_tutorial/mysite/__init__.py

Whitespace-only changes.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Copyright 2015 Google Inc.
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+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16+
import os
17+
18+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
19+
20+
# Quick-start development settings - unsuitable for production
21+
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
22+
23+
# SECURITY WARNING: keep the secret key used in production secret!
24+
SECRET_KEY = 'pf-@jxtojga)z+4s*uwbgjrq$aep62-thd0q7f&o77xtpka!_m'
25+
26+
# SECURITY WARNING: don't run with debug turned on in production!
27+
DEBUG = True
28+
29+
ALLOWED_HOSTS = []
30+
31+
# Application definition
32+
33+
INSTALLED_APPS = (
34+
'django.contrib.admin',
35+
'django.contrib.auth',
36+
'django.contrib.contenttypes',
37+
'django.contrib.sessions',
38+
'django.contrib.messages',
39+
'django.contrib.staticfiles',
40+
'polls'
41+
)
42+
43+
MIDDLEWARE_CLASSES = (
44+
'django.contrib.sessions.middleware.SessionMiddleware',
45+
'django.middleware.common.CommonMiddleware',
46+
'django.middleware.csrf.CsrfViewMiddleware',
47+
'django.contrib.auth.middleware.AuthenticationMiddleware',
48+
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
49+
'django.contrib.messages.middleware.MessageMiddleware',
50+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
51+
'django.middleware.security.SecurityMiddleware',
52+
)
53+
54+
ROOT_URLCONF = 'mysite.urls'
55+
56+
TEMPLATES = [
57+
{
58+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
59+
'DIRS': [],
60+
'APP_DIRS': True,
61+
'OPTIONS': {
62+
'context_processors': [
63+
'django.template.context_processors.debug',
64+
'django.template.context_processors.request',
65+
'django.contrib.auth.context_processors.auth',
66+
'django.contrib.messages.context_processors.messages',
67+
],
68+
},
69+
},
70+
]
71+
72+
WSGI_APPLICATION = 'mysite.wsgi.application'
73+
74+
# Database
75+
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
76+
77+
# [START dbconfig]
78+
DATABASES = {
79+
'default': {
80+
'ENGINE': 'django.db.backends.mysql',
81+
'NAME': '<your-database-name>',
82+
'USER': '<your-database-user>',
83+
'PASSWORD': '<your-database-password>',
84+
'HOST': '<your-database-host>',
85+
'PORT': '3306',
86+
}
87+
}
88+
# [END dbconfig]
89+
90+
# Internationalization
91+
# https://docs.djangoproject.com/en/1.8/topics/i18n/
92+
93+
LANGUAGE_CODE = 'en-us'
94+
95+
TIME_ZONE = 'UTC'
96+
97+
USE_I18N = True
98+
99+
USE_L10N = True
100+
101+
USE_TZ = True
102+
103+
# Static files (CSS, JavaScript, Images)
104+
# https://docs.djangoproject.com/en/1.8/howto/static-files/
105+
106+
# [START staticurl]
107+
# Fill in your cloud bucket and switch which one of the following 2 lines
108+
# is commented to serve static content from GCS
109+
# STATIC_URL = 'https://storage.googleapis.com/<your-cloud-bucket>/static/'
110+
STATIC_URL = '/static/'
111+
# [END staticurl]
112+
113+
STATIC_ROOT = 'static/'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2015 Google Inc.
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+
from django.conf.urls import include, url
16+
from django.contrib import admin
17+
18+
urlpatterns = [url(r'^polls/', include('polls.urls')),
19+
url(r'^admin/', admin.site.urls)]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2015 Google Inc.
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+
16+
import os
17+
18+
from django.core.wsgi import get_wsgi_application
19+
20+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
21+
22+
application = get_wsgi_application()

managed_vms/django_tutorial/polls/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)