示例#1
0
def do_ssh(hosts, command):
    hosts_l = [
        full_hostname(host) for host in ClusterShell.NodeSet.NodeSet(hosts)
    ]
    hosts = ClusterShell.NodeSet.NodeSet('')
    hosts.updaten(hosts_l)

    os.environ["SSHPASS"] = \
        value_from_file(get_from_config("common", "master_passwd_file"),
                        "IMMPASSWORD")
    imm_user = \
        value_from_file(get_from_config("common", "master_passwd_file"),
                        "IMMUSER")

    task = ClusterShell.Task.task_self()
    task.set_info("ssh_user", imm_user)
    task.set_info("ssh_path", "/usr/bin/sshpass -e /usr/bin/ssh")
    task.set_info("ssh_options", "-oBatchMode=no")
    task.shell(command, nodes=hosts.__str__())
    task.resume()

    for buf, nodes in task.iter_buffers():
        print "---\n%s:\n---\n %s" \
              % (ClusterShell.NodeSet.fold(",".join(nodes)),
                 buf)
示例#2
0
文件: clara_ipmi.py 项目: hmlth/clara
def ipmi_do(hosts, *cmd):

    imm_user = value_from_file(get_from_config("common", "master_passwd_file"),
                               "IMMUSER")
    os.environ["IPMI_PASSWORD"] = value_from_file(
        get_from_config("common", "master_passwd_file"), "IMMPASSWORD")
    nodeset = ClusterShell.NodeSet.NodeSet(hosts)

    p = multiprocessing.Pool(parallel)
    result_map = {}

    for host in nodeset:

        pat = re.compile("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
        if not pat.match(host):
            prefix = get_from_config("ipmi", "prefix")
            host = prefix + host

        ipmitool = [
            "ipmitool", "-I", "lanplus", "-H", host, "-U", imm_user, "-E",
            "-e!"
        ]
        ipmitool.extend(cmd)
        logging.debug("ipmi/ipmi_do: {0}".format(" ".join(ipmitool)))
        result_map[host] = p.apply_async(ipmi_run, (ipmitool, ))

    p.close()
    p.join()

    for host, result in result_map.items():
        print host, result.get()
示例#3
0
def getmac(hosts):
    install_cfg()
    imm_user = value_from_file(get_from_config("common", "master_passwd_file"), "IMMUSER")
    os.environ["IPMI_PASSWORD"] = value_from_file(get_from_config("common", "master_passwd_file"), "PASSWD")
    nodeset = ClusterShell.NodeSet.NodeSet(hosts)
    for host in nodeset:
        print "%s: " % host
        cmd = ["ipmitool", "-I", "lanplus", "-H", "imm" + host,
               "-U", imm_user, "-E", "fru", "print", "0"]

        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
        # The data we want is in line 15
        line = proc.stdout.readlines()[14]
        full_mac = line.split(":")[1].strip().upper()
        mac_address1 = "{0}:{1}:{2}:{3}:{4}:{5}".format(full_mac[0:2],
                                                        full_mac[2:4],
                                                        full_mac[4:6],
                                                        full_mac[6:8],
                                                        full_mac[8:10],
                                                        full_mac[10:12])

        mac_address2 = "{0}:{1}:{2}:{3}:{4}:{5}".format(full_mac[12:14],
                                                        full_mac[14:16],
                                                        full_mac[16:18],
                                                        full_mac[18:20],
                                                        full_mac[20:22],
                                                        full_mac[22:24])

        print "ETH0's MAC address is {0}\n" \
              "ETH1's MAC address is {1}\n".format(mac_address1, mac_address2)
示例#4
0
def main():
    logging.debug(sys.argv)
    dargs = docopt.docopt(__doc__)

    # Use the value provided by the user in the command line
    if dargs['--p'] is not None and dargs['--p'].isdigit():
        _opts['parallel'] = int(dargs['--p'])
    # Read the value from the config file and use 1 if it hasn't been set
    elif has_config_value("ipmi", "parallel"):
        _opts['parallel']= int(get_from_config("ipmi", "parallel"))
    else:
        logging.debug("parallel hasn't been set in config.ini, using 1 as default")

    if dargs['connect']:
        do_connect(dargs['<host>'], dargs['-j'], dargs['-f'])
    elif dargs['deconnect']:
        ipmi_do(dargs['<hostlist>'], "sol", "deactivate")
    elif dargs['status']:
        ipmi_do(dargs['<hostlist>'], "power", "status")
    elif dargs['setpwd']:
        imm_user = value_from_file(get_from_config("common", "master_passwd_file"), "IMMUSER")
        imm_pwd = value_from_file(get_from_config("common", "master_passwd_file"), "IMMPASSWORD")
        ipmi_do(dargs['<hostlist>'], "user", "set", "name", "2", imm_user)
        ipmi_do(dargs['<hostlist>'], "user", "set", "password", "2", imm_pwd)
    elif dargs['getmac']:
        getmac(dargs['<hostlist>'])
    elif dargs['on']:
        ipmi_do(dargs['<hostlist>'], "power", "on")
    elif dargs['off']:
        ipmi_do(dargs['<hostlist>'], "power", "off")
    elif dargs['soft']:
        ipmi_do(dargs['<hostlist>'], "power", "soft")
    elif dargs['reboot']:
        ipmi_do(dargs['<hostlist>'], "chassis", "power", "reset")
    elif dargs['blink']:
        ipmi_do(dargs['<hostlist>'], "chassis", "identify", "1")
    elif dargs['bios']:
        ipmi_do(dargs['<hostlist>'], "chassis", "bootparam", "set", "bootflag", "force_bios")
    elif dargs['immdhcp']:
        ipmi_do(dargs['<hostlist>'], "lan", "set", "1", "ipsrc", "dhcp")
    elif dargs['pxe']:
        ipmi_do(dargs['<hostlist>'], "chassis", "bootdev", "pxe")
    elif dargs['disk']:
        ipmi_do(dargs['<hostlist>'], "chassis", "bootdev", "disk")
    elif dargs['reset']:
        response = raw_input('Do you really want to restart BMC interface? (N/y) ')
        if response in ('Y', 'y'):
            ipmi_do(dargs['<hostlist>'], "mc", "reset", "cold")
    elif dargs['sellist']:
        ipmi_do(dargs['<hostlist>'], "sel", "list")
    elif dargs['selclear']:
        ipmi_do(dargs['<hostlist>'], "sel", "clear")
    elif dargs['ping']:
        do_ping(dargs['<hostlist>'])
    elif dargs['ssh']:
        do_ssh(dargs['<hostlist>'], dargs['<command>'])
    elif dargs['command']:
        ipmi_do(dargs['<hostlist>'], *dargs['<command>'])
示例#5
0
文件: clara_ipmi.py 项目: hmlth/clara
def main():
    logging.debug(sys.argv)
    dargs = docopt.docopt(__doc__)

    global parallel
    # Read the value from the config file and use 1 if it hasn't been set
    try:
        parallel = int(get_from_config("ipmi", "parallel"))
    except:
        logging.warning(
            "parallel hasn't been set in config.ini, using 1 as value")
        parallel = 1
    # Use the value provided by the user in the command line
    if dargs['--p'] is not None and dargs['--p'].isdigit():
        parallel = int(dargs['--p'])

    if dargs['connect']:
        do_connect(dargs['<host>'], dargs['-j'], dargs['-f'])
    elif dargs['deconnect']:
        ipmi_do(dargs['<hostlist>'], "sol", "deactivate")
    elif dargs['status']:
        ipmi_do(dargs['<hostlist>'], "power", "status")
    elif dargs['setpwd']:
        imm_user = value_from_file(
            get_from_config("common", "master_passwd_file"), "IMMUSER")
        imm_pwd = value_from_file(
            get_from_config("common", "master_passwd_file"), "IMMPASSWORD")
        ipmi_do(dargs['<hostlist>'], "user", "set", "name", "2", imm_user)
        ipmi_do(dargs['<hostlist>'], "user", "set", "password", "2", imm_pwd)
    elif dargs['getmac']:
        getmac(dargs['<hostlist>'])
    elif dargs['on']:
        ipmi_do(dargs['<hostlist>'], "power", "on")
    elif dargs['off']:
        ipmi_do(dargs['<hostlist>'], "power", "off")
    elif dargs['reboot']:
        ipmi_do(dargs['<hostlist>'], "chassis", "power", "reset")
    elif dargs['blink']:
        ipmi_do(dargs['<hostlist>'], "chassis", "identify", "1")
    elif dargs['bios']:
        ipmi_do(dargs['<hostlist>'], "chassis", "bootparam", "set", "bootflag",
                "force_bios")
    elif dargs['immdhcp']:
        ipmi_do(dargs['<hostlist>'], "lan", "set", "1", "ipsrc", "dhcp")
    elif dargs['pxe']:
        ipmi_do(dargs['<hostlist>'], "chassis", "bootdev", "pxe")
    elif dargs['disk']:
        ipmi_do(dargs['<hostlist>'], "chassis", "bootdev", "disk")
    elif dargs['reset']:
        ipmi_do(dargs['<hostlist>'], "mc", "reset", "cold")
    elif dargs['sellist']:
        ipmi_do(dargs['<hostlist>'], "sel", "list")
    elif dargs['selclear']:
        ipmi_do(dargs['<hostlist>'], "sel", "clear")
    elif dargs['ping']:
        do_ping(dargs['<hostlist>'])
    elif dargs['ssh']:
        do_ssh(dargs['<hostlist>'], dargs['<command>'])
示例#6
0
def ipmi_do(hosts, cmd):
    install_cfg()
    imm_user = value_from_file(get_from_config("common", "master_passwd_file"), "IMMUSER")
    os.environ["IPMI_PASSWORD"] = value_from_file(get_from_config("common", "master_passwd_file"), "PASSWD")
    nodeset = ClusterShell.NodeSet.NodeSet(hosts)
    for host in nodeset:
        print "%s: " % host
        run(["ipmitool", "-I", "lanplus", "-H", "imm" + host,
             "-U", imm_user, "-E", cmd])
示例#7
0
文件: clara_nodes.py 项目: fmdm/clara
def init_config():
	passwd_file = get_from_config("common", "master_passwd_file")
	if os.path.isfile(passwd_file):
		imm_user = value_from_file(passwd_file, "IMMUSER")
		os.environ["IPMI_PASSWORD"] = value_from_file(passwd_file, "IMMPASSWORD")
		return imm_user
	else:
	        print "%s not found. Please check config." % passwd_file
		sys.exit(1)
示例#8
0
def do_connect_ipmi(host):

    imm_user = value_from_file(get_from_config("common", "master_passwd_file"), "USER")
    os.environ["IPMI_PASSWORD"] = value_from_file(get_from_config("common", "master_passwd_file"), "IMMPASSWORD")

    pat = re.compile("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
    if not pat.match(host):
        host = full_hostname(host)

    ipmitool = ["ipmitool", "-I", "lanplus", "-H", host, "-U", imm_user, "-E", "-e!", "sol", "activate"]
    logging.debug("ipmi/ipmi_do: {0}".format(" ".join(ipmitool)))
    run(ipmitool)
示例#9
0
def get_encryption_key():

    master_passwd_file = get_from_config("common", "master_passwd_file")

    if os.path.isfile(master_passwd_file):
        password = value_from_file(master_passwd_file, "ASUPASSWD")
        if len(password) > 20:
            return password
        else:
            clara_exit("There was some problem reading the value of ASUPASSWD")
    else:
        clara_exit("Unable to read: {0}".format(master_passwd_file))
示例#10
0
def install_cfg():
    passwd_file = get_from_config("common", "master_passwd_file")
    if not os.path.isfile(passwd_file) and os.path.isfile(passwd_file + ".enc"):
        password = value_from_file(get_from_config("common", "master_passwd_file"), "PASSPHRASE")

        if len(password) > 20:
            cmd = ['openssl', 'aes-256-cbc', '-d', '-in', passwd_file + ".enc",
                   '-out', passwd_file, '-k', password]
            run(cmd)
            os.chmod(passwd_file, 0o400)
        else:
            sys.exit('There was some problem reading the PASSPHRASE')
示例#11
0
def do_key():
    key = get_from_config("repo", "gpg_key")
    fnull = open(os.devnull, 'w')
    cmd = ['gpg', '--list-secret-keys', key]
    logging.debug("repo/do_key: {0}".format(" ".join(cmd)))
    retcode = subprocess.call(cmd, stdout=fnull, stderr=fnull)
    fnull.close()

    # We import the key if it hasn't been imported before
    if retcode != 0:
        file_stored_key = get_from_config("repo", "stored_enc_key")
        if os.path.isfile(file_stored_key):
            password = value_from_file(
                get_from_config("common", "master_passwd_file"), "ASUPASSWD")

            if len(password) > 20:
                fdesc, temp_path = tempfile.mkstemp(prefix="tmpClara")
                cmd = [
                    'openssl', 'aes-256-cbc', '-d', '-in', file_stored_key,
                    '-out', temp_path, '-k', password
                ]
                logging.debug("repo/do_key: {0}".format(" ".join(cmd)))
                retcode = subprocess.call(cmd)

                if retcode != 0:
                    os.close(fdesc)
                    os.remove(temp_path)
                    clara_exit('Command failed {0}'.format(" ".join(cmd)))
                else:
                    logging.info("Trying to import key {0}".format(key))
                    fnull = open(os.devnull, 'w')
                    cmd = [
                        'gpg', '--allow-secret-key-import', '--import',
                        temp_path
                    ]
                    logging.debug("repo/do_key: {0}".format(" ".join(cmd)))
                    retcode = subprocess.call(cmd)
                    fnull.close()
                    os.close(fdesc)
                    os.remove(temp_path)
                    if retcode != 0:
                        logging.info(
                            "\nThere was a problem with the import, make sure the key you imported "
                            "from {0} is the same you have in your configuration: {1}"
                            .format(file_stored_key, key))

            else:
                clara_exit(
                    'There was some problem reading the value of ASUPASSWD')
        else:
            clara_exit('Unable to read:  {0}'.format(file_stored_key))
    else:
        logging.info("GPG key was already imported.")
示例#12
0
def getmac(hosts):
    imm_user = value_from_file(get_from_config("common", "master_passwd_file"), "IMMUSER")
    os.environ["IPMI_PASSWORD"] = value_from_file(get_from_config("common", "master_passwd_file"), "IMMPASSWORD")
    nodeset = ClusterShell.NodeSet.NodeSet(hosts)
    for host in nodeset:

        pat = re.compile("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
        if not pat.match(host):
            host = "imm" + host

        logging.info("{0}: ".format(host))
        cmd = ["ipmitool", "-I", "lanplus", "-H", host,
               "-U", imm_user, "-E", "fru", "print", "0"]

        logging.debug("ipmi/getmac: {0}".format(" ".join(cmd)))

        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
        # The data we want is in line 15
        lines = proc.stdout.readlines()
        if (len(lines) < 14):
            clara_exit("The host {0} can't be reached".format(host))
        full_mac = lines[14].split(":")[1].strip().upper()
        mac_address1 = "{0}:{1}:{2}:{3}:{4}:{5}".format(full_mac[0:2],
                                                        full_mac[2:4],
                                                        full_mac[4:6],
                                                        full_mac[6:8],
                                                        full_mac[8:10],
                                                        full_mac[10:12])

        mac_address2 = "{0}:{1}:{2}:{3}:{4}:{5}".format(full_mac[12:14],
                                                        full_mac[14:16],
                                                        full_mac[16:18],
                                                        full_mac[18:20],
                                                        full_mac[20:22],
                                                        full_mac[22:24])

        logging.info("  eth0's MAC address is {0}\n"
                     "  eth1's MAC address is {1}".format(mac_address1, mac_address2))
示例#13
0
文件: clara_ipmi.py 项目: hmlth/clara
def getmac(hosts):
    imm_user = value_from_file(get_from_config("common", "master_passwd_file"),
                               "IMMUSER")
    os.environ["IPMI_PASSWORD"] = value_from_file(
        get_from_config("common", "master_passwd_file"), "IMMPASSWORD")
    nodeset = ClusterShell.NodeSet.NodeSet(hosts)
    for host in nodeset:

        pat = re.compile("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
        if not pat.match(host):
            prefix = get_from_config("ipmi", "prefix")
            host = prefix + host

        logging.info("{0}: ".format(host))
        cmd = [
            "ipmitool", "-I", "lanplus", "-H", host, "-U", imm_user, "-E",
            "fru", "print", "0"
        ]

        logging.debug("ipmi/getmac: {0}".format(" ".join(cmd)))

        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
        # The data we want is in line 15
        lines = proc.stdout.readlines()
        if (len(lines) < 14):
            clara_exit("The host {0} can't be reached".format(host))
        full_mac = lines[14].split(":")[1].strip().upper()
        mac_address1 = "{0}:{1}:{2}:{3}:{4}:{5}".format(
            full_mac[0:2], full_mac[2:4], full_mac[4:6], full_mac[6:8],
            full_mac[8:10], full_mac[10:12])

        mac_address2 = "{0}:{1}:{2}:{3}:{4}:{5}".format(
            full_mac[12:14], full_mac[14:16], full_mac[16:18], full_mac[18:20],
            full_mac[20:22], full_mac[22:24])

        logging.info("  eth0's MAC address is {0}\n"
                     "  eth1's MAC address is {1}".format(
                         mac_address1, mac_address2))
示例#14
0
文件: clara_ipmi.py 项目: hmlth/clara
def do_ssh(hosts, command):

    prefix = get_from_config("ipmi", "prefix")
    hosts = prefix + hosts

    os.environ["SSHPASS"] = \
        value_from_file(get_from_config("common", "master_passwd_file"),
                        "IMMPASSWORD")
    imm_user = \
        value_from_file(get_from_config("common", "master_passwd_file"),
                        "IMMUSER")

    task = ClusterShell.Task.task_self()
    task.set_info("ssh_user", imm_user)
    task.set_info("ssh_path", "/usr/bin/sshpass -e /usr/bin/ssh")
    task.set_info("ssh_options", "-oBatchMode=no")
    task.shell(command, nodes=hosts)
    task.resume()

    for buf, nodes in task.iter_buffers():
        print "---\n%s:\n---\n %s" \
              % (ClusterShell.NodeSet.fold(",".join(nodes)),
                 buf)
示例#15
0
def main():
    logging.debug(sys.argv)
    dargs = docopt.docopt(__doc__)

    if dargs['connect']:
        do_connect(dargs['<host>'], dargs['-j'], dargs['-f'])
    elif dargs['deconnect']:
        ipmi_do(dargs['<hostlist>'], "sol", "deactivate")
    elif dargs['status']:
        ipmi_do(dargs['<hostlist>'], "power", "status")
    elif dargs['setpwd']:
        imm_user = value_from_file(get_from_config("common", "master_passwd_file"), "IMMUSER")
        imm_pwd = value_from_file(get_from_config("common", "master_passwd_file"), "IMMPASSWORD")
        ipmi_do(dargs['<hostlist>'], "user", "set", "name", "2", imm_user)
        ipmi_do(dargs['<hostlist>'], "user", "set", "password", "2", imm_pwd)
    elif dargs['getmac']:
        getmac(dargs['<hostlist>'])
    elif dargs['on']:
        ipmi_do(dargs['<hostlist>'], "power", "on")
    elif dargs['off']:
        ipmi_do(dargs['<hostlist>'], "power", "off")
    elif dargs['reboot']:
        ipmi_do(dargs['<hostlist>'], "chassis", "power", "reset")
    elif dargs['blink']:
        ipmi_do(dargs['<hostlist>'], "chassis", "identify", "1")
    elif dargs['bios']:
        ipmi_do(dargs['<hostlist>'], "chassis", "bootparam", "set", "bootflag", "force_bios")
    elif dargs['immdhcp']:
        ipmi_do(dargs['<hostlist>'], "lan", "set", "1", "ipsrc", "dhcp")
    elif dargs['pxe']:
        ipmi_do(dargs['<hostlist>'], "chassis", "bootdev", "pxe")
    elif dargs['disk']:
        ipmi_do(dargs['<hostlist>'], "chassis", "bootdev", "disk")
    elif dargs['reset']:
        ipmi_do(dargs['<hostlist>'], "mc", "reset", "cold")
    elif dargs['ping']:
        do_ping(dargs['<hostlist>'])
示例#16
0
def ipmi_do(hosts, pty=False, *cmd):
    command = []
    if not isinstance(pty, bool):
        command.append(pty)
        pty = False
    command.extend(cmd)
    imm_user = value_from_file(get_from_config("common", "master_passwd_file"), "IMMUSER")
    os.environ["IPMI_PASSWORD"] = value_from_file(get_from_config("common", "master_passwd_file"), "IMMPASSWORD")
    nodeset = ClusterShell.NodeSet.NodeSet(hosts)
    for host in nodeset:

        pat = re.compile("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
        if not pat.match(host):
            host = "imm" + host

        ipmitool = ["ipmitool", "-I", "lanplus", "-H", host, "-U", imm_user, "-E", "-e!"]
        ipmitool.extend(command)

        logging.debug("ipmi/ipmi_do: {0}".format(" ".join(ipmitool)))

        if pty:
            run(ipmitool)
        else:
            os.system("echo -n '%s: ' ;" % host + " ".join(ipmitool))
示例#17
0
def do_key():
    key = get_from_config("repo", "gpg_key")
    fnull = open(os.devnull, 'w')
    cmd = ['gpg', '--list-secret-keys', key]
    logging.debug("repo/do_key: {0}".format(" ".join(cmd)))
    retcode = subprocess.call(cmd, stdout=fnull, stderr=fnull)
    fnull.close()

    # We import the key if it hasn't been imported before
    if retcode != 0:
        file_stored_key = get_from_config("repo", "stored_enc_key")
        if os.path.isfile(file_stored_key):
            password = value_from_file(get_from_config("common", "master_passwd_file"), "ASUPASSWD")

            if len(password) > 20:
                fdesc, temp_path = tempfile.mkstemp(prefix="tmpClara")
                cmd = ['openssl', 'aes-256-cbc', '-d', '-in', file_stored_key, '-out', temp_path, '-k', password]
                logging.debug("repo/do_key: {0}".format(" ".join(cmd)))
                retcode = subprocess.call(cmd)

                if retcode != 0:
                    os.close(fdesc)
                    os.remove(temp_path)
                    clara_exit('Command failed {0}'.format(" ".join(cmd)))
                else:
                    logging.info("Trying to import key {0}".format(key))
                    fnull = open(os.devnull, 'w')
                    cmd = ['gpg', '--allow-secret-key-import', '--import', temp_path]
                    logging.debug("repo/do_key: {0}".format(" ".join(cmd)))
                    retcode = subprocess.call(cmd)
                    fnull.close()
                    os.close(fdesc)
                    os.remove(temp_path)
                    if retcode != 0:
                        logging.info("\nThere was a problem with the import, make sure the key you imported "
                              "from {0} is the same you have in your configuration: {1}".format(file_stored_key, key))

            else:
                clara_exit('There was some problem reading the value of ASUPASSWD')
        else:
            clara_exit('Unable to read:  {0}'.format(file_stored_key))
    else:
        logging.info("GPG key was already imported.")
示例#18
0
文件: clara_nodes.py 项目: fmdm/clara
def do_connect(hosts):
    nodeset = ClusterShell.NodeSet.NodeSet(hosts)
    if (len(nodeset) != 1):
	    sys.exit('Only one host allowed for this command')
    else:
            try:
                cmd = ["service", "conman", "status"]
                retcode = subprocess.call(cmd, stdout=DEVNULL, stderr=STDOUT)
            except OSError, e:
                if (e.errno == errno.ENOENT):
                    sys.exit("Binary not found, check your path and/or retry as root."
                             "You were trying to run:\n {0}".format(" ".join(cmd)))

            if retcode == 0:  # if conman is running
                os.environ["CONMAN_ESCAPE"] = '!'
                conmand = value_from_file(get_from_config("nodes", "conmand"))
                run(["conman", "-d", conmand, hosts])
            elif retcode == 1 or retcode == 3:  # if conman is NOT running
                ipmi_do(hosts, ["sol", "activate"], pty=True)
            else:
                sys.exit('E: ' + ' '.join(cmd))
示例#19
0
文件: clara_repo.py 项目: fmdm/clara
def do_key():
    fnull = open(os.devnull, 'w')
    cmd = ['gpg', '--list-secret-keys', get_from_config("repo", "gpg_key")]
    retcode = subprocess.call(cmd, stdout=fnull)
    fnull.close()

    # We import the key if it hasn't been imported before
    if retcode != 0:
        file_stored_key = get_from_config("repo", "stored_enc_key")
        if os.path.isfile(file_stored_key):
            password = value_from_file(get_from_config("common", "master_passwd_file"), "PASSPHRASE")

            if len(password) > 20:
                fdesc, temp_path = tempfile.mkstemp()
                cmd = ['openssl', 'aes-256-cbc', '-d', '-in', file_stored_key,
                       '-out', temp_path, '-k', password]
                retcode = subprocess.call(cmd)

                if retcode != 0:
                    os.close(fdesc)
                    os.remove(temp_path)
                    sys.exit('Command failed {0}'.format(" ".join(cmd)))
                else:
                    fnull = open(os.devnull, 'w')
                    cmd = ['gpg', '--allow-secret-key-import',
                           '--import', temp_path]
                    retcode = subprocess.call(cmd, stdout=fnull)
                    fnull.close()
                    os.close(fdesc)
                    os.remove(temp_path)
                    if retcode != 0:
                        sys.exit('Command failed {0}'.format(" ".join(cmd)))

            else:
                sys.exit('There was some problem reading the PASSPHRASE')
        else:
            sys.exit('Unable to read:  {0}'.format(file_stored_key))
    else:
        print "GPG key was already imported."
示例#20
0
def do_key():
    key = get_from_config("repo", "gpg_key")
    fnull = open(os.devnull, 'w')
    cmd = ['gpg', '--list-secret-keys', key]
    logging.debug("repo/do_key: {0}".format(" ".join(cmd)))
    retcode = subprocess.call(cmd, stdout=fnull, stderr=fnull)
    fnull.close()

    # We import the key if it hasn't been imported before
    if retcode != 0:
        file_stored_key = get_from_config("repo", "stored_enc_key")
        if os.path.isfile(file_stored_key):
            password = value_from_file(
                get_from_config("common", "master_passwd_file"), "ASUPASSWD")

            digest = get_from_config_or("common",
                                        "digest_type",
                                        default="sha256")
            if digest not in [
                    'md2', 'md5', 'mdc2', 'rmd160', 'sha', 'sha1', 'sha224',
                    'sha256', 'sha384', 'sha512'
            ]:
                logging.warning(
                    "Invalid digest type : {0}, using default digest type: sha256"
                    .format(digest))
                digest = "sha256"

            if len(password) > 20:
                fdesc, temp_path = tempfile.mkstemp(prefix="tmpClara")
                cmd = [
                    'openssl', 'aes-256-cbc', '-md', digest, '-d', '-in',
                    file_stored_key, '-out', temp_path, '-k', password
                ]
                logging.debug("repo/do_key: {0}".format(" ".join(cmd)))
                retcode = subprocess.call(cmd)

                if retcode != 0:
                    os.close(fdesc)
                    os.remove(temp_path)
                    clara_exit('Command failed {0}'.format(" ".join(cmd)))
                else:
                    logging.info("Trying to import key {0}".format(key))
                    fnull = open(os.devnull, 'w')
                    cmd = [
                        'gpg', '--allow-secret-key-import', '--import',
                        temp_path
                    ]
                    logging.debug("repo/do_key: {0}".format(" ".join(cmd)))
                    retcode = subprocess.call(cmd)
                    fnull.close()
                    os.close(fdesc)
                    os.remove(temp_path)
                    if retcode != 0:
                        logging.info(
                            "\nThere was a problem with the import, make sure the key you imported "
                            "from {0} is the same you have in your configuration: {1}"
                            .format(file_stored_key, key))

            else:
                clara_exit(
                    "There was some problem while reading ASUPASSWD's value")
        else:
            clara_exit('Unable to read:  {0}'.format(file_stored_key))
    else:
        logging.info("GPG key was already imported.")
示例#21
0
              "ETH1's MAC address is {1}\n".format(mac_address1, mac_address2)


def do_connect(hosts):

    try:
        cmd = ["service", "conman", "status"]
        retcode = subprocess.call(cmd)
    except OSError, e:
        if (e.errno == errno.ENOENT):
            sys.exit("Binary not found, check your path and/or retry as root."
                     "You were trying to run:\n {0}".format(" ".join(cmd)))

    if retcode == 0:  # if conman is running
        os.environ["CONMAN_ESCAPE"] = '!'
        conmand = value_from_file(get_from_config("nodes", "conmand"))
        run(["conman", "-d", conmand, hosts])
    elif retcode == 1:  # if conman is NOT running
        ipmi_do(hosts, "-e! sol activate")
    else:
        sys.exit('E: ' + ' '.join(cmd))


def do_ping(hosts):
    nodes = ClusterShell.NodeSet.NodeSet(hosts)
    cmd = ["fping", "-r1", "-u", "-s"] + list(nodes)
    run(cmd)


def main():
    dargs = docopt.docopt(__doc__)