1
|
|
|
"""Here you can control the config parameters to run Kytos controller. |
2
|
|
|
|
3
|
|
|
Basically you can use a config file (-c option) and use arguments on command |
4
|
|
|
line. If you specify a config file, then and option configured inside this file |
5
|
|
|
will be overridden by the option on command line. |
6
|
|
|
""" |
7
|
|
|
|
8
|
1 |
|
import json |
9
|
1 |
|
import os |
10
|
1 |
|
import warnings |
11
|
1 |
|
from argparse import ArgumentParser, RawDescriptionHelpFormatter |
12
|
1 |
|
from configparser import ConfigParser |
13
|
|
|
|
14
|
1 |
|
from kytos.core.metadata import __version__ |
15
|
|
|
|
16
|
1 |
|
BASE_ENV = os.environ.get('VIRTUAL_ENV', None) or '/' |
17
|
|
|
|
18
|
|
|
|
19
|
1 |
|
class KytosConfig(): |
20
|
|
|
"""Handle settings of Kytos.""" |
21
|
|
|
|
22
|
1 |
|
def __init__(self): |
23
|
|
|
"""Parse the command line. |
24
|
|
|
|
25
|
|
|
The contructor set defaults parameters that can be used by KytosConfig. |
26
|
|
|
""" |
27
|
1 |
|
self.options = {} |
28
|
1 |
|
conf_parser = ArgumentParser(add_help=False) |
29
|
|
|
|
30
|
1 |
|
conf_parser.add_argument("-c", "--conf", |
31
|
|
|
help="Specify a config file", |
32
|
|
|
metavar="FILE") |
33
|
|
|
|
34
|
1 |
|
parser = ArgumentParser(prog='kytosd', |
35
|
|
|
parents=[conf_parser], |
36
|
|
|
formatter_class=RawDescriptionHelpFormatter, |
37
|
|
|
description=__doc__) |
38
|
|
|
|
39
|
1 |
|
parser.add_argument('-v', '--version', |
40
|
|
|
action='version', |
41
|
|
|
version="kytosd %s" % __version__) |
42
|
|
|
|
43
|
1 |
|
parser.add_argument('-D', '--debug', |
44
|
|
|
action='store_true', |
45
|
|
|
help="Run in debug mode") |
46
|
|
|
|
47
|
1 |
|
parser.add_argument('-f', '--foreground', |
48
|
|
|
action='store_true', |
49
|
|
|
help="Run in foreground (ctrl+c to stop)") |
50
|
|
|
|
51
|
1 |
|
parser.add_argument('-l', '--listen', |
52
|
|
|
action='store', |
53
|
|
|
help="IP/Interface to be listened") |
54
|
|
|
|
55
|
1 |
|
parser.add_argument('-n', '--napps', |
56
|
|
|
action='store', |
57
|
|
|
help="Specify the napps directory") |
58
|
|
|
|
59
|
1 |
|
parser.add_argument('-P', '--port', |
60
|
|
|
action='store', |
61
|
|
|
help="Port to be listened") |
62
|
|
|
|
63
|
1 |
|
parser.add_argument('-p', '--pidfile', |
64
|
|
|
action='store', |
65
|
|
|
help="Specify the PID file to save.") |
66
|
|
|
|
67
|
1 |
|
parser.add_argument('-w', '--workdir', |
68
|
|
|
action='store', |
69
|
|
|
help="Specify the working directory") |
70
|
|
|
|
71
|
1 |
|
parser.add_argument('-s', '--protocol_name', |
72
|
|
|
action='store', |
73
|
|
|
help="Specify the southbound protocol") |
74
|
|
|
|
75
|
1 |
|
parser.add_argument('-E', '--enable_entities_by_default', |
76
|
|
|
action='store_true', |
77
|
|
|
help="Enable all new Entities by default.") |
78
|
|
|
|
79
|
1 |
|
parser.add_argument('-C', '--create_superuser', |
80
|
|
|
action='store_true', |
81
|
|
|
help="Create a kytos superuser.") |
82
|
|
|
|
83
|
1 |
|
self.conf_parser, self.parser = conf_parser, parser |
84
|
1 |
|
self.parse_args() |
85
|
|
|
|
86
|
1 |
|
def parse_args(self): |
87
|
|
|
"""Get the command line options and update kytos settings. |
88
|
|
|
|
89
|
|
|
When installed via pip, defaults values are: |
90
|
|
|
|
91
|
|
|
.. code-block:: python |
92
|
|
|
|
93
|
|
|
defaults = {'pidfile': '/var/run/kytos/kytosd.pid', |
94
|
|
|
'workdir': '/var/lib/kytos', |
95
|
|
|
'napps': '/var/lib/kytos/napps/', |
96
|
|
|
'conf': '/etc/kytos/kytos.conf', |
97
|
|
|
'logging': '/etc/kytos/logging.ini', |
98
|
|
|
'listen': '0.0.0.0', |
99
|
|
|
'port': 6653, |
100
|
|
|
'foreground': False, |
101
|
|
|
'protocol_name': '', |
102
|
|
|
'enable_entities_by_default': False, |
103
|
|
|
'debug': False} |
104
|
|
|
|
105
|
|
|
""" |
106
|
1 |
|
defaults = {'pidfile': os.path.join(BASE_ENV, |
107
|
|
|
'var/run/kytos/kytosd.pid'), |
108
|
|
|
'workdir': os.path.join(BASE_ENV, 'var/lib/kytos'), |
109
|
|
|
'napps': os.path.join(BASE_ENV, 'var/lib/kytos/napps/'), |
110
|
|
|
'napps_repositories': "['https://napps.kytos.io/repo/']", |
111
|
|
|
'installed_napps': os.path.join(BASE_ENV, |
112
|
|
|
'var/lib/kytos/napps/', |
113
|
|
|
'.installed'), |
114
|
|
|
'conf': os.path.join(BASE_ENV, 'etc/kytos/kytos.conf'), |
115
|
|
|
'logging': os.path.join(BASE_ENV, 'etc/kytos/logging.ini'), |
116
|
|
|
'listen': '0.0.0.0', |
117
|
|
|
'port': 6653, |
118
|
|
|
'foreground': False, |
119
|
|
|
'protocol_name': '', |
120
|
|
|
'enable_entities_by_default': False, |
121
|
|
|
'napps_pre_installed': [], |
122
|
|
|
'vlan_pool': {}, |
123
|
|
|
'debug': False} |
124
|
|
|
|
125
|
1 |
|
options, argv = self.conf_parser.parse_known_args() |
126
|
|
|
|
127
|
1 |
|
config = ConfigParser() |
128
|
1 |
|
result = config.read([options.conf or defaults.get('conf')]) |
129
|
|
|
|
130
|
1 |
|
if result: |
131
|
1 |
|
defaults.update(dict(config.items("daemon"))) |
132
|
|
|
else: |
133
|
|
|
print('There is no config file.') |
134
|
|
|
exit(-1) |
135
|
|
|
|
136
|
1 |
|
self.parser.set_defaults(**defaults) |
137
|
|
|
|
138
|
1 |
|
self.options['daemon'] = self._parse_options(argv) |
139
|
|
|
|
140
|
1 |
|
def _parse_options(self, argv): |
141
|
|
|
"""Create a Namespace using the given argv. |
142
|
|
|
|
143
|
|
|
Args: |
144
|
|
|
argv(dict): Python Dict used to create the namespace. |
145
|
|
|
|
146
|
|
|
Returns: |
147
|
|
|
options(Namespace): Namespace with the args given |
148
|
|
|
|
149
|
|
|
""" |
150
|
1 |
|
options, unknown = self.parser.parse_known_args(argv) |
151
|
1 |
|
if unknown: |
152
|
|
|
warnings.warn(f"Unknown arguments: {unknown}") |
153
|
1 |
|
options.napps_repositories = json.loads(options.napps_repositories) |
154
|
1 |
|
options.debug = True if options.debug in ['True', True] else False |
155
|
1 |
|
options.daemon = True if options.daemon in ['True', True] else False |
156
|
1 |
|
options.port = int(options.port) |
157
|
1 |
|
options.api_port = int(options.api_port) |
158
|
1 |
|
options.protocol_name = str(options.protocol_name) |
159
|
|
|
|
160
|
1 |
|
result = options.enable_entities_by_default in ['True', True] |
161
|
1 |
|
options.enable_entities_by_default = result |
162
|
|
|
|
163
|
1 |
|
if isinstance(options.napps_pre_installed, str): |
164
|
1 |
|
napps = options.napps_pre_installed |
165
|
1 |
|
options.napps_pre_installed = json.loads(napps) |
166
|
|
|
|
167
|
|
|
return options |
168
|
|
|
|