From cf0357197e27d9cb4bf5039606c4b255e1943f46 Mon Sep 17 00:00:00 2001 From: nico Date: Mon, 17 Feb 2020 02:05:33 +0100 Subject: config file location overwrite + add environment toggle to overwrite the config directory setting ejabberd_metrics_dev to 1 / true -> set the config path inside the dev directory --- config.py | 18 +++++++++++------- config_example.json | 18 ------------------ ejabberd-metrics.conf.default | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 25 deletions(-) delete mode 100644 config_example.json create mode 100644 ejabberd-metrics.conf.default diff --git a/config.py b/config.py index 55b7639..da20e66 100644 --- a/config.py +++ b/config.py @@ -2,15 +2,19 @@ # -*- coding: utf-8 -*- import json import sys +from os import environ from pathlib import Path class Config: def __init__(self): - # global config path - conf_path = '/etc/ejabberd-metrics.conf' - self.file = Path(conf_path) + # class variables self.content = None + self.conf_file = Path('/etc/ejabberd-metrics.conf') + + # dev config overwrite + if environ.get('ejabberd_metrics_dev'): + self.conf_file = Path('config.json') # read config file self._read() @@ -20,7 +24,7 @@ class Config: self._check() # open and load json content from config - with open(self.file, 'r', encoding='utf-8') as f: + with open(self.conf_file, 'r', encoding='utf-8') as f: try: self.content = json.load(f) @@ -32,13 +36,13 @@ class Config: def _check(self): """internal method to check if the config file exists""" try: - # if file is present try to read it's contents - if self.file.exists(): + # if file is present continue + if self.conf_file.exists(): return # if not create a blank file else: - Path.touch(self.file) + self.conf_file.touch(mode=0o640) # catch permission exceptions as this tries to write to /etc/ except PermissionError as err: diff --git a/config_example.json b/config_example.json deleted file mode 100644 index ad984aa..0000000 --- a/config_example.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "url": "http://[::1]:5280/api", - "login": { - "user": "metric-user", - "server": "chat.sum7.eu", - "password": "password" - }, - "api": "rest", - - "--comment": "influxdb", - "influxdb_host": "localhost", - "influxdb_port": 8086, - "influxdb_db": "ejabberd", - - "--comment": "prometheus", - "prometheus_port": 8080, - "prometheus_refresh": 10 -} diff --git a/ejabberd-metrics.conf.default b/ejabberd-metrics.conf.default new file mode 100644 index 0000000..ad984aa --- /dev/null +++ b/ejabberd-metrics.conf.default @@ -0,0 +1,18 @@ +{ + "url": "http://[::1]:5280/api", + "login": { + "user": "metric-user", + "server": "chat.sum7.eu", + "password": "password" + }, + "api": "rest", + + "--comment": "influxdb", + "influxdb_host": "localhost", + "influxdb_port": 8086, + "influxdb_db": "ejabberd", + + "--comment": "prometheus", + "prometheus_port": 8080, + "prometheus_refresh": 10 +} -- cgit v1.2.3-54-g00ecf