blob: 188e6aa36f8af135007da7903dc3ef420010677a [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 Harasztiadbb88d2016-09-12 21:24:57 -070024
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070025from structlog_setup import setup_logging
26from coordinator import Coordinator
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070027from northbound.rest.health_check import init_rest_service
28from nethelpers import get_my_primary_interface, get_my_primary_local_ipv4
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070029
30defs = dict(
31 consul=os.environ.get('CONSUL', 'localhost:8500'),
Zsolt Haraszti28e0f1c2016-09-13 23:55:43 -070032 instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070033 config=os.environ.get('CONFIG', './voltha.yml'),
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070034 interface=os.environ.get('INTERFACE', get_my_primary_interface()),
35 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS', get_my_primary_local_ipv4()),
36 external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS', get_my_primary_local_ipv4()),
37 fluentd=os.environ.get('FLUENTD', None),
38 rest_port=os.environ.get('REST_PORT', 8880),
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070039)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070040
41
42def parse_args():
43
44 parser = argparse.ArgumentParser()
45
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070046 parser.add_argument('-c', '--config', dest='config', action='store',
47 default=defs['config'],
48 help='Path to voltha.yml config file (default: %s). '
49 'If relative, it is relative to main.py of voltha.' % defs['config'])
50
51 parser.add_argument('-C', '--consul', dest='consul', action='store',
52 default=defs['consul'],
53 help='<hostname>:<port> to consul agent (default: %s)' % defs['consul'])
54
55 parser.add_argument('-E', '--external-host-address', dest='external_host_address', action='store',
56 default=defs['external_host_address'],
57 help='<hostname> or <ip> at which Voltha is reachable from outside the cluster'
58 '(default: %s)' % defs['external_host_address'])
59
60 parser.add_argument('-F', '--fluentd', dest='fluentd', action='store',
61 default=defs['fluentd'],
62 help='<hostname>:<port> to fluentd server (default: %s).'
63 '(If not specified (None), the address from the config file is used'
64 % defs['fluentd'])
65
66 parser.add_argument('-H', '--internal-host-address', dest='internal_host_address', action='store',
67 default=defs['internal_host_address'],
68 help='<hostname> or <ip> at which Voltha is reachable from inside the cluster'
69 '(default: %s)' % defs['internal_host_address'])
70
71 parser.add_argument('-i', '--instance-id', dest='instance_id', action='store',
72 default=defs['instance_id'],
73 help='unique string id of this voltha instance (default: %s)' % defs['interface'])
74
75 # TODO placeholder, not used yet
76 parser.add_argument('-I', '--interface', dest='interface', action='store',
77 default=defs['interface'],
78 help='ETH interface to send (default: %s)' % defs['interface'])
79
80 parser.add_argument('-n', '--no-banner', dest='no_banner', action='store_true', default=False,
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070081 help='omit startup banner log lines')
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070082
83 parser.add_argument('-N', '--no-heartbeat', dest='no_heartbeat', action='store_true', default=False,
84 help='do not emit periodic heartbeat log messages')
85
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070086 parser.add_argument('-R', '--rest-port', dest='rest_port', action='store', type=int,
87 default=defs['rest_port'],
88 help='port number for the rest service (default: %d)' % defs['rest_port'])
89
Zsolt Haraszti59b7a882016-09-12 14:42:59 -070090 parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', default=False,
91 help="suppress debug and info logs")
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -070092
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070093 parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', default=False,
94 help='enable verbose logging')
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -070095
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070096 return parser.parse_args()
97
98
Zsolt Harasztid7c7c482016-09-13 00:45:38 -070099def load_config(args):
100 path = args.config
101 if path.startswith('.'):
102 dir = os.path.dirname(os.path.abspath(__file__))
103 path = os.path.join(dir, path)
104 path = os.path.abspath(path)
105 with open(path) as fd:
106 config = yaml.load(fd)
107 return config
108
109
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700110def print_banner(args, log):
111 log.info(' _ ______ __ ________ _____ ')
112 log.info('| | / / __ \/ / /_ __/ / / / |')
113 log.info('| | / / / / / / / / / /_/ / /| |')
114 log.info('| |/ / /_/ / /___/ / / __ / ___ |')
115 log.info('|___/\____/_____/_/ /_/ /_/_/ |_|')
Zsolt Haraszti59b7a882016-09-12 14:42:59 -0700116 log.info('(to stop: press Ctrl-C)')
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700117
118
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700119def startup(log, args, config):
120 log.info('starting-internal-services')
121 coordinator = Coordinator(
122 internal_host_address=args.internal_host_address,
123 external_host_address=args.external_host_address,
Zsolt Harasztide22bbc2016-09-14 15:27:33 -0700124 rest_port=args.rest_port,
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700125 instance_id=args.instance_id,
126 consul=args.consul)
Zsolt Harasztide22bbc2016-09-14 15:27:33 -0700127 init_rest_service(args.rest_port)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700128 log.info('started-internal-services')
129
130
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700131def cleanup(log):
132 """Execute before the reactor is shut down"""
133 log.info('exiting-on-keyboard-interrupt')
134
135
136def start_reactor(args, log):
137 from twisted.internet import reactor
138 reactor.callWhenRunning(lambda: log.info('twisted-reactor-started'))
139 reactor.addSystemEventTrigger('before', 'shutdown', lambda: cleanup(log))
140 reactor.run()
141
142
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700143def start_heartbeat(log):
144
145 t0 = time.time()
146 t0s = time.ctime(t0)
147
148 def heartbeat():
149 log.info(status='up', since=t0s, uptime=time.time() - t0)
150
151 from twisted.internet.task import LoopingCall
152 lc = LoopingCall(heartbeat)
153 lc.start(10)
154
155
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700156def main():
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700157
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700158 args = parse_args()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700159
Zsolt Harasztid7c7c482016-09-13 00:45:38 -0700160 config = load_config(args)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700161
162 log = setup_logging(config.get('logging', {}), fluentd=args.fluentd)
163
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700164 if not args.no_banner:
165 print_banner(args, log)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700166
167 if not args.no_heartbeat:
168 start_heartbeat(log)
169
170 startup(log, args, config)
171
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700172 start_reactor(args, log) # will not return except Keyboard interrupt
173
174
175if __name__ == '__main__':
176 main()