def release(self, connection): "Release the connection back to the pool, meanwhile check validation" discard = False if (connection.host, connection.port) not in self.proxy_list: discard = True # Make sure we haven't changed process. self._checkpid() if connection.pid != self.pid: return # Put the connection back into the pool. try: if not discard: self.pool.put_nowait(connection) log_debug("put connection %s:%s back to pool" % (connection.host, connection.port)) else: connection.disconnect() self._connections.remove(connection) self.pool.put_nowait(None) log_warning("discard connection %s:%s" % (connection.host, connection.port)) except Full: # perhaps the pool has been reset() after a fork? regardless, # we don't want this connection pass
def power(self, params): remote = self.get_remote() token = self.get_token() log_debug('systems[{0}] power{1}'.format(params.get('systems'), params.get('power'))) result = remote.background_power_system(params, token) return result
def modify(self, system_name, params): remote = self.get_remote() token = self.get_token() log_debug('modify system params:{0}'.format(params)) interfaces = {} if params.has_key('interfaces'): interfaces = params.pop('interfaces') if not remote.has_item(self.TYPE, system_name): log_error('System {0} not found'.format(system_name)) system_id = remote.get_system_handle(system_name, token) for key, val in params.items(): log_info("set params {0} = {1}".format(key, val)) remote.modify_system(system_id, key, val, token) for interface_name, params in interfaces.items(): temp_dict = {} log_info("struct interface params {0}".format(interface_name)) for key, val in params.items(): temp_dict['%s-%s' % (key, interface_name)] = val log_info("update interface {0}".format(temp_dict)) remote.modify_system(system_id, 'modify_interface', temp_dict, token) del temp_dict log_info("save system {0}".format(system_id)) remote.save_system(system_id, token) log_info("sync system info") sync_task = remote.sync(token) return sync_task
def power(self, params): remote = self.get_remote() token = self.get_token() log_debug('systems[{0}] power{1}'.format( params.get('systems'), params.get('power'))) result = remote.background_power_system(params, token) return result
def default_echo_handler(message, ovsconn): log_debug("responding to echo") ovsconn.send({ "result": message.get("params", None), "error": None, "id": message['id'] })
def make_connection(self): "Make a fresh random connection from proxy list." host, port = random.choice(self.proxy_list) self.connection_kwargs.update({'host': host, 'port': port}) connection = self.connection_class(**self.connection_kwargs) log_debug("choose HostAndPort %s:%s from zk proxy path" % (host, port)) self._connections.append(connection) return connection
def _refresh_alicdn(self, domain, files): args = {} args['Action'] = 'RefreshObjectCaches' args['ObjectType'] = 'File' for f in files: args['ObjectPath'] = domain + '/' + f log_debug('flush http://%s' % args['ObjectPath']) try: log_debug(json.dumps(args)) aliyun = Connection(region_id='cn-hangzhou', service='cdn') aliyun.get(args) except Exception as e: log_error(e) raise Exception(e) return True
def _do_job(job_id=None, is_retry=False): if job_id is None: return server_group = {} if is_retry: res = Tasks.select().where((Tasks.job == job_id) & (( Tasks.result_status == 2) | (Tasks.result_status == 0))) else: res = Tasks.select().where((Tasks.job == job_id) & ( Tasks.result_status == 2)) for item in res: if not item.group in server_group.keys(): server_group[item.group] = [] server_group[item.group].append(item.server_ip) for key, item in enumerate(server_group): lb_ip = item app_ip_list = server_group.get(lb_ip) for app_ip in app_ip_list: log_debug(app_ip)
def _on_remote_message(self, message): log_debug("message %s", message) try: json_m = json.loads(message, object_pairs_hook=collections.OrderedDict) # handler for it handler_method = json_m.get('method', None) if handler_method: self.handlers.get(handler_method, self.default_message_handler)(json_m, self) elif json_m.get("result", None) and json_m['id'] in self.callbacks: id = json_m['id'] if not self.callbacks[id](json_m, self): self.callbacks.pop(id) else: self.default_message_handler(message, self) except Exception as e: log_exception("exception [%s] while handling message [%s]", e.message, message)
def _on_remote_message(self, message): log_debug("message %s", message) try: json_m = json.loads( message, object_pairs_hook=collections.OrderedDict) # handler for it handler_method = json_m.get('method', None) if handler_method: self.handlers.get(handler_method, self.default_message_handler)(json_m, self) elif json_m.get("result", None) and json_m['id'] in self.callbacks: id = json_m['id'] if not self.callbacks[id](json_m, self): self.callbacks.pop(id) else: self.default_message_handler(message, self) except Exception as e: log_exception("exception [%s] while handling message [%s]", e.message, message)
def add(self, req, resp): try: cmd = req.get_param(name='c') ip = req.get_param(name='i') timeout = req.get_param(name='5') or 3 if 'c' is None: return '-c(cmd) require' if 'i' is None: return '-i(ip) require' data = {'value': cmd.encode('utf-8')} req = requests.post( url="http://%s/v2/keys%s/servers/%s/" % (self.etcd_config['server'], self.etcd_config['prefix'], ip), data=data) ret = req.json() index = str(ret['node']['createdIndex']) self.cmdkeys[index] = '' start = time.time() if ret['node']['value'] == cmd: while True: if (time.time() - start > timeout ) or self.cmdkeys[index] != '': log_debug('%s timeout' % data) break else: time.sleep(0.1) if self.cmdkeys[index] != '': ret = self.cmdkeys[index] del self.cmdkeys[index] return ret return '(success) submit command success' else: return '(unsafe) submit command success ' except Exception as er: log_error(er) return 'fail'
def default_message_handler(message, ovsconn): log_debug("default handler called for method %s", message['method']) ovsconn.responses.append(message)