async def post(self): """增加指定角色的权限 """ body = self.get_body_json() role = self.db.query(Role).filter_by(name=body["role"]).first() if not role: role = Role(name=body["role"]) self.db.add(role) self.db.commit() perms = [] for perm_name in body["permissions"]: perm = self.db.query(Permission).filter_by(name=perm_name).first() if not perm: perm = Permission(name=perm_name) self.db.add(perm) self.db.commit() perms.append(perm) if not perms: self.fail("no-permissions") return # sync to etcd if settings.SYCN_ETCD: # create etcd client for endpoint in settings.ETCD_ENDPOINTS.split(";"): host, port = endpoint.split(":") client = Client(host, int(port)) break # FIXME: try when failed key = get_permission_role_key(perm.name) new_roles = [] r = client.range(key) if r.count == 1: new_roles.extend(json.loads(r.kvs[0].value)) if role.name not in new_roles: new_roles.append(role.name) client.put(key, json.dumps(new_roles)) # append permissions role.permissions.extend(perms) self.db.commit() self.success()
class etcdConnect(): def __init__(self, log, server_ip, cert_file, key_file, port=5002): self.log = log self.server_ip = server_ip self.port = port self.cert_file = cert_file self.key_file = key_file self.client = None print(self.server_ip, self.port, self.cert_file, self.key_file) self.client = Client(self.server_ip, self.port, cert=(self.cert_file, self.key_file), verify=False) print(self.client) def get_cluster_version(self, ): return (self.client.cluster_version) def get_cluster_dict(self, ): cluster_dict = {} cobj = self.client.member_list() cluster_dict['cluster_id'] = cobj.header.cluster_id cluster_dict['member_dict'] = {} for mem_obj in cobj.members: mem_id = str(mem_obj.ID) cluster_dict['member_dict'][mem_id] = {} cluster_dict['member_dict'][mem_id]['name'] = mem_obj.name cluster_dict['member_dict'][mem_id]['peerurls'] = mem_obj.peerURLs[ 0] cluster_dict['member_dict'][mem_id][ 'clienturls'] = mem_obj.clientURLs print(cluster_dict) return cluster_dict def get_cluster_detailed_metrics(self, ): metrics_dict = {} output = self.client.metrics_raw() for line in output.split("\n"): if not re.search('^#', line): if re.search('([a-zA-Z\_]+)[\s]+([0-9\.\+e]+)', line): match = re.search('([a-zA-Z\_]+)[\s]+([0-9\.\+e]+)', line) metrics_dict[match.group(1)] = match.group(2) print(metrics_dict) return metrics_dict def get_venice_config_count_dict(self, etcd_key_list=venice_etcd_keys): venice_config_count_dict = {} for key_nam in etcd_key_list: obj = self.client.range(key=key_nam, prefix=True, count_only=True) venice_config_count_dict[key_nam] = int(obj.count) print(venice_config_count_dict) return venice_config_count_dict def get_venice_config_dict(self, etcd_key_list=venice_etcd_keys): venice_config_dict = {} for key_nam in etcd_key_list: obj = self.client.range(key=key_nam, prefix=True) venice_config_dict[key_nam] = obj.kvs print(venice_config_dict) return venice_config_dict def get_venice_config_value(self, key_prefix): obj = self.client.range(key=key_prefix, prefix=True) return obj.kvs