From 3493b44a461e0b121c6d2667b94343030bd98a86 Mon Sep 17 00:00:00 2001 From: Daniel Ramsay Date: Thu, 16 Feb 2017 01:23:55 +0000 Subject: [PATCH] add configure script, to setup probe from docker env vars --- Dockerfile | 1 + docker/configure.py | 58 +++++++++++++++++++++++++++++++++++++++++++++ docker/run-probe.sh | 29 +---------------------- 3 files changed, 60 insertions(+), 28 deletions(-) create mode 100644 docker/configure.py diff --git a/Dockerfile b/Dockerfile index 68b6ed5..a2f6b7e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,6 +14,7 @@ RUN mkdir /usr/local/probe COPY *.py /usr/local/probe/ COPY docker/run-probe.sh /usr/local/probe/run-probe.sh COPY docker/config.ini.tmpl /usr/local/probe/config.ini.tmpl +COPY docker/configure.py /usr/local/probe/configure.py RUN chmod a+x /usr/local/probe/run-probe.sh diff --git a/docker/configure.py b/docker/configure.py new file mode 100644 index 0000000..caac398 --- /dev/null +++ b/docker/configure.py @@ -0,0 +1,58 @@ + +import os +import sys +import argparse +import ConfigParser + +REQUIRED = [ + "PROBE_UUID","PROBE_SECRET","AMQP_USER","AMQP_PASSWD" + ] + +DEFAULTS = { + 'API_HOST': 'api.blocked.org.uk', + 'API_PORT': '443', + 'API_HTTPS': 'True' + } + +env = os.environ + +parser = argparse.ArgumentParser(description="Script to write OrgProbe config file") +parser.add_argument('--output','-o', help="Path to output file") +parser.add_argument('--template','-t', help="Path to template file", + default=os.path.join(os.path.dirname(sys.argv[0]), 'config.ini.tmpl'), + ) +args = parser.parse_args() + +missing = [] +for req in REQUIRED: + if req not in env: + missing.append(req) + +if missing: + print "Missing environment: " + ",".join(missing) + sys.exit(1) + +cfg = ConfigParser.ConfigParser() +cfg.read([args.template]) + +cfg.set('api','host', env.get('API_HOST', DEFAULTS['API_HOST'])) +cfg.set('api','port', env.get('API_PORT', DEFAULTS['API_PORT'])) +cfg.set('api','https', env.get('API_HTTPS', DEFAULTS['API_HTTPS'])) + +cfg.set('amqp','host', env.get('API_HOST', DEFAULTS['API_HOST'])) + +cfg.set('amqp','userid', env['AMQP_USER']) +cfg.set('amqp','passwd', env['AMQP_PASSWD']) + +cfg.set('public','uuid', env['PROBE_UUID']) +cfg.set('public','secret', env['PROBE_SECRET']) + +if 'PROBE_QUEUE' in env: + cfg.set('public','queue',env['PROBE_QUEUE']) +if 'PROBE_NETWORK' in env: + cfg.set('public','network', env['PROBE_NETWORK']) + +with open(args.output,'w') as fp: + cfg.write(fp) + + diff --git a/docker/run-probe.sh b/docker/run-probe.sh index 4d776e4..2495641 100644 --- a/docker/run-probe.sh +++ b/docker/run-probe.sh @@ -5,34 +5,7 @@ CONFIG=$PRBDIR/config.ini if [ ! -f $CONFIG ] then - [ -z "$PROBE_UUID" -o -z "$PROBE_SECRET" -o -z "$AMQP_USER" -o -z "$AMQP_PASSWD" ] && \ - echo "Required envvars: PROBE_UUID, PROBE_SECRET, AMQP_USER, AMQP_PASSWD." && \ - exit 1 - - # use API_HOST if defined - API_HOST=${API_HOST:-api.blocked.org.uk} - API_PORT=${API_PORT:-443} - API_HTTPS=${API_HTTPS:-True} - - # create config file from template - cp $PRBDIR/config.ini.tmpl $CONFIG - sed -i $CONFIG \ - -e "s/API_HOST/$API_HOST/" \ - -e "s/API_PORT/$API_PORT/" \ - -e "s/API_HTTPS/$API_HTTPS/" \ - -e "s/PROBE_UUID/$PROBE_UUID/" \ - -e "s/PROBE_SECRET/$PROBE_SECRET/" \ - -e "s/AMQP_USER/$AMQP_USER/" \ - -e "s/AMQP_PASSWD/$AMQP_PASSWD/" - - if [ ! -z "$REDIS" ] - then - PROBE_LIMIT=${PROBE_LIMIT:-200000000} - echo "limit = $PROBE_LIMIT" >> $CONFIG - echo "" >> $CONFIG - echo "[accounting]" >> $CONFIG - echo "redis_server = $REDIS" >> $CONFIG - fi + python $PRBDIR/configure.py -o $CONFIG fi exec /usr/bin/python $PRBDIR/__main__.py -c $PRBDIR/config.ini