blob: d02fc63ab2f9b8c8ca3ee5cfdbdfcd2d5b161dd2 [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()),
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
alshabib9d222022016-11-10 16:11:09 -0800113def load_file(file):
114 path = file
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:
alshabib9d222022016-11-10 16:11:09 -0800120 contents = fd.read()
121 return contents
122
123def load_config(config):
124 contents = load_file(config)
125 return yaml.load(contents)
126
alshabibc67ee3a2016-10-25 23:24:03 -0700127
128banner = r'''
129 _____
130| | | |
131| | | |
132|_____|_____ ____|____| ___ _
133| | | | |/ _ \ /
134| |_____|____|____|\____|
135'''
136
137def print_banner(log):
138 for line in banner.strip('\n').splitlines():
139 log.info(line)
140 log.info('(to stop: press Ctrl-C)')
141
142class Main(object):
143
144 def __init__(self):
145 self.args = args = parse_args()
alshabib7941d402016-11-08 00:11:20 +0100146 self.config = load_config(args.config)
alshabib9d222022016-11-10 16:11:09 -0800147 self.slave_config = load_file(args.slaves)
alshabibc67ee3a2016-10-25 23:24:03 -0700148
149 verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
150 self.log = setup_logging(self.config.get('logging', {}),
151 args.instance_id,
152 verbosity_adjust=verbosity_adjust,
153 fluentd=args.fluentd)
154
155 self.consul_manager = None
156
157 if not args.no_banner:
158 print_banner(self.log)
159
alshabib7941d402016-11-08 00:11:20 +0100160 def start(self):
alshabibc67ee3a2016-10-25 23:24:03 -0700161 self.startup_components()
162
alshabibc67ee3a2016-10-25 23:24:03 -0700163 def startup_components(self):
164 self.log.info('starting-internal-components')
165 args = self.args
alshabib7941d402016-11-08 00:11:20 +0100166 self.podder = Podder(args, self.slave_config)
alshabibc67ee3a2016-10-25 23:24:03 -0700167 self.log.info('started-internal-components')
alshabib7941d402016-11-08 00:11:20 +0100168 self.podder.run()
alshabibc67ee3a2016-10-25 23:24:03 -0700169
170
171if __name__ == '__main__':
172 Main().start()