def backup_db(host, db_user, db_password, backup_db_name): """ :param DB_PARAMS:传入的db参数格式如下 { "host": "xxxx", "db_user": "******", "db_password": "******", "backup_db_name": [] } :return: """ connect_host = { "hostname": host, "username": "******", "password": Server.objects.get(wan_ip=host).password, "port": 22 } print("链接参数显示:", connect_host) ssh = ssh_plugin.SSHConnect(connect_host) sftp = ssh_plugin.SFTPConnect(connect_host) result_list = [] for db_name in backup_db_name: result = ssh.run_command( """/usr/local/mysql5.6/bin/mysqldump -u{} -p{} {} > /tmp/{}""". format(db_user, db_password, db_name, db_name)) result_list.append(result) print(result_list) ssh.close()
def docker_local_registry(request): """ docker本地仓库列表 使用curl 检查url check_registry_url 获取到所有的repositories 使用curl 检查url http://local_registry_server:local_registry_port/v2/{获取的repository}/tags/list 获取到所有的tags 每次刷新页面的时候 都要返回值 前段页面返回的是一个json 用遍历key value的方法显示 :param request: :return: """ logged_user = request.session.get("login_info", "").get("name", "") logged_role = request.session.get("login_info", "").get("role", "") images_name = {} local_registry_server = "192.168.1.251" local_registry_port = "5000" check_registry_url = "http://{}:{}/v2/_catalog".format( local_registry_server, local_registry_port) host = { "hostname": local_registry_server, "username": "******", "password": Server.objects.get(wan_ip=local_registry_server).password, "port": 22 } ssh = ssh_plugin.SSHConnect(host) repository_result = eval( ssh.run_command("""curl -s {}""".format(check_registry_url))) all_repository = repository_result.get("repositories") for rep in all_repository: tag = eval( ssh.run_command("curl -s http://{}:{}/v2/{}/tags/list".format( local_registry_server, local_registry_port, rep))).get("tags") images_name[rep] = tag[0] ssh.close() return render(request, "docker_local_registry.html", locals())
def logs_gather(request): """ 检查日志的views, 显示固定行数日志到本地显示 :param request: :return: """ logged_user = request.session.get("login_info", "").get("name", "") logged_role = request.session.get("login_info", "").get("role", "") all_logs = ApplicationLogs.objects.all() if request.POST: logs_id = request.POST.get("logs_id", "") # lines 参数决定返回最后多少行的日志 lines = request.POST.get("lines", "") # 模糊查找后返回给前段的列表 点击列表再返回的具体的日志名字 logfile = request.POST.get("logfile", "") # 过滤的关键字 filter_keywords = request.POST.get("filter_words", "") selected_logs = ApplicationLogs.objects.get(id=logs_id) host = { "hostname": selected_logs.ApplicationOnTheServer.wan_ip, "username": "******", "password": selected_logs.ApplicationOnTheServer.password, "port": 22 } ssh = ssh_plugin.SSHConnect(host) check_log_files_cmd = """ls {} | grep {}""".format( selected_logs.PathOfLogs, selected_logs.LogsFileName) check_result = ssh.run_command(check_log_files_cmd) # 分段去除空白的项 check_result_list = check_result.split("\n")[:-1] # 前段有返回logfile的 # lambda 函数确定前端有没有返回过滤的关键字 如果有则拼接grep if logfile: check_cmd = """tail -n{} {} {}""".format( lines, os.path.join(selected_logs.PathOfLogs, logfile), (lambda x: "| grep " + x if x else "")(filter_keywords)) else: ssh.close() return HttpResponse("<br>".join(check_result_list)) # 替换成网页版的换行符 log_result = ssh.run_command(check_cmd).replace("\n", "<br>") ssh.close() return HttpResponse(log_result) return render(request, "logs_gather.html", locals())
def version_update_task(application_id): selected_application = Application.objects.get( id=int(application_id.split("_")[0])) application_time_flag = application_id.split("_")[1] application_tags = application_id.split("_")[2] application_option = application_id.split("_")[3] print("打印time flag", application_time_flag) # 运行本地拉取脚本 # pull_cmd = "/usr/bin/sh {} {} {} {} {}".format(selected_application.ApplicationUpdateScriptPath, # selected_application.ApplicationName, # (lambda x: x if x else "master")(selected_application.ApplicationBranch), # application_time_flag, application_tags) pull_cmd = "{} {} {} {} {}".format( selected_application.ApplicationUpdateScriptPath, selected_application.ApplicationName, (lambda x: x if x else "master")(selected_application.ApplicationBranch), application_time_flag, application_tags) print(pull_cmd) p = subprocess.Popen(pull_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout = p.stdout.read().decode("utf8") stderror = p.stderr.read().decode("utf8") # 传送脚本到remote服务器 #### host = { "hostname": selected_application.ApplicationServer.wan_ip, "username": "******", "password": selected_application.ApplicationServer.password, "port": 22 } transport = ssh_plugin.SFTPConnect(host) transport.send_file( selected_application.ApplicationUpdateScriptPathAfter, os.path.join( "/tmp", os.path.basename( selected_application.ApplicationUpdateScriptPathAfter))) # 传送tar文件到remote服务器 # transport.send_file("/root/workspace/{}/{}_{}.tar".format(selected_application.ApplicationName, # selected_application.ApplicationName, # application_time_flag), # "/tmp/{}_{}.tar".format(selected_application.ApplicationName, application_time_flag)) transport.send_file( "/tmp/workspace/{}/{}_{}.tar".format( selected_application.ApplicationName, selected_application.ApplicationName, application_time_flag), "/tmp/{}_{}.tar".format(selected_application.ApplicationName, application_time_flag)) transport.close() # 远程运行脚本 ## ssh = ssh_plugin.SSHConnect(host) cmd = "sh {} {} {} {}".format( os.path.join( "/tmp", os.path.basename( selected_application.ApplicationUpdateScriptPathAfter)), selected_application.ApplicationPath, application_time_flag, application_option) print(cmd) result = ssh.run_command(cmd) return (lambda x, y: x if x else y)(stdout, stderror) + result
def h5_update(request): """ 只处理h5的 更新 只处理applicationName=web_app的后台应用 除了post多传了一个sub_dir的参数外 其他的和version_update没有任何区别 :param request: :return: """ logged_user = request.session.get("login_info", "").get("name", "") logged_role = request.session.get("login_info", "").get("role", "") all_application = Application.objects.filter(ApplicationName="web_app") if request.POST: application_id = request.POST.get("application_id", "") sub_dir = request.POST.get("sub_dir", "") selected_application = Application.objects.get(id=int(application_id)) time_flag = str(int(time.time())) # 运行本地拉取脚本 pull_cmd = "/usr/bin/sh {} {} {} {}".format( selected_application.ApplicationUpdateScriptPath, selected_application.ApplicationName, (lambda x: x if x else "master")(selected_application.ApplicationBranch), time_flag) p = subprocess.Popen(pull_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout = p.stdout.read().decode("utf8") stderror = p.stderr.read().decode("utf8") # 传送脚本到remote服务器 #### host = { "hostname": selected_application.ApplicationServer.wan_ip, "username": "******", "password": selected_application.ApplicationServer.password, "port": 22 } try: transport = ssh_plugin.SFTPConnect(host) except paramiko.ssh_exception.SSHException: return HttpResponse("连接服务器超时!请检查服务器ip是否正确!") transport.send_file( selected_application.ApplicationUpdateScriptPathAfter, os.path.join( "/tmp", os.path.basename( selected_application.ApplicationUpdateScriptPathAfter))) # 传送tar文件到remote服务器 transport.send_file( "/root/workspace/{}/{}_{}.tar".format( selected_application.ApplicationName, selected_application.ApplicationName, time_flag), "/tmp/{}_{}.tar".format(selected_application.ApplicationName, time_flag)) transport.close() # 远程运行脚本 ## ssh = ssh_plugin.SSHConnect(host) cmd = "sh {} {} {} {}".format( os.path.join( "/tmp", os.path.basename( selected_application.ApplicationUpdateScriptPathAfter)), selected_application.ApplicationPath, sub_dir, time_flag) result = ssh.run_command(cmd) return HttpResponse((lambda x, y: x if x else y)(stdout, stderror) + result) return render(request, "h5_update.html", locals())