def __safe_delete(self, fp): try: os.remove(fp) except: logger.error("Delete svc %s file[%s] failed.", self.svc_name, fp) return False return True
def generate(svc_obj, template, output, auth = None): config_path = path.dirname(output) if not path.exists(config_path): logger.error('%s not exists.', config_path) raise Exception(config_path + ' Not Exists.') with open(output, 'w+') as f: generate_to_fp(svc_obj, template, f)
def delete(self, service_name): logger.debug("delete %s service", service_name) if not self.exists(service_name): # not exists, just return. logger.error('try delete %s but not exists', service_name) return self.configurations[service_name].delete() del self.configurations[service_name] # set service name to empty.
def container_start(self, cli, id=None, **kwargs): container = cli.containers.list(filters={'id': id}) if len(container) <= 0: logger.error('container started but not found %s', id) return for ctn in container: self.add_container(id, Container(ctn))
def parse_svc(self, key, val): svc_name, node_id, container_id = self.keydecoder.decode(key, extra=True) if not svc_name: logger.error('parse svc name from key fault: %s', key) return None svc = self.decoder.decode(val) svc.add_host(**svc.host, node_id=node_id, container_id=container_id) svc.name = svc_name return svc
def remove_container(self, id): self.lock.acquire() container = None if id in self.containers: container = self.containers[id] del self.containers[id] else: logger.error('remove container but not exists %s, just ignore.') if self.notifier: self.notifier.del_service(container) self.lock.release()
def merge(self, svc): if svc.name != self.name: logger.error( 'merge service failed, service name not match.(%s != %s)', svc.name, self.name) return if not svc.hosts: logger.error('merge empty hosts.') return for host in svc.hosts: self.add_host(**host)
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 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)")