-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from maykinmedia/feature/setup-config
add `setup_configuration` command
- Loading branch information
Showing
25 changed files
with
594 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
|
||
# setup initial configuration using environment variables | ||
# Run this script from the root of the repository | ||
|
||
#set -e | ||
${SCRIPTPATH}/wait_for_db.sh | ||
|
||
src/manage.py migrate | ||
|
||
src/manage.py setup_configuration --no-selftest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/sh | ||
|
||
set -e | ||
|
||
# Wait for the database container | ||
# See: https://docs.docker.com/compose/startup-order/ | ||
export PGHOST=${DB_HOST:-db} | ||
export PGPORT=${DB_PORT:-5432} | ||
|
||
until pg_isready; do | ||
>&2 echo "Waiting for database connection..." | ||
sleep 1 | ||
done | ||
|
||
>&2 echo "Database is up." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,20 +2,37 @@ version: '3' | |
|
||
services: | ||
db: | ||
# NOTE: No persistance storage configured. | ||
# See: https://hub.docker.com/_/postgres/ | ||
image: postgres | ||
image: postgres:11-alpine | ||
environment: | ||
- POSTGRES_USER=${DB_USER:-objecttypes} | ||
- POSTGRES_PASSWORD=${DB_PASSWORD:-objecttypes} | ||
- POSTGRES_HOST_AUTH_METHOD=trust | ||
volumes: | ||
- ./docker-init-db.sql:/docker-entrypoint-initdb.d/init_db.sql | ||
# - db:/var/lib/postgresql/data | ||
command: postgres -c max_connections=300 -c log_min_messages=LOG | ||
|
||
web: | ||
build: . | ||
environment: | ||
environment: &app-env | ||
- DJANGO_SETTINGS_MODULE=objecttypes.conf.docker | ||
- SECRET_KEY=${SECRET_KEY:-fgv=c0hz&tl*8*3m3893@m+1pstrvidc9e^5@fpspmg%cy$15d} | ||
- ALLOWED_HOSTS=* | ||
- TWO_FACTOR_FORCE_OTP_ADMIN=no | ||
- TWO_FACTOR_PATCH_ADMIN=no | ||
# setup_configuration env vars | ||
- OBJECTTYPES_DOMAIN=web:8000 | ||
- OBJECTTYPES_ORGANIZATION=ObjectTypes | ||
- OBJECTS_OBJECTTYPES_TOKEN=some-random-string | ||
- OBJECTS_OBJECTTYPES_PERSON=Some Person | ||
- [email protected] | ||
ports: | ||
- 8000:8000 | ||
depends_on: | ||
- db | ||
web-init: | ||
condition: service_completed_successfully | ||
|
||
web-init: | ||
build: . | ||
environment: *app-env | ||
command: /setup_configuration.sh | ||
depends_on: | ||
- db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
CREATE USER objecttypes; | ||
CREATE DATABASE objecttypes; | ||
GRANT ALL PRIVILEGES ON DATABASE objecttypes TO objecttypes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from django.conf import settings | ||
from django.urls import reverse | ||
|
||
import requests | ||
from django_setup_configuration.configuration import BaseConfigurationStep | ||
from django_setup_configuration.exceptions import SelfTestFailed | ||
|
||
from objecttypes.token.models import TokenAuth | ||
from objecttypes.utils import build_absolute_url | ||
|
||
|
||
class DemoUserStep(BaseConfigurationStep): | ||
""" | ||
Create demo user to request Objectypes API | ||
""" | ||
|
||
verbose_name = "Demo User Configuration" | ||
required_settings = [ | ||
"DEMO_TOKEN", | ||
"DEMO_PERSON", | ||
"DEMO_EMAIL", | ||
] | ||
enable_setting = "DEMO_CONFIG_ENABLE" | ||
|
||
def is_configured(self) -> bool: | ||
return TokenAuth.objects.filter(token=settings.DEMO_TOKEN).exists() | ||
|
||
def configure(self): | ||
TokenAuth.objects.update_or_create( | ||
token=settings.DEMO_TOKEN, | ||
defaults={ | ||
"contact_person": settings.DEMO_PERSON, | ||
"email": settings.DEMO_EMAIL, | ||
}, | ||
) | ||
|
||
def test_configuration(self): | ||
endpoint = reverse("v2:objecttype-list") | ||
full_url = build_absolute_url(endpoint, request=None) | ||
|
||
try: | ||
response = requests.get( | ||
full_url, | ||
headers={ | ||
"Authorization": f"Token {settings.DEMO_TOKEN}", | ||
"Accept": "application/json", | ||
}, | ||
) | ||
response.raise_for_status() | ||
except requests.RequestException as exc: | ||
raise SelfTestFailed( | ||
"Could not list objecttypes for the configured token" | ||
) from exc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from django.conf import settings | ||
from django.urls import reverse | ||
|
||
import requests | ||
from django_setup_configuration.configuration import BaseConfigurationStep | ||
from django_setup_configuration.exceptions import SelfTestFailed | ||
|
||
from objecttypes.token.models import TokenAuth | ||
from objecttypes.utils import build_absolute_url | ||
|
||
|
||
class ObjectsAuthStep(BaseConfigurationStep): | ||
""" | ||
Configure credentials for Objects API to request Objecttypes API | ||
""" | ||
|
||
verbose_name = "Objects API Authentication Configuration" | ||
required_settings = [ | ||
"OBJECTS_OBJECTTYPES_TOKEN", | ||
"OBJECTS_OBJECTTYPES_PERSON", | ||
"OBJECTS_OBJECTTYPES_EMAIL", | ||
] | ||
enable_setting = "OBJECTS_OBJECTTYPES_CONFIG_ENABLE" | ||
|
||
def is_configured(self) -> bool: | ||
return TokenAuth.objects.filter( | ||
token=settings.OBJECTS_OBJECTTYPES_TOKEN | ||
).exists() | ||
|
||
def configure(self): | ||
TokenAuth.objects.update_or_create( | ||
token=settings.OBJECTS_OBJECTTYPES_TOKEN, | ||
defaults={ | ||
"contact_person": settings.OBJECTS_OBJECTTYPES_PERSON, | ||
"email": settings.OBJECTS_OBJECTTYPES_EMAIL, | ||
}, | ||
) | ||
|
||
def test_configuration(self): | ||
endpoint = reverse("v2:objecttype-list") | ||
full_url = build_absolute_url(endpoint, request=None) | ||
|
||
try: | ||
response = requests.get( | ||
full_url, | ||
headers={ | ||
"Authorization": f"Token {settings.OBJECTS_OBJECTTYPES_TOKEN}", | ||
"Accept": "application/json", | ||
}, | ||
) | ||
response.raise_for_status() | ||
except requests.RequestException as exc: | ||
raise SelfTestFailed( | ||
"Could not list objecttypes for the configured token" | ||
) from exc |
Oops, something went wrong.