blob: 412ae58ecec86f9bc7b0509980fb8a2ec5181a93 [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(
alshabib9d222022016-11-10 16:11:09 -080028 slaves=os.environ.get('SLAVES', './slaves.yml.j2'),
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()),
alshabib05fb71f2016-12-04 16:08:29 -080038 work_dir=os.environ.get('WORK_DIR', '/tmp/podder'),
39 threads=os.environ.get('PODDER_THREADS', 5)
alshabibc67ee3a2016-10-25 23:24:03 -070040)
41
42def parse_args():
43
44 parser = argparse.ArgumentParser()
45
46 _help = ('Path to podder.yml config file (default: %s). '
47 'If relative, it is relative to main.py of podder.'
48 % defs['config'])
49 parser.add_argument('-c', '--config',
50 dest='config',
51 action='store',
52 default=defs['config'],
53 help=_help)
54
alshabib7941d402016-11-08 00:11:20 +010055 _help = ('Path to slaves configuration file (default %s).'
56 'If relative, it is relative to main.py of podder.'
57 % defs['slaves'])
58 parser.add_argument('-s', '--slaves',
59 dest='slaves',
60 action='store',
61 default=defs['slaves'],
62 help=_help)
63
alshabibc67ee3a2016-10-25 23:24:03 -070064 _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul']
65 parser.add_argument(
66 '-C', '--consul', dest='consul', action='store',
67 default=defs['consul'],
68 help=_help)
69
70
71 _help = ('<hostname>:<port> to fluentd server (default: %s). (If not '
72 'specified (None), the address from the config file is used'
73 % defs['fluentd'])
74 parser.add_argument('-F', '--fluentd',
75 dest='fluentd',
76 action='store',
77 default=defs['fluentd'],
78 help=_help)
79
80 _help = ('unique string id of this ofagent instance (default: %s)'
81 % defs['instance_id'])
82 parser.add_argument('-i', '--instance-id',
83 dest='instance_id',
84 action='store',
85 default=defs['instance_id'],
86 help=_help)
87
88 _help = 'omit startup banner log lines'
89 parser.add_argument('-n', '--no-banner',
90 dest='no_banner',
91 action='store_true',
92 default=False,
93 help=_help)
94
95 _help = "suppress debug and info logs"
96 parser.add_argument('-q', '--quiet',
97 dest='quiet',
98 action='count',
99 help=_help)
100
101 _help = 'enable verbose logging'
102 parser.add_argument('-v', '--verbose',
103 dest='verbose',
104 action='count',
105 help=_help)
106
alshabib05fb71f2016-12-04 16:08:29 -0800107 _help = 'Number of events to handle in parallel'
108 parser.add_argument('-e', '--events-in-parallel',
109 dest='threads',
110 type=int,
111 default=defs['threads'],
112 action='store',
113 help=_help)
114
alshabibc67ee3a2016-10-25 23:24:03 -0700115
116 args = parser.parse_args()
117
118 # post-processing
119
120 return args
121
alshabib9d222022016-11-10 16:11:09 -0800122def load_file(file):
123 path = file
alshabibc67ee3a2016-10-25 23:24:03 -0700124 if path.startswith('.'):
125 dir = os.path.dirname(os.path.abspath(__file__))
126 path = os.path.join(dir, path)
127 path = os.path.abspath(path)
128 with open(path) as fd:
alshabib9d222022016-11-10 16:11:09 -0800129 contents = fd.read()
130 return contents
131
132def load_config(config):
133 contents = load_file(config)
134 return yaml.load(contents)
135
alshabibc67ee3a2016-10-25 23:24:03 -0700136
137banner = r'''
138 _____
139| | | |
140| | | |
141|_____|_____ ____|____| ___ _
142| | | | |/ _ \ /
143| |_____|____|____|\____|
144'''
145
146def print_banner(log):
147 for line in banner.strip('\n').splitlines():
148 log.info(line)
149 log.info('(to stop: press Ctrl-C)')
150
151class Main(object):
152
153 def __init__(self):
154 self.args = args = parse_args()
alshabib7941d402016-11-08 00:11:20 +0100155 self.config = load_config(args.config)
alshabib9d222022016-11-10 16:11:09 -0800156 self.slave_config = load_file(args.slaves)
alshabibc67ee3a2016-10-25 23:24:03 -0700157
158 verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
159 self.log = setup_logging(self.config.get('logging', {}),
160 args.instance_id,
161 verbosity_adjust=verbosity_adjust,
162 fluentd=args.fluentd)
163
164 self.consul_manager = None
165
166 if not args.no_banner:
167 print_banner(self.log)
168
alshabib7941d402016-11-08 00:11:20 +0100169 def start(self):
alshabibc67ee3a2016-10-25 23:24:03 -0700170 self.startup_components()
171
alshabibc67ee3a2016-10-25 23:24:03 -0700172 def startup_components(self):
173 self.log.info('starting-internal-components')
174 args = self.args
alshabib7941d402016-11-08 00:11:20 +0100175 self.podder = Podder(args, self.slave_config)
alshabibc67ee3a2016-10-25 23:24:03 -0700176 self.log.info('started-internal-components')
alshabib7941d402016-11-08 00:11:20 +0100177 self.podder.run()
alshabibc67ee3a2016-10-25 23:24:03 -0700178
179
180if __name__ == '__main__':
181 Main().start()