blob: 2426cf1c2a9f790dc18c2abae44788002bafadd2 [file] [log] [blame]
Zsolt Harasztib71c2a02016-09-12 13:12:07 -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
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -070018"""Virtual OLT Hardware Abstraction main entry point"""
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070019
20import argparse
Zsolt Harasztid7c7c482016-09-13 00:45:38 -070021import os
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070022import time
Zsolt Harasztid7c7c482016-09-13 00:45:38 -070023import yaml
Zsolt Harasztie060a7d2016-09-16 11:08:24 -070024from twisted.internet.defer import inlineCallbacks
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -070025
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070026from structlog_setup import setup_logging
27from coordinator import Coordinator
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070028from northbound.rest.health_check import init_rest_service
29from nethelpers import get_my_primary_interface, get_my_primary_local_ipv4
Zsolt Harasztie060a7d2016-09-16 11:08:24 -070030from dockerhelpers import get_my_containers_name
31
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070032
33defs = dict(
34 consul=os.environ.get('CONSUL', 'localhost:8500'),
Zsolt Haraszti28e0f1c2016-09-13 23:55:43 -070035 instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070036 config=os.environ.get('CONFIG', './voltha.yml'),
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070037 interface=os.environ.get('INTERFACE', get_my_primary_interface()),
Zsolt Haraszti109db832016-09-16 16:32:36 -070038 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS',
39 get_my_primary_local_ipv4()),
40 external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS',
41 get_my_primary_local_ipv4()),
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070042 fluentd=os.environ.get('FLUENTD', None),
43 rest_port=os.environ.get('REST_PORT', 8880),
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070044)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070045
46
47def parse_args():
48
49 parser = argparse.ArgumentParser()
50
Zsolt Haraszti109db832016-09-16 16:32:36 -070051 _help = ('Path to voltha.yml config file (default: %s). '
52 'If relative, it is relative to main.py of voltha.'
53 % defs['config'])
54 parser.add_argument('-c', '--config',
55 dest='config',
56 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070057 default=defs['config'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070058 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070059
Zsolt Haraszti109db832016-09-16 16:32:36 -070060 _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)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070065
Zsolt Haraszti109db832016-09-16 16:32:36 -070066 _help = ('<hostname> or <ip> at which Voltha is reachable from outside '
67 'the cluster (default: %s)' % defs['external_host_address'])
68 parser.add_argument('-E', '--external-host-address',
69 dest='external_host_address',
70 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070071 default=defs['external_host_address'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070072 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070073
Zsolt Haraszti109db832016-09-16 16:32:36 -070074 _help = ('<hostname>:<port> to fluentd server (default: %s). (If not '
75 'specified (None), the address from the config file is used'
76 % defs['fluentd'])
77 parser.add_argument('-F', '--fluentd',
78 dest='fluentd',
79 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070080 default=defs['fluentd'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070081 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070082
Zsolt Haraszti109db832016-09-16 16:32:36 -070083 _help = ('<hostname> or <ip> at which Voltha is reachable from inside the'
84 'cluster (default: %s)' % defs['internal_host_address'])
85 parser.add_argument('-H', '--internal-host-address',
86 dest='internal_host_address',
87 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070088 default=defs['internal_host_address'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070089 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070090
Zsolt Haraszti109db832016-09-16 16:32:36 -070091 _help = ('unique string id of this voltha instance (default: %s)'
92 % defs['interface'])
93 parser.add_argument('-i', '--instance-id',
94 dest='instance_id',
95 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070096 default=defs['instance_id'],
Zsolt Haraszti109db832016-09-16 16:32:36 -070097 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070098
99 # TODO placeholder, not used yet
Zsolt Haraszti109db832016-09-16 16:32:36 -0700100 _help = 'ETH interface to send (default: %s)' % defs['interface']
101 parser.add_argument('-I', '--interface',
102 dest='interface',
103 action='store',
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700104 default=defs['interface'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700105 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700106
Zsolt Haraszti109db832016-09-16 16:32:36 -0700107 _help = 'omit startup banner log lines'
108 parser.add_argument('-n', '--no-banner',
109 dest='no_banner',
110 action='store_true',
111 default=False,
112 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700113
Zsolt Haraszti109db832016-09-16 16:32:36 -0700114 _help = 'do not emit periodic heartbeat log messages'
115 parser.add_argument('-N', '--no-heartbeat',
116 dest='no_heartbeat',
117 action='store_true',
118 default=False,
119 help=_help)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700120
Zsolt Haraszti109db832016-09-16 16:32:36 -0700121 _help = ('port number for the rest service (default: %d)'
122 % defs['rest_port'])
123 parser.add_argument('-R', '--rest-port',
124 dest='rest_port',
125 action='store',
126 type=int,
Zsolt Harasztide22bbc2016-09-14 15:27:33 -0700127 default=defs['rest_port'],
Zsolt Haraszti109db832016-09-16 16:32:36 -0700128 help=_help)
Zsolt Harasztide22bbc2016-09-14 15:27:33 -0700129
Zsolt Haraszti109db832016-09-16 16:32:36 -0700130 _help = "suppress debug and info logs"
131 parser.add_argument('-q', '--quiet',
132 dest='quiet',
133 action='store_true',
134 default=False,
135 help=_help)
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -0700136
Zsolt Haraszti109db832016-09-16 16:32:36 -0700137 _help = 'enable verbose logging'
138 parser.add_argument('-v', '--verbose',
139 dest='verbose',
140 action='store_true',
141 default=False,
142 help=_help)
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -0700143
Zsolt Haraszti109db832016-09-16 16:32:36 -0700144 _help = ('use docker container name as voltha instance id'
145 ' (overrides -i/--instance-id option)')
146 parser.add_argument('--instance-id-is-container-name',
147 dest='instance_id_is_container_name',
148 action='store_true',
149 default=False,
150 help=_help)
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700151
152 args = parser.parse_args()
153
154 # post-processing
155
156 if args.instance_id_is_container_name:
157 args.instance_id = get_my_containers_name()
158
159 return args
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700160
161
Zsolt Harasztid7c7c482016-09-13 00:45:38 -0700162def load_config(args):
163 path = args.config
164 if path.startswith('.'):
165 dir = os.path.dirname(os.path.abspath(__file__))
166 path = os.path.join(dir, path)
167 path = os.path.abspath(path)
168 with open(path) as fd:
169 config = yaml.load(fd)
170 return config
171
172
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700173def print_banner(log):
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700174 log.info(' _ ______ __ ________ _____ ')
175 log.info('| | / / __ \/ / /_ __/ / / / |')
176 log.info('| | / / / / / / / / / /_/ / /| |')
177 log.info('| |/ / /_/ / /___/ / / __ / ___ |')
178 log.info('|___/\____/_____/_/ /_/ /_/_/ |_|')
Zsolt Haraszti59b7a882016-09-12 14:42:59 -0700179 log.info('(to stop: press Ctrl-C)')
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700180
181
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700182class Main(object):
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700183
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700184 def __init__(self):
185 self.args = args = parse_args()
186 self.config = load_config(args)
Zsolt Haraszti109db832016-09-16 16:32:36 -0700187 self.log = setup_logging(self.config.get('logging', {}),
188 args.instance_id,
189 fluentd=args.fluentd)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700190
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700191 # components
192 self.coordinator = None
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700193
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700194 if not args.no_banner:
195 print_banner(self.log)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700196
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700197 if not args.no_heartbeat:
198 self.start_heartbeat()
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700199
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700200 self.startup_components()
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700201
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700202 def start(self):
203 self.start_reactor() # will not return except Keyboard interrupt
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700204
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700205 def startup_components(self):
206 self.log.info('starting-internal-components')
207 self.coordinator = Coordinator(
208 internal_host_address=self.args.internal_host_address,
209 external_host_address=self.args.external_host_address,
210 rest_port=self.args.rest_port,
211 instance_id=self.args.instance_id,
212 consul=self.args.consul)
213 init_rest_service(self.args.rest_port)
214 self.log.info('started-internal-services')
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700215
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700216 @inlineCallbacks
217 def shutdown_components(self):
218 """Execute before the reactor is shut down"""
219 self.log.info('exiting-on-keyboard-interrupt')
220 yield self.coordinator.shutdown()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700221
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700222 def start_reactor(self):
223 from twisted.internet import reactor
Zsolt Haraszti109db832016-09-16 16:32:36 -0700224 reactor.callWhenRunning(
225 lambda: self.log.info('twisted-reactor-started'))
226 reactor.addSystemEventTrigger('before', 'shutdown',
227 self.shutdown_components)
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700228 reactor.run()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700229
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700230 def start_heartbeat(self):
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700231
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700232 t0 = time.time()
233 t0s = time.ctime(t0)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700234
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700235 def heartbeat():
236 self.log.debug(status='up', since=t0s, uptime=time.time() - t0)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700237
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700238 from twisted.internet.task import LoopingCall
239 lc = LoopingCall(heartbeat)
240 lc.start(10)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700241
242
243if __name__ == '__main__':
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700244 Main().start()