def salt_pull(self, data, uid): shell = data.get("shell") host = data.get("host") node_list = [host[i] for i in host.keys()] # 自动化发布普通任务 token_api_id = token_id() tgt = data.get("tgt") if tgt: shell = "%s %s" % (shell, tgt) print shell code_checkout = SaltApiGit(arg=shell, tgt=node_list, token_api_id=token_api_id) else: code_checkout = SaltApiGit(arg=shell, tgt=node_list, token_api_id=token_api_id) rst = code_checkout.CmdRun() result = rst["return"][0] for k, v in result.items(): self.web_socket_api(k, uid) self.web_socket_api(v, uid) self.web_socket_api(u"shell执行完毕", uid) return True
def TomcatRestart(self, data, uid): host = data.get("host") choose = int(data.get("choose")) if choose == 3: # 自动化发布普通任务 token_api_id = token_id() # 将主机名放入list 传给salt api接口 node_list = [host[i] for i in host.keys()] tomcat_init = "%s stop" % data.get("tomcat_init") code_checkout = SaltApiGit(arg=tomcat_init, tgt=node_list, token_api_id=token_api_id) code_checkout.CmdRun() cache = data.get("cache").split() for i in cache: i_path = "rm -rf %s" % i code_checkout = SaltApiGit(arg=i_path, tgt=node_list, token_api_id=token_api_id) code_checkout.CmdRun() message = u"%s 目录清空完毕" % i self.web_socket_api(message, uid) tomcat_init = "%s restart" % data.get("tomcat_init") code_checkout = SaltApiGit(arg=tomcat_init, tgt=node_list, token_api_id=token_api_id) code_checkout.CmdRun() # self.web_socket_api(yaml.dump(rst, default_flow_style=False), uid) self.web_socket_api(u"tomcat重启完毕", uid) else: print "is not java" return True
def NodePull(self, data, uid): print "无git 中继模式发布" user = "******" % data.get("git_code_user") host = data.get("host") version = data.get("git_version") if not version: self.web_socket_api(u"发布失败选择分支名称或参数", uid) return False choose = data.get("choose") # 自动化发布普通任务 token_api_id = token_id() # 将主机名放入list 传给salt api接口 node_list = [host[i] for i in host.keys()] code_path = data.get("code_path") shell = data.get("shell") shell_status = int(data.get("shell_status")) if shell and shell_status == 0: print "需要先执行脚本 " code_checkout = SaltApiGit(arg=shell, tgt=node_list, token_api_id=token_api_id) rst = code_checkout.CmdRun() self.web_socket_api(yaml.dump(rst, default_flow_style=False), uid) self.web_socket_api(u"脚本执行完毕", uid) # 如未选择版本,默认为master分支 print "有无版本号" print version if version: node_code_checkout = SaltApiGit( tgt=node_list, arg=[code_path, version, user], token_api_id=token_api_id ) print node_code_checkout.checkout() print user code_pull = SaltApiGit( tgt=node_list, arg=[code_path, user], token_api_id=token_api_id ) code_pull.pull() else: node_code_checkout = SaltApiGit( tgt=node_list, arg=[code_path, "master", user], token_api_id=token_api_id ) node_code_checkout.checkout() code_pull = SaltApiGit( tgt=node_list, arg=[code_path, user], token_api_id=token_api_id, user=data.get("git_code_user") ) code_pull.pull() version_status = SaltApiGit(tgt=node_list, arg=code_path, token_api_id=token_api_id) rst = version_status.version().get("return")[0] for i in host.keys(): ip = host.get(i, False) host_rst = rst.get(ip, False) message = u'%s 版本号: %s' % (i, host_rst) self.web_socket_api(message, uid) self.web_socket_api(u"代码更新代完毕", uid) if shell and shell_status == 1: print "需要先执行脚本 " code_checkout = SaltApiGit(arg=shell, tgt=node_list, token_api_id=token_api_id) rst = code_checkout.CmdRun() self.web_socket_api(yaml.dump(rst, default_flow_style=False), uid) self.web_socket_api(u"脚本执行完毕", uid) return True
def salt_pull(self, data, uid, agent=True): host = data.get("host") version = data.get("git_version", False) print version if not version: self.web_socket_api(u"发布失败选择分支名称或参数", uid) return False arg = data.get("arg") CheckUrl = data.get("CheckUrl") # 自动化发布普通任务 token_api_id = token_id() # 将主机名放入list 传给salt api接口 node_list = [host[i] for i in host.keys()] git_code_path = "%s%s" % (data.get("git_minion_path"), data.get("code_name")) code_path = data.get("code_path") user = "******" % data.get("git_code_user") shell = data.get("shell") shell_status = int(data.get("shell_status")) if shell and shell == 0: print "需要先执行脚本 " code_checkout = SaltApiGit(arg=shell, tgt=node_list, token_api_id=token_api_id) rst = code_checkout.CmdRun() result = rst["return"][0] for k, v in result.items(): self.web_socket_api(k, uid) self.web_socket_api(v, uid) # self.web_socket_api(yaml.dump(rst, default_flow_style=False), uid) self.web_socket_api(u"脚本执行完毕", uid) # 如未选择版本,默认为master分支 # print u"选择", version, u"版本哦" if agent: for i in range(0, 2): code_pull = SaltApiGit(arg=[git_code_path, "origin"], token_api_id=token_api_id, tgt=data.get("git_minion") ) code_pull.pull() if i == 1: # self.web_socket_api(yaml.dump(status, default_flow_style=False), uid) self.web_socket_api(u"拉取代码完成", uid) code_checkout = SaltApiGit(arg=[git_code_path, version], tgt=data.get("git_minion"), token_api_id=token_api_id) code_checkout.checkout() if i == 1: self.web_socket_api(u"切分支完分完毕", uid) code_push = SaltApiGit(tgt=data.get("git_minion"), arg=[git_code_path, "ops", version, "-f"], token_api_id=token_api_id ) push_status = code_push.push() self.web_socket_api(yaml.dump(push_status, default_flow_style=False), uid) version_status = SaltApiGit(tgt=data.get("git_minion"), arg=[git_code_path, version], token_api_id=token_api_id) git_master_version = version_status.version()["return"][0][data.get("git_minion")] message = "发布版本号: %s" % git_master_version self.web_socket_api(message, uid) time.sleep(1) self.web_socket_api(u"push远程分支完成", uid) self.web_socket_api(u"等待10秒........", uid) time.sleep(10) self.web_socket_api(u"通知需要更新代码的主机开始拉取代码........", uid) else: self.web_socket_api(u"Error: 发布中止->请选择分支或参数", uid) return False if arg == "Single": for i in host.keys(): setname = SaltApiGit( tgt=node_list, arg=[code_path, "user.name", "ops", user], token_api_id=token_api_id ) setname.config_set() setname = SaltApiGit( tgt=node_list, arg=[code_path, "user.email", "*****@*****.**", user], token_api_id=token_api_id ) self.web_socket_api(u"开始拉取代码........", uid) setname.config_set() code_pull = SaltApiGit( tgt=node_list, arg=[code_path, "origin", user], token_api_id=token_api_id ) code_pull.pull() node_code_checkout = SaltApiGit( tgt=host[i], arg=[code_path, version, user], token_api_id=token_api_id ) node_code_checkout.checkout() self.web_socket_api(u"%s 代码下发完成" % host[i], uid) self.web_socket_api(u"判断是否需要检测接口........", uid) if CheckUrl: swan_status = True count = 0 while count < 6: result = CheckApi(ip=host[i], url=CheckUrl) # result = CheckApi(ip="192.168.111.6", url=CheckUrl) rst, retMsg = result.run() if rst: print u"接口检测正常,开始发布下一台服务器" self.web_socket_api(u"%s 接口检测正常" % host[i], uid) count += 10 swan_status = True else: self.web_socket_api(u"%s 接口检测异常,10秒钟后重试-> 接口返回信息:%s" % (host[i], retMsg), uid) time.sleep(10) count += 1 swan_status = False if not swan_status: self.web_socket_api(u"%s 接口1分钟检测全部异常,发布中止" % host[i], uid) return False else: self.web_socket_api(u"不用检测接口,发布完成........", uid) return True else: setname = SaltApiGit( tgt=node_list, arg=[code_path, "user.name", "ops", user], token_api_id=token_api_id ) setname.config_set() setname = SaltApiGit( tgt=node_list, arg=[code_path, "user.email", "*****@*****.**", user], token_api_id=token_api_id ) setname.config_set() self.web_socket_api(u"开始拉取代码........", uid) code_pull = SaltApiGit( tgt=node_list, arg=[code_path, "origin", user], token_api_id=token_api_id ) code_pull.pull() node_code_checkout = SaltApiGit( tgt=node_list, arg=[code_path, version], token_api_id=token_api_id ) print node_code_checkout.checkout() self.web_socket_api(u"拉取代码已完成,正在读取版本号......", uid) version_status = SaltApiGit(tgt=node_list, arg=code_path, token_api_id=token_api_id) rst = version_status.version().get("return")[0] self.web_socket_api(u"版本号读取完成......", uid) self.web_socket_api(u"开始验证版本号......", uid) if agent: for i in host.keys(): ip = host.get(i, False) host_rst = rst.get(ip, False) if host_rst and host_rst == git_master_version: message = u'%s 版本号: %s 发布成功' % (i, host_rst) self.web_socket_api(message, uid) elif host_rst and host_rst != git_master_version: message = u'%s 版本号: %s 发布失败' % (i, host_rst) self.web_socket_api(message, uid) else: message = u"%s未返回数据" % i self.web_socket_api(message, uid) else: for i in host.keys(): ip = host.get(i, False) host_rst = rst.get(ip, False) message = u'%s 版本号: %s' % (i, host_rst) self.web_socket_api(message, uid) self.web_socket_api(u"通知主机更新代完毕", uid) if shell and shell_status == 1: print "需要先执行脚本 " code_checkout = SaltApiGit(arg=shell, tgt=node_list, token_api_id=token_api_id) rst = code_checkout.CmdRun() self.web_socket_api(yaml.dump(rst, default_flow_style=False), uid) self.web_socket_api(u"脚本执行完毕", uid) return True
def NodeReset(self, data, uid): print "开始发布" user = "******" % data.get("git_code_user") host = data.get("host") version = data.get("git_version") reset_code = data.get("reset_code") # 自动化发布普通任务 token_api_id = token_id() # 将主机名放入list 传给salt api接口 node_list = [host[i] for i in host.keys()] code_path = data.get("code_path") # 如未选择版本,默认为master分支 if reset_code: setname = SaltApiGit( tgt=node_list, arg=[code_path, "user.name", "ops", user], token_api_id=token_api_id ) setname.config_set() setname = SaltApiGit( tgt=node_list, arg=[code_path, "user.email", "*****@*****.**", user], token_api_id=token_api_id ) setname.config_set() branch_name = "opts='--hard %s'" % reset_code node_code_checkout = SaltApiGit( tgt=node_list, arg=[code_path, version, user], token_api_id=token_api_id ) node_code_checkout.checkout() code_pull = SaltApiGit( tgt=node_list, arg=[code_path, branch_name, user], token_api_id=token_api_id ) print "开始回滚" rst = code_pull.reset() self.web_socket_api(yaml.dump(rst, default_flow_style=False), uid) self.web_socket_api(u"代码回滚代完毕", uid) version_status = SaltApiGit(tgt=node_list, arg=code_path, token_api_id=token_api_id) rst = version_status.version().get("return")[0] for i in host.keys(): ip = host.get(i, False) host_rst = rst.get(ip, False) message = u'%s 版本号: %s' % (i, host_rst) self.web_socket_api(message, uid) return True else: self.web_socket_api(u"请输入回滚版本号", uid) return False
def NodePull(self, data, uid): print "开始发布" user = "******" % data.get("git_code_user") host = data.get("host") version = data.get("git_version") # 自动化发布普通任务 token_api_id = token_id() # 将主机名放入list 传给salt api接口 node_list = [host[i] for i in host.keys()] code_path = data.get("code_path") # 如未选择版本,默认为master分支 shell = data.get("shell") shell_status = int(data.get("shell_status")) if shell and shell_status == 0: print "需要先执行脚本 " code_checkout = SaltApiGit(arg=shell, tgt=node_list, token_api_id=token_api_id) rst = code_checkout.CmdRun() self.web_socket_api(yaml.dump(rst, default_flow_style=False), uid) self.web_socket_api(u"脚本执行完毕", uid) if version: setname = SaltApiGit( tgt=node_list, arg=[code_path, "user.name", "ops", user], token_api_id=token_api_id ) setname.config_set() setname = SaltApiGit( tgt=node_list, arg=[code_path, "user.email", "*****@*****.**", user], token_api_id=token_api_id ) setname.config_set() code_pull = SaltApiGit( tgt=node_list, arg=[code_path, "origin"], token_api_id=token_api_id ) code_pull.pull() node_code_checkout = SaltApiGit( tgt=node_list, arg=[code_path, version, user], token_api_id=token_api_id ) node_code_checkout.checkout() version_status = SaltApiGit(tgt=node_list, arg=code_path, token_api_id=token_api_id) rst = version_status.version().get("return")[0] for i in host.keys(): ip = host.get(i, False) host_rst = rst.get(ip, False) message = u'%s 版本号: %s' % (i, host_rst) self.web_socket_api(message, uid) self.web_socket_api(u"代码更新代完毕", uid) if shell and shell_status == 1: print "需要先执行脚本 " code_checkout = SaltApiGit(arg=shell, tgt=node_list, token_api_id=token_api_id) rst = code_checkout.CmdRun() self.web_socket_api(yaml.dump(rst, default_flow_style=False), uid) self.web_socket_api(u"脚本执行完毕", uid) return True else: self.web_socket_api(u"Error: 发布中止->请选择分支或参数", uid) return False
def salt_pull(self, data, uid): host = data.get("host") version = data.get("git_version") choose = int(data.get("choose")) # 自动化发布普通任务 token_api_id = token_id() # 将主机名放入list 传给salt api接口 node_list = [host[i] for i in host.keys()] git_code_path = "%s%s" % (data.get("git_minion_path"), data.get("code_name")) code_path = data.get("code_path") user = "******" % data.get("git_code_user") shell = data.get("shell") shell_status = int(data.get("shell_status")) if shell and shell_status == 0: print "需要先执行脚本 " code_checkout = SaltApiGit(arg=shell, tgt=node_list, token_api_id=token_api_id) rst = code_checkout.CmdRun() result = rst["return"][0] for k, v in result.items(): self.web_socket_api(k, uid) self.web_socket_api(v, uid) # self.web_socket_api(yaml.dump(rst, default_flow_style=False), uid) self.web_socket_api(u"脚本执行完毕", uid) # 如未选择版本,默认为master分支 if version: code_pull = SaltApiGit(arg=[git_code_path, "origin"], token_api_id=token_api_id, tgt=data.get("git_minion") ) status = code_pull.pull() self.web_socket_api(yaml.dump(status, default_flow_style=False), uid) self.web_socket_api(u"拉取代码完成", uid) code_checkout = SaltApiGit(arg=[git_code_path, version], tgt=data.get("git_minion"), token_api_id=token_api_id) print code_checkout.checkout() self.web_socket_api(u"切分支完分完毕", uid) code_push = SaltApiGit(tgt=data.get("git_minion"), arg=[git_code_path, "ops", version, "-f"], token_api_id=token_api_id ) push_status = code_push.push() self.web_socket_api(yaml.dump(push_status, default_flow_style=False), uid) version_status = SaltApiGit(tgt=data.get("git_minion"), arg=[git_code_path, version], token_api_id=token_api_id) git_master_version = version_status.version()["return"][0][data.get("git_minion")] message = u"发布版本号: %s" % git_master_version self.web_socket_api(message, uid) else: self.web_socket_api(u"Error: 发布中止->请选择分支或参数", uid) return False time.sleep(1) self.web_socket_api(u"push远程分支完成", uid) self.web_socket_api(u"等待10秒........", uid) time.sleep(10) self.web_socket_api(u"通知需要更新代码的主机开始拉取代码........", uid) if version: u""" 添加git user config """ setname = SaltApiGit( tgt=node_list, arg=[code_path, "user.name", "ops", user], token_api_id=token_api_id ) setname.config_set() setname = SaltApiGit( tgt=node_list, arg=[code_path, "user.email", "*****@*****.**", user], token_api_id=token_api_id ) setname.config_set() self.web_socket_api(u"开始拉取代码........", uid) code_pull = SaltApiGit( tgt=node_list, arg=[code_path, "origin", user], token_api_id=token_api_id ) print code_pull.pull() node_code_checkout = SaltApiGit( tgt=node_list, arg=[code_path, version], token_api_id=token_api_id ) print(node_code_checkout.checkout()) else: self.web_socket_api(u"Error: 发布中止->请选择分支或参数", uid) return False self.web_socket_api(u"拉取代码已完成,正在读取版本号......", uid) version_status = SaltApiGit(tgt=node_list, arg=code_path, token_api_id=token_api_id) rst = version_status.version().get("return")[0] self.web_socket_api(u"版本号读取完成......", uid) self.web_socket_api(u"开始验证版本号......", uid) for i in host.keys(): ip = host.get(i, False) print ip host_rst = rst.get(ip, False) if host_rst and host_rst == git_master_version: message = u'%s 版本号: %s 发布成功' % (i, host_rst) self.web_socket_api(message, uid) elif host_rst and host_rst != git_master_version: message = u'%s 版本号: %s 发布失败' % (i, host_rst) self.web_socket_api(message, uid) else: message = u"%s未返回数据" % i self.web_socket_api(message, uid) self.web_socket_api(u"通知主机更新代完毕", uid) if shell and shell_status == 1: code_checkout = SaltApiGit(arg=shell, tgt=node_list, token_api_id=token_api_id) rst = code_checkout.CmdRun() self.web_socket_api(yaml.dump(rst, default_flow_style=False), uid) self.web_socket_api(u"脚本执行完毕", uid) return True return True