class AuthServe(object): def __init__(self, exchange="auth", exchange_type="topic"): self.host = conf_dict['master'] # 消息队列IP self.port = conf_dict['publish_port'] # 消息队列端口 self.minion_id = conf_dict['id'] # minion端的唯一ID self.exchange = exchange # 设置交换器名称 self.exchange_type = exchange_type # 设置交换器类型为关键字类型 self.message = {} # 初始化空消息字典 self.auth = MQServer(self.host, self.port, self.exchange, self.exchange_type) # 实例化消息对象 if not self.acl_auth(): # 检查客户端是否已授权 self.request_auth() # 开始请求授权 self.get_notice_auth() # 获取授权状态 self.message = {} # 重置消息 def acl_auth(self): if self.message and type(self.message) is dict: # 如果有消息,且类型是字典型 data = self.message # data值则为self.message else: data = database.get(self.minion_id) # 否则通过可持久化数据里找 if type(data) is not dict: # 如果找不到相关的则返回 return False is_auth = data.get("auth") # 获取授权状态 if is_auth is not None: if is_auth is True: return True # 只有授权了才返回True return False def request_auth(self): """ 请求授权 :return: """ auth = {"auth": "syn", "id": self.minion_id} self.message = bytes(str(auth), "utf8") # 转换消息编码 self.auth.publish(self.exchange, routing_key="auth", body=self.message) # 发布一条消息 def get_notice_auth(self): """ 获取授权状态 :return: """ self.exchange = self.minion_id # 设置交换器名以minion_id为命名,只能是为该ID的minion才能收到此消息 self.auth = MQServer(self.host, self.port, self.exchange, self.exchange_type) self.auth.consumer(exchange=self.exchange, routing_key="auth", callback=self.callback) # 获取一条消息 def callback(self, ch, method, properties, body): print("get {} message {}".format(method.routing_key, body)) self.message = yaml.load(str(body, "utf-8")) if self.acl_auth(): # 如果检查该客户端未授权,则进入授权操作 database[self.minion_id] = self.message connect_database["data"] = database self.auth.channel.close() # 关闭通道 self.auth.close() # 关闭连接
class CmdServe(object): def __init__(self, minion_id=server, exchange="cmd", exchange_type="topic"): self.host = conf_dict['master'] self.port = conf_dict['publish_port'] self.minion_id = minion_id self.exchange = exchange self.exchange_type = exchange_type self.message = {} self.auth = MQServer(self.host, self.port, self.exchange, self.exchange_type) if self.acl_auth(): self.run() self.get_request() self.message = {} # 重置消息 else: print( "\n\033[31;1mminion does not exists or has not authorized\033[0m\n" ) def acl_auth(self): data = database.get(self.minion_id) if type(data) is not dict: return False is_auth = data.get("auth") if is_auth is not None: if is_auth is True: return True return False def run(self): cmd = {"cmd": command} self.message = bytes(str(cmd), "utf8") self.auth.publish(self.exchange, routing_key=self.minion_id, body=self.message) def get_request(self): self.auth = MQServer(self.host, self.port, self.exchange, self.exchange_type) self.auth.consumer(exchange=self.exchange, routing_key=self.minion_id, callback=self.callback) def callback(self, ch, method, properties, body): print("{}\n[{}]:\n".format("".ljust(20, "-"), method.routing_key)) self.message = eval(str(body, "utf-8")) print(str(self.message["cmd_result"], "utf-8")) self.auth.channel.close() self.auth.close()
class CmdServe(object): def __init__(self, exchange="cmd", exchange_type="topic"): self.host = conf_dict['master'] self.port = conf_dict['publish_port'] self.minion_id = conf_dict["id"] self.exchange = exchange # 设置交换器名称 self.exchange_type = exchange_type # 设置交换器类型 self.message = {} # 初始化消息 self.auth = MQServer(self.host, self.port, self.exchange, self.exchange_type) if self.acl_auth(): # 判断自己是否已授权,未授权则不回应 self.get_request() # 然后去接收服务端响应 self.run() self.message = {} # 重置消息 def acl_auth(self): data = database.get(self.minion_id) if type(data) is not dict: return False is_auth = data.get("auth") if is_auth is not None: if is_auth is True: return True return False def run(self): self.message = bytes(str(self.message), "utf8") self.auth = MQServer(self.host, self.port, self.exchange, self.exchange_type) self.auth.publish(self.exchange, routing_key=self.minion_id, body=self.message) def get_request(self): self.auth.consumer(exchange=self.exchange, routing_key=self.minion_id, callback=self.callback) def callback(self, ch, method, properties, body): print("get {} message {}".format(method.routing_key, body)) self.message = yaml.load(str(body, "utf-8")) result = subprocess.Popen(self.message["cmd"], shell=True, stdout=subprocess.PIPE) result = result.stdout.read() self.message["cmd_result"] = result self.auth.channel.close() self.auth.close()
class CmdServe(object): def __init__(self, minion_id=server, exchange="cmd", exchange_type="topic"): self.host = conf_dict['master'] self.port = conf_dict['publish_port'] self.minion_id = minion_id self.exchange = exchange self.exchange_type = exchange_type self.message = {} self.auth = MQServer(self.host, self.port, self.exchange, self.exchange_type) if self.acl_auth(): self.run() self.get_request() self.message = {} # 重置消息 else: print("\n\033[31;1mminion does not exists or has not authorized\033[0m\n") def acl_auth(self): data = database.get(self.minion_id) if type(data) is not dict: return False is_auth = data.get("auth") if is_auth is not None: if is_auth is True: return True return False def run(self): cmd = {"cmd": command} self.message = bytes(str(cmd), "utf8") self.auth.publish(self.exchange, routing_key=self.minion_id, body=self.message) def get_request(self): self.auth = MQServer(self.host, self.port, self.exchange, self.exchange_type) self.auth.consumer(exchange=self.exchange, routing_key=self.minion_id, callback=self.callback) def callback(self, ch, method, properties, body): print("{}\n[{}]:\n".format("".ljust(20, "-"), method.routing_key)) self.message = eval(str(body, "utf-8")) print(str(self.message["cmd_result"], "utf-8")) self.auth.channel.close() self.auth.close()
class AuthServe(object): def __init__(self, exchange="auth", exchange_type="topic"): self.host = conf_dict['master'] self.port = conf_dict['publish_port'] self.exchange = exchange self.exchange_type = exchange_type self.message = {} self.auth = MQServer(self.host, self.port, self.exchange, self.exchange_type) self.approval_auth() connect_database.close() def callback(self, ch, method, properties, body): """ 解析队列消息 :param ch: :param method: :param properties: :param body: :return: """ print("get {} message {}".format(method.routing_key, body)) self.message = yaml.load(str(body, "utf-8")) if not self.acl_auth(): # 如果检查该客户端未授权,则进入授权操作 self.request_auth() self.auth.channel.close() # 关闭通道 self.auth.close() # 关闭连接 self.notice_auth() def approval_auth(self): """ 接受授权请求 :return: """ self.auth.consumer(exchange=self.exchange, routing_key="auth", callback=self.callback) def request_auth(self): """ 标记授权状态 :return: """ request = None while True: request = str(input("是否授权? yes/no: ")).strip() if request.lower() == "yes": request = True break elif request.lower() == "no": request = False break if type(self.message) == dict: self.message["auth"] = request minion_id = self.message.get("id") if minion_id: database[minion_id] = self.message connect_database["data"] = database self.exchange = minion_id print("{}\n[{}]:\n\t[\033[33;1m{}\033[0m]".format("".ljust(20, "-"), minion_id, self.message["auth"])) self.message = bytes(str(self.message), "utf8") def notice_auth(self): """ 通告授权状态 :return: """ ack_auth = MQServer(self.host, self.port, self.exchange, self.exchange_type) ack_auth.publish(self.exchange, routing_key="auth", body=self.message) def acl_auth(self): """ 检查auth状态 :return: """ if type(self.message) is not dict: return False is_auth = self.message.get("auth") if is_auth is not None: if is_auth is True: return True return False