forked from GithubBackups/healthchecks
Load settings from environment variables. Fixes #187
This commit is contained in:
parent
cbb5e8cae5
commit
c4543bce58
@ -1,6 +1,12 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## Unreleased
|
||||||
|
|
||||||
|
### Improvements
|
||||||
|
- Load settings from environment variables
|
||||||
|
|
||||||
|
|
||||||
## 1.2.0 - 2018-10-20
|
## 1.2.0 - 2018-10-20
|
||||||
|
|
||||||
### Improvements
|
### Improvements
|
||||||
|
109
README.md
109
README.md
@ -76,10 +76,54 @@ visit `http://localhost:8080/admin`
|
|||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
Site configuration is kept in `hc/settings.py`. Additional configuration
|
Site configuration is loaded from environment variables. This is
|
||||||
is loaded from `hc/local_settings.py` file, if it exists. You
|
done in `hc/settings.py`. Additional configuration is loaded
|
||||||
can create this file (should be right next to `settings.py` in the filesystem)
|
from `hc/local_settings.py` file, if it exists. You can create this file
|
||||||
and override settings as needed.
|
(should be right next to `settings.py` in the filesystem) and override
|
||||||
|
settings, or add extra settings as needed.
|
||||||
|
|
||||||
|
Configurations settings loaded from environment variables:
|
||||||
|
|
||||||
|
| Environment variable | Default value | Notes
|
||||||
|
| -------------------- | ------------- | ----- |
|
||||||
|
| [SECRET_KEY](https://docs.djangoproject.com/en/2.1/ref/settings/#secret-key) | `"---"`
|
||||||
|
| [DEBUG](https://docs.djangoproject.com/en/2.1/ref/settings/#debug) | `True` | Set to `False` for production
|
||||||
|
| [ALLOWED_HOSTS](https://docs.djangoproject.com/en/2.1/ref/settings/#allowed-hosts) | `*` | Separate multiple hosts with commas
|
||||||
|
| [DEFAULT_FROM_EMAIL](https://docs.djangoproject.com/en/2.1/ref/settings/#default-from-email) | `"healthchecks@example.org"`
|
||||||
|
| USE_PAYMENTS | `False`
|
||||||
|
| REGISTRATION_OPEN | `True`
|
||||||
|
| DB | `"sqlite"` | Set to `"postgres"` or `"mysql"`
|
||||||
|
| [DB_HOST](https://docs.djangoproject.com/en/2.1/ref/settings/#host) | `""` *(empty string)*
|
||||||
|
| [DB_PORT](https://docs.djangoproject.com/en/2.1/ref/settings/#port) | `""` *(empty string)*
|
||||||
|
| [DB_NAME](https://docs.djangoproject.com/en/2.1/ref/settings/#name) | `"hc"`
|
||||||
|
| [DB_USER](https://docs.djangoproject.com/en/2.1/ref/settings/#user) | `"postgres"` or `"root"`
|
||||||
|
| [DB_PASSWORD](https://docs.djangoproject.com/en/2.1/ref/settings/#password) | `""` *(empty string)*
|
||||||
|
| [DB_CONN_MAX_AGE](https://docs.djangoproject.com/en/2.1/ref/settings/#conn-max-age) | `0`
|
||||||
|
| DB_SSLMODE | `"prefer"` | PostgreSQL-specific, [details](https://blog.github.com/2018-10-21-october21-incident-report/)
|
||||||
|
| DB_TARGET_SESSION_ATTRS | `"read-write"` | PostgreSQL-specific, [details](https://www.postgresql.org/docs/10/static/libpq-connect.html#LIBPQ-CONNECT-TARGET-SESSION-ATTRS)
|
||||||
|
| SITE_ROOT | `"http://localhost:8000"`
|
||||||
|
| SITE_NAME | `"Mychecks"`
|
||||||
|
| MASTER_BADGE_LABEL | `"Mychecks"`
|
||||||
|
| PING_ENDPOINT | `"http://localhost:8000/ping/"`
|
||||||
|
| PING_EMAIL_DOMAIN | `"localhost"`
|
||||||
|
| DISCORD_CLIENT_ID | `None`
|
||||||
|
| DISCORD_CLIENT_SECRET | `None`
|
||||||
|
| SLACK_CLIENT_ID | `None`
|
||||||
|
| SLACK_CLIENT_SECRET | `None`
|
||||||
|
| PUSHOVER_API_TOKEN | `None`
|
||||||
|
| PUSHOVER_SUBSCRIPTION_URL | `None`
|
||||||
|
| PUSHOVER_EMERGENCY_RETRY_DELAY | `300`
|
||||||
|
| PUSHOVER_EMERGENCY_EXPIRATION | `86400`
|
||||||
|
| PUSHBULLET_CLIENT_ID | `None`
|
||||||
|
| PUSHBULLET_CLIENT_SECRET | `None`
|
||||||
|
| TELEGRAM_BOT_NAME | `"ExampleBot"`
|
||||||
|
| TELEGRAM_TOKEN | `None`
|
||||||
|
| TWILIO_ACCOUNT | `None`
|
||||||
|
| TWILIO_AUTH | `None`
|
||||||
|
| TWILIO_FROM | `None`
|
||||||
|
| PD_VENDOR_KEY | `None`
|
||||||
|
| TRELLO_APP_KEY | `None`
|
||||||
|
|
||||||
|
|
||||||
Some useful settings keys to override are:
|
Some useful settings keys to override are:
|
||||||
|
|
||||||
@ -109,10 +153,9 @@ to your team account.
|
|||||||
|
|
||||||
## Database Configuration
|
## Database Configuration
|
||||||
|
|
||||||
Database configuration is stored in `hc/settings.py` and can be overriden
|
Database configuration is loaded from environment variables. If you
|
||||||
in `hc/local_settings.py`. The default database engine is SQLite. To use
|
need to use a non-standard configuration, you can override the
|
||||||
PostgreSQL, create `hc/local_settings.py` if it does not exist, and put the
|
database configuration in `hc/local_settings.py` like so:
|
||||||
following in it, changing it as neccessary:
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
@ -121,39 +164,11 @@ DATABASES = {
|
|||||||
'NAME': 'your-database-name-here',
|
'NAME': 'your-database-name-here',
|
||||||
'USER': 'your-database-user-here',
|
'USER': 'your-database-user-here',
|
||||||
'PASSWORD': 'your-database-password-here',
|
'PASSWORD': 'your-database-password-here',
|
||||||
'TEST': {'CHARSET': 'UTF8'}
|
'TEST': {'CHARSET': 'UTF8'},
|
||||||
|
'OPTIONS': {
|
||||||
|
... your custom options here ...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
|
||||||
|
|
||||||
For MySQL:
|
|
||||||
|
|
||||||
```python
|
|
||||||
DATABASES = {
|
|
||||||
'default': {
|
|
||||||
'ENGINE': 'django.db.backends.mysql',
|
|
||||||
'NAME': 'your-database-name-here',
|
|
||||||
'USER': 'your-database-user-here',
|
|
||||||
'PASSWORD': 'your-database-password-here',
|
|
||||||
'TEST': {'CHARSET': 'UTF8'}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
You can also use `hc/local_settings.py` to read database
|
|
||||||
configuration from environment variables like so:
|
|
||||||
|
|
||||||
```python
|
|
||||||
import os
|
|
||||||
|
|
||||||
DATABASES = {
|
|
||||||
'default': {
|
|
||||||
'ENGINE': os.environ['DB_ENGINE'],
|
|
||||||
'NAME': os.environ['DB_NAME'],
|
|
||||||
'USER': os.environ['DB_USER'],
|
|
||||||
'PASSWORD': os.environ['DB_PASSWORD'],
|
|
||||||
'TEST': {'CHARSET': 'UTF8'}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -262,9 +277,9 @@ To enable Discord integration, you will need to:
|
|||||||
`SITE_ROOT/integrations/add_discord/`. For example, if you are running a
|
`SITE_ROOT/integrations/add_discord/`. For example, if you are running a
|
||||||
development server on `localhost:8000` then the redirect URI would be
|
development server on `localhost:8000` then the redirect URI would be
|
||||||
`http://localhost:8000/integrations/add_discord/`
|
`http://localhost:8000/integrations/add_discord/`
|
||||||
* Look up your Discord app's Client ID and Client Secret. Add them
|
* Look up your Discord app's Client ID and Client Secret. Put them
|
||||||
to your `hc/local_settings.py` file as `DISCORD_CLIENT_ID` and
|
in `DISCORD_CLIENT_ID` and `DISCORD_CLIENT_SECRET` environment
|
||||||
`DISCORD_CLIENT_SECRET` fields.
|
variables.
|
||||||
|
|
||||||
|
|
||||||
### Pushover
|
### Pushover
|
||||||
@ -274,17 +289,17 @@ To enable Pushover integration, you will need to:
|
|||||||
* register a new application on https://pushover.net/apps/build
|
* register a new application on https://pushover.net/apps/build
|
||||||
* enable subscriptions in your application and make sure to enable the URL
|
* enable subscriptions in your application and make sure to enable the URL
|
||||||
subscription type
|
subscription type
|
||||||
* add the application token and subscription URL to `hc/local_settings.py`, as
|
* put the application token and the subscription URL in
|
||||||
`PUSHOVER_API_TOKEN` and `PUSHOVER_SUBSCRIPTION_URL`
|
`PUSHOVER_API_TOKEN` and `PUSHOVER_SUBSCRIPTION_URL` environment
|
||||||
|
variables
|
||||||
|
|
||||||
### Telegram
|
### Telegram
|
||||||
|
|
||||||
* Create a Telegram bot by talking to the
|
* Create a Telegram bot by talking to the
|
||||||
[BotFather](https://core.telegram.org/bots#6-botfather). Set the bot's name,
|
[BotFather](https://core.telegram.org/bots#6-botfather). Set the bot's name,
|
||||||
description, user picture, and add a "/start" command.
|
description, user picture, and add a "/start" command.
|
||||||
* After creating the bot you will have the bot's name and token. Add them
|
* After creating the bot you will have the bot's name and token. Put them
|
||||||
to your `hc/local_settings.py` file as `TELEGRAM_BOT_NAME` and
|
in `TELEGRAM_BOT_NAME` and `TELEGRAM_TOKEN` environment variables.
|
||||||
`TELEGRAM_TOKEN` fields.
|
|
||||||
* Run `settelegramwebhook` management command. This command tells Telegram
|
* Run `settelegramwebhook` management command. This command tells Telegram
|
||||||
where to forward channel messages by invoking Telegram's
|
where to forward channel messages by invoking Telegram's
|
||||||
[setWebhook](https://core.telegram.org/bots/api#setwebhook) API call:
|
[setWebhook](https://core.telegram.org/bots/api#setwebhook) API call:
|
||||||
|
121
hc/settings.py
121
hc/settings.py
@ -1,13 +1,8 @@
|
|||||||
"""
|
"""
|
||||||
Django settings for hc project.
|
Django settings for healthchecks project.
|
||||||
|
|
||||||
Generated by 'django-admin startproject' using Django 1.8.2.
|
|
||||||
|
|
||||||
For more information on this file, see
|
|
||||||
https://docs.djangoproject.com/en/1.8/topics/settings/
|
|
||||||
|
|
||||||
For the full list of settings and their values, see
|
For the full list of settings and their values, see
|
||||||
https://docs.djangoproject.com/en/1.8/ref/settings/
|
https://docs.djangoproject.com/en/2.1/ref/settings
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
@ -15,13 +10,29 @@ import warnings
|
|||||||
|
|
||||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
|
||||||
HOST = "localhost"
|
|
||||||
SECRET_KEY = "---"
|
def envbool(s, default):
|
||||||
DEBUG = True
|
v = os.getenv(s, default=default)
|
||||||
ALLOWED_HOSTS = []
|
if v not in ("", "True", "False"):
|
||||||
DEFAULT_FROM_EMAIL = 'healthchecks@example.org'
|
msg = "Unexpected value %s=%s, use 'True' or 'False'" % (s, v)
|
||||||
USE_PAYMENTS = False
|
raise Exception(msg)
|
||||||
REGISTRATION_OPEN = True
|
return v == "True"
|
||||||
|
|
||||||
|
|
||||||
|
def envint(s, default):
|
||||||
|
v = os.getenv(s, default)
|
||||||
|
if v == "None":
|
||||||
|
return None
|
||||||
|
|
||||||
|
return int(v)
|
||||||
|
|
||||||
|
|
||||||
|
SECRET_KEY = os.getenv("SECRET_KEY", "---")
|
||||||
|
DEBUG = envbool("DEBUG", "True")
|
||||||
|
ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS", "*").split(",")
|
||||||
|
DEFAULT_FROM_EMAIL = os.getenv("DEFAULT_FROM_EMAIL", "healthchecks@example.org")
|
||||||
|
USE_PAYMENTS = envbool("DEBUG", "False")
|
||||||
|
REGISTRATION_OPEN = envbool("DEBUG", "True")
|
||||||
|
|
||||||
|
|
||||||
INSTALLED_APPS = (
|
INSTALLED_APPS = (
|
||||||
@ -90,28 +101,35 @@ DATABASES = {
|
|||||||
|
|
||||||
# You can switch database engine to postgres or mysql using environment
|
# You can switch database engine to postgres or mysql using environment
|
||||||
# variable 'DB'. Travis CI does this.
|
# variable 'DB'. Travis CI does this.
|
||||||
if os.environ.get("DB") == "postgres":
|
if os.getenv("DB") == "postgres":
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
'default': {
|
'default': {
|
||||||
'ENGINE': 'django.db.backends.postgresql',
|
'ENGINE': 'django.db.backends.postgresql',
|
||||||
'USER': 'postgres',
|
'HOST': os.getenv("DB_HOST", ""),
|
||||||
'NAME': 'hc',
|
'PORT': os.getenv("DB_PORT", ""),
|
||||||
'TEST': {'CHARSET': 'UTF8'}
|
'NAME': os.getenv("DB_NAME", "hc"),
|
||||||
|
'USER': os.getenv("DB_USER", "postgres"),
|
||||||
|
'PASSWORD': os.getenv("DB_PASSWORD", ""),
|
||||||
|
'CONN_MAX_AGE': envint("DB_CONN_MAX_AGE", "0"),
|
||||||
|
'TEST': {'CHARSET': 'UTF8'},
|
||||||
|
'OPTIONS': {
|
||||||
|
"sslmode": os.getenv("DB_SSLMODE", "prefer"),
|
||||||
|
"target_session_attrs": os.getenv("DB_TARGET_SESSION_ATTRS", "read-write")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if os.environ.get("DB") == "mysql":
|
if os.getenv("DB") == "mysql":
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
'default': {
|
'default': {
|
||||||
'ENGINE': 'django.db.backends.mysql',
|
'ENGINE': 'django.db.backends.mysql',
|
||||||
'USER': 'root',
|
'NAME': os.getenv("DB_NAME", "hc"),
|
||||||
'NAME': 'hc',
|
'USER': os.getenv("DB_USER", "root"),
|
||||||
|
'PASSWORD': os.getenv("DB_PASSWORD", ""),
|
||||||
'TEST': {'CHARSET': 'UTF8'}
|
'TEST': {'CHARSET': 'UTF8'}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LANGUAGE_CODE = 'en-us'
|
|
||||||
|
|
||||||
TIME_ZONE = 'UTC'
|
TIME_ZONE = 'UTC'
|
||||||
|
|
||||||
USE_I18N = True
|
USE_I18N = True
|
||||||
@ -120,10 +138,11 @@ USE_L10N = True
|
|||||||
|
|
||||||
USE_TZ = True
|
USE_TZ = True
|
||||||
|
|
||||||
SITE_ROOT = "http://localhost:8000"
|
SITE_ROOT = os.getenv("SITE_ROOT", "http://localhost:8000")
|
||||||
SITE_NAME = MASTER_BADGE_LABEL = "Mychecks"
|
SITE_NAME = os.getenv("SITE_NAME", "Mychecks")
|
||||||
PING_ENDPOINT = SITE_ROOT + "/ping/"
|
MASTER_BADGE_LABEL = os.getenv("MASTER_BADGE_LABEL", SITE_NAME)
|
||||||
PING_EMAIL_DOMAIN = HOST
|
PING_ENDPOINT = os.getenv("PING_ENDPOINT", SITE_ROOT + "/ping/")
|
||||||
|
PING_EMAIL_DOMAIN = os.getenv("PING_EMAIL_DOMAIN", "localhost")
|
||||||
STATIC_URL = '/static/'
|
STATIC_URL = '/static/'
|
||||||
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
|
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
|
||||||
STATIC_ROOT = os.path.join(BASE_DIR, 'static-collected')
|
STATIC_ROOT = os.path.join(BASE_DIR, 'static-collected')
|
||||||
@ -135,42 +154,42 @@ STATICFILES_FINDERS = (
|
|||||||
COMPRESS_OFFLINE = True
|
COMPRESS_OFFLINE = True
|
||||||
COMPRESS_CSS_HASHING_METHOD = "content"
|
COMPRESS_CSS_HASHING_METHOD = "content"
|
||||||
|
|
||||||
# Discord integration -- override these in local_settings
|
# Discord integration
|
||||||
DISCORD_CLIENT_ID = None
|
DISCORD_CLIENT_ID = os.getenv("DISCORD_CLIENT_ID")
|
||||||
DISCORD_CLIENT_SECRET = None
|
DISCORD_CLIENT_SECRET = os.getenv("DISCORD_CLIENT_SECRET")
|
||||||
|
|
||||||
# Slack integration -- override these in local_settings
|
# Slack integration
|
||||||
SLACK_CLIENT_ID = None
|
SLACK_CLIENT_ID = os.getenv("SLACK_CLIENT_ID")
|
||||||
SLACK_CLIENT_SECRET = None
|
SLACK_CLIENT_SECRET = os.getenv("SLACK_CLIENT_SECRET")
|
||||||
|
|
||||||
# Pushover integration -- override these in local_settings
|
# Pushover integration
|
||||||
PUSHOVER_API_TOKEN = None
|
PUSHOVER_API_TOKEN = os.getenv("PUSHOVER_API_TOKEN")
|
||||||
PUSHOVER_SUBSCRIPTION_URL = None
|
PUSHOVER_SUBSCRIPTION_URL = os.getenv("PUSHOVER_SUBSCRIPTION_URL")
|
||||||
PUSHOVER_EMERGENCY_RETRY_DELAY = 300
|
PUSHOVER_EMERGENCY_RETRY_DELAY = int(os.getenv("PUSHOVER_EMERGENCY_RETRY_DELAY", "300"))
|
||||||
PUSHOVER_EMERGENCY_EXPIRATION = 86400
|
PUSHOVER_EMERGENCY_EXPIRATION = int(os.getenv("PUSHOVER_EMERGENCY_EXPIRATION", "86400"))
|
||||||
|
|
||||||
# Pushbullet integration -- override these in local_settings
|
# Pushbullet integration
|
||||||
PUSHBULLET_CLIENT_ID = None
|
PUSHBULLET_CLIENT_ID = os.getenv("PUSHBULLET_CLIENT_ID")
|
||||||
PUSHBULLET_CLIENT_SECRET = None
|
PUSHBULLET_CLIENT_SECRET = os.getenv("PUSHBULLET_CLIENT_SECRET")
|
||||||
|
|
||||||
# Telegram integration -- override in local_settings.py
|
# Telegram integration -- override in local_settings.py
|
||||||
TELEGRAM_BOT_NAME = "ExampleBot"
|
TELEGRAM_BOT_NAME = os.getenv("TELEGRAM_BOT_NAME", "ExampleBot")
|
||||||
TELEGRAM_TOKEN = None
|
TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN")
|
||||||
|
|
||||||
# SMS (Twilio) integration -- override in local_settings.py
|
# SMS (Twilio) integration
|
||||||
TWILIO_ACCOUNT = None
|
TWILIO_ACCOUNT = os.getenv("TWILIO_ACCOUNT")
|
||||||
TWILIO_AUTH = None
|
TWILIO_AUTH = os.getenv("TWILIO_AUTH")
|
||||||
TWILIO_FROM = None
|
TWILIO_FROM = os.getenv("TWILIO_FROM")
|
||||||
|
|
||||||
# PagerDuty
|
# PagerDuty
|
||||||
PD_VENDOR_KEY = None
|
PD_VENDOR_KEY = os.getenv("PD_VENDOR_KEY")
|
||||||
|
|
||||||
# Zendesk
|
# Zendesk
|
||||||
ZENDESK_CLIENT_ID = None
|
ZENDESK_CLIENT_ID = os.getenv("ZENDESK_CLIENT_ID")
|
||||||
ZENDESK_CLIENT_SECRET = None
|
ZENDESK_CLIENT_SECRET = os.getenv("ZENDESK_CLIENT_ID")
|
||||||
|
|
||||||
# Trello
|
# Trello
|
||||||
TRELLO_APP_KEY = None
|
TRELLO_APP_KEY = os.getenv("TRELLO_APP_KEY")
|
||||||
|
|
||||||
if os.path.exists(os.path.join(BASE_DIR, "hc/local_settings.py")):
|
if os.path.exists(os.path.join(BASE_DIR, "hc/local_settings.py")):
|
||||||
from .local_settings import *
|
from .local_settings import *
|
||||||
|
Loading…
x
Reference in New Issue
Block a user