Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 1 | #!/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 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 |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 19 | import sys |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 20 | import yaml |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 21 | from twisted.internet.defer import inlineCallbacks |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 22 | |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 23 | base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 24 | sys.path.append(base_dir) |
| 25 | sys.path.append(os.path.join(base_dir, '/ofagent/protos/third_party')) |
| 26 | |
| 27 | from common.utils.dockerhelpers import get_my_containers_name |
| 28 | from common.utils.nethelpers import get_my_primary_local_ipv4 |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 29 | from common.utils.structlog_setup import setup_logging |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 30 | from connection_mgr import ConnectionManager |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 31 | |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 32 | defs = dict( |
| 33 | config=os.environ.get('CONFIG', './ofagent.yml'), |
| 34 | consul=os.environ.get('CONSUL', 'localhost:8500'), |
| 35 | controller=os.environ.get('CONTROLLER', 'localhost:6633'), |
| 36 | external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS', |
| 37 | get_my_primary_local_ipv4()), |
| 38 | grpc_endpoint=os.environ.get('GRPC_ENDPOINT', 'localhost:50055'), |
| 39 | fluentd=os.environ.get('FLUENTD', None), |
| 40 | instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')), |
| 41 | internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS', |
| 42 | get_my_primary_local_ipv4()), |
| 43 | work_dir=os.environ.get('WORK_DIR', '/tmp/ofagent') |
| 44 | ) |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 45 | |
| 46 | |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 47 | def 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 = '<hostname>:<port> to openflow controller (default: %s)' % \ |
| 67 | defs['controller'] |
| 68 | parser.add_argument( |
| 69 | '-O', '--controller', 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 | |
| 81 | _help = ('<hostname>:<port> to fluentd server (default: %s). (If not ' |
| 82 | 'specified (None), the address from the config file is used' |
| 83 | % defs['fluentd']) |
| 84 | parser.add_argument('-F', '--fluentd', |
| 85 | dest='fluentd', |
| 86 | action='store', |
| 87 | default=defs['fluentd'], |
| 88 | help=_help) |
| 89 | |
| 90 | _help = ('gRPC end-point to connect to. It can either be a direct' |
| 91 | 'definition in the form of <hostname>:<port>, or it can be an' |
| 92 | 'indirect definition in the form of @<service-name> where' |
| 93 | '<service-name> is the name of the grpc service as registered' |
| 94 | 'in consul (example: @voltha-grpc). (default: %s' |
| 95 | % defs['grpc_endpoint']) |
| 96 | parser.add_argument('-G', '--grpc-endpoint', |
| 97 | dest='grpc_endpoint', |
| 98 | action='store', |
| 99 | default=defs['grpc_endpoint'], |
| 100 | help=_help) |
| 101 | |
| 102 | _help = ('<hostname> or <ip> at which ofagent is reachable from inside' |
| 103 | 'the cluster (default: %s)' % defs['internal_host_address']) |
| 104 | parser.add_argument('-H', '--internal-host-address', |
| 105 | dest='internal_host_address', |
| 106 | action='store', |
| 107 | default=defs['internal_host_address'], |
| 108 | help=_help) |
| 109 | |
| 110 | _help = ('unique string id of this ofagent instance (default: %s)' |
| 111 | % defs['instance_id']) |
| 112 | parser.add_argument('-i', '--instance-id', |
| 113 | dest='instance_id', |
| 114 | action='store', |
| 115 | default=defs['instance_id'], |
| 116 | help=_help) |
| 117 | |
| 118 | _help = 'omit startup banner log lines' |
| 119 | parser.add_argument('-n', '--no-banner', |
| 120 | dest='no_banner', |
| 121 | action='store_true', |
| 122 | default=False, |
| 123 | help=_help) |
| 124 | |
| 125 | _help = "suppress debug and info logs" |
| 126 | parser.add_argument('-q', '--quiet', |
| 127 | dest='quiet', |
| 128 | action='count', |
| 129 | help=_help) |
| 130 | |
| 131 | _help = 'enable verbose logging' |
| 132 | parser.add_argument('-v', '--verbose', |
| 133 | dest='verbose', |
| 134 | action='count', |
| 135 | help=_help) |
| 136 | |
| 137 | _help = ('work dir to compile and assemble generated files (default=%s)' |
| 138 | % defs['work_dir']) |
| 139 | parser.add_argument('-w', '--work-dir', |
| 140 | dest='work_dir', |
| 141 | action='store', |
| 142 | default=defs['work_dir'], |
| 143 | help=_help) |
| 144 | |
| 145 | _help = ('use docker container name as ofagent instance id' |
| 146 | ' (overrides -i/--instance-id option)') |
| 147 | parser.add_argument('--instance-id-is-container-name', |
| 148 | dest='instance_id_is_container_name', |
| 149 | action='store_true', |
| 150 | default=False, |
| 151 | help=_help) |
| 152 | |
| 153 | args = parser.parse_args() |
| 154 | |
| 155 | # post-processing |
| 156 | |
| 157 | if args.instance_id_is_container_name: |
| 158 | args.instance_id = get_my_containers_name() |
| 159 | |
| 160 | return args |
| 161 | |
| 162 | |
| 163 | def load_config(args): |
| 164 | path = args.config |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 165 | if path.startswith('.'): |
| 166 | dir = os.path.dirname(os.path.abspath(__file__)) |
| 167 | path = os.path.join(dir, path) |
| 168 | path = os.path.abspath(path) |
| 169 | with open(path) as fd: |
| 170 | config = yaml.load(fd) |
| 171 | return config |
| 172 | |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 173 | banner = r''' |
| 174 | ___ _____ _ _ |
| 175 | / _ \| ___/ \ __ _ ___ _ __ | |_ |
| 176 | | | | | |_ / _ \ / _` |/ _ \ '_ \| __| |
| 177 | | |_| | _/ ___ \ (_| | __/ | | | |_ |
| 178 | \___/|_|/_/ \_\__, |\___|_| |_|\__| |
| 179 | |___/ |
| 180 | ''' |
| 181 | |
| 182 | def print_banner(log): |
| 183 | for line in banner.strip('\n').splitlines(): |
| 184 | log.info(line) |
| 185 | log.info('(to stop: press Ctrl-C)') |
| 186 | |
| 187 | |
| 188 | class Main(object): |
| 189 | |
| 190 | def __init__(self): |
| 191 | |
| 192 | self.args = args = parse_args() |
| 193 | self.config = load_config(args) |
| 194 | |
| 195 | verbosity_adjust = (args.verbose or 0) - (args.quiet or 0) |
| 196 | self.log = setup_logging(self.config.get('logging', {}), |
| 197 | args.instance_id, |
| 198 | verbosity_adjust=verbosity_adjust, |
| 199 | fluentd=args.fluentd) |
| 200 | |
| 201 | # components |
| 202 | self.connection_manager = None |
| 203 | |
| 204 | self.exiting = False |
| 205 | |
| 206 | if not args.no_banner: |
| 207 | print_banner(self.log) |
| 208 | |
| 209 | self.startup_components() |
| 210 | |
| 211 | def start(self): |
| 212 | self.start_reactor() # will not return except Keyboard interrupt |
| 213 | |
| 214 | @inlineCallbacks |
| 215 | def startup_components(self): |
| 216 | self.log.info('starting-internal-components') |
| 217 | args = self.args |
| 218 | self.connection_manager = yield ConnectionManager(args.consul, |
| 219 | args.grpc_endpoint, |
| 220 | args.controller).run() |
| 221 | self.log.info('started-internal-services') |
| 222 | |
| 223 | @inlineCallbacks |
| 224 | def shutdown_components(self): |
| 225 | """Execute before the reactor is shut down""" |
| 226 | self.log.info('exiting-on-keyboard-interrupt') |
| 227 | self.exiting = True |
| 228 | if self.connection_manager is not None: |
| 229 | yield self.connection_manager.shutdown() |
| 230 | |
| 231 | def start_reactor(self): |
| 232 | from twisted.internet import reactor |
| 233 | reactor.callWhenRunning( |
| 234 | lambda: self.log.info('twisted-reactor-started')) |
| 235 | |
| 236 | reactor.addSystemEventTrigger('before', 'shutdown', |
| 237 | self.shutdown_components) |
| 238 | reactor.run() |
| 239 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 240 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 241 | if __name__ == '__main__': |
Khen Nursimulu | 68b9be3 | 2016-10-25 11:57:04 -0400 | [diff] [blame] | 242 | Main().start() |