From 7a95b46c73b6869636c4e01740b241f592dc700f Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Tue, 17 Sep 2019 02:11:45 +0200 Subject: [PATCH] added support for fallback config files --- config.json.example | 8 ++------ lib/safe-harbor.py | 25 +++++++++++++++++++------ systemd/safe-harbor@.service | 2 +- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/config.json.example b/config.json.example index e6836ae..b5f2e90 100644 --- a/config.json.example +++ b/config.json.example @@ -1,14 +1,10 @@ { - "host": "decade.cz", + "host": "nice.wafe.eu", "user": "tunnel", "mapping": [ { - "remote": 50022, + "remote": 39999, "local": 22 - }, - { - "remote": 8000, - "local": 80 } ] } diff --git a/lib/safe-harbor.py b/lib/safe-harbor.py index 6e8c5ec..83f3f69 100755 --- a/lib/safe-harbor.py +++ b/lib/safe-harbor.py @@ -6,14 +6,27 @@ import json import subprocess import sys +def load_cfg(path): + with open(path) as f: + return json.loads(jsmin.jsmin(f.read())) + if __name__ == "__main__": - if len(sys.argv) == 2: - cfg_path = sys.argv[1] - else: - cfg_path = "/etc/safe-harbor/config.json" + # try config files in the order they are passed to us + # first one wins - with open(cfg_path) as f: - cfg = json.loads(jsmin.jsmin(f.read())) + cfg = None + + for path in sys.argv[1:]: + try: + cfg = load_cfg(path) + break + except Exception as e: + print("! cfg file %s failed to load (%s)" %(path, e)) + + + if not cfg: + print("!!! unable to load any configuration") + sys.exit(1) command = ["/usr/bin/ssh", "-o", "ServerAliveInterval 45", "-o", "ServerAliveCountMax 2", "-o", "ExitOnForwardFailure yes", "-N"] diff --git a/systemd/safe-harbor@.service b/systemd/safe-harbor@.service index 2eb52be..9a105cf 100644 --- a/systemd/safe-harbor@.service +++ b/systemd/safe-harbor@.service @@ -4,7 +4,7 @@ After=network.target [Service] User=%i -ExecStart=/usr/bin/python3 /usr/lib/safe-harbor/safe-harbor.py +ExecStart=/usr/bin/python3 /usr/lib/safe-harbor/safe-harbor.py /state/safe-harbor.json /etc/safe-harbor/config.json Restart=always RestartSec=90