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