def reset(self): code_pull = salt_api_token( { 'client': 'local', 'fun': 'git.reset', 'tgt': self.data.get("tgt"), "arg": self.data.get("arg"), 'timeout': 100, 'expr_form': 'list' }, salt_api_url, {"X-Auth-Token": self.data.get("token_api_id")}) rst = code_pull.run() return rst
def setemail(self): """执行""" code_checkout = salt_api_token( { 'client': 'local', 'fun': 'git.config_set', 'tgt': self.data.get("tgt"), "arg": self.data.get("arg"), 'timeout': 100, 'expr_form': 'list' }, salt_api_url, {"X-Auth-Token": self.data.get("token_api_id")}) rst = code_checkout.run() return rst
def CmdRun(self): code_pull = salt_api_token( { 'client': 'local', 'fun': 'cmd.run', 'tgt': self.data.get("tgt"), "arg": [self.data.get("arg"), "env='{\"LC_ALL\": \"zh_CN.UTF-8\"}'"], 'timeout': 100, 'expr_form': 'list' }, salt_api_url, {"X-Auth-Token": self.data.get("token_api_id")}) rst = code_pull.run() return rst
def post(self): """ 接收数据后确认返回收到消息 异步传给后端逻缉进行处理 """ print "接收到数据" self.write(json.dumps({"retCode": 200, "result": "OK"}, indent=4)) self.finish() data = json.loads(self.request.body) uid = data.get("uid") if data.get("choose") == 0: script = data.get("script") swan_name = data.get("code_name") host = data.get("host") tgt = data.get("tgt") arg = data.get("arg") yield self.web_socket_api("正常业务发布,发布正在运行中,逻缉比较多,请耐心等待", uid) # 自动化发布普通任务 token_api_id = token_id() # 将主机名放入list 传给salt api接口 node_list = [host[i] for i in host.keys()] node_count = len(node_list) add_key = salt_api_token( {'client': 'local_async', 'fun': 'cmd.script', 'tgt': node_list, "arg": ["salt://%s" % (script), tgt], 'timeout': 100, 'expr_form': 'list'}, salt_api_url, {"X-Auth-Token": token_api_id} ) status = add_key.run() rst = yield self.web_socket_api("正在执行中", uid) if rst: print "已通知salt开始执行" jid_data = status.get("return") print jid_data jid_id = {"jid": ""} for i in jid_data: s = i["jid"] jid_id["jid"] = s yield self.web_socket_api("saltstack jid %s" % (s), uid) # rst = yield self.jobs_data_find(s, uid, len(node_list)) print "程序正在后台运行中" yield self.salt_job(jid_id, node_count, uid) yield self.web_socket_api("********************************************", uid) print "发布结束" # 选择配置中心则走以下逻缉 if data.get("choose") == 1: print "通知配置中心" status = {"retCode": 1} host = data.get("host") ip_data = data.get("ip_data") for i in ip_data: Open_push = open_service(data.get("config_name"), i) rest = Open_push.offline_run() msg = rest.get("retMsg") if int(rest.get("retCode")) == 200: # if 200 == 200: yield self.web_socket_api("下线成功", uid) print "下线成功" else: status["retCode"] = 0 yield self.web_socket_api("下线失败,发布任务停止", uid) yield self.web_socket_api(msg, uid) print "下线失败,发布任务停止" break # 发布操作 if status["retCode"]: print "请求salt开始执行salt文件" yield self.web_socket_api("发布代码中....", uid) # 执行模块 token_api_id = token_id() print data.get("sls") list = salt_api_token( {'client': 'local_async', 'fun': 'state.sls', 'tgt': host.get(i), "arg": data.get("sls"), 'timeout': 100}, salt_api_url, {"X-Auth-Token": token_api_id} ) master_status = list.run() print master_status yield self.web_socket_api("saltstack执行完成", uid) print "已通知salt开始执行" yield self.web_socket_api("********************************************", uid) yield self.web_socket_api("检测端口中....", uid) open_port = data.get("check_port") p = "-p %s" % (str(open_port)) port_sum = len(open_port) start_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) print "-" * 100 print i ip = u"%s发布完成" % (i) rst = yield self.config_push(i, p, open_port, data.get("config_name"), uid) if rst: yield self.web_socket_api(ip, uid) rst = yield self.time_sleep(10) if rst: yield self.web_socket_api(ip, uid) print "发布完成" else: yield self.web_socket_api("发布失败", uid) if data.get("choose") == 2: print(uid) yield self.web_socket_api("正常业务发布,发布正在运行中,逻缉比较多,请耐心等待", uid) print("set ok")