blob: e75de77fcd1445129d7af6c060fe06b7191f1d83 [file] [log] [blame]
Zsolt Haraszti86be6f12016-09-27 09:56:49 -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#
17
18"""A REST protocol gateway to self-describing GRPC end-points"""
19
20import argparse
21import os
22import sys
23import time
24import yaml
25from twisted.internet.defer import inlineCallbacks
26
27base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
28sys.path.append(base_dir)
Zsolt Haraszti5cd64702016-09-27 13:48:35 -070029sys.path.append(os.path.join(base_dir, '/chameleon/protos/third_party'))
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070030
31from chameleon.structlog_setup import setup_logging
32from chameleon.nethelpers import get_my_primary_local_ipv4
33from chameleon.dockerhelpers import get_my_containers_name
Zsolt Haraszti5cd64702016-09-27 13:48:35 -070034from chameleon.grpc_client.grpc_client import GrpcClient
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070035
36
37defs = dict(
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070038 config=os.environ.get('CONFIG', './chameleon.yml'),
Zsolt Haraszti553826c2016-09-27 10:24:27 -070039 consul=os.environ.get('CONSUL', 'localhost:8500'),
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070040 external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS',
41 get_my_primary_local_ipv4()),
Zsolt Haraszti553826c2016-09-27 10:24:27 -070042 grpc_endpoint=os.environ.get('GRPC_ENDPOINT', 'localhost:50055'),
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070043 fluentd=os.environ.get('FLUENTD', None),
Zsolt Haraszti553826c2016-09-27 10:24:27 -070044 instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
45 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS',
46 get_my_primary_local_ipv4()),
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070047 rest_port=os.environ.get('REST_PORT', 8881),
48)
49
50
51def parse_args():
52
53 parser = argparse.ArgumentParser()
54
55 _help = ('Path to chameleon.yml config file (default: %s). '
56 'If relative, it is relative to main.py of chameleon.'
57 % defs['config'])
58 parser.add_argument('-c', '--config',
59 dest='config',
60 action='store',
61 default=defs['config'],
62 help=_help)
63
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070064 _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul']
65 parser.add_argument(
66 '-C', '--consul', dest='consul', action='store',
67 default=defs['consul'],
68 help=_help)
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070069
70 _help = ('<hostname> or <ip> at which Chameleon 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
Zsolt Haraszti5cd64702016-09-27 13:48:35 -070087 _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
Zsolt Haraszti86be6f12016-09-27 09:56:49 -070099 _help = ('<hostname> or <ip> at which Chameleon 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 Chameleon 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 = ('port number for the rest service (default: %d)'
123 % defs['rest_port'])
124 parser.add_argument('-R', '--rest-port',
125 dest='rest_port',
126 action='store',
127 type=int,
128 default=defs['rest_port'],
129 help=_help)
130
131 _help = "suppress debug and info logs"
132 parser.add_argument('-q', '--quiet',
133 dest='quiet',
134 action='count',
135 help=_help)
136
137 _help = 'enable verbose logging'
138 parser.add_argument('-v', '--verbose',
139 dest='verbose',
140 action='count',
141 help=_help)
142
143 _help = ('use docker container name as Chameleon instance id'
144 ' (overrides -i/--instance-id option)')
145 parser.add_argument('--instance-id-is-container-name',
146 dest='instance_id_is_container_name',
147 action='store_true',
148 default=False,
149 help=_help)
150
151 args = parser.parse_args()
152
153 # post-processing
154
155 if args.instance_id_is_container_name:
156 args.instance_id = get_my_containers_name()
157
158 return args
159
160
161def load_config(args):
162 path = args.config
163 if path.startswith('.'):
164 dir = os.path.dirname(os.path.abspath(__file__))
165 path = os.path.join(dir, path)
166 path = os.path.abspath(path)
167 with open(path) as fd:
168 config = yaml.load(fd)
169 return config
170
171banner = r'''
172 ____ _ _
173 / ___| | |__ __ _ _ __ ___ ___ | | ___ ___ _ __
174 | | | '_ \ / _` | | '_ ` _ \ / _ \ | | / _ \ / _ \ | '_ \
175 | |___ | | | | | (_| | | | | | | | | __/ | | | __/ | (_) | | | | |
176 \____| |_| |_| \__,_| |_| |_| |_| \___| |_| \___| \___/ |_| |_|
177
178'''
179def 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
185class 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.rest_server = None
200 self.grpc_client = None
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 def startup_components(self):
211 self.log.info('starting-internal-components')
212
Zsolt Haraszti5cd64702016-09-27 13:48:35 -0700213 self.grpc_client = \
214 GrpcClient(self.args.consul, self.args.grpc_endpoint).run()
215
Zsolt Haraszti86be6f12016-09-27 09:56:49 -0700216 # TODO init server
217
218 self.log.info('started-internal-services')
219
220 @inlineCallbacks
221 def shutdown_components(self):
222 """Execute before the reactor is shut down"""
223 self.log.info('exiting-on-keyboard-interrupt')
224 if self.rest_server is not None:
225 yield self.rest_server.shutdown()
226 if self.grpc_client is not None:
227 yield self.grpc_client.shutdown()
228
229 def start_reactor(self):
230 from twisted.internet import reactor
231 reactor.callWhenRunning(
232 lambda: self.log.info('twisted-reactor-started'))
233 reactor.addSystemEventTrigger('before', 'shutdown',
234 self.shutdown_components)
235 reactor.run()
236
237
238if __name__ == '__main__':
239 Main().start()