Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
Zsolt Haraszti | accad4a | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 3 | # Copyright 2017 the original author or authors. |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -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 | # |
| 17 | |
| 18 | """A REST protocol gateway to self-describing GRPC end-points""" |
| 19 | |
| 20 | import argparse |
| 21 | import os |
| 22 | import sys |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 23 | |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 24 | import yaml |
| 25 | from twisted.internet.defer import inlineCallbacks |
| 26 | |
alshabib | 6ca0562 | 2017-01-31 14:08:36 -0800 | [diff] [blame] | 27 | from chameleon.utils.dockerhelpers import get_my_containers_name |
| 28 | from chameleon.utils.nethelpers import get_my_primary_local_ipv4 |
| 29 | from chameleon.utils.structlog_setup import setup_logging |
| 30 | |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 31 | base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 32 | sys.path.append(base_dir) |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 33 | sys.path.append(os.path.join(base_dir, '/chameleon/protos/third_party')) |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 34 | |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 35 | from chameleon.grpc_client.grpc_client import GrpcClient |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 36 | from chameleon.web_server.web_server import WebServer |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 37 | |
| 38 | |
| 39 | defs = dict( |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 40 | config=os.environ.get('CONFIG', './chameleon.yml'), |
Zsolt Haraszti | 5807432 | 2016-09-27 10:24:27 -0700 | [diff] [blame] | 41 | consul=os.environ.get('CONSUL', 'localhost:8500'), |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 42 | external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS', |
| 43 | get_my_primary_local_ipv4()), |
Zsolt Haraszti | 5807432 | 2016-09-27 10:24:27 -0700 | [diff] [blame] | 44 | grpc_endpoint=os.environ.get('GRPC_ENDPOINT', 'localhost:50055'), |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 45 | fluentd=os.environ.get('FLUENTD', None), |
Zsolt Haraszti | 5807432 | 2016-09-27 10:24:27 -0700 | [diff] [blame] | 46 | instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')), |
| 47 | internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS', |
| 48 | get_my_primary_local_ipv4()), |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 49 | rest_port=os.environ.get('REST_PORT', 8881), |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 50 | work_dir=os.environ.get('WORK_DIR', '/tmp/chameleon') |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 51 | ) |
| 52 | |
| 53 | |
| 54 | def parse_args(): |
| 55 | |
| 56 | parser = argparse.ArgumentParser() |
| 57 | |
| 58 | _help = ('Path to chameleon.yml config file (default: %s). ' |
| 59 | 'If relative, it is relative to main.py of chameleon.' |
| 60 | % defs['config']) |
| 61 | parser.add_argument('-c', '--config', |
| 62 | dest='config', |
| 63 | action='store', |
| 64 | default=defs['config'], |
| 65 | help=_help) |
| 66 | |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 67 | _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul'] |
| 68 | parser.add_argument( |
| 69 | '-C', '--consul', dest='consul', action='store', |
| 70 | default=defs['consul'], |
| 71 | help=_help) |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 72 | |
| 73 | _help = ('<hostname> or <ip> at which Chameleon 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 | |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 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 | |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 102 | _help = ('<hostname> or <ip> at which Chameleon 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 Chameleon 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 = ('port number for the rest service (default: %d)' |
| 126 | % defs['rest_port']) |
| 127 | parser.add_argument('-R', '--rest-port', |
| 128 | dest='rest_port', |
| 129 | action='store', |
| 130 | type=int, |
| 131 | default=defs['rest_port'], |
| 132 | help=_help) |
| 133 | |
| 134 | _help = "suppress debug and info logs" |
| 135 | parser.add_argument('-q', '--quiet', |
| 136 | dest='quiet', |
| 137 | action='count', |
| 138 | help=_help) |
| 139 | |
| 140 | _help = 'enable verbose logging' |
| 141 | parser.add_argument('-v', '--verbose', |
| 142 | dest='verbose', |
| 143 | action='count', |
| 144 | help=_help) |
| 145 | |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 146 | _help = ('work dir to compile and assemble generated files (default=%s)' |
| 147 | % defs['work_dir']) |
| 148 | parser.add_argument('-w', '--work-dir', |
| 149 | dest='work_dir', |
| 150 | action='store', |
| 151 | default=defs['work_dir'], |
| 152 | help=_help) |
| 153 | |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 154 | _help = ('use docker container name as Chameleon instance id' |
| 155 | ' (overrides -i/--instance-id option)') |
| 156 | parser.add_argument('--instance-id-is-container-name', |
| 157 | dest='instance_id_is_container_name', |
| 158 | action='store_true', |
| 159 | default=False, |
| 160 | help=_help) |
| 161 | |
| 162 | args = parser.parse_args() |
| 163 | |
| 164 | # post-processing |
| 165 | |
| 166 | if args.instance_id_is_container_name: |
| 167 | args.instance_id = get_my_containers_name() |
| 168 | |
| 169 | return args |
| 170 | |
| 171 | |
| 172 | def load_config(args): |
| 173 | path = args.config |
| 174 | if path.startswith('.'): |
| 175 | dir = os.path.dirname(os.path.abspath(__file__)) |
| 176 | path = os.path.join(dir, path) |
| 177 | path = os.path.abspath(path) |
| 178 | with open(path) as fd: |
| 179 | config = yaml.load(fd) |
| 180 | return config |
| 181 | |
| 182 | banner = r''' |
| 183 | ____ _ _ |
| 184 | / ___| | |__ __ _ _ __ ___ ___ | | ___ ___ _ __ |
| 185 | | | | '_ \ / _` | | '_ ` _ \ / _ \ | | / _ \ / _ \ | '_ \ |
| 186 | | |___ | | | | | (_| | | | | | | | | __/ | | | __/ | (_) | | | | | |
| 187 | \____| |_| |_| \__,_| |_| |_| |_| \___| |_| \___| \___/ |_| |_| |
| 188 | |
| 189 | ''' |
Zsolt Haraszti | 1189c30 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 190 | |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 191 | def print_banner(log): |
| 192 | for line in banner.strip('\n').splitlines(): |
| 193 | log.info(line) |
| 194 | log.info('(to stop: press Ctrl-C)') |
| 195 | |
| 196 | |
| 197 | class Main(object): |
| 198 | |
| 199 | def __init__(self): |
| 200 | |
| 201 | self.args = args = parse_args() |
| 202 | self.config = load_config(args) |
| 203 | |
| 204 | verbosity_adjust = (args.verbose or 0) - (args.quiet or 0) |
| 205 | self.log = setup_logging(self.config.get('logging', {}), |
| 206 | args.instance_id, |
| 207 | verbosity_adjust=verbosity_adjust, |
| 208 | fluentd=args.fluentd) |
| 209 | |
| 210 | # components |
| 211 | self.rest_server = None |
| 212 | self.grpc_client = None |
| 213 | |
| 214 | if not args.no_banner: |
| 215 | print_banner(self.log) |
| 216 | |
| 217 | self.startup_components() |
| 218 | |
| 219 | def start(self): |
| 220 | self.start_reactor() # will not return except Keyboard interrupt |
| 221 | |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 222 | @inlineCallbacks |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 223 | def startup_components(self): |
Zsolt Haraszti | 267a906 | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 224 | try: |
| 225 | self.log.info('starting-internal-components') |
| 226 | args = self.args |
| 227 | self.grpc_client = yield \ |
| 228 | GrpcClient(args.consul, args.work_dir, args.grpc_endpoint) |
| 229 | self.rest_server = yield \ |
| 230 | WebServer(args.rest_port, args.work_dir, self.grpc_client).start() |
| 231 | self.grpc_client.set_reconnect_callback( |
| 232 | self.rest_server.reload_generated_routes).start() |
| 233 | self.log.info('started-internal-services') |
| 234 | except Exception, e: |
| 235 | self.log.exception('startup-failed', e=e) |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 236 | |
| 237 | @inlineCallbacks |
| 238 | def shutdown_components(self): |
| 239 | """Execute before the reactor is shut down""" |
| 240 | self.log.info('exiting-on-keyboard-interrupt') |
| 241 | if self.rest_server is not None: |
Zsolt Haraszti | dca6fa1 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 242 | yield self.rest_server.stop() |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 243 | if self.grpc_client is not None: |
Zsolt Haraszti | dca6fa1 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 244 | yield self.grpc_client.stop() |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 245 | |
| 246 | def start_reactor(self): |
| 247 | from twisted.internet import reactor |
| 248 | reactor.callWhenRunning( |
| 249 | lambda: self.log.info('twisted-reactor-started')) |
| 250 | reactor.addSystemEventTrigger('before', 'shutdown', |
| 251 | self.shutdown_components) |
| 252 | reactor.run() |
| 253 | |
| 254 | |
| 255 | if __name__ == '__main__': |
| 256 | Main().start() |