diff --git a/Dockerfile b/Dockerfile index 3288260..9abf21c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM summerwind/whitebox-controller:latest AS base +FROM summerwind/whitebox-controller:patch-1 AS base FROM python:3.6-alpine diff --git a/app/common/__init__.py b/app/common/__init__.py new file mode 100644 index 0000000..e90f16a --- /dev/null +++ b/app/common/__init__.py @@ -0,0 +1,54 @@ +import os +import sys +import yaml +import requests + + +def log(*vargs, **kwargs): + print(file=sys.stderr, *vargs, **kwargs) + + +def load_config(): + config_file = os.environ.get("CONTROLLER_CONFIG", "/config/controller.yaml") + config = {} + err = False + + try: + if os.path.exists(config_file): + with open(config_file, 'r') as cf: + config = yaml.safe_load(cf) + except Exception as ex: + log("Error loading config: %s: %s" % (config_file, str(ex))) + err = True + + return config, err + + +base_project_list_url = os.environ.get("GETUP_API_URL", "http://api.getup.svc:8080").strip('/') + '/api/v1/project/' +try: + getup_api_username = os.environ["GETUP_API_USERNAME"] + getup_api_password = os.environ["GETUP_API_PASSWORD"] +except KeyError as ex: + log("Missing environment variable:", ex) + sys.exit(1) + +def get_project(name): + project_url = base_project_list_url + name + '/' + auth = (getup_api_username, getup_api_password) + + res = requests.get(project_url, auth=auth, params={"status":"active"}, allow_redirects=True) + + if not res.ok: + return None, res + return res.json(), res + + +def getup_api_create_project(name, owner) + data = {'owner':owner, 'name':name, 'family':'default'} + log('Creating Project "%s"' % data) + + res = requests.post(base_project_list_url, auth=auth, data=data, params={'sync':True}) + + if not res.ok: + return None, res + return res.json(), res diff --git a/app/entrypoint b/app/entrypoint index 68e04d5..0db39a3 100755 --- a/app/entrypoint +++ b/app/entrypoint @@ -3,6 +3,8 @@ cd /app source /app/.version +export PYTHONPATH=$PYTHONPATH:/app/common + if ${LOG_LINES:-false}; then exec /bin/whitebox-controller -c /config/config.yaml "$@" 2>&1 | jq -r '[(.ts|todateiso8601),(.level|ascii_upcase),.msg]|@tsv' | tr -s "\t" " " else diff --git a/app/project-reconciler.py b/app/project-reconciler.py index fed7796..7c679af 100755 --- a/app/project-reconciler.py +++ b/app/project-reconciler.py @@ -3,32 +3,9 @@ from __future__ import print_function import os import sys -import errno import json -import yaml import logging -import requests - - -def log(*vargs, **kwargs): - print(file=sys.stderr, *vargs, **kwargs) - - -def load_config(): - config_file = os.environ.get("CONTROLLER_CONFIG", "/config/controller.yaml") - config = {} - err = False - - try: - if os.path.exists(config_file): - with open(config_file, 'r') as cf: - config = yaml.safe_load(cf) -# log('Loaded config from', config_file) - except Exception as ex: - log("Error loading config: %s: %s" % (config_file, str(ex))) - err = True - - return config, err +from common import load_config, getup_api_get_project, getup_api_create_project def main(): @@ -59,28 +36,17 @@ def main(): log('Project "%s" owner should be "%s"' % (name, owner)) - project_list_url = os.environ["GETUP_API_URL"].strip('/') + '/api/v1/project/' - project_url = project_list_url + name + '/' - auth = (os.environ["GETUP_API_USERNAME"], os.environ["GETUP_API_PASSWORD"]) - - res = requests.get(project_url, auth=auth, params={"status":"active"}, allow_redirects=True) - project = res.json() - - if res.status_code == 200: + project, res = getup_api_get_project(name): + if project: if project['owner'] == owner: log('Project "%s" already owned by "%s".' % (name, owner)) else: log('Conflict: project "%s" owned by "%s", but namespace says it should be owned by "%s"' % (name, project['owner'], owner)) return - if res.status_code == 404: - data = {'owner':owner, 'name':name, 'family':'default'} - log('Will create project with "%s"' % data) - res = requests.post(project_list_url, auth=auth, data=data, params={'sync':True}) - log('Project create "%s" returned: %s %s' % (name, res.status_code, res.reason)) - - if not res.ok: - log('Error creating billing Project:', res.text) + project, res = getup_api_create_project(name, owner) + if not res.ok: + log('Error creating Project: %s: %s %s: %s' % (name, res.status_code, res.reason, res.text)) if __name__ == "__main__": try: diff --git a/app/project-validator.py b/app/project-validator.py index fc0f9a3..e034cf6 100755 --- a/app/project-validator.py +++ b/app/project-validator.py @@ -3,14 +3,9 @@ from __future__ import print_function import os import sys -import errno import json -import yaml import logging -import requests - -def log(*vargs, **kwargs): - print(file=sys.stderr, *vargs, **kwargs) +from common import log, load_config def print_response(allowed, reason='', code=200): @@ -25,23 +20,6 @@ def print_response(allowed, reason='', code=200): json.dump(res, sys.stdout) -def load_config(): - config_file = os.environ.get("CONTROLLER_CONFIG", "/config/controller.yaml") - config = {} - err = False - - try: - if os.path.exists(config_file): - with open(config_file, 'r') as cf: - config = yaml.safe_load(cf) -# log('Loaded config from', config_file) - except Exception as ex: - log("Error loading config: %s: %s" % (config_file, str(ex))) - err = True - - return config, err - - def main(): allowed = True reason = "Namespace accepted"