summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2018-02-02 18:10:27 +0200
committerLars Wirzenius <liw@liw.fi>2018-02-02 18:16:01 +0200
commit481aebba3bc400ddb61dd5e952fb0ebc4e600290 (patch)
tree4dfae1665dae28408e340aa1bd701da36449b395
parenta22e4e1587de894bbb8c359965060b1d254dd9ca (diff)
downloadqvisqve-481aebba3bc400ddb61dd5e952fb0ebc4e600290.tar.gz
Refactor: start_salami and how Salami is started
-rw-r--r--salami/__init__.py2
-rw-r--r--salami/backend.py37
-rwxr-xr-xstart_salami111
3 files changed, 67 insertions, 83 deletions
diff --git a/salami/__init__.py b/salami/__init__.py
index baef7cc..14b0f73 100644
--- a/salami/__init__.py
+++ b/salami/__init__.py
@@ -13,7 +13,6 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-
from .version import __version__, __version_info__
from .responses import (
bad_request_response,
@@ -29,3 +28,4 @@ from .version_router import VersionRouter
from .token_router import TokenRouter
from .api import SalamiAPI
+from .backend import create_app
diff --git a/salami/backend.py b/salami/backend.py
index 8279000..18b27dc 100644
--- a/salami/backend.py
+++ b/salami/backend.py
@@ -1,5 +1,4 @@
-#!/usr/bin/python3
-# Copyright (C) 2017 Lars Wirzenius
+# Copyright (C) 2017-2018 Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
@@ -17,10 +16,12 @@
import os
+
import apifw
import slog
import yaml
+
import salami
@@ -53,26 +54,26 @@ def counter():
default_config = {
'log': [],
'token-issuer': None,
- 'token-audience': None,
'token-public-key': None,
+ 'token-private-key': None,
+ 'token-lifetime': None,
+ 'clients': None,
}
-config_filename = os.environ.get('SALAMI_CONFIG', DEFAULT_CONFIG_FILE)
-actual_config = read_config(config_filename)
-config = dict(default_config)
-config.update(actual_config or {})
-check_config(config)
-salami.setup_logging(config)
-salami.log.log('info', msg_text='Salami starting')
+def create_app():
+ config_filename = os.environ.get('SALAMI_CONFIG', DEFAULT_CONFIG_FILE)
+ actual_config = read_config(config_filename)
+ config = dict(default_config)
+ config.update(actual_config or {})
+ if 'token-audience' not in config:
+ config['token-audience'] = config.get('token-issuer')
+ check_config(config)
+ salami.setup_logging(config)
+ salami.log.log('info', msg_text='Salami starting')
-api = salami.SalamiAPI(config)
-app = apifw.create_bottle_application(api, counter, dict_logger, config)
+ api = salami.SalamiAPI(config)
+ return apifw.create_bottle_application(api, counter, dict_logger, config)
-# If we are running this program directly with Python, and not via
-# gunicorn, we can use the Bottle built-in debug server, which can
-# make some things easier to debug.
-if __name__ == '__main__':
- print('running in debug mode')
- app.run(host='127.0.0.1', port=12765)
+app = create_app()
diff --git a/start_salami b/start_salami
index 76ced9f..16a09f5 100755
--- a/start_salami
+++ b/start_salami
@@ -15,90 +15,73 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-# Usage: start_salami prod
-# start_salami debug FILE
-# The first one is for production use. The second is to run a debug
-# instance, with a generated token signing key, and an access token
-# written to FILE.
-
-
set -eu
-start_prod()
+get()
{
- env SALAMI_CONFIG="/etc/salami/salami.yaml" \
- gunicorn3 \
- --bind 127.0.0.1:12765 \
- -w1 \
- --log-file /var/log/salami/gunicorn3.log \
- --log-level debug \
- salami.backend:app
+ set -eu
+ python3 -c '
+import yaml, sys
+filename, varname = sys.argv[1:]
+config = yaml.safe_load(open(filename))
+print(config.get(varname, ""))
+' "$@"
}
-default_scopes()
+gunicorn_wanted()
{
- echo uapi_version_get
+ set -eu
+ case "$(get "$1" gunicorn)" in
+ yes|True)
+ return 0
+ ;;
+ esac
+ return 1
}
-cleanup()
+run_bottle()
{
- rm -rf "$tmp"
+ set -eu
+ export SALAMI_CONFIG="$1"
+ python3 -c 'import salami; salami.create_app().run(host="127.0.0.1", port=12765)'
}
-config()
-{
- cat <<EOF
-log:
- - filename: salami.log
-token-issuer: $1
-token-audience: $2
-token-public-key: $(cat "$3")
-EOF
-}
-
-start_debug()
+run_gunicorn()
{
- local token="$1"
- local pid="$2"
- local port="$3"
- shift 3
+ set -eu
+ local config="$1"
+ local log_file="$(get "$config" gunicorn-log)"
+ local pid_file="$(get "$config" gunicorn-pid-file)"
+ local port="$(get "$config" gunicorn-port)"
- tmp="$(mktemp -d)"
- trap cleanup EXIT
-
- ISS=test
- AUD=aud
-
- local dir="$(dirname "$0")"
- "$dir/generate-rsa-key" "$tmp/key"
- cp "$tmp/key.pub" key
- "$dir/create-token" "$tmp/key" "$ISS" "$AUD" "$(default_scopes)" > "$token"
-
- if [ "${SALAMI_CONFIG:-no}" = no ]
+ if [ "${pid_file:=no}" = no ]
then
- export SALAMI_CONFIG="$tmp/salami.yaml"
- config "$ISS" "$AUD" "$tmp/key.pub" > "$SALAMI_CONFIG"
+ pid_opt=""
+ else
+ pid_opt="-p $pid_file"
fi
- gunicorn3 --bind "127.0.0.1:$port" -p "$pid" -w1 --log-file g.log \
- --log-level debug "$@" \
- salami.backend:app
+
+ export SALAMI_CONFIG="$config"
+ gunicorn3 \
+ --bind 127.0.0.1:"$port" \
+ -w1 \
+ --log-file "$log_file" \
+ "$pid_opt" \
+ --log-level debug \
+ --daemon \
+ salami.backend:app
}
-case "$1" in
- prod)
- start_prod
- ;;
- debug)
- shift
- start_debug "$@"
- ;;
- *)
- echo "wat?" 1>&2
- exit 1
-esac
+config="$1"
+if gunicorn_wanted "$config"
+then
+ run_gunicorn "$config"
+else
+ run_bottle "$config"
+fi