def do_action(login_info, cmdstr="", pattern="", timeout=120, debug=False, raw=True): try: sshbin = bin_path + "/../tools/ssh.exp" res = execute(sshbin, login_info, cmdstr=cmdstr, pattern=pattern, timeout=timeout, debug=debug, raw=raw) retcode = res["retcode"] last_stdo = res["last_stdo"] stdo = res["stdo"] stde = res["stde"] if retcode == 0: for so in stdo: if not re.search(pattern, str.strip(so)): print so else: for so in stdo: ot.error(so) for eo in stde: ot.error(eo) print "" except Exception, data: common.print_traceback_detail()
def deploy(login_info, src, dest, direction="push", pattern="", timeout=120, debug=False, raw=True, show=False): """ log_info = {ip:ip, user:user, passwd:passwd, port:port} return: none """ try: if not os.path.exists(src): ot.error("src: %s not found!" % (src)) return; scpbin = bin_path+"/../tools/scp.exp" res = send(scpbin, login_info, src, dest, direction=direction, pattern=pattern, timeout=timeout, debug=debug, raw=raw) retcode = res["retcode"] last_stdo = res["last_stdo"] stdo = res["stdo"] stde = res["stde"] ip = login_info["ip"] print "[deploy] " print "[result] " if retcode == 0: print "FROM src:<%s> TO <%s@%s>,SUCCESS!" % (src, ip, dest) if show == True: print "\n".join(handle_stdo_list(stdo,"", pattern)) else: print "FROM src:<%s> TO <%s@%s>,FAILURE!" % (src, ip, dest) if show == True: for so in stdo: ot.error(so) for eo in stde: ot.error(eo) return res except Exception,data: common.print_traceback_detail()
def do_action(login_info, cmdstr="", pattern="", timeout=120, debug=False, raw=True): try: sshbin = bin_path+"/../tools/ssh.exp" res = execute(sshbin, login_info, cmdstr=cmdstr, pattern=pattern, timeout=timeout, debug=debug, raw=raw) retcode = res["retcode"] last_stdo = res["last_stdo"] stdo = res["stdo"] stde = res["stde"] if retcode == 0: print "[execute] ",cmdstr print "[result] " print "\n".join(handle_stdo_list(stdo, cmdstr, pattern)) else: for so in stdo: ot.error(so) for eo in stde: ot.error(eo) print "" return res except Exception,data: common.print_traceback_detail()
def deploy(login_info, src, dest, direction="push", pattern="", timeout=120, debug=False, raw=True, show=False): """ log_info = {ip:ip, user:user, passwd:passwd, port:port} return: none """ try: if not os.path.exists(src): ot.error("src: %s not found!" % (src)) return scpbin = bin_path + "/../tools/scp.exp" res = send(scpbin, login_info, src, dest, direction=direction, pattern=pattern, timeout=timeout, debug=debug, raw=raw) retcode = res["retcode"] last_stdo = res["last_stdo"] stdo = res["stdo"] stde = res["stde"] if retcode == 0: ot.info("deploy src:%s to dest:%s success!\n" % (src, dest)) if show == True: for so in stdo: if not re.search(pattern, str.strip(so)): print so else: ot.info("deploy src:%s to dest:%s failure!\n" % (src, dest)) if show == True: for so in stdo: ot.error(so) for eo in stde: ot.error(eo) except Exception, data: common.print_traceback_detail()
def main(): try: signal.signal(signal.SIGINT, signal_handler) try: options, args = getopt.getopt(sys.argv[1:], "h:c:g:i:s:z:t:dm", [ "host=", "config=", "group=", "id=", "src=", "dest=", "timeout=", "debug", "man" ]) except getopt.GetoptError: sys.exit() default_hosts_path = bin_path + "/../etc/hosts" (hosts, cmdstr, group, id, timeout, debug, man) = (default_hosts_path, False, False, False, 120, False, False) (srcfiles, destdir) = ("", "") for name, value in options: if name in ("-h", "--hosts"): hosts = value if name in ("-c", "--cmd"): cmdstr = value if name in ("-g", "--group"): group = value if name in ("-i", "--id"): id = value if name in ("-a", "--action"): action = value if name in ("-s", "--src"): srcfiles = value if name in ("-z", "--dest"): destdir = value if name in ("-t", "--timeout"): timeout = int(value) if name in ("-d", "--debug"): debug = True if name in ("-m", "--man"): man = True if man == True: usage(sys.argv[0]) exit() if not os.path.exists(hosts): ot.error("hosts file:%s not exist!" % (hosts)) exit() if group == False and id == False: ot.warn("not assign group or id, this tool will affect all host!") answer = raw_input("Are you sure?[yes|no]:") answer = str.lower(answer) if answer not in ("yes", "y"): exit() cfghelper.parse_host(hosts) hosts = cfghelper.get_hosts(group, id) for srv_host in hosts: srv_host = dict(srv_host) gp_name = srv_host.get("group", "") lid = srv_host.get("id", "") ip = srv_host.get("ip", "") user = srv_host.get("user", "") passwd = srv_host.get("passwd", "") port = srv_host.get("port", "") ot.emphasize( "==== group:%(group)s id:%(id)s ip:%(ip)s user:%(user)s port:%(port)s ====" % srv_host) pattern = get_pattern("%s$" % (cmdstr)) deploy(srv_host, srcfiles, destdir, direction="push", pattern="", timeout=120, debug=debug, raw=True, show=True) except Exception, data: common.print_traceback_detail()
def main(): """ 远程执行 """ try: import signal import getopt signal.signal(signal.SIGINT, signal_handler) try: options,args = getopt.getopt(sys.argv[1:],"h:c:g:i:t:dmy", ["host=", "config=", "group=", "id=", "timeout=", "debug", "man", "yes"]) except getopt.GetoptError: common.print_traceback_detail() sys.exit() #命令行参数数据存储 default_deploy_path = bin_path+"/../etc/deploy.cfg" default_hosts_path = bin_path+"/../etc/hosts" (hosts, config, group, id, timeout, debug, man, yesflag ) = (default_hosts_path, default_deploy_path, False, False, 120, False, False, False) for name,value in options: if name in ("-h","--hosts"): hosts = value if name in ("-c","--config"): config = value if name in ("-g","--group"): group = value if name in ("-i","--id"): id = value if name in ("-t","--timeout"): timeout = int(value) if name in ("-d","--debug"): debug = True if name in ("-m","--man"): man = True if name in ("-y","--yes"): yesflag = True if man == True: usage(sys.argv[0]) exit() if not os.path.exists(hosts): ot.error("hosts file:%s not exist" % (hosts)) exit() if not os.path.exists(config): ot.error("config file:%s not exist" % (config)) exit() if group == False and id ==False: if not yesflag: ot.warn("not assign group or id, this tool will affect all host!") answer = raw_input("Are you sure?[yes|no]:") answer = str.lower(answer) if answer not in ("yes", "y"): exit() cfghelper.parse_host(hosts) hosts = cfghelper.get_hosts(group,id) if not hosts: ot.info("no any host match!") fd = open(config, "r") config_contents = fd.readlines() fd.close() ip = None rgt_ip_count = 0 #总有效IP数 ip_total = 0 #总IP数,排除空行、注释行 wrong_ip_conf = [] #错误的IP配置 wrong_config_conf = [] #错误的文件传输、命令执行配置 cmd_result = dict() for srv_host in hosts: infostr = "".join(["="] * 30) ip = srv_host["ip"] port = srv_host["port"] setting = { "padding": infostr, "ip": ip, "port": port } ot.info("%(padding)s %(ip)s PORT:%(port)s %(padding)s" % setting) for config_line in config_contents: config_line = str.strip(config_line) if re.search("^$|^#", config_line): continue config_arg = re.split(":::", config_line) if not config_arg: wrong_config_conf.append(config_line) continue type_flag = config_arg[0] if type_flag not in ("file", "cmd") or len(config_arg) < 1 : wrong_config_conf.append(config_line) continue #如果是命令 if type_flag == "cmd" and len(config_arg) > 1: cmdstr = config_arg[1] pattern = get_pattern("%s$" % (cmdstr)) do_action(srv_host, cmdstr=cmdstr, pattern=pattern, timeout=timeout, debug=debug, raw=True) #如果是文件传输 if type_flag == "file": less_config_str = str.strip(config_arg[1]) less_config_ary = re.split("[ |\t]+", less_config_str) if len(less_config_ary) < 3: wrong_config_conf.append(config_line) continue src_file = less_config_ary[0] dest_dir = less_config_ary[1] direction = less_config_ary[2] if direction not in ("push", "pull"): wrong_config_conf.append(config_line) continue pattern = get_pattern("") deploy(srv_host, src_file, dest_dir, direction=direction, pattern=pattern, timeout=timeout, debug=debug, raw=True, show=False) print "\n" #print aps execute logs # now = mydate.get_datetime_str() # filename = "remote_exe_" + now # fd = open(filename, "w") # for ip_port in cmd_result: # data = cmd_result[ip_port] # fd.write("--------- %s ---------- " % (ip_port) + "\n") # for element in data: # cmdstr = element["cmd"] # result = element["result"] # errors = element["error"] # retcode = element["retcode"] # if result: # fd.write("[ std out ] " + "\n") # fd.write("command : " + cmdstr + "\n") # for line in result: # fd.write(line.strip() + "\n") # fd.write("\n") # if errors: # fd.write("[ std error ] " + "\n") # fd.write("command : " + cmdstr + "\n") # for line in errors: # fd.write(line.strip() + "\n") # fd.write("\n\n") # fd.close() # print "all command result save in %s" % (filename) # print "maybe you want to do as follow:" # print "vi %s" % (filename) # print "cat %s" % (filename) # ot.info("vi %s" % (filename)) # ot.info("cat %s" % (filename)) if wrong_ip_conf: print "====== wrong ip config:" print wrong_ip_conf if wrong_config_conf: print "====== wrong config line:" print wrong_config_conf except Exception,data: common.print_traceback_detail() print data
def main(): try: signal.signal(signal.SIGINT, signal_handler) try: options,args = getopt.getopt(sys.argv[1:],"h:g:i:t:dm", ["host=", "group=", "id=", "timeout=", "debug", "man"]) except getopt.GetoptError: print "parse parameters error!" common.print_traceback_detail() sys.exit() default_hosts_path = bin_path+"/../etc/hosts" (hosts, group, id, timeout, debug, man ) = (default_hosts_path, False, False, 120, False, False) for name,value in options: if name in ("-h","--hosts"): hosts = value if name in ("-g","--group"): group = value if name in ("-i","--id"): id = value if name in ("-t","--timeout"): timeout = int(value) if name in ("-d","--debug"): debug = True if name in ("-m","--man"): man = True if man == True: usage(sys.argv[0]) exit() if not os.path.exists(hosts): ot.error("hosts file:%s not exist!" % (hosts)) exit() if group == False and id ==False: if not yesflag: ot.warn("not assign group or id, this tool will affect all host!") answer = raw_input("Are you sure?[yes|no]:") answer = str.lower(answer) if answer not in ("yes", "y"): exit() cfghelper.parse_host(hosts) hosts = cfghelper.get_hosts(group,id) if len(hosts) > 1: print "match more than one host!" exit() for srv_host in hosts: srv_host = dict(srv_host) gp_name = srv_host.get("group", "") lid = srv_host.get("id", "") ip = srv_host.get("ip", "") user = srv_host.get("user", "") passwd = srv_host.get("passwd", "") port = srv_host.get("port", "") ot.emphasize("==== group:%(group)s id:%(id)s ip:%(ip)s user:%(user)s port:%(port)s ====" % srv_host) login(srv_host, timeout=120, debug=False, raw=True) except Exception,data: common.print_traceback_detail()
def main(): try: signal.signal(signal.SIGINT, signal_handler) try: options, args = getopt.getopt(sys.argv[1:], "h:c:g:i:a:t:dmy", [ "host=", "config=", "group=", "id=", "action=", "timeout=", "debug", "man", "yes" ]) except getopt.GetoptError: sys.exit() #命令行参数数据存储 default_deploy_path = bin_path + "/../etc/deploy.cfg" default_hosts_path = bin_path + "/../etc/hosts" (hosts, config, group, id, action, timeout, debug, man, yesflag) = (default_hosts_path, default_deploy_path, False, False, False, 120, False, False, False) for name, value in options: if name in ("-h", "--hosts"): hosts = value if name in ("-c", "--config"): config = value if name in ("-g", "--group"): group = value if name in ("-i", "--id"): id = value if name in ("-a", "--action"): action = value if name in ("-t", "--timeout"): timeout = int(value) if name in ("-d", "--debug"): debug = True if name in ("-m", "--man"): man = True if name in ("-y", "--yes"): yesflag = True if man == True: usage(sys.argv[0]) exit() if not os.path.exists(hosts): ot.error("hosts file:%s not exist" % (hosts)) exit() if not os.path.exists(config): ot.error("config file:%s not exist" % (config)) exit() if action not in ("start", "stop", "status", "restart", "deploy", "undeploy"): ot.error("wrong action:%s" % (action)) exit() if group == False and id == False: if not yesflag: ot.warn( "not assign group or id, this tool will affect all host!") answer = raw_input("Are you sure?[yes|no]:") answer = str.lower(answer) if answer not in ("yes", "y"): exit() cfghelper.parse_host(hosts) hosts = cfghelper.get_hosts(group, id) deploy_conf = cfghelper.parse_deploy_cfg(config) default_src = deploy_conf.get("src", "") default_dest = deploy_conf.get("dest", "") if not hosts: ot.info("no any host match!") for srv_host in hosts: srv_host = dict(srv_host) gp_name = srv_host.get("group", "") lid = srv_host.get("id", "") ip = srv_host.get("ip", "") user = srv_host.get("user", "") passwd = srv_host.get("passwd", "") port = srv_host.get("port", "") src = srv_host.get("src", default_src) dest = srv_host.get("dest", default_dest) if action in ("deploy"): pattern = get_pattern() ot.emphasize( "==== group:%(group)s id:%(id)s ip:%(ip)s user:%(user)s port:%(port)s ====" % srv_host) deploy(srv_host, src, dest, direction="push", pattern=pattern, timeout=120, debug=False, raw=True, show=False) if action in ("start", "stop", "status", "restart", "undeploy"): cmdstr = "cd %s && chmod a+x ./* && ./run.sh %s" % (dest, action) if action == "undeploy": if not dest: continue cmdstr = "rm %s -fr" % (dest) ot.emphasize( "==== group:%(group)s id:%(id)s ip:%(ip)s user:%(user)s port:%(port)s ====" % srv_host) ot.info("execute: %s" % cmdstr) pattern = get_pattern("%s$" % (cmdstr)) do_action(srv_host, cmdstr=cmdstr, pattern=pattern, timeout=120, debug=debug, raw=True) except Exception, data: common.print_traceback_detail()