alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 1 | #!/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 | |
| 18 | import argparse |
| 19 | import os |
Zsolt Haraszti | d70cd4d | 2016-11-03 23:23:36 -0700 | [diff] [blame] | 20 | |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 21 | import yaml |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 22 | from podder import Podder |
Zsolt Haraszti | d70cd4d | 2016-11-03 23:23:36 -0700 | [diff] [blame] | 23 | |
| 24 | from common.structlog_setup import setup_logging |
| 25 | from common.utils.nethelpers import get_my_primary_local_ipv4 |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 26 | |
| 27 | defs = dict( |
alshabib | 9d22202 | 2016-11-10 16:11:09 -0800 | [diff] [blame] | 28 | slaves=os.environ.get('SLAVES', './slaves.yml.j2'), |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 29 | 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()), |
alshabib | 05fb71f | 2016-12-04 16:08:29 -0800 | [diff] [blame] | 38 | work_dir=os.environ.get('WORK_DIR', '/tmp/podder'), |
| 39 | threads=os.environ.get('PODDER_THREADS', 5) |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 40 | ) |
| 41 | |
| 42 | def 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 | |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 55 | _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 | |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 64 | _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 | |
alshabib | 05fb71f | 2016-12-04 16:08:29 -0800 | [diff] [blame] | 107 | _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 | |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 115 | |
| 116 | args = parser.parse_args() |
| 117 | |
| 118 | # post-processing |
| 119 | |
| 120 | return args |
| 121 | |
alshabib | 9d22202 | 2016-11-10 16:11:09 -0800 | [diff] [blame] | 122 | def load_file(file): |
| 123 | path = file |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 124 | 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: |
alshabib | 9d22202 | 2016-11-10 16:11:09 -0800 | [diff] [blame] | 129 | contents = fd.read() |
| 130 | return contents |
| 131 | |
| 132 | def load_config(config): |
| 133 | contents = load_file(config) |
| 134 | return yaml.load(contents) |
| 135 | |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 136 | |
| 137 | banner = r''' |
| 138 | _____ |
| 139 | | | | | |
| 140 | | | | | |
| 141 | |_____|_____ ____|____| ___ _ |
| 142 | | | | | |/ _ \ / |
| 143 | | |_____|____|____|\____| |
| 144 | ''' |
| 145 | |
| 146 | def print_banner(log): |
| 147 | for line in banner.strip('\n').splitlines(): |
| 148 | log.info(line) |
| 149 | log.info('(to stop: press Ctrl-C)') |
| 150 | |
| 151 | class Main(object): |
| 152 | |
| 153 | def __init__(self): |
| 154 | self.args = args = parse_args() |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 155 | self.config = load_config(args.config) |
alshabib | 9d22202 | 2016-11-10 16:11:09 -0800 | [diff] [blame] | 156 | self.slave_config = load_file(args.slaves) |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 157 | |
| 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 | |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 169 | def start(self): |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 170 | self.startup_components() |
| 171 | |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 172 | def startup_components(self): |
| 173 | self.log.info('starting-internal-components') |
| 174 | args = self.args |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 175 | self.podder = Podder(args, self.slave_config) |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 176 | self.log.info('started-internal-components') |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 177 | self.podder.run() |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 178 | |
| 179 | |
| 180 | if __name__ == '__main__': |
| 181 | Main().start() |