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

Add new django tutorial #4869

Merged
merged 119 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
119 commits
Select commit Hold shift + click to select a range
ff73fca
Cloud Run with Django tutorial
glasnt May 4, 2020
b5ccc3c
Cloud Run with Django tutorial
glasnt May 4, 2020
6ea8b1b
Merge branch 'tutorial/django-run' of github.com:glasnt/python-docs-s…
glasnt May 4, 2020
1ba9285
black, .env file update
glasnt May 11, 2020
e6c9362
Document Dockerfile, add PYTHONUNBUFFERED
glasnt Jun 15, 2020
7cd94bb
Document cloudmigrate.yaml
glasnt Jun 15, 2020
01fbedf
Formatiting
glasnt Jun 15, 2020
711a50f
Bump django, remove mysql
glasnt Jun 15, 2020
b01118d
Merge branch 'master' into tutorial/django-run
glasnt Sep 28, 2020
97088bd
Update secretmanager
glasnt Oct 2, 2020
a3960e8
Update pinned dependencies
glasnt Oct 2, 2020
c0a4753
ensure path is set correctly with api update
glasnt Oct 2, 2020
e75ee59
format
glasnt Oct 2, 2020
55db6c4
Generalise cloudmigrate file
glasnt Oct 12, 2020
b0d3e61
update service default
glasnt Oct 12, 2020
b39521b
add tests
glasnt Oct 19, 2020
a0c90a4
Update CODEOWNERS
glasnt Oct 19, 2020
79a0c11
Merge branch 'master' into django-run
glasnt Oct 19, 2020
60f54fd
Merge branch 'master' into django-run
glasnt Oct 19, 2020
083145a
Licence headers
glasnt Oct 19, 2020
0508de3
Remove envvar declaration
glasnt Oct 19, 2020
4ae043e
cleanup readme
glasnt Oct 19, 2020
0fea1a4
Fix import names, region tags
glasnt Oct 19, 2020
59a690a
black
glasnt Oct 19, 2020
3297a13
Merge branch 'django-run' of github.com:glasnt/python-docs-samples in…
glasnt Oct 19, 2020
43311df
Cloud Run with Django tutorial
glasnt May 4, 2020
192547a
black, .env file update
glasnt May 11, 2020
4d8ec6f
Document Dockerfile, add PYTHONUNBUFFERED
glasnt Jun 15, 2020
9fb3094
Document cloudmigrate.yaml
glasnt Jun 15, 2020
885d44f
Formatiting
glasnt Jun 15, 2020
b56ecdf
Bump django, remove mysql
glasnt Jun 15, 2020
3897bad
Update secretmanager
glasnt Oct 2, 2020
a453f02
Update pinned dependencies
glasnt Oct 2, 2020
7981375
ensure path is set correctly with api update
glasnt Oct 2, 2020
756daf9
format
glasnt Oct 2, 2020
86573fe
Generalise cloudmigrate file
glasnt Oct 12, 2020
9a94cb2
update service default
glasnt Oct 12, 2020
6426d01
add tests
glasnt Oct 19, 2020
8fa8c19
Update CODEOWNERS
glasnt Oct 19, 2020
be5911a
Licence headers
glasnt Oct 19, 2020
cb5992e
Remove envvar declaration
glasnt Oct 19, 2020
8c643d4
cleanup readme
glasnt Oct 19, 2020
98837e1
Fix import names, region tags
glasnt Oct 19, 2020
5e1cb0a
black
glasnt Oct 19, 2020
0308d64
Merge branch 'django-run' of github.com:glasnt/python-docs-samples in…
glasnt Oct 19, 2020
911ca50
black
glasnt Oct 19, 2020
877c658
Format YAML
glasnt Oct 20, 2020
700a2cd
Merge branch 'master' into django-run
glasnt Nov 5, 2020
466a801
bump dependencies
glasnt Nov 5, 2020
c188733
Update var names, remove sql instance creation
glasnt Nov 5, 2020
4d33c68
PR Feedback
glasnt Nov 8, 2020
344f35e
Merge branch 'master' into django-run
glasnt Nov 8, 2020
3ed2a6e
Debug failing command
glasnt Nov 8, 2020
8fa7cc3
Merge branch 'django-run' of github.com:glasnt/python-docs-samples in…
glasnt Nov 8, 2020
8f854e9
Remove IAM calls - possible permissions issues
glasnt Nov 8, 2020
1809ab5
debug
glasnt Nov 8, 2020
cefb39a
force local settings if in nox/test
glasnt Nov 9, 2020
2b61310
add IDs to cloud build
glasnt Nov 11, 2020
cd0b8d1
Update region tags
glasnt Nov 11, 2020
9492d67
Get instance name from envvar
glasnt Nov 11, 2020
2601b22
Make secrets dynamic, optionally overload names by envvar
glasnt Nov 11, 2020
2ca3427
Remove create/destroy of static setting
glasnt Nov 11, 2020
f1b79c2
Consolidate envvars
glasnt Nov 11, 2020
e9cbe76
Merge branch 'master' into django-run
glasnt Nov 11, 2020
e8556fc
Merge branch 'django-run' of github.com:glasnt/python-docs-samples in…
glasnt Nov 11, 2020
738a9b5
fix region tags
glasnt Nov 11, 2020
e62e4d3
lint, import error
glasnt Nov 11, 2020
e51cd2b
Testing instance name is fqdn, need just name for gcloud sql calls
glasnt Nov 11, 2020
7837611
Ensure project specified on gcloud calls
glasnt Nov 11, 2020
460b5ff
set project when using gsutil
glasnt Nov 11, 2020
6fce68c
order of flags in gsutil matters
glasnt Nov 11, 2020
2b09698
ensure all subprocesses include project
glasnt Nov 11, 2020
cb5ea1e
Add and remove more project tags
glasnt Nov 11, 2020
9877d70
Update comments
glasnt Nov 11, 2020
04c0900
match on exact project name, not substring
glasnt Nov 11, 2020
895f8fb
lint
glasnt Nov 11, 2020
05aa42c
Can't hide the settings name in the setting itself 🧐
glasnt Nov 11, 2020
1db86b5
big oof
glasnt Nov 11, 2020
d8a557f
migrations require __init__.py to be detected
glasnt Nov 11, 2020
87ebb84
Bump overnight patch
glasnt Nov 11, 2020
adc6fda
Debug 503
glasnt Nov 11, 2020
4a956fb
Service requires custom settings envvar
glasnt Nov 12, 2020
9a48ab4
attempt fixing local tests
glasnt Nov 12, 2020
6d544f2
lint
glasnt Nov 12, 2020
7986d66
Merge branch 'master' into django-run
glasnt Nov 12, 2020
7ffff2d
revert accidental appengine change
glasnt Nov 12, 2020
d529d19
envvars
glasnt Nov 12, 2020
e6caaf7
Merge branch 'django-run' of github.com:glasnt/python-docs-samples in…
glasnt Nov 12, 2020
1edf555
ohno
glasnt Nov 12, 2020
d95a698
wrong varname
glasnt Nov 12, 2020
9bf42d4
Merge branch 'master' into django-run
glasnt Nov 12, 2020
28b12a0
Merge branch 'master' into django-run
glasnt Nov 16, 2020
c55cdea
Merge branch 'master' into django-run
glasnt Nov 17, 2020
c7034b1
Update run/django/noxfile_config.py
glasnt Nov 17, 2020
14371a4
Add licence header
glasnt Nov 17, 2020
e31e692
Formatting
glasnt Nov 17, 2020
e4dd31f
black
glasnt Nov 17, 2020
8712b3f
Add typehinting, remove debugging
glasnt Nov 18, 2020
eeca3c2
Merge branch 'master' into django-run
glasnt Nov 23, 2020
652d5ee
⚠️ noxfile change - add typehints
glasnt Nov 23, 2020
08d2961
⚠️ noxfile change - fix method signature
glasnt Nov 23, 2020
e441b59
⚠️ noxfile change - fix nox session type
glasnt Nov 23, 2020
763ad5b
⚠️ noxfile change - import order
glasnt Nov 24, 2020
82df36f
hadolint Dockerfile
glasnt Nov 24, 2020
5395697
revert hadolint
glasnt Nov 24, 2020
620b626
Merge branch 'master' into django-run
glasnt Nov 24, 2020
e1d465c
Merge branch 'master' into django-run
glasnt Nov 24, 2020
4f1063a
Merge branch 'master' into django-run
glasnt Nov 25, 2020
0d78748
Merge branch 'master' into django-run
glasnt Nov 25, 2020
80c66ab
Merge branch 'master' into django-run
glasnt Nov 30, 2020
2fd21a4
Merge branch 'master' into django-run
glasnt Dec 2, 2020
a18d3fe
Add linking
glasnt Dec 2, 2020
5e78fdb
Remove gcloudignore
glasnt Dec 2, 2020
2109542
Merge branch 'django-run' of github.com:glasnt/python-docs-samples in…
glasnt Dec 2, 2020
6f9c0ff
Merge branch 'master' into django-run
glasnt Dec 2, 2020
7dd2052
Merge branch 'master' into django-run
glasnt Dec 2, 2020
60699a9
Testing: i have a theory this is why tests started failing
glasnt Dec 2, 2020
2574096
Merge branch 'django-run' of github.com:glasnt/python-docs-samples in…
glasnt Dec 2, 2020
83fc6b2
Merge branch 'master' into django-run
glasnt Dec 2, 2020
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
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
/profiler/**/*.py @kalyanac @GoogleCloudPlatform/python-samples-owners
/pubsub/**/*.py @anguillanneuf @hongalex @GoogleCloudPlatform/python-samples-owners
/run/**/*.py @averikitsch @grant @GoogleCloudPlatform/python-samples-owners
/run/django/**/*.py @glasnt @GoogleCloudPlatform/python-samples-owners
/scheduler/**/*.py @averikitsch @GoogleCloudPlatform/python-samples-owners
/spanner/**/*.py @larkee @GoogleCloudPlatform/python-samples-owners
/speech/**/*.py @telpirion @sirtorry @GoogleCloudPlatform/python-samples-owners
Expand Down
20 changes: 11 additions & 9 deletions noxfile-template.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
from pathlib import Path
import sys
from typing import Callable, Dict, List, Optional
leahecole marked this conversation as resolved.
Show resolved Hide resolved

import nox

Expand Down Expand Up @@ -68,7 +69,7 @@
TEST_CONFIG.update(TEST_CONFIG_OVERRIDE)


def get_pytest_env_vars():
def get_pytest_env_vars() -> Dict[str, str]:
"""Returns a dict for pytest invocation."""
ret = {}

Expand Down Expand Up @@ -98,7 +99,7 @@ def get_pytest_env_vars():
#


def _determine_local_import_names(start_dir):
def _determine_local_import_names(start_dir: str) -> List[str]:
"""Determines all import names that should be considered "local".

This is used when running the linter to insure that import order is
Expand Down Expand Up @@ -136,7 +137,7 @@ def _determine_local_import_names(start_dir):


@nox.session
def lint(session):
def lint(session: nox.sessions.Session) -> None:
if not TEST_CONFIG['enforce_type_hints']:
session.install("flake8", "flake8-import-order")
else:
Expand All @@ -156,7 +157,7 @@ def lint(session):
#

@nox.session
def blacken(session):
def blacken(session: nox.sessions.Session) -> None:
session.install("black")
python_files = [path for path in os.listdir(".") if path.endswith(".py")]

Expand All @@ -171,7 +172,7 @@ def blacken(session):
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"]


def _session_tests(session, post_install=None):
def _session_tests(session: nox.sessions.Session, post_install: Callable = None) -> None:
"""Runs py.test for a particular project."""
if os.path.exists("requirements.txt"):
session.install("-r", "requirements.txt")
Expand All @@ -197,7 +198,7 @@ def _session_tests(session, post_install=None):


@nox.session(python=ALL_VERSIONS)
def py(session):
def py(session: nox.sessions.Session) -> None:
"""Runs py.test for a sample using the specified version of Python."""
if session.python in TESTED_VERSIONS:
_session_tests(session)
Expand All @@ -212,9 +213,10 @@ def py(session):
#


def _get_repo_root():
def _get_repo_root() -> Optional[str]:
""" Returns the root folder of the project. """
# Get root of this repository. Assume we don't have directories nested deeper than 10 items.
# Get root of this repository.
# Assume we don't have directories nested deeper than 10 items.
p = Path(os.getcwd())
for i in range(10):
if p is None:
Expand All @@ -230,7 +232,7 @@ def _get_repo_root():

@nox.session
@nox.parametrize("path", GENERATED_READMES)
def readmegen(session, path):
def readmegen(session: nox.sessions.Session, path: str) -> None:
"""(Re-)generates the readme for a sample."""
session.install("jinja2", "pyyaml")
dir_ = os.path.dirname(path)
Expand Down
2 changes: 2 additions & 0 deletions run/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This directory contains samples for [Google Cloud Run](https://cloud.run). [Clou
|[Cloud Pub/Sub][pubsub] | Handling Pub/Sub push messages | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_pubsub] |
|[Cloud SQL (MySQL)][mysql] | Use MySQL with Cloud Run | - |
|[Cloud SQL (Postgres)][postgres] | Use Postgres with Cloud Run | - |
|[Django][django] | Deploy Django on Cloud Run | - |
glasnt marked this conversation as resolved.
Show resolved Hide resolved

For more Cloud Run samples beyond Python, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples).

Expand Down Expand Up @@ -109,6 +110,7 @@ for more information.
[pubsub]: pubsub/
[mysql]: ../cloud-sql/mysql/sqlalchemy
[postgres]: ../cloud-sql/postgres/sqlalchemy
[django]: django/
[run_button_helloworld]: https://deploy.cloud.run/?git_repo=https://github.com/knative/docs&dir=docs/serving/samples/hello-world/helloworld-python
[run_button_pubsub]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&dir=run/pubsub
[testing]: https://cloud.google.com/run/docs/testing/local#running_locally_using_docker_with_access_to_services
1 change: 1 addition & 0 deletions run/django/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
36 changes: 36 additions & 0 deletions run/django/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2020 Google LLC
#
# 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.

# Use an official lightweight Python image.
glasnt marked this conversation as resolved.
Show resolved Hide resolved
# https://hub.docker.com/_/python
FROM python:3.8-slim

ENV APP_HOME /app
WORKDIR $APP_HOME

# Removes output stream buffering, allowing for more efficient logging
ENV PYTHONUNBUFFERED 1

# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy local code to the container image.
COPY . .

# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn --bind 0.0.0.0:$PORT --workers 1 --threads 8 --timeout 0 mysite.wsgi:application
15 changes: 15 additions & 0 deletions run/django/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Getting started with Django on Cloud Run

[![Open in Cloud Shell][shell_img]][shell_link]
averikitsch marked this conversation as resolved.
Show resolved Hide resolved

[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=run/django/README.md

This repository is an example of how to run a [Django](https://www.djangoproject.com/)
app on Google Cloud Run (fully managed).

The Django application used in this tutorial is the [Writing your first Django app](https://docs.djangoproject.com/en/3.0/#first-steps), after completing [Part 1](https://docs.djangoproject.com/en/3.0/intro/tutorial01/) and [Part 2](https://docs.djangoproject.com/en/3.0/intro/tutorial02/).


# Tutorial
See our [Running Django on Cloud Run (fully managed)](https://cloud.google.com/python/django/run) tutorial for instructions for setting up and deploying this sample application.
64 changes: 64 additions & 0 deletions run/django/cloudmigrate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright 2020 Google LLC
#
# 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.

steps:
- id: "build image"
name: "gcr.io/cloud-builders/docker"
args: ["build", "-t", "gcr.io/${PROJECT_ID}/${_SERVICE_NAME}", "."]

- id: "push image"
name: "gcr.io/cloud-builders/docker"
args: ["push", "gcr.io/${PROJECT_ID}/${_SERVICE_NAME}"]

- id: "apply migrations"
name: "gcr.io/google-appengine/exec-wrapper"
args:
[
"-i",
"gcr.io/$PROJECT_ID/${_SERVICE_NAME}",
"-s",
"${PROJECT_ID}:${_REGION}:${_INSTANCE_NAME}",
"-e",
"SETTINGS_NAME=${_SECRET_SETTINGS_NAME}",
"--",
"python",
"manage.py",
"migrate",
]

- id: "collect static"
name: "gcr.io/google-appengine/exec-wrapper"
args:
[
"-i",
"gcr.io/$PROJECT_ID/${_SERVICE_NAME}",
"-s",
"${PROJECT_ID}:${_REGION}:${_INSTANCE_NAME}",
"-e",
"SETTINGS_NAME=${_SECRET_SETTINGS_NAME}",
"--",
"python",
"manage.py",
"collectstatic",
"--no-input",
]

substitutions:
_INSTANCE_NAME: django-instance
_REGION: us-central1
_SERVICE_NAME: polls-service
_SECRET_SETTINGS_NAME: django-settings

images:
- "gcr.io/${PROJECT_ID}/${_SERVICE_NAME}"
Loading