Skip to content

Commit

Permalink
Update use of environ for when there is no .env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchrisadams committed May 15, 2024
1 parent f437109 commit 40647df
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 31 deletions.
66 changes: 39 additions & 27 deletions greenweb/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
https://docs.djangoproject.com/en/dev/ref/settings/
"""

import os
import environ
import pathlib
from dramatiq import middleware as dramatiq_middleware
Expand All @@ -19,27 +18,42 @@
ROOT = environ.Path(__file__) - 3
env = environ.Env(
DEBUG=(bool, False),
SECRET_KEY=(str, os.getenv("SECRET_KEY")),
DATABASE_URL=(str, os.getenv("DATABASE_URL")),
DATABASE_URL_READ_ONLY=(str, os.getenv("DATABASE_URL_READ_ONLY")),
DOMAIN_SNAPSHOT_BUCKET=(str, os.getenv("DOMAIN_SNAPSHOT_BUCKET")),
# add for object storage
OBJECT_STORAGE_ENDPOINT=(str, os.getenv("OBJECT_STORAGE_ENDPOINT")),
OBJECT_STORAGE_REGION=(str, os.getenv("OBJECT_STORAGE_REGION")),
OBJECT_STORAGE_ACCESS_KEY_ID=(str, os.getenv("OBJECT_STORAGE_ACCESS_KEY_ID")),
SECRET_KEY=(str, "some-key"),
DJANGO_LOG_LEVEL=(str, "INFO"),
# databases
DATABASE_URL=(str, "sqlite:///db.sqlite3"),
DATABASE_URL_READ_ONLY=(str, "sqlite:///db.sqlite3"),
EXPLORER_TOKEN=(str, "some-token"),
# object storage
OBJECT_STORAGE_ENDPOINT=(str, "https://s3.nl-ams.scw.cloud"),
OBJECT_STORAGE_REGION=(str, "nl-ams"),
OBJECT_STORAGE_ACCESS_KEY_ID=(str, "xxxxxxxxxxxxxxxxxxxx"),
OBJECT_STORAGE_SECRET_ACCESS_KEY=(
str,
os.getenv("OBJECT_STORAGE_SECRET_ACCESS_KEY"),
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
),
# add for basicauth
DOMAIN_SNAPSHOT_BUCKET=(str, "tgwf-green-domains-dev"),
# basicauth for staging environments
BASICAUTH_DISABLE=(bool, True),
BASICAUTH_USER=(str, "staging_user"),
BASICAUTH_PASSWORD=(str, "strong_password"),
API_URL=(str, os.getenv("API_URL")),
# Swagger API docs url
API_URL=(str, "https://greenweb.localhost"),
TRELLO_REGISTRATION_EMAIL_TO_BOARD_ADDRESS=(
str,
os.getenv("TRELLO_REGISTRATION_EMAIL_TO_BOARD_ADDRESS"),
"mail-to-board@localhost",
),
RABBITMQ_URL=(str, "amqp://USERNAME:PASSWORD@localhost:5672/"),
# cloud providers updated on cronjobs
GOOGLE_PROVIDER_ID=(int, 0),
GOOGLE_DATASET_ENDPOINT=(str, "https://www.gstatic.com/ipranges/cloud.json"),
MICROSOFT_PROVIDER_ID=(int, 0),
EQUINIX_PROVIDER_ID=(int, 0),
EQUINIX_REMOTE_API_ENDPOINT=(str, "https://domain/link/to/file.txt"),
AMAZON_PROVIDER_ID=(int, 0),
AMAZON_REMOTE_API_ENDPOINT=(str, "https://domain/link/to/file.json"),
MAXMIND_USER_ID=(str, "123456"),
MAXMIND_LICENCE_KEY=(str, "xxxxxxxxxxxxxxxx"),
)

# in some cases we don't have a .env file to work from - the environment
Expand Down Expand Up @@ -237,7 +251,7 @@
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
# only support API access with the sql explorer if we
# explicitly set the token
EXPLORER_TOKEN = os.getenv("EXPLORER_TOKEN")
EXPLORER_TOKEN = env("EXPLORER_TOKEN")
if EXPLORER_TOKEN:
EXPLORER_TOKEN_AUTH_ENABLED = True

Expand All @@ -246,8 +260,8 @@
GEOIP_PROVIDER_DOWNLOAD_URL = (
"https://download.maxmind.com/geoip/databases/GeoLite2-City/download?suffix=tar.gz"
)
GEOIP_USER = env("MAXMIND_USER_ID", default=None)
GEOIP_PASSWORD = env("MAXMIND_LICENCE_KEY", default=None)
GEOIP_USER = env("MAXMIND_USER_ID")
GEOIP_PASSWORD = env("MAXMIND_LICENCE_KEY")

# Allow requests from any origin, but only make the API urls available
# CORS_URLS_REGEX = r"^/api/.*$"
Expand Down Expand Up @@ -319,24 +333,22 @@

# Importer variables
# Microsoft
MICROSOFT_PROVIDER_ID = env("MICROSOFT_PROVIDER_ID", default=None)
MICROSOFT_PROVIDER_ID = env("MICROSOFT_PROVIDER_ID")

# Equinix
EQUINIX_PROVIDER_ID = env("EQUINIX_PROVIDER_ID", default=None)
EQUINIX_REMOTE_API_ENDPOINT = env("EQUINIX_REMOTE_API_ENDPOINT", default=None)
EQUINIX_PROVIDER_ID = env("EQUINIX_PROVIDER_ID")
EQUINIX_REMOTE_API_ENDPOINT = env("EQUINIX_REMOTE_API_ENDPOINT")

# Amazon
AMAZON_PROVIDER_ID = env("AMAZON_PROVIDER_ID", default=None)
AMAZON_REMOTE_API_ENDPOINT = env("AMAZON_REMOTE_API_ENDPOINT", default=None)
AMAZON_PROVIDER_ID = env("AMAZON_PROVIDER_ID")
AMAZON_REMOTE_API_ENDPOINT = env("AMAZON_REMOTE_API_ENDPOINT")

# Google
GOOGLE_PROVIDER_ID = env("GOOGLE_PROVIDER_ID", default=None)
GOOGLE_DATASET_ENDPOINT = env(
"GOOGLE_DATASET_ENDPOINT", default="https://www.gstatic.com/ipranges/cloud.json"
)
GOOGLE_PROVIDER_ID = env("GOOGLE_PROVIDER_ID")
GOOGLE_DATASET_ENDPOINT = env("GOOGLE_DATASET_ENDPOINT")


RABBITMQ_URL = env("RABBITMQ_URL", default=None)
RABBITMQ_URL = env("RABBITMQ_URL")


REST_FRAMEWORK = {
Expand Down Expand Up @@ -380,7 +392,7 @@
"root": {"handlers": ["console"], "level": "INFO"},
"handlers": {
"console": {
"level": os.getenv("DJANGO_LOG_LEVEL", "INFO"),
"level": env("DJANGO_LOG_LEVEL"),
"class": "logging.StreamHandler",
"formatter": "simple",
},
Expand Down
17 changes: 13 additions & 4 deletions greenweb/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration

import environ


prod_env = environ.Env(
SENTRY_DSN=(str, ""),
SENTRY_ENVIRONMENT=(str, "production"),
SENTRY_RELEASE=(str, "[email protected]"),
SENTRY_SAMPLE_RATE=(str, 0),
)

ANYMAIL = {
"MAILGUN_API_KEY": env("MAILGUN_API_KEY"), # noqa
Expand Down Expand Up @@ -42,9 +51,9 @@
AWS_S3_FILE_OVERWRITE = False

# report when things asplode
SENTRY_DSN = os.environ.get("SENTRY_DSN", False) # noqa
SENTRY_ENVIRONMENT = os.environ.get("SENTRY_ENVIRONMENT", "production") # noqa
SENTRY_RELEASE = os.environ.get("SENTRY_RELEASE", "[email protected]") # noqa
SENTRY_DSN = prod_env("SENTRY_DSN")
SENTRY_ENVIRONMENT = prod_env("SENTRY_ENVIRONMENT")
SENTRY_RELEASE = prod_env("SENTRY_RELEASE")

# Set to a value between 0 for 0% of request and
# 1.0 to capture 100% of requests and annotate
Expand All @@ -56,7 +65,7 @@
# For more:
# https://docs.sentry.io/platforms/python/guides/django/
# https://docs.sentry.io/platforms/python/guides/django/performance/
sentry_sample_rate = os.environ.get("SENTRY_SAMPLE_RATE", 0) # noqa
sentry_sample_rate = prod_env("SENTRY_SAMPLE_RATE")

if SENTRY_DSN:
sentry_sdk.init(
Expand Down

0 comments on commit 40647df

Please sign in to comment.