blob: 3f744465d517fdf45acddb3d62502b105ae2bd02 [file] [log] [blame]
alshabibc67ee3a2016-10-25 23:24:03 -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
18import argparse
19import os
Zsolt Harasztid70cd4d2016-11-03 23:23:36 -070020
alshabibc67ee3a2016-10-25 23:24:03 -070021import yaml
alshabib7941d402016-11-08 00:11:20 +010022from podder import Podder
Zsolt Harasztid70cd4d2016-11-03 23:23:36 -070023
24from common.structlog_setup import setup_logging
25from common.utils.nethelpers import get_my_primary_local_ipv4
alshabibc67ee3a2016-10-25 23:24:03 -070026
27defs = dict(
alshabib7941d402016-11-08 00:11:20 +010028 slaves=os.environ.get('SLAVES', './slaves.yml'),
alshabibc67ee3a2016-10-25 23:24:03 -070029 config=os.environ.get('CONFIG', './podder.yml'),
30 consul=os.environ.get('CONSUL', 'localhost:8500'),
31 external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS',
32 get_my_primary_local_ipv4()),
33 grpc_endpoint=os.environ.get('GRPC_ENDPOINT', 'localhost:50055'),
34 fluentd=os.environ.get('FLUENTD', None),
35 instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')),
36 internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS',
37 get_my_primary_local_ipv4()),
38 work_dir=os.environ.get('WORK_DIR', '/tmp/podder')
39)
40
41def parse_args():
42
43 parser = argparse.ArgumentParser()
44
45 _help = ('Path to podder.yml config file (default: %s). '
46 'If relative, it is relative to main.py of podder.'
47 % defs['config'])
48 parser.add_argument('-c', '--config',
49 dest='config',
50 action='store',
51 default=defs['config'],
52 help=_help)
53
alshabib7941d402016-11-08 00:11:20 +010054 _help = ('Path to slaves configuration file (default %s).'
55 'If relative, it is relative to main.py of podder.'
56 % defs['slaves'])
57 parser.add_argument('-s', '--slaves',
58 dest='slaves',
59 action='store',
60 default=defs['slaves'],
61 help=_help)
62
alshabibc67ee3a2016-10-25 23:24:03 -070063 _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul']
64 parser.add_argument(
65 '-C', '--consul', dest='consul', action='store',
66 default=defs['consul'],
67 help=_help)
68
69
70 _help = ('<hostname>:<port> to fluentd server (default: %s). (If not '
71 'specified (None), the address from the config file is used'
72 % defs['fluentd'])
73 parser.add_argument('-F', '--fluentd',
74 dest='fluentd',
75 action='store',
76 default=defs['fluentd'],
77 help=_help)
78
79 _help = ('unique string id of this ofagent instance (default: %s)'
80 % defs['instance_id'])
81 parser.add_argument('-i', '--instance-id',
82 dest='instance_id',
83 action='store',
84 default=defs['instance_id'],
85 help=_help)
86
87 _help = 'omit startup banner log lines'
88 parser.add_argument('-n', '--no-banner',
89 dest='no_banner',
90 action='store_true',
91 default=False,
92 help=_help)
93
94 _help = "suppress debug and info logs"
95 parser.add_argument('-q', '--quiet',
96 dest='quiet',
97 action='count',
98 help=_help)
99
100 _help = 'enable verbose logging'
101 parser.add_argument('-v', '--verbose',
102 dest='verbose',
103 action='count',
104 help=_help)
105
106
107 args = parser.parse_args()
108
109 # post-processing
110
111 return args
112
alshabib7941d402016-11-08 00:11:20 +0100113def load_config(config):
114 path = config
alshabibc67ee3a2016-10-25 23:24:03 -0700115 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
123banner = r'''
124 _____
125| | | |
126| | | |
127|_____|_____ ____|____| ___ _
128| | | | |/ _ \ /
129| |_____|____|____|\____|
130'''
131
132def print_banner(log):
133 for line in banner.strip('\n').splitlines():
134 log.info(line)
135 log.info('(to stop: press Ctrl-C)')
136
137class Main(object):
138
139 def __init__(self):
140 self.args = args = parse_args()
alshabib7941d402016-11-08 00:11:20 +0100141 self.config = load_config(args.config)
142 self.slave_config = load_config(args.slaves)
alshabibc67ee3a2016-10-25 23:24:03 -0700143
144 verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
145 self.log = setup_logging(self.config.get('logging', {}),
146 args.instance_id,
147 verbosity_adjust=verbosity_adjust,
148 fluentd=args.fluentd)
149
150 self.consul_manager = None
151
152 if not args.no_banner:
153 print_banner(self.log)
154
alshabib7941d402016-11-08 00:11:20 +0100155 def start(self):
alshabibc67ee3a2016-10-25 23:24:03 -0700156 self.startup_components()
157
alshabibc67ee3a2016-10-25 23:24:03 -0700158 def startup_components(self):
159 self.log.info('starting-internal-components')
160 args = self.args
alshabib7941d402016-11-08 00:11:20 +0100161 self.podder = Podder(args, self.slave_config)
alshabibc67ee3a2016-10-25 23:24:03 -0700162 self.log.info('started-internal-components')
alshabib7941d402016-11-08 00:11:20 +0100163 self.podder.run()
alshabibc67ee3a2016-10-25 23:24:03 -0700164
165
166if __name__ == '__main__':
167 Main().start()