forked from openwisp/netjsonconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netjsonconfig
198 lines (165 loc) · 6.15 KB
/
netjsonconfig
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python
import argparse
import os
import sys
import six
import netjsonconfig
description = """
Converts a NetJSON DeviceConfiguration object to native router configurations.
Exhaustive documentation is available at: http://netjsonconfig.openwisp.org/
"""
license = """
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
parser = argparse.ArgumentParser(description=description,
epilog=license,
prog='netjsonconfig')
config = parser.add_argument_group('input')
config.add_argument('--config', '-c',
action='store',
type=str,
default=None,
help='config file or string, must be valid NetJSON DeviceConfiguration')
config.add_argument('--templates', '-t',
nargs='*', # zero or more
action='store',
type=str,
default=[],
help='list of template config files or strings separated by space')
config.add_argument('--native', '-n',
action='store',
type=str,
default=None,
help='path to native configuration file or archive')
output = parser.add_argument_group('output')
output.add_argument('--backend', '-b',
required=True,
choices=netjsonconfig.get_backends().keys(),
action='store',
type=str,
help='Configuration backend')
output.add_argument('--method', '-m',
required=True,
choices=['render', 'generate', 'write', 'validate', 'json'],
action='store',
help='Backend method to use. '
'"render" returns the configuration in text format; '
'"generate" returns a tar.gz archive as output; '
'"write" is like generate but writes to disk; '
'"validate" validates the combination of config '
'and templates passed in input; '
'"json" returns NetJSON output; ')
output.add_argument('--args', '-a',
nargs='*', # zero or more
action='store',
type=str,
default=[],
help='Optional arguments that can be passed to methods')
debug = parser.add_argument_group('debug')
debug.add_argument('--verbose',
action='store_true',
default=False,
help='verbose output')
debug.add_argument('--version', '-v',
action='version',
version=netjsonconfig.get_version())
def _load(config, read=True):
"""
if config argument does not look like a JSON string
try to read the contents of a file
"""
if not config.strip().startswith('{'):
try:
f = open(config, 'r')
return f.read() if read else f
except IOError:
print('netjsonconfig: cannot open "{0}": '
'file not found'.format(config))
sys.exit(1)
else:
return config.strip()
def parse_method_arguments(arguments):
"""
ensures arguments format is correct
"""
kwargs = {}
for method_arg in arguments:
if method_arg.count('=') != 1:
message = '--arg option expects arguments in the following format: '\
'arg1=val1 arg2=val2'
print('netjsonconfig: {0}'.format(message))
sys.exit(3)
key, val = method_arg.split('=')
kwargs[key] = recognize_method_argument(val)
return kwargs
def recognize_method_argument(arg_string):
"""
allows to recognize booleans
"""
if arg_string in ['True', '1']:
return True
elif arg_string in ['False', '0']:
return False
return arg_string
def print_output(output):
"""
prints result to standard output
"""
# if file object, get bytes
if hasattr(output, 'getvalue'):
output = output.getvalue()
# python2: always print
# python3: if string, just print
if isinstance(output, six.string_types):
print(output)
# python2: never enters this block
# python3: writes binary data to standard output
else:
sys.stdout.buffer.write(output)
args = parser.parse_args()
if args.config:
config = _load(args.config)
elif args.native:
native = _load(args.native, read=False)
else:
print('Expected one of the following parameters: "config" or "native"; none found')
sys.exit(1)
templates = [_load(template) for template in args.templates]
context = dict(os.environ)
method = args.method
method_arguments = parse_method_arguments(args.args)
backend_class = netjsonconfig.get_backends()[args.backend]
try:
options = dict(templates=templates, context=context)
if args.config:
options['config'] = config
else:
options['native'] = native
instance = backend_class(**options)
except TypeError as e:
print('netjsonconfig: invalid JSON passed in config or templates')
sys.exit(2)
try:
output = getattr(instance, method)(**method_arguments)
if output:
print_output(output)
except netjsonconfig.exceptions.ValidationError as e:
message = 'netjsonconfig: JSON Schema violation\n'
if not args.verbose:
info = 'For more information repeat the command using --verbose'
else:
info = str(e)
print(message + info)
sys.exit(4)
except TypeError as e:
print('netjsonconfig: {0}'.format(e))
sys.exit(5)