Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
Zsolt Haraszti | 3eb27a5 | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 3 | # Copyright 2017 the original author or authors. |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 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 Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 17 | import argparse |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 18 | import os |
Zsolt Haraszti | d70cd4d | 2016-11-03 23:23:36 -0700 | [diff] [blame] | 19 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 20 | import yaml |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 21 | from twisted.internet import reactor |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 22 | from twisted.internet.defer import inlineCallbacks |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 23 | |
Zsolt Haraszti | d70cd4d | 2016-11-03 23:23:36 -0700 | [diff] [blame] | 24 | from common.structlog_setup import setup_logging |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 25 | from common.utils.dockerhelpers import get_my_containers_name |
| 26 | from common.utils.nethelpers import get_my_primary_local_ipv4 |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 27 | from connection_mgr import ConnectionManager |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 28 | |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 29 | defs = dict( |
| 30 | config=os.environ.get('CONFIG', './ofagent.yml'), |
| 31 | consul=os.environ.get('CONSUL', 'localhost:8500'), |
Zsolt Haraszti | ee5c4c8 | 2017-01-09 14:37:57 -0800 | [diff] [blame] | 32 | controller=os.environ.get('CONTROLLER', 'localhost:6653'), |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 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 Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 42 | |
| 43 | |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 44 | def 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 | |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 63 | _help = '<hostname1>:<port1> <hostname2>:<port2> <hostname3>:<port3> ... <hostnamen>:<portn> to openflow controller (default: %s)' % \ |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 64 | defs['controller'] |
| 65 | parser.add_argument( |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 66 | '-O', '--controller',nargs = '*', dest='controller', action='store', |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 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 | |
| 160 | def load_config(args): |
| 161 | path = args.config |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 162 | 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 Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 170 | banner = r''' |
| 171 | ___ _____ _ _ |
| 172 | / _ \| ___/ \ __ _ ___ _ __ | |_ |
| 173 | | | | | |_ / _ \ / _` |/ _ \ '_ \| __| |
| 174 | | |_| | _/ ___ \ (_| | __/ | | | |_ |
| 175 | \___/|_|/_/ \_\__, |\___|_| |_|\__| |
| 176 | |___/ |
| 177 | ''' |
| 178 | |
| 179 | def 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 | |
| 185 | class 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 Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 215 | self.connection_manager = yield ConnectionManager( |
Zsolt Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 216 | args.consul, args.grpc_endpoint, args.controller).start() |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 217 | 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 Haraszti | 2bdb6b3 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 225 | yield self.connection_manager.stop() |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 226 | |
| 227 | def start_reactor(self): |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 228 | 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 Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 235 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 236 | if __name__ == '__main__': |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 237 | Main().start() |