-
Notifications
You must be signed in to change notification settings - Fork 1
/
nacld.py
executable file
·92 lines (74 loc) · 2.46 KB
/
nacld.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/python3
#
# Maximilian Wilhelm <max@rfc2324.org>
# -- Sun 17 Mar 2019 09:27:09 PM CET
#
import argparse
import logging
import os.path
import sys
from werkzeug.serving import run_simple
from nacl.webservice import NaclWS
from nacl.app import Nacl
# Parse command line arguments
parser = argparse.ArgumentParser (description = 'Netbox Automation and Caching Layer for FFHO Salt')
parser.add_argument ('--cache', '-C', help = 'Enable caching', action = 'store_true', default = True)
parser.add_argument ('--config', '-c', help = 'Path to config file (json format)')
parser.add_argument ('--debug', '-D', help = 'Activate werkzeug debugger', action = 'store_true')
parser.add_argument ('--reload', '-R', help = 'Activate werkzeug reloader', action = 'store_true')
parser.add_argument ('--listen', help = 'Local address to listen on.', default = '127.0.0.1')
parser.add_argument ('--port', help = "TCP port to listen on.", default = '5000', type = int)
parser.add_argument ('--log-level', help = "Log level", choices = ['debug', 'info', 'warning', 'error', 'critical'], default = 'info')
parser.add_argument ('--log-file', help = "Path to log file, - for stdout (default)", default = '-')
args = parser.parse_args ()
#
# Default config file paths
#
default_configs = [
'nacl_config.json',
'/etc/nacl/nacl_config.json'
]
if not getattr (args, 'config'):
for path in default_configs:
if os.path.isfile (path):
setattr (args, 'config', path)
break
#
# Set up logging
#
def setup_logging (args):
# Fire up a logger for NACL and one which will be used by werkzeug
nacl = logging.getLogger ('nacl')
# Log level?
level_map = {
'debug' : logging.DEBUG,
'info' : logging.INFO,
'warning' : logging.WARNING,
'error' : logging.ERROR,
'critical' : logging.CRITICAL,
}
# Set NACL log level
nacl.setLevel (level_map[args.log_level])
# Where to log to?
if args.log_file == '-':
handler = logging.StreamHandler ()
else:
handler = logging.FileHandler (args.log_file)
# Log format
formatter = logging.Formatter ('%(asctime)s %(levelname)s %(message)s')
handler.setFormatter (formatter)
nacl.addHandler (handler)
return nacl
#
# Let's go
#
log = setup_logging (args)
log.info ("NACL starting...")
# Fire up NACL application
nacl = Nacl (args.config, log, args.cache)
# Fire up web service
app = NaclWS (nacl)
try:
run_simple (args.listen, args.port, app, use_debugger = args.debug, use_reloader = args.reload)
except Exception as e:
print ("Failed to start nacld: %s" % e)