示例#1
0
def generate_hostlists(cfg, tmpdir):
    os.makedirs(tmpdir + "/hostlists/tags")
    dns_domain = cfg["vnet"].get("dns_domain", None)
    dns_domain_end = ""
    if dns_domain:
        dns_domain_end = f".{dns_domain}"
    hosts = {}
    tags = {}
    for rname in cfg.get("resources", {}).keys():
        rtype = cfg["resources"][rname]["type"]
        if rtype == "vm":
            instances = cfg["resources"][rname].get("instances", 1)
            if instances == 1:
                hosts[rname] = [rname]
            else:
                hosts[rname] = [
                    f"{rname}{n:04}" for n in range(1, instances + 1)
                ]
        elif rtype == "vmss":
            hosts[rname] = azutil.get_vmss_instances(cfg["resource_group"],
                                                     rname)

        for tname in cfg["resources"][rname].get("tags", []):
            # handle partial VMSS for a tag with python [] notation
            p = re.compile("([\w-]+)\[(\d*)([:]?)([-]?[\d]*)\]")
            matches = p.findall(tname)
            if matches:
                m = matches[0]
                lower = 0
                upper = None
                if m[1] != "":
                    lower = int(m[1])
                if m[2] != ":":
                    # single item
                    upper = lower + 1
                else:
                    if m[3] != "":
                        upper = int(m[3])
                log.debug(
                    f"using partial VMSS: name={m[0]}, lower={lower}, upper={upper} (original={tname})"
                )
                tags.setdefault(m[0],
                                []).extend(hosts.get(rname, [])[lower:upper])
            else:
                tags.setdefault(tname, []).extend(hosts.get(rname, []))

        if not cfg["resources"][rname].get("password", None):
            hosts.setdefault("linux", []).extend(hosts.get(rname, []))

    for n in hosts.keys():
        with open(f"{tmpdir}/hostlists/{n}", "w") as f:
            f.writelines(f"{h}{dns_domain_end}\n" for h in hosts[n])

    for n in tags.keys():
        with open(f"{tmpdir}/hostlists/tags/{n}", "w") as f:
            f.writelines(f"{h}{dns_domain_end}\n" for h in tags[n])
示例#2
0
def do_run(args):
    log.debug("reading config file ({})".format(args.config_file))
    c = azconfig.ConfigFile()
    c.open(args.config_file)

    adminuser = c.read_value("admin_user")
    ssh_private_key = "{}_id_rsa".format(adminuser)
    # TODO: check ssh key exists

    if args.user == None:
        sshuser = adminuser
    else:
        sshuser = args.user

    jumpbox = c.read_value("install_from")
    if not jumpbox:
        log.error(f"Missing 'install_from' property")
        sys.exit(1)

    # TODO : Why is this unused ?
    resource_group = c.read_value("resource_group")
    fqdn = c.get_install_from_destination()

    hosts = []
    if args.nodes:
        for r in args.nodes.split(","):
            rtype = c.read_value(f"resources.{r}.type")
            if not rtype:
                log.debug(f"resource {r} does not exist in config")
                hosts.append(r)
            if rtype == "vm":
                instances = c.read_value(f"resources.{r}.instances", 1)
                if instances == 1:
                    hosts.append(r)
                else:
                    hosts += [f"{r}{n:04}" for n in range(1, instances + 1)]
            elif rtype == "vmss":
                hosts += azutil.get_vmss_instances(
                    c.read_value("resource_group"), r)

    if not hosts:
        hosts.append(jumpbox)

    hostlist = " ".join(hosts)
    cmd = " ".join(args.args)
    _exec_command(fqdn, sshuser, ssh_private_key,
                  f"pssh -H '{hostlist}' -i -t 0 '{cmd}'")
示例#3
0
文件: azhpc.py 项目: HPC-Dev/azurehpc
def do_run(args):
    log.debug("reading config file ({})".format(args.config_file))
    c = azconfig.ConfigFile()
    c.open(args.config_file)

    adminuser = c.read_value("admin_user")
    ssh_private_key = "{}_id_rsa".format(adminuser)
    # TODO: check ssh key exists

    if args.user == None:
        sshuser = adminuser
    else:
        sshuser = args.user

    jumpbox = c.read_value("install_from")
    resource_group = c.read_value("resource_group")
    fqdn = azutil.get_fqdn(resource_group, jumpbox + "pip")

    if fqdn == "":
        log.warning(
            "The install node does not have a public IP - trying hostname ({})"
            .format(jumpbox))

    hosts = []
    if args.nodes:
        for r in args.nodes.split(" "):
            rtype = c.read_value(f"resources.{r}.type", None)
            if not rtype:
                log.error(f"resource {r} does not exist in config")
                sys.exit(1)
            if rtype == "vm":
                instances = c.read_value(f"resources.{r}.instances", 1)
                if instances == 1:
                    hosts.append(r)
                else:
                    hosts += [f"{r}{n:04}" for n in range(1, instances + 1)]
            elif rtype == "vmss":
                hosts += azutil.get_vmss_instances(
                    c.read_value("resource_group"), r)

    if not hosts:
        hosts.append(jumpbox)

    hostlist = " ".join(hosts)
    cmd = " ".join(args.args)
    _exec_command(fqdn, sshuser, ssh_private_key,
                  f"pssh -H '{hostlist}' -i -t 0 '{cmd}'")
示例#4
0
def do_connect(args):
    log.debug("reading config file ({})".format(args.config_file))
    c = azconfig.ConfigFile()
    c.open(args.config_file)

    adminuser = c.read_value("admin_user")
    ssh_private_key = "{}_id_rsa".format(adminuser)
    # TODO: check ssh key exists

    if not args.user:
        sshuser = adminuser
    else:
        sshuser = args.user

    jumpbox = c.read_value("install_from")
    if not jumpbox:
        log.error(f"Missing 'install_from' property")
        sys.exit(1)

    resource_group = c.read_value("resource_group")
    fqdn = c.get_install_from_destination()

    log.debug("Getting resource name")

    rtype = c.read_value(f"resources.{args.resource}.type", "hostname")
    rimage = c.read_value(f"resources.{args.resource}.image", "hostname")
    log.debug(f"image is - {rimage}")

    target = args.resource

    if rtype == "vm":
        instances = c.read_value(f"resources.{args.resource}.instances", 1)

        if instances > 1:
            target = f"{args.resource}{1:04}"
            log.info(
                f"Multiple instances of {args.resource}, connecting to {target}"
            )

    elif rtype == "vmss":
        vmssnodes = azutil.get_vmss_instances(resource_group, args.resource)
        if len(vmssnodes) == 0:
            log.error("There are no instances in the vmss")
            sys.exit(1)
        target = vmssnodes[0]
        if len(vmssnodes) > 1:
            log.info(
                f"Multiple instances of {args.resource}, connecting to {target}"
            )

    elif rtype == "hostname":
        pass

    else:
        log.debug(f"Unknown resource type - {rtype}")
        sys.exit(1)

    ros = rimage.split(':')
    if ros[0] == "MicrosoftWindowsServer" or ros[
            0] == "MicrosoftWindowsDesktop":
        log.debug(f"os is - {ros[0]} for node {args.resource}")
        fqdn = azutil.get_fqdn(c.read_value("resource_group"),
                               args.resource + "_pip")
        winpassword = c.read_value("variables.win_password")
        log.debug(f"fqdn is {fqdn} for node {args.resource}")
        cmdkey_exe = "cmdkey.exe"
        mstsc_exe = "mstsc.exe"
        cmdline = []
        if len(args.args) > 0:
            cmdline.append(" ".join(args.args))

        cmdkey_args = [
            "cmdkey.exe", f"/generic:{fqdn}", f"/user:{sshuser}",
            f"/password:{winpassword}"
        ]
        mstsc_args = ["mstsc.exe", f"/v:{fqdn}"]
        log.debug(" ".join(cmdkey_args + cmdline))
        cmdkey_cmdline = " ".join(cmdkey_args)
        os.system(cmdkey_cmdline)
        log.debug(" ".join(mstsc_args + cmdline))
        os.execvp(mstsc_exe, mstsc_args)

    else:
        ssh_exe = "ssh"
        cmdline = []
        if len(args.args) > 0:
            cmdline.append(" ".join(args.args))

        if args.resource == jumpbox:
            log.info("logging directly into {}".format(fqdn))
            ssh_args = [
                "ssh", "-t", "-q", "-o", "StrictHostKeyChecking=no", "-o",
                "UserKnownHostsFile=/dev/null", "-i", ssh_private_key,
                f"{sshuser}@{fqdn}"
            ]
            log.debug(" ".join(ssh_args + cmdline))
            os.execvp(ssh_exe, ssh_args + cmdline)
        else:
            log.info("logging in to {} (via {})".format(target, fqdn))
            ssh_args = [
                ssh_exe, "-t", "-q", "-o", "StrictHostKeyChecking=no", "-o",
                "UserKnownHostsFile=/dev/null", "-i", ssh_private_key, "-o",
                f"ProxyCommand=ssh -i {ssh_private_key} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -W %h:%p {sshuser}@{fqdn}",
                f"{sshuser}@{target}"
            ]
            log.debug(" ".join(ssh_args + cmdline))
            os.execvp(ssh_exe, ssh_args + cmdline)
示例#5
0
文件: azhpc.py 项目: HPC-Dev/azurehpc
def do_connect(args):
    log.debug("reading config file ({})".format(args.config_file))
    c = azconfig.ConfigFile()
    c.open(args.config_file)

    adminuser = c.read_value("admin_user")
    ssh_private_key = "{}_id_rsa".format(adminuser)
    # TODO: check ssh key exists

    if args.user == None:
        sshuser = adminuser
    else:
        sshuser = args.user

    jumpbox = c.read_value("install_from")
    resource_group = c.read_value("resource_group")
    fqdn = azutil.get_fqdn(resource_group, jumpbox + "pip")

    if fqdn == "":
        log.warning(
            f"The install node does not have a public IP - trying hostname ({jumpbox})"
        )

    log.debug("Getting resource name")

    rtype = c.read_value(f"resources.{args.resource}.type", "hostname")

    target = args.resource

    if rtype == "vm":
        instances = c.read_value(f"resources.{args.resource}.instances", 1)

        if instances > 1:
            target = f"{args.resource}{1:04}"
            log.info(
                f"Multiple instances of {args.resource}, connecting to {target}"
            )

    elif rtype == "vmss":
        vmssnodes = azutil.get_vmss_instances(resource_group, args.resource)
        if len(vmssnodes) == 0:
            log.error("There are no instances in the vmss")
            sys.exit(1)
        target = vmssnodes[0]
        if len(vmssnodes) > 1:
            log.info(
                f"Multiple instances of {args.resource}, connecting to {target}"
            )

    elif rtype == "hostname":
        pass

    else:
        log.debug(f"Unknown resource type - {rtype}")
        sys.exit(1)

    ssh_exe = "ssh"
    cmdline = []
    if len(args.args) > 0:
        cmdline.append(" ".join(args.args))

    if args.resource == jumpbox:
        log.info("logging directly into {}".format(fqdn))
        ssh_args = [
            "ssh", "-t", "-q", "-o", "StrictHostKeyChecking=no", "-o",
            "UserKnownHostsFile=/dev/null", "-i", ssh_private_key,
            f"{sshuser}@{fqdn}"
        ]
        log.debug(" ".join(ssh_args + cmdline))
        os.execvp(ssh_exe, ssh_args + cmdline)
    else:
        log.info("logging in to {} (via {})".format(target, fqdn))
        ssh_args = [
            ssh_exe, "-t", "-q", "-o", "StrictHostKeyChecking=no", "-o",
            "UserKnownHostsFile=/dev/null", "-i", ssh_private_key, "-o",
            f"ProxyCommand=ssh -i {ssh_private_key} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -W %h:%p {sshuser}@{fqdn}",
            f"{sshuser}@{target}"
        ]
        log.debug(" ".join(ssh_args + cmdline))
        os.execvp(ssh_exe, ssh_args + cmdline)