blob: 200043c017fdcb17d27767003373227f4044c1bc [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
27
28
29defs = dict(
30 consul=os.environ.get('CONSUL', 'localhost:8500'),
31 instance_id=os.environ.get('INSTANCE_ID', '1'),
32 config=os.environ.get('CONFIG', './voltha.yml'),
33 interface=os.environ.get('INTERFACE', 'eth0'),
34 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS', 'localhost'),
35 external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS', 'localhost'),
36 fluentd=os.environ.get('FLUENTD', None)
37)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070038
39
40def parse_args():
41
42 parser = argparse.ArgumentParser()
43
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070044 parser.add_argument('-c', '--config', dest='config', action='store',
45 default=defs['config'],
46 help='Path to voltha.yml config file (default: %s). '
47 'If relative, it is relative to main.py of voltha.' % defs['config'])
48
49 parser.add_argument('-C', '--consul', dest='consul', action='store',
50 default=defs['consul'],
51 help='<hostname>:<port> to consul agent (default: %s)' % defs['consul'])
52
53 parser.add_argument('-E', '--external-host-address', dest='external_host_address', action='store',
54 default=defs['external_host_address'],
55 help='<hostname> or <ip> at which Voltha is reachable from outside the cluster'
56 '(default: %s)' % defs['external_host_address'])
57
58 parser.add_argument('-F', '--fluentd', dest='fluentd', action='store',
59 default=defs['fluentd'],
60 help='<hostname>:<port> to fluentd server (default: %s).'
61 '(If not specified (None), the address from the config file is used'
62 % defs['fluentd'])
63
64 parser.add_argument('-H', '--internal-host-address', dest='internal_host_address', action='store',
65 default=defs['internal_host_address'],
66 help='<hostname> or <ip> at which Voltha is reachable from inside the cluster'
67 '(default: %s)' % defs['internal_host_address'])
68
69 parser.add_argument('-i', '--instance-id', dest='instance_id', action='store',
70 default=defs['instance_id'],
71 help='unique string id of this voltha instance (default: %s)' % defs['interface'])
72
73 # TODO placeholder, not used yet
74 parser.add_argument('-I', '--interface', dest='interface', action='store',
75 default=defs['interface'],
76 help='ETH interface to send (default: %s)' % defs['interface'])
77
78 parser.add_argument('-n', '--no-banner', dest='no_banner', action='store_true', default=False,
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070079 help='omit startup banner log lines')
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070080
81 parser.add_argument('-N', '--no-heartbeat', dest='no_heartbeat', action='store_true', default=False,
82 help='do not emit periodic heartbeat log messages')
83
Zsolt Haraszti59b7a882016-09-12 14:42:59 -070084 parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', default=False,
85 help="suppress debug and info logs")
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -070086
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070087 parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', default=False,
88 help='enable verbose logging')
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -070089
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070090 return parser.parse_args()
91
92
Zsolt Harasztid7c7c482016-09-13 00:45:38 -070093def load_config(args):
94 path = args.config
95 if path.startswith('.'):
96 dir = os.path.dirname(os.path.abspath(__file__))
97 path = os.path.join(dir, path)
98 path = os.path.abspath(path)
99 with open(path) as fd:
100 config = yaml.load(fd)
101 return config
102
103
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700104def print_banner(args, log):
105 log.info(' _ ______ __ ________ _____ ')
106 log.info('| | / / __ \/ / /_ __/ / / / |')
107 log.info('| | / / / / / / / / / /_/ / /| |')
108 log.info('| |/ / /_/ / /___/ / / __ / ___ |')
109 log.info('|___/\____/_____/_/ /_/ /_/_/ |_|')
Zsolt Haraszti59b7a882016-09-12 14:42:59 -0700110 log.info('(to stop: press Ctrl-C)')
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700111
112
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700113def startup(log, args, config):
114 log.info('starting-internal-services')
115 coordinator = Coordinator(
116 internal_host_address=args.internal_host_address,
117 external_host_address=args.external_host_address,
118 instance_id=args.instance_id,
119 consul=args.consul)
120 log.info('started-internal-services')
121
122
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700123def cleanup(log):
124 """Execute before the reactor is shut down"""
125 log.info('exiting-on-keyboard-interrupt')
126
127
128def start_reactor(args, log):
129 from twisted.internet import reactor
130 reactor.callWhenRunning(lambda: log.info('twisted-reactor-started'))
131 reactor.addSystemEventTrigger('before', 'shutdown', lambda: cleanup(log))
132 reactor.run()
133
134
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700135def start_heartbeat(log):
136
137 t0 = time.time()
138 t0s = time.ctime(t0)
139
140 def heartbeat():
141 log.info(status='up', since=t0s, uptime=time.time() - t0)
142
143 from twisted.internet.task import LoopingCall
144 lc = LoopingCall(heartbeat)
145 lc.start(10)
146
147
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700148def main():
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700149
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700150 args = parse_args()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700151
Zsolt Harasztid7c7c482016-09-13 00:45:38 -0700152 config = load_config(args)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700153
154 log = setup_logging(config.get('logging', {}), fluentd=args.fluentd)
155
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700156 if not args.no_banner:
157 print_banner(args, log)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700158
159 if not args.no_heartbeat:
160 start_heartbeat(log)
161
162 startup(log, args, config)
163
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700164 start_reactor(args, log) # will not return except Keyboard interrupt
165
166
167if __name__ == '__main__':
168 main()