forked from oliverisaac/grafana-ldap-sync-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
59 lines (48 loc) · 2.27 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from script.core import startUserSync
import argparse
import logging
import sys
class DispatchingFormatter:
def __init__(self, formatters, default_formatter):
self._formatters = formatters
self._default_formatter = default_formatter
def format(self, record):
formatter = self._formatters.get(record.name, self._default_formatter)
return formatter.format(record)
def setup_logger(log_level = "INFO"):
"""
Setting up the used logger. The 'mutate' logger will print whether dry-run is used and changes are being applied.
"""
log_format = '%(asctime)s - %(levelname)5s - %(module)7s - %(message)s'
log_format_mut = log_format
if args.dry_run:
log_format_mut = '%(asctime)s - %(levelname)5s - %(module)7s - [SKIPPED] %(message)s'
else:
log_format_mut = log_format
logger = logging.getLogger()
while logger.handlers:
logger.handlers.pop()
formatter = DispatchingFormatter({
'mutate': logging.Formatter(log_format_mut),
},
logging.Formatter(log_format)
)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.setLevel(logging.getLevelName(log_level))
logger.addHandler(handler)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Process config path')
parser.add_argument('--config', dest='config_path', help='Path to the config_mock.yml file', required=True)
parser.add_argument('--bind', dest='bind', help='Path to the binding file', required=True)
parser.add_argument('--log-level', dest='log_level', help='Log level to use: DEBUG, INFO, WARN, ERROR', required=False, default="INFO")
parser.add_argument('--dry-run', dest='dry_run', default=False, help='If this flag is set, the script does not '
'apply changes to grafana. The would-be '
'changes are printed to the console.',
action='store_true')
args = parser.parse_args()
# setup the logger
setup_logger(log_level=args.log_level.upper())
# starts the sync process
exit_code = startUserSync(args.config_path, args.bind, args.dry_run)
sys.exit(exit_code)