blob: e18e4800497fd7a938a491efcb13479e74ae6d77 [file] [log] [blame]
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001#!/usr/bin/env python
2#
3# Copyright 2017 the original author or authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17import argparse
18import os
19
20import yaml
21from twisted.internet import reactor
22from twisted.internet.defer import inlineCallbacks
23
William Kurkianfc0dcda2019-04-08 16:54:36 -040024from pyvoltha.common.structlog_setup import setup_logging
25from pyvoltha.common.utils.dockerhelpers import get_my_containers_name
26from pyvoltha.common.utils.nethelpers import get_my_primary_local_ipv4
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050027from connection_mgr import ConnectionManager
28
29defs = dict(
30 config=os.environ.get('CONFIG', './ofagent.yml'),
31 consul=os.environ.get('CONSUL', 'localhost:8500'),
32 controller=os.environ.get('CONTROLLER', 'localhost:6653'),
33 external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS',
34 get_my_primary_local_ipv4()),
35 grpc_endpoint=os.environ.get('GRPC_ENDPOINT', 'localhost:50055'),
Richard Jankowski46464e92019-03-05 11:53:55 -050036 grpc_timeout=os.environ.get('GRPC_TIMEOUT', '10'),
37 core_binding_key=os.environ.get('CORE_BINDING_KEY', 'voltha_backend_name'),
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050038 instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
39 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS',
40 get_my_primary_local_ipv4()),
41 work_dir=os.environ.get('WORK_DIR', '/tmp/ofagent'),
42 key_file=os.environ.get('KEY_FILE', '/ofagent/pki/voltha.key'),
43 cert_file=os.environ.get('CERT_FILE', '/ofagent/pki/voltha.crt')
44)
45
46
47def parse_args():
48
49 parser = argparse.ArgumentParser()
50
51 _help = ('Path to ofagent.yml config file (default: %s). '
52 'If relative, it is relative to main.py of ofagent.'
53 % defs['config'])
54 parser.add_argument('-c', '--config',
55 dest='config',
56 action='store',
57 default=defs['config'],
58 help=_help)
59
60 _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul']
61 parser.add_argument(
62 '-C', '--consul', dest='consul', action='store',
63 default=defs['consul'],
64 help=_help)
65
66 _help = '<hostname1>:<port1> <hostname2>:<port2> <hostname3>:<port3> ... <hostnamen>:<portn> to openflow controller (default: %s)' % \
67 defs['controller']
68 parser.add_argument(
69 '-O', '--controller',nargs = '*', dest='controller', action='store',
70 default=defs['controller'],
71 help=_help)
72
73 _help = ('<hostname> or <ip> at which ofagent is reachable from outside '
74 'the cluster (default: %s)' % defs['external_host_address'])
75 parser.add_argument('-E', '--external-host-address',
76 dest='external_host_address',
77 action='store',
78 default=defs['external_host_address'],
79 help=_help)
80
Richard Jankowski46464e92019-03-05 11:53:55 -050081 _help = ('gRPC end-point to connect to. It can either be a direct '
82 'definition in the form of <hostname>:<port>, or it can be an '
83 'indirect definition in the form of @<service-name> where '
84 '<service-name> is the name of the grpc service as registered '
85 'in consul (example: @voltha-grpc). (default: %s)'
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050086 % defs['grpc_endpoint'])
87 parser.add_argument('-G', '--grpc-endpoint',
88 dest='grpc_endpoint',
89 action='store',
90 default=defs['grpc_endpoint'],
91 help=_help)
92
Richard Jankowski46464e92019-03-05 11:53:55 -050093 _help = 'gRPC timeout in seconds (default: %s)' % defs['grpc_timeout']
94 parser.add_argument('-T', '--grpc-timeout',
95 dest='grpc_timeout',
96 action='store',
97 default=defs['grpc_timeout'],
98 help=_help)
99
100 _help = ('The name of the meta-key whose value is the rw-core group '
101 'to which the ofagent\'s gRPC client is bound. '
102 '(default: %s)' % defs['core_binding_key'])
103 parser.add_argument('-B', '--core-binding-key',
104 dest='core_binding_key',
105 action='store',
106 default=defs['core_binding_key'],
107 help=_help)
108
109 _help = ('<hostname> or <ip> at which ofagent is reachable from inside '
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500110 'the cluster (default: %s)' % defs['internal_host_address'])
111 parser.add_argument('-H', '--internal-host-address',
112 dest='internal_host_address',
113 action='store',
114 default=defs['internal_host_address'],
115 help=_help)
116
117 _help = ('unique string id of this ofagent instance (default: %s)'
118 % defs['instance_id'])
119 parser.add_argument('-i', '--instance-id',
120 dest='instance_id',
121 action='store',
122 default=defs['instance_id'],
123 help=_help)
124
125 _help = 'omit startup banner log lines'
126 parser.add_argument('-n', '--no-banner',
127 dest='no_banner',
128 action='store_true',
129 default=False,
130 help=_help)
131
132 _help = "suppress debug and info logs"
133 parser.add_argument('-q', '--quiet',
134 dest='quiet',
135 action='count',
136 help=_help)
137
138 _help = 'enable verbose logging'
139 parser.add_argument('-v', '--verbose',
140 dest='verbose',
141 action='count',
142 help=_help)
143
144 _help = ('work dir to compile and assemble generated files (default=%s)'
145 % defs['work_dir'])
146 parser.add_argument('-w', '--work-dir',
147 dest='work_dir',
148 action='store',
149 default=defs['work_dir'],
150 help=_help)
151
152 _help = ('use docker container name as ofagent instance id'
153 ' (overrides -i/--instance-id option)')
154 parser.add_argument('--instance-id-is-container-name',
155 dest='instance_id_is_container_name',
156 action='store_true',
157 default=False,
158 help=_help)
159
160 _help = ('Specify this option to enable TLS security between ofagent \
161 and onos.')
162 parser.add_argument('-t', '--enable-tls',
163 dest='enable_tls',
164 action='store_true',
165 help=_help)
166
167 _help = ('key file to be used for tls security (default=%s)'
168 % defs['key_file'])
169 parser.add_argument('-k', '--key-file',
170 dest='key_file',
171 action='store',
172 default=defs['key_file'],
173 help=_help)
174
175 _help = ('certificate file to be used for tls security (default=%s)'
176 % defs['cert_file'])
177 parser.add_argument('-r', '--cert-file',
178 dest='cert_file',
179 action='store',
180 default=defs['cert_file'],
181 help=_help)
182
183 args = parser.parse_args()
184
185 # post-processing
186
187 if args.instance_id_is_container_name:
188 args.instance_id = get_my_containers_name()
189
190 return args
191
192
193def load_config(args):
194 path = args.config
195 if path.startswith('.'):
196 dir = os.path.dirname(os.path.abspath(__file__))
197 path = os.path.join(dir, path)
198 path = os.path.abspath(path)
199 with open(path) as fd:
200 config = yaml.load(fd)
201 return config
202
203banner = r'''
204 ___ _____ _ _
205 / _ \| ___/ \ __ _ ___ _ __ | |_
206| | | | |_ / _ \ / _` |/ _ \ '_ \| __|
207| |_| | _/ ___ \ (_| | __/ | | | |_
208 \___/|_|/_/ \_\__, |\___|_| |_|\__|
209 |___/
210'''
211
212def print_banner(log):
213 for line in banner.strip('\n').splitlines():
214 log.info(line)
215 log.info('(to stop: press Ctrl-C)')
216
217
218class Main(object):
219
220 def __init__(self):
221
222 self.args = args = parse_args()
223 self.config = load_config(args)
Richard Jankowski46464e92019-03-05 11:53:55 -0500224 self.grpc_timeout = int(self.args.grpc_timeout)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500225
226 verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
227 self.log = setup_logging(self.config.get('logging', {}),
228 args.instance_id,
229 verbosity_adjust=verbosity_adjust)
230
231 # components
232 self.connection_manager = None
233
234 self.exiting = False
235
236 if not args.no_banner:
237 print_banner(self.log)
238
239 self.startup_components()
240
241 def start(self):
242 self.start_reactor() # will not return except Keyboard interrupt
243
244 @inlineCallbacks
245 def startup_components(self):
246 self.log.info('starting-internal-components')
247 args = self.args
248 self.connection_manager = yield ConnectionManager(
249 args.consul, args.grpc_endpoint, self.grpc_timeout,
Richard Jankowski46464e92019-03-05 11:53:55 -0500250 args.core_binding_key, args.controller, args.instance_id,
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500251 args.enable_tls, args.key_file, args.cert_file).start()
252 self.log.info('started-internal-services')
253
254 @inlineCallbacks
255 def shutdown_components(self):
256 """Execute before the reactor is shut down"""
257 self.log.info('exiting-on-keyboard-interrupt')
258 self.exiting = True
259 if self.connection_manager is not None:
260 yield self.connection_manager.stop()
261
262 def start_reactor(self):
263 reactor.callWhenRunning(
264 lambda: self.log.info('twisted-reactor-started'))
265
266 reactor.addSystemEventTrigger('before', 'shutdown',
267 self.shutdown_components)
268 reactor.run()
269
270
271if __name__ == '__main__':
272 Main().start()