def main(): try: signal.signal(signal.SIGINT, signal_handler) if len(sys.argv) < 2: usage(sys.argv[0]) exit() arg1 = sys.argv[1] if arg1 == "-h" or arg1 == "--help": usage(sys.argv[0]) exit() default_hosts_path = bin_path+"/../etc/hosts" (hosts,man ) = (default_hosts_path, False) cfghelper.parse_host(hosts) hosts = cfghelper.get_hosts("","") for srv_host in hosts: ip = srv_host.get("ip", "") if ip == sys.argv[1]: srv_host = dict(srv_host) gp_name = srv_host.get("group", "") lid = srv_host.get("id", "") 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) break; 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: 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()