Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Django + PostgreSQL Google Cloud Flexible App Engine deployment templates [app.yaml, settings.py, Conda environment loader shell scripts]
runtime: python
# the PROJECT-DIRECTORY is the one with settings.py and wsgi.py
entrypoint: gunicorn -b :$PORT PROJECT-DIRECTORY.wsgi # specific to a GUnicorn HTTP server deployment
env: flex # for Google Cloud Flexible App Engine
# any environment variables you want to pass to your application.
# accessible through os.environ['VARIABLE_NAME']
env_variables:
# the secret key used for the Django app (from PROJECT-DIRECTORY/settings.py)
SECRET_KEY: 'DJANGO-SECRET-KEY'
DEBUG: 'False' # always False for deployment
# everything after /cloudsql/ can be found by entering >> gcloud sql instances describe DATABASE-NAME << in your Terminal
# the DATABASE-NAME is the name you gave your project's PostgreSQL database
# the second line from the describe output called connectionName can be copied and pasted after /cloudsql/
DB_HOST: '/cloudsql/PROJECT-ID:COMPUTE-ENGINE-ZONE:DATABASE-NAME'
DB_PORT: '5432' # PostgreSQL port
DB_NAME: 'DATABASE-NAME'
DB_USER: 'DATABASE-USERNAME' # either 'postgres' (default) or one you created on the PostgreSQL instance page
DB_PASSWORD: 'DATABASE-PASSWORD'
STATIC_URL: 'https://storage.googleapis.com/BUCKET-NAME/static/' # this is the url that you sync static files to
handlers:
- url: /static
static_dir: static
beta_settings:
# from command >> gcloud sql instances describe DATABASE-NAME <<
cloud_sql_instances: PROJECT-ID:COMPUTE-ENGINE-ZONE:DATABASE-NAME
runtime_config:
python_version: 3 # enter your Python version BASE ONLY here. Enter 2 for 2.7.9 or 3 for 3.6.4
# when you call conda activate <env name> this script will be executed
# loads the environment variables automatically on environment activation
# this goes in /anaconda3/envs/ENVIRONMENT-NAME/etc/conda/activate.d
# note: you must create etc/conda in the environment directory to place this file
# for windows users: https://conda.io/docs/user-guide/tasks/manage-environments.html#windows
# spread across multiple lines for readability
# you can write them space-separated in one export statement if you want
export DEBUG='True' # "converted" to boolean in settings.py line 5
export SECRET_KEY='' # TODO: enter your Django secret key here
export DB_HOST='127.0.0.1' # home IP for connecting to the sql proxy connection with the Google Cloud database instance
export DB_PORT='3306' # set this port when using the cloud_sql_proxy tool if you want a different port
export DB_NAME='' # TODO: add the database name [instance id] (the database instance name from Google Cloud -> SQL)
export DB_USER='' # TODO: add the instance username (default is postgres if you did not make a custom one)
export DB_PASSWORD='' # TODO: add the instance password (default is the generated password associated with 'postgres')
export STATIC_URL='/static/' # the name of your static (development) directory
# when you call conda deactivate this script will be executed
# removes the environment variables automatically on environment deactivation
# this goes in /anaconda3/envs/ENVIRONMENT-NAME/etc/conda/deactivate.d
# CHANGE FILE NAME TO 'env_vars.sh' (gist requires unique file names...)
unset DEBUG SECRET_KEY DB_HOST DB_PORT DB_NAME DB_USER DB_PASSWORD STATIC_URL
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = os.environ['DEBUG'] == 'True' # environment vars are strings. "convert" to boolean. lol, Python
SECRET_KEY = os.environ['SECRET_KEY']
ALLOWED_HOSTS = [
# TODO: add your Google Cloud Project-ID here
'PROJECT-ID.appspot.com', # must add the app engine (project-id) domain here
'127.0.0.1', # for local testing
]
# TODO: add your project apps here
INSTALLED_APPS = [
'YOUR-PROJECT-APP-NAME', # add your other project apps here
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
# TODO: update root project directory for urlconf and wsgi app
ROOT_URLCONF = 'ROOT-PROJECT-DIRECTORY.urls' # ROOT-PROJECT-DIRECTORY is the directory where this settings.py file is
WSGI_APPLICATION = 'ROOT-PROJECT-DIRECTORY.wsgi.application'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# TODO: configure to point at your templates. this setup is for a global templates directory
'DIRS': [os.path.join(BASE_DIR, 'templates')], # global templates directory (in root directory where manage.py is)
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'HOST': os.environ['DB_HOST'],
'PORT': os.environ['DB_PORT'],
'NAME': os.environ['DB_NAME'],
'USER': os.environ['DB_USER'],
'PASSWORD': os.environ['DB_PASSWORD']
}
}
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
STATIC_URL = os.environ['STATIC_URL'] # /static/ if DEBUG else Google Cloud bucket url
# collectstatic directory (located OUTSIDE the base directory)
# TODO: configure the name and path to your static bucket directory (where collectstatic will copy to)
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'STATIC-BUCKET-NAME')
STATICFILES_DIRS = [
# TODO: configure the name and path to your development static directory
os.path.join(BASE_DIR, 'static'), # static directory (in the top level directory) for local testing
]
@the-vampiire

This comment has been minimized.

Copy link
Owner Author

@the-vampiire the-vampiire commented Mar 25, 2018

This is a bare-bones configuration for a PostgreSQL Django app on Google Cloud Flexible App Engine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment