production.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import os
  2. import random
  3. import string
  4. import dj_database_url
  5. from .base import * # noqa: F403
  6. DEBUG = False
  7. # DJANGO_SECRET_KEY *should* be specified in the environment. If it's not, generate an ephemeral key.
  8. if "DJANGO_SECRET_KEY" in os.environ:
  9. SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
  10. else:
  11. # Use if/else rather than a default value to avoid calculating this if we don't need it
  12. print( # noqa: T201
  13. "WARNING: DJANGO_SECRET_KEY not found in os.environ. Generating ephemeral SECRET_KEY."
  14. )
  15. SECRET_KEY = "".join(
  16. [random.SystemRandom().choice(string.printable) for i in range(50)]
  17. )
  18. # Make sure Django can detect a secure connection properly on Heroku:
  19. SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
  20. # Redirect all requests to HTTPS
  21. SECURE_SSL_REDIRECT = os.getenv("DJANGO_SECURE_SSL_REDIRECT", "off") == "on"
  22. # Accept all hostnames, since we don't know in advance which hostname will be used for any given Heroku instance.
  23. # IMPORTANT: Set this to a real hostname when using this in production!
  24. # See https://docs.djangoproject.com/en/3.2/ref/settings/#allowed-hosts
  25. ALLOWED_HOSTS = os.getenv("DJANGO_ALLOWED_HOSTS", "*").split(";")
  26. EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
  27. # WAGTAILADMIN_BASE_URL required for notification emails
  28. WAGTAILADMIN_BASE_URL = "http://localhost:8000"
  29. db_from_env = dj_database_url.config(conn_max_age=500)
  30. DATABASES["default"].update(db_from_env)
  31. # AWS creds may be used for S3 and/or Elasticsearch
  32. AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID", "")
  33. AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY", "")
  34. AWS_REGION = os.getenv("AWS_REGION", "")
  35. # Server-side cache settings. Do not confuse with front-end cache.
  36. # https://docs.djangoproject.com/en/stable/topics/cache/
  37. # If the server has a Redis instance exposed via a URL string in the REDIS_URL
  38. # environment variable, prefer that. Otherwise use the database backend. We
  39. # usually use Redis in production and database backend on staging and dev. In
  40. # order to use database cache backend you need to run
  41. # "./manage.py createcachetable" to create a table for the cache.
  42. #
  43. # Do not use the same Redis instance for other things like Celery!
  44. # Prefer the TLS connection URL over non
  45. REDIS_URL = os.environ.get("REDIS_TLS_URL", os.environ.get("REDIS_URL"))
  46. if REDIS_URL:
  47. connection_pool_kwargs = {}
  48. if REDIS_URL.startswith("rediss"):
  49. # Heroku Redis uses self-signed certificates for secure redis connections
  50. # When using TLS, we need to disable certificate validation checks.
  51. connection_pool_kwargs["ssl_cert_reqs"] = None
  52. redis_options = {
  53. "IGNORE_EXCEPTIONS": True,
  54. "SOCKET_CONNECT_TIMEOUT": 2, # seconds
  55. "SOCKET_TIMEOUT": 2, # seconds
  56. "CONNECTION_POOL_KWARGS": connection_pool_kwargs,
  57. }
  58. CACHES = {
  59. "default": {
  60. "BACKEND": "django_redis.cache.RedisCache",
  61. "LOCATION": REDIS_URL + "/0",
  62. "OPTIONS": redis_options,
  63. },
  64. "renditions": {
  65. "BACKEND": "django_redis.cache.RedisCache",
  66. "LOCATION": REDIS_URL + "/1",
  67. "OPTIONS": redis_options,
  68. },
  69. }
  70. DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS = True
  71. else:
  72. CACHES = {
  73. "default": {
  74. "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
  75. "LOCATION": "bakerydemo",
  76. }
  77. }
  78. # Configure Elasticsearch, if present in os.environ
  79. ELASTICSEARCH_ENDPOINT = os.getenv("ELASTICSEARCH_ENDPOINT", "")
  80. if ELASTICSEARCH_ENDPOINT:
  81. from elasticsearch import RequestsHttpConnection
  82. WAGTAILSEARCH_BACKENDS = {
  83. "default": {
  84. "BACKEND": "wagtail.search.backends.elasticsearch5",
  85. "HOSTS": [
  86. {
  87. "host": ELASTICSEARCH_ENDPOINT,
  88. "port": int(os.getenv("ELASTICSEARCH_PORT", "9200")),
  89. "use_ssl": os.getenv("ELASTICSEARCH_USE_SSL", "off") == "on",
  90. "verify_certs": os.getenv("ELASTICSEARCH_VERIFY_CERTS", "off")
  91. == "on",
  92. }
  93. ],
  94. "OPTIONS": {
  95. "connection_class": RequestsHttpConnection,
  96. },
  97. }
  98. }
  99. if AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY:
  100. from aws_requests_auth.aws_auth import AWSRequestsAuth
  101. WAGTAILSEARCH_BACKENDS["default"]["HOSTS"][0]["http_auth"] = AWSRequestsAuth(
  102. aws_access_key=AWS_ACCESS_KEY_ID,
  103. aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
  104. aws_token=os.getenv("AWS_SESSION_TOKEN", ""),
  105. aws_host=ELASTICSEARCH_ENDPOINT,
  106. aws_region=AWS_REGION,
  107. aws_service="es",
  108. )
  109. elif AWS_REGION:
  110. # No API keys in the environ, so attempt to discover them with Boto instead, per:
  111. # https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html#configuring-credentials
  112. # This may be useful if your credentials are obtained via EC2 instance meta data.
  113. from aws_requests_auth.boto_utils import BotoAWSRequestsAuth
  114. WAGTAILSEARCH_BACKENDS["default"]["HOSTS"][0][
  115. "http_auth"
  116. ] = BotoAWSRequestsAuth(
  117. aws_host=ELASTICSEARCH_ENDPOINT,
  118. aws_region=AWS_REGION,
  119. aws_service="es",
  120. )
  121. # Simplified static file serving.
  122. # https://warehouse.python.org/project/whitenoise/
  123. MIDDLEWARE.append("whitenoise.middleware.WhiteNoiseMiddleware")
  124. STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
  125. if "AWS_STORAGE_BUCKET_NAME" in os.environ:
  126. AWS_STORAGE_BUCKET_NAME = os.getenv("AWS_STORAGE_BUCKET_NAME")
  127. AWS_QUERYSTRING_AUTH = False
  128. INSTALLED_APPS.append("storages")
  129. DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
  130. AWS_S3_FILE_OVERWRITE = False
  131. AWS_DEFAULT_ACL = "private"
  132. if "GS_BUCKET_NAME" in os.environ:
  133. GS_BUCKET_NAME = os.getenv("GS_BUCKET_NAME")
  134. GS_PROJECT_ID = os.getenv("GS_PROJECT_ID")
  135. GS_DEFAULT_ACL = "publicRead"
  136. GS_AUTO_CREATE_BUCKET = True
  137. INSTALLED_APPS.append("storages")
  138. DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
  139. LOGGING = {
  140. "version": 1,
  141. "disable_existing_loggers": False,
  142. "handlers": {
  143. "console": {
  144. "class": "logging.StreamHandler",
  145. },
  146. },
  147. "loggers": {
  148. "django": {
  149. "handlers": ["console"],
  150. "level": os.getenv("DJANGO_LOG_LEVEL", "INFO"),
  151. },
  152. },
  153. }
  154. # Front-end cache
  155. # This configuration is used to allow purging pages from cache when they are
  156. # published.
  157. # These settings are usually used only on the production sites.
  158. # This is a configuration of the CDN/front-end cache that is used to cache the
  159. # production websites.
  160. # https://docs.wagtail.org/en/latest/reference/contrib/frontendcache.html
  161. # The backend can be configured to use an account-wide API key, or an API token with
  162. # restricted access.
  163. if (
  164. "FRONTEND_CACHE_CLOUDFLARE_TOKEN" in os.environ
  165. or "FRONTEND_CACHE_CLOUDFLARE_BEARER_TOKEN" in os.environ
  166. ):
  167. INSTALLED_APPS.append("wagtail.contrib.frontend_cache")
  168. WAGTAILFRONTENDCACHE = {
  169. "default": {
  170. "BACKEND": "wagtail.contrib.frontend_cache.backends.CloudflareBackend",
  171. "ZONEID": os.environ["FRONTEND_CACHE_CLOUDFLARE_ZONEID"],
  172. }
  173. }
  174. if "FRONTEND_CACHE_CLOUDFLARE_TOKEN" in os.environ:
  175. # To use an account-wide API key, set the following:
  176. # * $FRONTEND_CACHE_CLOUDFLARE_TOKEN
  177. # * $FRONTEND_CACHE_CLOUDFLARE_EMAIL
  178. # * $FRONTEND_CACHE_CLOUDFLARE_ZONEID
  179. # These can be obtained from a sysadmin.
  180. WAGTAILFRONTENDCACHE["default"].update(
  181. {
  182. "EMAIL": os.environ["FRONTEND_CACHE_CLOUDFLARE_EMAIL"],
  183. "TOKEN": os.environ["FRONTEND_CACHE_CLOUDFLARE_TOKEN"],
  184. }
  185. )
  186. else:
  187. # To use an API token with restricted access, set the following:
  188. # * $FRONTEND_CACHE_CLOUDFLARE_BEARER_TOKEN
  189. # * $FRONTEND_CACHE_CLOUDFLARE_ZONEID
  190. WAGTAILFRONTENDCACHE["default"].update(
  191. {"BEARER_TOKEN": os.environ["FRONTEND_CACHE_CLOUDFLARE_BEARER_TOKEN"]}
  192. )