From ef2160e82e41209542e74776bafac8339359b22f Mon Sep 17 00:00:00 2001 From: Tim Kelsey Date: Tue, 1 Mar 2016 11:17:37 +0000 Subject: [PATCH] Anchor can now be installed and invoked as simply "anchor" This installs stuff in the right places to run anchor from the included startup scripts. The config is installed into /etc/anchor This will work from within a venv or without. The anchor config.py file has been moved into the project package so that it will install with the other stuff. Eventually we should strip it out as much as possible and move the details into the JSON file. Change-Id: Iffaa7669ce8118fbd41011f9e965704c2ad51b44 --- anchor/app.py | 3 ++- config.py => anchor/config.py | 2 +- bin/anchor | 43 +++++++++++++++++++++++++++++++++++ bin/anchor_production | 6 ----- setup.cfg | 3 +-- tests/controllers/test_app.py | 7 ++++-- tests/test_functional.py | 2 +- 7 files changed, 53 insertions(+), 13 deletions(-) rename config.py => anchor/config.py (97%) create mode 100755 bin/anchor delete mode 100755 bin/anchor_production diff --git a/anchor/app.py b/anchor/app.py index 17fd39f..00f9266 100644 --- a/anchor/app.py +++ b/anchor/app.py @@ -198,7 +198,8 @@ def load_config(): user_config_path = os.path.join( os.environ['HOME'], '.config', 'anchor', 'config.json') - sys_config_path = os.path.join(os.sep, 'etc', 'anchor', 'config.json') + prefix = os.environ.get('VIRTUAL_ENV', os.sep) + sys_config_path = os.path.join(prefix, 'etc', 'anchor', 'config.json') if 'registration_authority' not in jsonloader.conf.config: config_path = "" diff --git a/config.py b/anchor/config.py similarity index 97% rename from config.py rename to anchor/config.py index f9fe12a..e0dad12 100644 --- a/config.py +++ b/anchor/config.py @@ -1,6 +1,6 @@ server = { 'port': '5016', - 'host': '0.0.0.0' + 'host': '0.0.0.0' # nosec } # Pecan Application Configurations diff --git a/bin/anchor b/bin/anchor new file mode 100755 index 0000000..cb03733 --- /dev/null +++ b/bin/anchor @@ -0,0 +1,43 @@ +#!/bin/sh + +if [ "$1" = "-h" -o "$1" = "--help" ] ; then + echo "Usage: [PY=optional/path/python] $0" + echo + echo "Run Anchor with default uwsgi settings. It will spawn 4 workers" + echo "and use either the default reachable 'python' or one defined in the" + echo "\$PY variable." + echo + exit 0 +fi + +if ! which uwsgi > /dev/null ; then + echo "You need to install uwsgi to run anchor using this script." + exit 1 +fi + +PY=${PY:-python} + +if ! [ -x "$PY" ] ; then + if ! [ -x "$(which "$PY")" ] ; then + echo "Python interpreter not found (use PY variable to specify)." + exit 1 + fi +fi + +STR="import pkg_resources; print(pkg_resources.get_distribution('anchor').location)" +PKG_PATH=$("$PY" -c "$STR") + +if ! [ -d "$PKG_PATH" ] ; then + echo "Cannot find installed anchor package." + exit 1 +fi + +if [ -z "$VIRTUAL_ENV" ]; then + OPTS="-p 4" +else + OPTS="--venv ""${VIRTUAL_ENV}"" -p 4" +fi + +uwsgi --http-socket :5016 \ + --pecan "${PKG_PATH}/anchor/config.py" \ + ${OPTS} diff --git a/bin/anchor_production b/bin/anchor_production deleted file mode 100755 index b91abd4..0000000 --- a/bin/anchor_production +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh -VENV=$1 - -[ -n "$VENV" ] || ( echo "provide virtual env path as parameter" && exit 1 ) - -"$VENV/bin/uwsgi" --http-socket :5000 --venv "$VENV" --pecan config.py -p 4 diff --git a/setup.cfg b/setup.cfg index 06f5ce9..0eacdbc 100644 --- a/setup.cfg +++ b/setup.cfg @@ -57,9 +57,8 @@ anchor.fixups = data_files = etc/anchor = config.json - config.py packages = anchor scripts = - bin/anchor_production + bin/anchor bin/anchor_debug diff --git a/tests/controllers/test_app.py b/tests/controllers/test_app.py index 72ffdc1..6c5bab0 100644 --- a/tests/controllers/test_app.py +++ b/tests/controllers/test_app.py @@ -16,6 +16,7 @@ import json +import os import stat import unittest @@ -247,7 +248,9 @@ class TestApp(tests.DefaultConfigMixin, unittest.TestCase): @mock.patch('anchor.jsonloader.conf.load_file_data') def test_config_paths_system(self, conf): - ret = lambda x: True if x == '/etc/anchor/config.json' else False + path = os.path.join(os.environ.get('VIRTUAL_ENV', os.sep), + 'etc/anchor/config.json') + ret = lambda x: x == path with mock.patch('os.path.isfile', ret): app.load_config() - conf.assert_called_with('/etc/anchor/config.json') + conf.assert_called_with(path) diff --git a/tests/test_functional.py b/tests/test_functional.py index 011c0ff..b05b7f9 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -26,9 +26,9 @@ import pecan from pecan import testing as pecan_testing import stevedore +from anchor import config from anchor import jsonloader from anchor.X509 import certificate as X509_cert -import config import tests