def add_container(self, id, container): if id in self.containers: logger.info('add container but already exists. try replace %s', id) self.lock.acquire() self.containers[id] = container if self.notifier: self.notifier.add_service(container) self.lock.release()
def __init__(self, ngx, decoder=None, key_decoder=None): self.ngx = ngx self.ngx.clear() self.services = {} if not decoder: logger.info('no decoder specified, use default JsonDecoder') from chipmunks.coder import coder self.decoder = coder if not key_decoder: logger.info('no key decoder specified, use default.') from chipmunks.coder import key_coder self.keydecoder = key_coder
def load(cls, config_dir): for fn in os.listdir(config_dir): if path.isdir(fn): logger.info('%s is dir, ignore.', fn) continue filename, ext = path.splitext(fn) if ext != '.conf': logger.info('%s not a nginx configuration file.', fn) continue m = location_pattern.match(filename) if not m: logger.info("%s is not location file, ignore.", fn) continue svc_name = m.group(1) location_path = path.join(config_dir, fn) upstream_filepath = path.join(config_dir, '{}.upstream.conf'.format(svc_name)) if not path.exists(upstream_filepath): logger.info("%s missing upstream file, ignore.", fn) continue yield cls(svc_name=svc_name, location=location_path, upstream=upstream_filepath)
def agent(ctx, config): ''' This program watch docker container changes and send changed through etcd3 ''' from chipmunks import config as munks_config munks_config.load(config) from chipmunks.log import logger from chipmunks.agent import Agent import logging if ctx.verbose: logger.setLevel(logging.DEBUG) logger.info('load configuration from %s', config) app = make_agent_app(munks_config) app.run()
def chips(ctx, config): ''' This program watch etcd3 event and generate nginx configuration file. ''' from chipmunks import config as munks_config munks_config.load(config) from chipmunks.log import logger from chipmunks.chips import Chipmunks import logging if ctx.verbose: logger.setLevel(logging.DEBUG) logger.info('load configuration from %s', config) app = make_chips_app(munks_config) app.run()
def __init__(self, docker, etcd): ''' kwargs: docker: base_url: host tls_verify: False tls: tls configure object. ''' self.cli = docker node_id, node_ip = self.ensure_node_id() logger.info('current node is %s[%s]', node_id, node_ip) notifier = Notifier(etcd, node={'id': node_id, 'ip': node_ip}) self.monitor = Monitor(Container.load_all(self.cli)) self.monitor.set_notifier(notifier) self.events = self.cli.events()
def generate_to_fp(svc_obj, template, fp): logger.debug('generate configure of %s', svc_obj.name) auth_backends = svc_obj.auth_backend or config.get('auth.backend') need_auth = auth_backends and not svc_obj.auth_bypass and not svc_obj.is_authorize_backend if need_auth: logger.info('use auth backends: %s', auth_backends) params = {'svc': svc_obj, 'auth': { 'backend': auth_backends, 'bypass': not need_auth } } if config.getboolean('template.debug', False): logger.debug(template.render(params)) template.stream(params).dump(fp)
def del_svc(self, key): svc_name, node_id, container_id = self.keydecoder.decode(key, extra=True) if not svc_name: logger.error("'%s' can't decode as service-name,", key) return False if svc_name not in self.services: logger.error("'%s' not exists.", svc_name) return False svc = self.services[svc_name] svc.delete_host(node_id, container_id) if len(svc.hosts) <= 0: logger.info("all hosts get down, rm service %s", svc.name) del self.services[svc_name] self.ngx_del_svc(svc_name) else: self.ngx_add_svc(svc) return True
def __init__(self, configuration_path=None, nginx_pid_file=None): if not nginx_pid_file: nginx_pid_file = self.__get_default_nginx_pid() logger.info('nginx_pid_file is empty, use default pid file %s', nginx_pid_file) if not configuration_path: configuration_path = self.__get_default_nginx_conf() logger.info( 'configuration_path is empty, use default configure path %s', configuration_path) logger.debug("nginx configuration path is %s", configuration_path) logger.debug('nginx pid file is %s', nginx_pid_file) self.nginx_pid = self.__get_master_pid_from(nginx_pid_file) self.configuration_path = configuration_path self.configurations = {} self.__load_config() self.__dump_config()
def run(self): ''' connect to etcd. init all data and then watch for it. ''' logger.info('chipmunks watch etced started at %s', datetime.now().strftime('%Y-%m-%d %H:%M:%S')) svcs = {v.key.decode('UTF-8'): val.decode('UTF-8') for val, v in self.client.get_prefix(self.prefix)} logger.info('persisted services "%s"', json.dumps(svcs)) self.monitor.init_svcs(svcs) event_generator, self.cancel = self.client.watch_prefix(self.prefix) for event in event_generator: if isinstance(event, events.PutEvent): key, value = event.key.decode('utf-8'), event.value.decode('utf-8') logger.info('get etcd put event: %s: %s', key, value) self.monitor.add_svc(key, value) elif isinstance(event, events.DeleteEvent): key = event.key.decode('utf-8') logger.info('get etcd delete event %s', key) self.monitor.del_svc(key) else: logger.error("receive etcd unknown type of event(neither Put nor Delete)")
def stop(self, *args): logger.info('stop watch etcd3 at %s', datetime.now().strftime('%Y-%m-%d %H:%M:%S')) if self.cancel: self.cancel()
def __dump_config(self): for key in self.configurations: logger.info('chipmunks generated config %s:%s', key, self.configurations[key])