blob: c1e531b3a6ccadcef3fecbe4e7a19aa71e37791d [file] [log] [blame]
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -07001#!/usr/bin/env python
2#
Zsolt Haraszti3eb27a52017-01-03 21:56:48 -08003# Copyright 2017 the original author or authors.
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -07004#
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#
Khen Nursimulu68b9be32016-10-25 11:57:04 -040017import argparse
Zsolt Haraszti8a774382016-10-24 18:25:54 -070018import os
Zsolt Harasztid70cd4d2016-11-03 23:23:36 -070019
Zsolt Haraszti8a774382016-10-24 18:25:54 -070020import yaml
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070021from twisted.internet import reactor
Khen Nursimulu68b9be32016-10-25 11:57:04 -040022from twisted.internet.defer import inlineCallbacks
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070023
Zsolt Harasztid70cd4d2016-11-03 23:23:36 -070024from common.structlog_setup import setup_logging
Khen Nursimulu68b9be32016-10-25 11:57:04 -040025from common.utils.dockerhelpers import get_my_containers_name
26from common.utils.nethelpers import get_my_primary_local_ipv4
Khen Nursimulu68b9be32016-10-25 11:57:04 -040027from connection_mgr import ConnectionManager
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070028
Khen Nursimulu68b9be32016-10-25 11:57:04 -040029defs = dict(
30 config=os.environ.get('CONFIG', './ofagent.yml'),
31 consul=os.environ.get('CONSUL', 'localhost:8500'),
32 controller=os.environ.get('CONTROLLER', 'localhost:6633'),
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'),
36 fluentd=os.environ.get('FLUENTD', None),
37 instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
38 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS',
39 get_my_primary_local_ipv4()),
40 work_dir=os.environ.get('WORK_DIR', '/tmp/ofagent')
41)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070042
43
Khen Nursimulu68b9be32016-10-25 11:57:04 -040044def parse_args():
45
46 parser = argparse.ArgumentParser()
47
48 _help = ('Path to ofagent.yml config file (default: %s). '
49 'If relative, it is relative to main.py of ofagent.'
50 % defs['config'])
51 parser.add_argument('-c', '--config',
52 dest='config',
53 action='store',
54 default=defs['config'],
55 help=_help)
56
57 _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul']
58 parser.add_argument(
59 '-C', '--consul', dest='consul', action='store',
60 default=defs['consul'],
61 help=_help)
62
63 _help = '<hostname>:<port> to openflow controller (default: %s)' % \
64 defs['controller']
65 parser.add_argument(
66 '-O', '--controller', dest='controller', action='store',
67 default=defs['controller'],
68 help=_help)
69
70 _help = ('<hostname> or <ip> at which ofagent is reachable from outside '
71 'the cluster (default: %s)' % defs['external_host_address'])
72 parser.add_argument('-E', '--external-host-address',
73 dest='external_host_address',
74 action='store',
75 default=defs['external_host_address'],
76 help=_help)
77
78 _help = ('<hostname>:<port> to fluentd server (default: %s). (If not '
79 'specified (None), the address from the config file is used'
80 % defs['fluentd'])
81 parser.add_argument('-F', '--fluentd',
82 dest='fluentd',
83 action='store',
84 default=defs['fluentd'],
85 help=_help)
86
87 _help = ('gRPC end-point to connect to. It can either be a direct'
88 'definition in the form of <hostname>:<port>, or it can be an'
89 'indirect definition in the form of @<service-name> where'
90 '<service-name> is the name of the grpc service as registered'
91 'in consul (example: @voltha-grpc). (default: %s'
92 % defs['grpc_endpoint'])
93 parser.add_argument('-G', '--grpc-endpoint',
94 dest='grpc_endpoint',
95 action='store',
96 default=defs['grpc_endpoint'],
97 help=_help)
98
99 _help = ('<hostname> or <ip> at which ofagent is reachable from inside'
100 'the cluster (default: %s)' % defs['internal_host_address'])
101 parser.add_argument('-H', '--internal-host-address',
102 dest='internal_host_address',
103 action='store',
104 default=defs['internal_host_address'],
105 help=_help)
106
107 _help = ('unique string id of this ofagent instance (default: %s)'
108 % defs['instance_id'])
109 parser.add_argument('-i', '--instance-id',
110 dest='instance_id',
111 action='store',
112 default=defs['instance_id'],
113 help=_help)
114
115 _help = 'omit startup banner log lines'
116 parser.add_argument('-n', '--no-banner',
117 dest='no_banner',
118 action='store_true',
119 default=False,
120 help=_help)
121
122 _help = "suppress debug and info logs"
123 parser.add_argument('-q', '--quiet',
124 dest='quiet',
125 action='count',
126 help=_help)
127
128 _help = 'enable verbose logging'
129 parser.add_argument('-v', '--verbose',
130 dest='verbose',
131 action='count',
132 help=_help)
133
134 _help = ('work dir to compile and assemble generated files (default=%s)'
135 % defs['work_dir'])
136 parser.add_argument('-w', '--work-dir',
137 dest='work_dir',
138 action='store',
139 default=defs['work_dir'],
140 help=_help)
141
142 _help = ('use docker container name as ofagent instance id'
143 ' (overrides -i/--instance-id option)')
144 parser.add_argument('--instance-id-is-container-name',
145 dest='instance_id_is_container_name',
146 action='store_true',
147 default=False,
148 help=_help)
149
150 args = parser.parse_args()
151
152 # post-processing
153
154 if args.instance_id_is_container_name:
155 args.instance_id = get_my_containers_name()
156
157 return args
158
159
160def load_config(args):
161 path = args.config
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700162 if path.startswith('.'):
163 dir = os.path.dirname(os.path.abspath(__file__))
164 path = os.path.join(dir, path)
165 path = os.path.abspath(path)
166 with open(path) as fd:
167 config = yaml.load(fd)
168 return config
169
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400170banner = r'''
171 ___ _____ _ _
172 / _ \| ___/ \ __ _ ___ _ __ | |_
173| | | | |_ / _ \ / _` |/ _ \ '_ \| __|
174| |_| | _/ ___ \ (_| | __/ | | | |_
175 \___/|_|/_/ \_\__, |\___|_| |_|\__|
176 |___/
177'''
178
179def print_banner(log):
180 for line in banner.strip('\n').splitlines():
181 log.info(line)
182 log.info('(to stop: press Ctrl-C)')
183
184
185class Main(object):
186
187 def __init__(self):
188
189 self.args = args = parse_args()
190 self.config = load_config(args)
191
192 verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
193 self.log = setup_logging(self.config.get('logging', {}),
194 args.instance_id,
195 verbosity_adjust=verbosity_adjust,
196 fluentd=args.fluentd)
197
198 # components
199 self.connection_manager = None
200
201 self.exiting = False
202
203 if not args.no_banner:
204 print_banner(self.log)
205
206 self.startup_components()
207
208 def start(self):
209 self.start_reactor() # will not return except Keyboard interrupt
210
211 @inlineCallbacks
212 def startup_components(self):
213 self.log.info('starting-internal-components')
214 args = self.args
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700215 self.connection_manager = yield ConnectionManager(
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -0700216 args.consul, args.grpc_endpoint, args.controller).start()
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400217 self.log.info('started-internal-services')
218
219 @inlineCallbacks
220 def shutdown_components(self):
221 """Execute before the reactor is shut down"""
222 self.log.info('exiting-on-keyboard-interrupt')
223 self.exiting = True
224 if self.connection_manager is not None:
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -0700225 yield self.connection_manager.stop()
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400226
227 def start_reactor(self):
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400228 reactor.callWhenRunning(
229 lambda: self.log.info('twisted-reactor-started'))
230
231 reactor.addSystemEventTrigger('before', 'shutdown',
232 self.shutdown_components)
233 reactor.run()
234
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700235
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700236if __name__ == '__main__':
Khen Nursimulu68b9be32016-10-25 11:57:04 -0400237 Main().start()