blob: a85fb2f4bd44eb7a521adde0feb714ab2cdea171 [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()),
38 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS', get_my_primary_local_ipv4()),
39 external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS', get_my_primary_local_ipv4()),
40 fluentd=os.environ.get('FLUENTD', None),
41 rest_port=os.environ.get('REST_PORT', 8880),
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070042)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070043
44
45def parse_args():
46
47 parser = argparse.ArgumentParser()
48
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070049 parser.add_argument('-c', '--config', dest='config', action='store',
50 default=defs['config'],
51 help='Path to voltha.yml config file (default: %s). '
52 'If relative, it is relative to main.py of voltha.' % defs['config'])
53
54 parser.add_argument('-C', '--consul', dest='consul', action='store',
55 default=defs['consul'],
56 help='<hostname>:<port> to consul agent (default: %s)' % defs['consul'])
57
58 parser.add_argument('-E', '--external-host-address', dest='external_host_address', action='store',
59 default=defs['external_host_address'],
60 help='<hostname> or <ip> at which Voltha is reachable from outside the cluster'
61 '(default: %s)' % defs['external_host_address'])
62
63 parser.add_argument('-F', '--fluentd', dest='fluentd', action='store',
64 default=defs['fluentd'],
65 help='<hostname>:<port> to fluentd server (default: %s).'
66 '(If not specified (None), the address from the config file is used'
67 % defs['fluentd'])
68
69 parser.add_argument('-H', '--internal-host-address', dest='internal_host_address', action='store',
70 default=defs['internal_host_address'],
71 help='<hostname> or <ip> at which Voltha is reachable from inside the cluster'
72 '(default: %s)' % defs['internal_host_address'])
73
74 parser.add_argument('-i', '--instance-id', dest='instance_id', action='store',
75 default=defs['instance_id'],
76 help='unique string id of this voltha instance (default: %s)' % defs['interface'])
77
78 # TODO placeholder, not used yet
79 parser.add_argument('-I', '--interface', dest='interface', action='store',
80 default=defs['interface'],
81 help='ETH interface to send (default: %s)' % defs['interface'])
82
83 parser.add_argument('-n', '--no-banner', dest='no_banner', action='store_true', default=False,
Zsolt Harasztib71c2a02016-09-12 13:12:07 -070084 help='omit startup banner log lines')
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070085
86 parser.add_argument('-N', '--no-heartbeat', dest='no_heartbeat', action='store_true', default=False,
87 help='do not emit periodic heartbeat log messages')
88
Zsolt Harasztide22bbc2016-09-14 15:27:33 -070089 parser.add_argument('-R', '--rest-port', dest='rest_port', action='store', type=int,
90 default=defs['rest_port'],
91 help='port number for the rest service (default: %d)' % defs['rest_port'])
92
Zsolt Haraszti59b7a882016-09-12 14:42:59 -070093 parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', default=False,
94 help="suppress debug and info logs")
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -070095
Zsolt Harasztif2da1d02016-09-13 23:21:35 -070096 parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', default=False,
97 help='enable verbose logging')
Zsolt Harasztiadbb88d2016-09-12 21:24:57 -070098
Zsolt Harasztie060a7d2016-09-16 11:08:24 -070099 parser.add_argument('--instance-id-is-container-name', dest='instance_id_is_container_name', action='store_true',
100 default=False, help='use docker container name as voltha instance id'
101 ' (overrides -i/--instance-id option)')
102
103 args = parser.parse_args()
104
105 # post-processing
106
107 if args.instance_id_is_container_name:
108 args.instance_id = get_my_containers_name()
109
110 return args
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700111
112
Zsolt Harasztid7c7c482016-09-13 00:45:38 -0700113def load_config(args):
114 path = args.config
115 if path.startswith('.'):
116 dir = os.path.dirname(os.path.abspath(__file__))
117 path = os.path.join(dir, path)
118 path = os.path.abspath(path)
119 with open(path) as fd:
120 config = yaml.load(fd)
121 return config
122
123
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700124def print_banner(log):
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700125 log.info(' _ ______ __ ________ _____ ')
126 log.info('| | / / __ \/ / /_ __/ / / / |')
127 log.info('| | / / / / / / / / / /_/ / /| |')
128 log.info('| |/ / /_/ / /___/ / / __ / ___ |')
129 log.info('|___/\____/_____/_/ /_/ /_/_/ |_|')
Zsolt Haraszti59b7a882016-09-12 14:42:59 -0700130 log.info('(to stop: press Ctrl-C)')
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700131
132
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700133class Main(object):
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700134
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700135 def __init__(self):
136 self.args = args = parse_args()
137 self.config = load_config(args)
138 self.log = setup_logging(self.config.get('logging', {}), args.instance_id, fluentd=args.fluentd)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700139
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700140 # components
141 self.coordinator = None
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700142
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700143 if not args.no_banner:
144 print_banner(self.log)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700145
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700146 if not args.no_heartbeat:
147 self.start_heartbeat()
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700148
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700149 self.startup_components()
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700150
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700151 def start(self):
152 self.start_reactor() # will not return except Keyboard interrupt
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700153
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700154 def startup_components(self):
155 self.log.info('starting-internal-components')
156 self.coordinator = Coordinator(
157 internal_host_address=self.args.internal_host_address,
158 external_host_address=self.args.external_host_address,
159 rest_port=self.args.rest_port,
160 instance_id=self.args.instance_id,
161 consul=self.args.consul)
162 init_rest_service(self.args.rest_port)
163 self.log.info('started-internal-services')
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700164
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700165 @inlineCallbacks
166 def shutdown_components(self):
167 """Execute before the reactor is shut down"""
168 self.log.info('exiting-on-keyboard-interrupt')
169 yield self.coordinator.shutdown()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700170
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700171 def start_reactor(self):
172 from twisted.internet import reactor
173 reactor.callWhenRunning(lambda: self.log.info('twisted-reactor-started'))
174 reactor.addSystemEventTrigger('before', 'shutdown', self.shutdown_components)
175 reactor.run()
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700176
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700177 def start_heartbeat(self):
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700178
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700179 t0 = time.time()
180 t0s = time.ctime(t0)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700181
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700182 def heartbeat():
183 self.log.debug(status='up', since=t0s, uptime=time.time() - t0)
Zsolt Harasztif2da1d02016-09-13 23:21:35 -0700184
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700185 from twisted.internet.task import LoopingCall
186 lc = LoopingCall(heartbeat)
187 lc.start(10)
Zsolt Harasztib71c2a02016-09-12 13:12:07 -0700188
189
190if __name__ == '__main__':
Zsolt Harasztie060a7d2016-09-16 11:08:24 -0700191 Main().start()