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()), |
| 38 | work_dir=os.environ.get('WORK_DIR', '/tmp/podder') |
| 39 | ) |
| 40 | |
| 41 | def 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 | |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 54 | _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 | |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 63 | _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 | |
alshabib | 9d22202 | 2016-11-10 16:11:09 -0800 | [diff] [blame] | 113 | def load_file(file): |
| 114 | path = file |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 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: |
alshabib | 9d22202 | 2016-11-10 16:11:09 -0800 | [diff] [blame] | 120 | contents = fd.read() |
| 121 | return contents |
| 122 | |
| 123 | def load_config(config): |
| 124 | contents = load_file(config) |
| 125 | return yaml.load(contents) |
| 126 | |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 127 | |
| 128 | banner = r''' |
| 129 | _____ |
| 130 | | | | | |
| 131 | | | | | |
| 132 | |_____|_____ ____|____| ___ _ |
| 133 | | | | | |/ _ \ / |
| 134 | | |_____|____|____|\____| |
| 135 | ''' |
| 136 | |
| 137 | def print_banner(log): |
| 138 | for line in banner.strip('\n').splitlines(): |
| 139 | log.info(line) |
| 140 | log.info('(to stop: press Ctrl-C)') |
| 141 | |
| 142 | class Main(object): |
| 143 | |
| 144 | def __init__(self): |
| 145 | self.args = args = parse_args() |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 146 | self.config = load_config(args.config) |
alshabib | 9d22202 | 2016-11-10 16:11:09 -0800 | [diff] [blame] | 147 | self.slave_config = load_file(args.slaves) |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 148 | |
| 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 | |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 160 | def start(self): |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 161 | self.startup_components() |
| 162 | |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 163 | def startup_components(self): |
| 164 | self.log.info('starting-internal-components') |
| 165 | args = self.args |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 166 | self.podder = Podder(args, self.slave_config) |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 167 | self.log.info('started-internal-components') |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 168 | self.podder.run() |
alshabib | c67ee3a | 2016-10-25 23:24:03 -0700 | [diff] [blame] | 169 | |
| 170 | |
| 171 | if __name__ == '__main__': |
| 172 | Main().start() |