def ansible_playbook_func(playbook_file, playbook_path, hosts, serial, log_file): sklog = sklog_original(log_file) sklog.info("%s INFO the ansible playbook task started" % datetime.datetime.now()) vars_dic = {"hosts": hosts, "serial": serial} ansible_cmd = "ansible-playbook %s%s -e '%s'" % (playbook_path, playbook_file, vars_dic) try: pcmd = Popen(ansible_cmd, stdout=PIPE, stderr=STDOUT, shell=True) while True: for i in iter(pcmd.stdout.readline, b''): i = i.decode().strip('\n') if "FAILED" in i or " ERROR" in i: sklog.error(i) else: sklog.info(i) if pcmd.poll() is not None: break except: exinfo = sys.exc_info() print(exinfo) sklog.error(exinfo) # return False finally: retcode = pcmd.wait() if retcode == 0: pass else: raise Exception( "task failed,please check pl_ansible_playbook.log for details")
def ansible_cmd_func(hosts, forks, cmd, log_file): sklog = sklog_original(log_file) ansible_cmd = "ansible %s -f %s -m shell -a '%s' " % (hosts, forks, cmd) sklog.info("%s INFO the ansible shell task started" % datetime.datetime.now()) try: pcmd = Popen(ansible_cmd, stdout=PIPE, stderr=STDOUT, shell=True) while True: for line in iter(pcmd.stdout.readline, b''): line = line.decode('utf-8').strip('\n') if line: sklog.info(line) else: break if pcmd.poll() is not None: break except: exinfo = sys.exc_info() sklog.error(exinfo) finally: retcode = pcmd.wait() if retcode == 0: pass else: raise Exception( "task failed,please check pl_ansible_shell.log for details")
def static_deploy(change_owner_tag, hosts, deploy_src_path, deploy_dest_path, delete_enable, owner, group, log_file, rsync_opts, serial): sklog = sklog_original(log_file) sklog.info("start deploy static files") vars_dic = { "hosts": hosts, "deploy_src_path": deploy_src_path, "deploy_dest_path": deploy_dest_path, "delete_enable": delete_enable, "owner": owner, "group": group, "rsync_opts": rsync_opts, "Serial": serial } ansible_cmd = "ansible-playbook sc_static_sync.yml --tags common,%s -e '%s' " % ( change_owner_tag, vars_dic) try: pcmd = Popen(ansible_cmd, stdout=PIPE, stderr=STDOUT, shell=True) while True: for i in iter(pcmd.stdout.readline, b''): i = i.decode().strip('\n') if "FAILED" in i or " ERROR" in i: sklog.error(i) else: sklog.info(i) if pcmd.poll() is not None: break except: exinfo = sys.exc_info() sklog.error(exinfo) finally: retcode = pcmd.wait() if retcode == 0: pass else: raise Exception( "task failed,please check pl_deploy_git.log for details")
def docker_deploy(hosts,proj,tag,docker_run,docker_image_url,wait_times,eureka_url,app_spring_name,exec_mode,log_file,check_time,task_id,serial): sklog = sklog_original(log_file) if app_spring_name == "null": if exec_mode == "update": ansible_cmd = "ansible-playbook sc_update_hard.yml -v -e \"hosts=%s DockerApp=%s DockerImageTag=%s DockerRun='%s' DockerImageURL=%s AppSpringName=%s TaskId=%s Serial=%s\" " % \ (hosts, proj, tag, docker_run, docker_image_url, app_spring_name, task_id, serial) elif exec_mode == "restart_soft": ansible_cmd = "ansible-playbook sc_restart_hard.yml --skip-tags 'eureka' -v -e \"hosts=%s DockerApp=%s TaskId=%s Serial=%s\" " % (hosts, proj, task_id, serial) elif exec_mode == "restart_hard": ansible_cmd = "ansible-playbook sc_restart_hard.yml --skip-tags 'eureka' -v -e \"hosts=%s DockerApp=%s TaskId=%s Serial=%s\" " % (hosts, proj, task_id, serial) elif exec_mode == "rollback": ansible_cmd = "ansible-playbook sc_rollback.yml --skip-tags eureka,manual -v -e \"hosts=%s serial=1 DockerApp=%s WaitTimes=%s EurekaUrl=%s AppSpringName=%s TaskId=%s Serial=%s\" " % \ (hosts, proj, wait_times, eureka_url, app_spring_name, task_id, serial) elif exec_mode == "inquiry": ansible_cmd = "ansible %s -m shell -a \"docker ps -a|egrep '%s(\ |:|-20)|NAMES' \" " % \ (hosts, proj) elif exec_mode == "update_hard": ansible_cmd = "ansible-playbook sc_update_hard.yml -v -e \"hosts=%s DockerApp=%s DockerImageTag=%s DockerRun='%s' DockerImageURL=%s TaskId=%s Serial=%s\" " % \ (hosts, proj, tag, docker_run, docker_image_url, task_id, serial) elif exec_mode == "stop_soft": ansible_cmd = "ansible-playbook sc_stop_hard.yml -v -e \"hosts=%s DockerApp=%s TaskId=%s\" " % \ (hosts,proj,docker_run,task_id) elif exec_mode == "update2": ansible_cmd = "ansible-playbook sc_update_hard.yml -v -e \"hosts=%s DockerApp=%s DockerImageTag=%s DockerRun='%s' DockerImageURL=%s TaskId=%s Serial=%s\" " % \ (hosts, proj, tag, docker_run, docker_image_url, task_id, serial) else: sklog.error("please choose the ExecMode ") exit(1) else: if exec_mode == "update": ansible_cmd = "ansible-playbook sc_update_soft.yml -v -e \"hosts=%s DockerApp=%s DockerImageTag=%s DockerRun='%s' DockerImageURL=%s WaitTimes=%s EurekaUrl=%s AppSpringName=%s MaxCheckTime=%s TaskId=%s Serial=%s\" " % \ (hosts,proj,tag,docker_run,docker_image_url,wait_times,eureka_url,app_spring_name,check_time,task_id,serial) elif exec_mode == "restart_hard": ansible_cmd = "ansible-playbook sc_restart_hard.yml -v -e \"hosts=%s DockerApp=%s WaitTimes=%s EurekaUrl=%s AppSpringName=%s MaxCheckTime=%s TaskId=%s Serial=%s\" " % \ (hosts, proj,wait_times,eureka_url,app_spring_name,check_time,task_id,serial) elif exec_mode == "restart_soft": ansible_cmd = "ansible-playbook sc_restart_soft.yml -v -e \"hosts=%s DockerApp=%s WaitTimes=%s EurekaUrl=%s AppSpringName=%s MaxCheckTime=%s TaskId=%s Serial=%s\" " % \ (hosts,proj,wait_times,eureka_url,app_spring_name,check_time,task_id,serial) elif exec_mode == "rollback": ansible_cmd = "ansible-playbook sc_rollback.yml --skip-tags manual -v -e \"hosts=%s DockerApp=%s serial=1 WaitTimes=%s EurekaUrl=%s AppSpringName=%s MaxCheckTime=%s TaskId=%s Serial=%s\" " % \ (hosts, proj, wait_times, eureka_url, app_spring_name,check_time,task_id,serial) elif exec_mode == "inquiry": ansible_cmd = "ansible %s -m shell -a \"docker ps -a|egrep '%s(\ |:|-20)|NAMES' \" " % \ (hosts, proj) elif exec_mode == "update_hard": ansible_cmd = "ansible-playbook sc_update_hard.yml -v -e \"hosts=%s DockerApp=%s DockerImageTag=%s DockerRun='%s' DockerImageURL=%s TaskId=%s Serial=%s\" " % \ (hosts, proj, tag, docker_run, docker_image_url,task_id,serial) elif exec_mode == "stop_soft": ansible_cmd = "ansible-playbook sc_stop_soft.yml -v -e \"hosts=%s DockerApp=%s WaitTimes=%s EurekaUrl=%s AppSpringName=%s TaskId=%s Serial=%s\" " % \ (hosts,proj,wait_times,eureka_url,app_spring_name,task_id,serial) elif exec_mode == "update2": ansible_cmd = "ansible-playbook sc_update_soft2.yml -v -e \"hosts=%s DockerApp=%s DockerImageTag=%s DockerRun='%s' DockerImageURL=%s WaitTimes=%s EurekaUrl=%s AppSpringName=%s MaxCheckTime=%s TaskId=%s Serial=%s\" " % \ (hosts,proj,tag,docker_run,docker_image_url,wait_times,eureka_url,app_spring_name,check_time,task_id,serial) else: sklog.error("please choose the Exec Mode ") exit(1) try: pcmd = Popen(ansible_cmd, stdout=PIPE, stderr=STDOUT, shell=True) if exec_mode in ["inquiry",]: while True: for line in iter(pcmd.stdout.readline,b''): line=line.decode().strip('\n') if line: sklog.info(line) else: break if pcmd.poll() is not None: break else: while True: for i in iter(pcmd.stdout.readline,b''): i=i.decode().strip('\n') if "FAILED" in i or " ERROR" in i: sklog.error(i) else: sklog.info(i) if pcmd.poll() is not None: break except: exinfo=sys.exc_info() sklog.error(exinfo) finally: retcode = pcmd.wait() if retcode == 0: pass else: print("ERROR:the task failed,please check pl_deploy_docker.log for details") sys.exit(1)