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 | |
| 27 | base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 28 | sys.path.append(base_dir) |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 29 | sys.path.append(os.path.join(base_dir, '/chameleon/protos/third_party')) |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 30 | |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 31 | from chameleon.grpc_client.grpc_client import GrpcClient |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 32 | from chameleon.web_server.web_server import WebServer |
Zsolt Haraszti | abae591 | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 33 | from common.utils.dockerhelpers import get_my_containers_name |
| 34 | from common.utils.nethelpers import get_my_primary_local_ipv4 |
Zsolt Haraszti | 770cc2a | 2016-11-03 23:23:36 -0700 | [diff] [blame] | 35 | from common.structlog_setup import setup_logging |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 36 | |
| 37 | |
| 38 | defs = dict( |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 39 | config=os.environ.get('CONFIG', './chameleon.yml'), |
Zsolt Haraszti | 5807432 | 2016-09-27 10:24:27 -0700 | [diff] [blame] | 40 | consul=os.environ.get('CONSUL', 'localhost:8500'), |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 41 | external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS', |
| 42 | get_my_primary_local_ipv4()), |
Zsolt Haraszti | 5807432 | 2016-09-27 10:24:27 -0700 | [diff] [blame] | 43 | grpc_endpoint=os.environ.get('GRPC_ENDPOINT', 'localhost:50055'), |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 44 | fluentd=os.environ.get('FLUENTD', None), |
Zsolt Haraszti | 5807432 | 2016-09-27 10:24:27 -0700 | [diff] [blame] | 45 | instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')), |
| 46 | internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS', |
| 47 | get_my_primary_local_ipv4()), |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 48 | rest_port=os.environ.get('REST_PORT', 8881), |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 49 | work_dir=os.environ.get('WORK_DIR', '/tmp/chameleon') |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 50 | ) |
| 51 | |
| 52 | |
| 53 | def parse_args(): |
| 54 | |
| 55 | parser = argparse.ArgumentParser() |
| 56 | |
| 57 | _help = ('Path to chameleon.yml config file (default: %s). ' |
| 58 | 'If relative, it is relative to main.py of chameleon.' |
| 59 | % defs['config']) |
| 60 | parser.add_argument('-c', '--config', |
| 61 | dest='config', |
| 62 | action='store', |
| 63 | default=defs['config'], |
| 64 | help=_help) |
| 65 | |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 66 | _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul'] |
| 67 | parser.add_argument( |
| 68 | '-C', '--consul', dest='consul', action='store', |
| 69 | default=defs['consul'], |
| 70 | help=_help) |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 71 | |
| 72 | _help = ('<hostname> or <ip> at which Chameleon is reachable from outside ' |
| 73 | 'the cluster (default: %s)' % defs['external_host_address']) |
| 74 | parser.add_argument('-E', '--external-host-address', |
| 75 | dest='external_host_address', |
| 76 | action='store', |
| 77 | default=defs['external_host_address'], |
| 78 | help=_help) |
| 79 | |
| 80 | _help = ('<hostname>:<port> to fluentd server (default: %s). (If not ' |
| 81 | 'specified (None), the address from the config file is used' |
| 82 | % defs['fluentd']) |
| 83 | parser.add_argument('-F', '--fluentd', |
| 84 | dest='fluentd', |
| 85 | action='store', |
| 86 | default=defs['fluentd'], |
| 87 | help=_help) |
| 88 | |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 89 | _help = ('gRPC end-point to connect to. It can either be a direct' |
| 90 | 'definition in the form of <hostname>:<port>, or it can be an' |
| 91 | 'indirect definition in the form of @<service-name> where' |
| 92 | '<service-name> is the name of the grpc service as registered' |
| 93 | 'in consul (example: @voltha-grpc). (default: %s' |
| 94 | % defs['grpc_endpoint']) |
| 95 | parser.add_argument('-G', '--grpc-endpoint', |
| 96 | dest='grpc_endpoint', |
| 97 | action='store', |
| 98 | default=defs['grpc_endpoint'], |
| 99 | help=_help) |
| 100 | |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 101 | _help = ('<hostname> or <ip> at which Chameleon is reachable from inside' |
| 102 | 'the cluster (default: %s)' % defs['internal_host_address']) |
| 103 | parser.add_argument('-H', '--internal-host-address', |
| 104 | dest='internal_host_address', |
| 105 | action='store', |
| 106 | default=defs['internal_host_address'], |
| 107 | help=_help) |
| 108 | |
| 109 | _help = ('unique string id of this Chameleon instance (default: %s)' |
| 110 | % defs['instance_id']) |
| 111 | parser.add_argument('-i', '--instance-id', |
| 112 | dest='instance_id', |
| 113 | action='store', |
| 114 | default=defs['instance_id'], |
| 115 | help=_help) |
| 116 | |
| 117 | _help = 'omit startup banner log lines' |
| 118 | parser.add_argument('-n', '--no-banner', |
| 119 | dest='no_banner', |
| 120 | action='store_true', |
| 121 | default=False, |
| 122 | help=_help) |
| 123 | |
| 124 | _help = ('port number for the rest service (default: %d)' |
| 125 | % defs['rest_port']) |
| 126 | parser.add_argument('-R', '--rest-port', |
| 127 | dest='rest_port', |
| 128 | action='store', |
| 129 | type=int, |
| 130 | default=defs['rest_port'], |
| 131 | help=_help) |
| 132 | |
| 133 | _help = "suppress debug and info logs" |
| 134 | parser.add_argument('-q', '--quiet', |
| 135 | dest='quiet', |
| 136 | action='count', |
| 137 | help=_help) |
| 138 | |
| 139 | _help = 'enable verbose logging' |
| 140 | parser.add_argument('-v', '--verbose', |
| 141 | dest='verbose', |
| 142 | action='count', |
| 143 | help=_help) |
| 144 | |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 145 | _help = ('work dir to compile and assemble generated files (default=%s)' |
| 146 | % defs['work_dir']) |
| 147 | parser.add_argument('-w', '--work-dir', |
| 148 | dest='work_dir', |
| 149 | action='store', |
| 150 | default=defs['work_dir'], |
| 151 | help=_help) |
| 152 | |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 153 | _help = ('use docker container name as Chameleon instance id' |
| 154 | ' (overrides -i/--instance-id option)') |
| 155 | parser.add_argument('--instance-id-is-container-name', |
| 156 | dest='instance_id_is_container_name', |
| 157 | action='store_true', |
| 158 | default=False, |
| 159 | help=_help) |
| 160 | |
| 161 | args = parser.parse_args() |
| 162 | |
| 163 | # post-processing |
| 164 | |
| 165 | if args.instance_id_is_container_name: |
| 166 | args.instance_id = get_my_containers_name() |
| 167 | |
| 168 | return args |
| 169 | |
| 170 | |
| 171 | def load_config(args): |
| 172 | path = args.config |
| 173 | if path.startswith('.'): |
| 174 | dir = os.path.dirname(os.path.abspath(__file__)) |
| 175 | path = os.path.join(dir, path) |
| 176 | path = os.path.abspath(path) |
| 177 | with open(path) as fd: |
| 178 | config = yaml.load(fd) |
| 179 | return config |
| 180 | |
| 181 | banner = r''' |
| 182 | ____ _ _ |
| 183 | / ___| | |__ __ _ _ __ ___ ___ | | ___ ___ _ __ |
| 184 | | | | '_ \ / _` | | '_ ` _ \ / _ \ | | / _ \ / _ \ | '_ \ |
| 185 | | |___ | | | | | (_| | | | | | | | | __/ | | | __/ | (_) | | | | | |
| 186 | \____| |_| |_| \__,_| |_| |_| |_| \___| |_| \___| \___/ |_| |_| |
| 187 | |
| 188 | ''' |
Zsolt Haraszti | 1189c30 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 189 | |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 190 | def print_banner(log): |
| 191 | for line in banner.strip('\n').splitlines(): |
| 192 | log.info(line) |
| 193 | log.info('(to stop: press Ctrl-C)') |
| 194 | |
| 195 | |
| 196 | class Main(object): |
| 197 | |
| 198 | def __init__(self): |
| 199 | |
| 200 | self.args = args = parse_args() |
| 201 | self.config = load_config(args) |
| 202 | |
| 203 | verbosity_adjust = (args.verbose or 0) - (args.quiet or 0) |
| 204 | self.log = setup_logging(self.config.get('logging', {}), |
| 205 | args.instance_id, |
| 206 | verbosity_adjust=verbosity_adjust, |
| 207 | fluentd=args.fluentd) |
| 208 | |
| 209 | # components |
| 210 | self.rest_server = None |
| 211 | self.grpc_client = None |
| 212 | |
| 213 | if not args.no_banner: |
| 214 | print_banner(self.log) |
| 215 | |
| 216 | self.startup_components() |
| 217 | |
| 218 | def start(self): |
| 219 | self.start_reactor() # will not return except Keyboard interrupt |
| 220 | |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 221 | @inlineCallbacks |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 222 | def startup_components(self): |
Zsolt Haraszti | 267a906 | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 223 | try: |
| 224 | self.log.info('starting-internal-components') |
| 225 | args = self.args |
| 226 | self.grpc_client = yield \ |
| 227 | GrpcClient(args.consul, args.work_dir, args.grpc_endpoint) |
| 228 | self.rest_server = yield \ |
| 229 | WebServer(args.rest_port, args.work_dir, self.grpc_client).start() |
| 230 | self.grpc_client.set_reconnect_callback( |
| 231 | self.rest_server.reload_generated_routes).start() |
| 232 | self.log.info('started-internal-services') |
| 233 | except Exception, e: |
| 234 | self.log.exception('startup-failed', e=e) |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 235 | |
| 236 | @inlineCallbacks |
| 237 | def shutdown_components(self): |
| 238 | """Execute before the reactor is shut down""" |
| 239 | self.log.info('exiting-on-keyboard-interrupt') |
| 240 | if self.rest_server is not None: |
Zsolt Haraszti | dca6fa1 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 241 | yield self.rest_server.stop() |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 242 | if self.grpc_client is not None: |
Zsolt Haraszti | dca6fa1 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 243 | yield self.grpc_client.stop() |
Zsolt Haraszti | d7ae9d5 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 244 | |
| 245 | def start_reactor(self): |
| 246 | from twisted.internet import reactor |
| 247 | reactor.callWhenRunning( |
| 248 | lambda: self.log.info('twisted-reactor-started')) |
| 249 | reactor.addSystemEventTrigger('before', 'shutdown', |
| 250 | self.shutdown_components) |
| 251 | reactor.run() |
| 252 | |
| 253 | |
| 254 | if __name__ == '__main__': |
| 255 | Main().start() |