Exemplo n.º 1
0
    def expectSendline(self, pattern, cmd, timeoutMessage="EOF or TIMEOUT", replaceCMD=True, addAfter=True, **kwargs):
        """custom expect helper.
        It added EOF and TIMEOUT patterns and raise excemption with timeoutMessage message in match case. 
        It also print to stdout the output if verbosity>=3.
        If a pattern is list return index of match
        If pattern is not list return output cleared from special symbols"""

        p = [pexpect.EOF, pexpect.TIMEOUT]
        if type(pattern) is list:
            p += pattern
        else:
            p.append(pattern)
        imatch = self.expect(p, **kwargs)
        output = self.getCmdOutput(clearSpecialSymbols=False, addAfter=addAfter, replaceCMD=False)
        if hasattr(self, 'output'): self.output += output
        log.debug(output)
        if imatch == 0 or imatch == 1:
            msg = timeoutMessage
            if hasattr(self, 'timeoutMessage') and timeoutMessage == "EOF or TIMEOUT":
                msg = self.timeoutMessage
            raise Exception(msg)

        self.lastcmd = cmd + "\n"
        self.sendline(cmd)

        if type(pattern) is list:
            return imatch - 2
        else:
            return self.getCmdOutput(clearSpecialSymbols=True, addAfter=addAfter, replaceCMD=replaceCMD)
Exemplo n.º 2
0
    def update_bashrc(self):
        """Add AKRR enviroment variables to .bashrc"""
        log.info("Updating .bashrc")

        bashcontentNew = []
        akrrHeader = '#AKRR Server Environment Variables'
        if os.path.exists(os.path.expanduser("~/.bashrc")):
            log.info(
                "Updating AKRR record in $HOME/.bashrc, backing to $HOME/.bashrc_akrrbak"
            )
            if not dry_run:
                subprocess.call("cp ~/.bashrc ~/.bashrcakrr", shell=True)
            with open(os.path.expanduser('~/.bashrc'), 'r') as f:
                bashcontent = f.readlines()
                inAKRR = False
                for l in bashcontent:
                    if l.count(akrrHeader + ' [Start]') > 0: inAKRR = True
                    if not inAKRR: bashcontentNew.append(l)
                    if l.count(akrrHeader + ' [End]') > 0: inAKRR = False
        bashcontentNew.append("\n" + akrrHeader + " [Start]\n")
        bashcontentNew.append(
            "export PATH=\"{0}/bin:$PATH\"\n".format(akrr_home))
        bashcontentNew.append(akrrHeader + " [End]\n\n")
        if not dry_run:
            with open(os.path.expanduser('~/.bashrc'), 'w') as f:
                for l in bashcontentNew:
                    f.write(l)
            log.info("Appended AKRR records to $HOME/.bashrc")
        else:
            log.debug("New .bashrc should be like" + "\n".join(bashcontentNew))
Exemplo n.º 3
0
def _make_dirs(path):
    """Recursively create directories if not in dry run mode"""
    if not dry_run:
        log.debug("Creating directory: {}".format(path))
        os.makedirs(path)
    else:
        log.dry_run("_make_dirs({})".format(path))
Exemplo n.º 4
0
def create_and_populate_tables(
        default_tables, 
        population_statements, 
        starting_comment, ending_comment,
        connection_function,
        host=None, user=None, password=None, db=None,
        dry_run=False
        ):
    """
    :param default_tables:
    :param population_statements:
    :param starting_comment:
    :param ending_comment:
    :param connection_function:
    :type connection_function: function
    """
    log.info(starting_comment)

    try:
        if not dry_run:
            if host and user and password and db:
                connection = MySQLdb.connect(host, user, password, db)
                cursor = connection.cursor()
            else:
                connection, cursor = connection_function(True)

            with connection:
                for (table_name, table_script) in default_tables:
                    log.info("CREATING: %s" % table_name)
                    try:
                        result = cursor.execute(table_script)
                        log.debug("Result of: %s -> %d" % (table_name, result))
                        log.info("CREATED: %s SUCCESSFULLY!" % table_name)
                    except MySQLdb.Warning:
                        pass

                for (description, statement) in population_statements:
                    log.info("EXECUTING: %s" % description)

                    result = cursor.execute(statement)
                    log.debug("Result of: %s -> %d" % (table_name, result))
                    log.info("EXECUTED: %s SUCCESSFULLY!" % description)
        else:
            for (table_name, table_script) in default_tables:
                log.dry_run("CREATING: %s" % table_name)
                #log.info("CREATED: %s SUCCESSFULLY!" % table_name)

            for (description, statement) in population_statements:
                log.dry_run("EXECUTING: %s" % description)
                #log.info("EXECUTED: %s SUCCESSFULLY!" % description)
        log.info(ending_comment)
    except MySQLdb.Error as e:
        log.critical("Error %d: %s" % (e.args[0], e.args[1]))
        sys.exit(1)
Exemplo n.º 5
0
def retrieve_resources():
    """
    Retrieve the applicable contents of the `modw`.`resourcefact` table.
    :return: a tuple of strings containing the name of the resources.
    """
    con, cur = cfg.getXDDB()

    if con is None:
        # i.e. AKRR running without modw
        return tuple()

    cur.execute("SELECT `name`,`id` FROM `modw`.`resourcefact`")
    rows = cur.fetchall()

    log.debug("Retrieved %s Resource records...", len(rows) if rows else 0)

    return rows
Exemplo n.º 6
0
def set_default_value_for_unset_vars():
    """post process settings"""
    import os
    from .util import run_cmd_getoutput
    from akrr import log

    global which_akrr
    global akrr_conf
    global akrr_conf_dir
    global akrr_log_dir

    if which_akrr is None or which_akrr == "akrr":
        try:
            which_akrr = run_cmd_getoutput("which akrr").strip()
        except:
            which_akrr = None

    if which_akrr is not None:
        if akrr_conf is None:
            if os.path.dirname(which_akrr) == "/usr/bin":
                akrr_conf = os.path.expanduser("~/akrr/etc/akrr.conf")
                akrr_conf_dir = os.path.expanduser("~/akrr/etc")
                akrr_log_dir = os.path.expanduser("~/akrr/etc")
            else:
                akrr_conf_dir = os.path.abspath(
                    os.path.join(os.path.dirname(os.path.dirname(which_akrr)),
                                 "etc"))
                akrr_conf = os.path.join(akrr_conf_dir, 'akrr.conf')
                akrr_log_dir = os.path.abspath(
                    os.path.join(os.path.dirname(os.path.dirname(which_akrr)),
                                 "log"))
        else:
            akrr_conf_dir = os.path.abspath(
                os.path.join(os.path.dirname(os.path.dirname(akrr_conf)),
                             "etc"))
            akrr_log_dir = os.path.abspath(
                os.path.join(os.path.dirname(os.path.dirname(akrr_conf)),
                             "log"))

    log.debug("AKRR conf dir and log dir locations:\n"
              "    akrr_conf: {}\n"
              "    akrr_conf_dir: {}\n"
              "    akrr_log_dir: {}\n"
              "".format(akrr_conf, akrr_conf_dir, akrr_log_dir))
Exemplo n.º 7
0
def run_test_job(resource, app_name="test", nodes=2):
    log.info(
        "Will send test job to queue, wait till it executed and will analyze the output"
    )
    log.debug("Will use AKRR REST API at {}".format(
        akrrrestclient.restapi_host))

    check_connection_to_rest_api()

    if dry_run:
        return

    task_id = check_if_test_job_already_submitted(resource, app_name)

    if task_id is None:
        task_id = submit_test_job(resource, app_name, nodes)

    monitor_test_job(task_id)
    analyse_test_job_results(task_id, resource, app_name)

    os.remove(get_test_job_lock_filename(resource, app_name))
Exemplo n.º 8
0
def remove(
        db_akrr=False, db_appkernel=False, db_modw=False, db_user=False,
        conf_dir=False, log_dir=False,
        bashrc=False,
        crontab=False, crontab_remove_mailto=False,
        **kwargs):
    log.debug(
        "Removal options for removal:\n"
        "    db_akrr: {}\n"
        "    db_appkernel: {}\n"
        "    db_modw: {}\n"
        "    db_user: {}\n"
        "    conf_dir: {}\n"
        "    log_dir: {}\n"
        "    bashrc: {}\n"
        "    crontab: {} , crontab_remove_mailto: {}\n"
        "".format(
            db_akrr, db_appkernel, db_modw, db_user, conf_dir, log_dir, bashrc, crontab, crontab_remove_mailto)
    )
    _stop_akrr()

    log.debug2("Unused keyword arguments: {}".format(kwargs))

    if db_user:
        _remove_user()
    if db_akrr:
        _remove_akrr_db()
    if db_appkernel:
        _remove_ak_db()
    if db_modw:
        _remove_modw_db()
    if conf_dir:
        _remove_conf_dir()
    if log_dir:
        _remove_log_dir()
    if bashrc:
        _remove_from_bashrc()
    if crontab:
        _remove_from_crontab(crontab_remove_mailto)
Exemplo n.º 9
0
def _remove_from_crontab(remove_mailto=False):
    """remove from cron"""

    try:

        crontab_content = subprocess.check_output("crontab -l", shell=True)
    except subprocess.CalledProcessError:
        log.error("Can not run crontab -l")
        return

    new_crontab = False
    crontab_content = crontab_content.decode("utf-8").splitlines(True)

    with open(os.path.expanduser('.crontmp'), 'w') as f:
        for l in crontab_content:
            not_akrr = True
            if l.count('akrr') > 0 and (l.count('checknrestart.sh') > 0 or l.count('restart.sh') > 0):
                not_akrr = False
            if remove_mailto and l.count('MAILTO') > 0:
                not_akrr = False
            if not_akrr:
                f.write(l)
            else:
                new_crontab = True
    if new_crontab:
        log.info("AKRR Section present in crontab. Cleaning crontab.")
        try:
            if not dry_run:
                output = subprocess.check_output("crontab .crontmp", shell=True).decode("utf-8")
                log.debug(output)
            else:
                log.info("DRY RUN: should run `crontab .crontmp`. .crontmp:" + open(".crontmp", "rt").read())
        except subprocess.CalledProcessError:
            log.error("Can not run crontab .crontmp")
        os.remove(".crontmp")
    else:
        log.info("There was no AKRR records detected in crontab list")
Exemplo n.º 10
0
def copy_exec_sources_and_inputs(rsh, resource):
    """Copy exec sources and inputs to remote resource"""
    log.info(
        "Preparing to copy application signature calculator,\n"
        "    app. kernel input files and \n"
        "    HPCC, IMB, IOR and Graph500 source code to remote resource\n")

    try:
        cfg.sshCommand(rsh, "cd %s" % resource['appKerDir'])
        out = cfg.sshCommand(rsh, "ls " + resource['appKerDir'])
        files_in_appker_dir = out.strip().split()

        if not ("inputs" in files_in_appker_dir
                or "inputs/" in files_in_appker_dir):
            log.info("Copying app. kernel input tarball to %s",
                     resource['appKerDir'])
            if not dry_run:
                cfg.scpToResource(resource,
                                  cfg.appker_repo_dir + "/inputs.tar.gz",
                                  resource['appKerDir'])

            log.info("Unpacking app. kernel input files to %s/inputs",
                     resource['appKerDir'])
            if not dry_run:
                out = cfg.sshCommand(
                    rsh, "tar xvfz %s/inputs.tar.gz" % resource['appKerDir'])
                log.debug(out)

                out = cfg.sshCommand(rsh,
                                     "du -h %s/inputs" % resource['appKerDir'])
                log.debug(out)

                if out.count("No such file or directory") == 0:
                    log.info("App. kernel input files are in %s/inputs\n",
                             resource['appKerDir'])
                else:
                    raise Exception("files are not copied!")
        else:
            log.warning_count += 1
            log.warning(
                "WARNING %d: App. kernel inputs directory %s/inputs is present, assume they are correct.\n",
                log.warning_count, resource['appKerDir'])

        if not ("execs" in files_in_appker_dir
                or "execs/" in files_in_appker_dir):
            log.info(
                "Copying app. kernel execs tarball to %s\n" %
                (resource['appKerDir']) +
                "It contains HPCC,IMB,IOR and Graph500 source code and app.signature calculator"
            )
            if not dry_run:
                cfg.scpToResource(resource,
                                  cfg.appker_repo_dir + "/execs.tar.gz",
                                  resource['appKerDir'])
            log.info(
                "Unpacking HPCC,IMB,IOR and Graph500 source code and app.signature calculator files to %s/execs",
                resource['appKerDir'])
            if not dry_run:
                out = cfg.sshCommand(
                    rsh, "tar xvfz %s/execs.tar.gz" % resource['appKerDir'])
                log.debug(out)

                out = cfg.sshCommand(rsh,
                                     "df -h %s/execs" % resource['appKerDir'])
                log.debug(out)

                if out.count("No such file or directory") == 0:
                    log.info(
                        "HPCC,IMB,IOR and Graph500 source code and app.signature calculator are in %s/execs\n",
                        resource['appKerDir'])
                else:
                    raise Exception("files are not copied!")
        else:
            log.warning_count += 1
            log.warning(
                "WARNING %d: App. kernel executables directory %s/execs is present, assume they are correct.",
                log.warning_count, resource['appKerDir'])
            log.warning(
                "It should contain HPCC,IMB,IOR and Graph500 source code and app.signature calculator\n"
            )

        cfg.sshCommand(rsh, "rm execs.tar.gz  inputs.tar.gz")
    except Exception as e:
        log.critical("Can not copy files to %s", resource['name'])
        raise e
Exemplo n.º 11
0
def resource_add(config):
    """add resource, config should have following members
        dry_run - Dry Run No files will actually be created
        minimalistic - Minimize questions number, configuration files will be edited manually
        no-ping - do not run ping to test headnode name
        verbose
    """
    global verbose
    global dry_run
    global no_ping
    global minimalistic
    global resource_name
    global remoteAccessNode
    global remoteAccessMethod
    global remoteCopyMethod
    global sshUserName
    global sshPassword
    global sshPrivateKeyFile
    global sshPrivateKeyPassword
    global networkScratch
    global localScratch
    global akrrData
    global appKerDir
    global batchScheduler
    global batchJobHeaderTemplate

    if config.verbose:
        verbose = True

    log.info("Beginning Initiation of New Resource...")
    verbose = config.verbose
    dry_run = config.dry_run
    resource_deploy.dry_run = config.dry_run
    no_ping = config.no_ping
    minimalistic = config.minimalistic

    log.info("Retrieving Resources from XDMoD Database...")
    # RETRIEVE: the resources from XDMoD
    resources = retrieve_resources()
    log.info("Found following resources from XDMoD Database:\n" +
             "    resource_id  name\n" + "\n".join([
                 "    %11d  %-40s" % (resource_id, resource_name)
                 for resource_name, resource_id in resources
             ]) + "\n")

    if len(resources) > 0:
        while True:
            log.log_input(
                'Enter resource_id for import (enter 0 for no match):')
            resource_id = input()
            if validate_resource_id(resource_id, resources):
                break
            log.warning("Incorrect resource_id try again")
        log.empty_line()
        resource_id = int(resource_id)
    else:
        resource_id = 0

    if resource_id <= 0:  # i.e. no match from XDMoD DB
        resource_id = None

    resource_name = ""
    while True:
        if resource_id is None:
            log.log_input('Enter AKRR resource name:')
            resource_name = input()
        else:
            resource_name2 = get_resource_name_by_id(resource_id, resources)
            log.log_input(
                'Enter AKRR resource name, hit enter to use same name as in XDMoD Database [%s]:'
                % (resource_name2, ))
            resource_name = input()
            if resource_name.strip() == "":
                resource_name = resource_name2

        if validate_resource_name(resource_name):
            break
    log.empty_line()

    while True:
        log.log_input('Enter queuing system on resource (slurm or pbs): ')
        queuing_system = input()
        if validate_queuing_system(queuing_system):
            break
        else:
            log.error("Incorrect queuing_system try again")

    batchScheduler = queuing_system
    log.empty_line()

    if minimalistic is False:
        get_remote_access_method()
        get_system_characteristics()
        get_file_system_access_points()

    log.debug("Summary of parameters" +
              "resource_name: {}".format(resource_name) +
              "remoteAccessNode: {}".format(remoteAccessNode) +
              "remoteAccessMethod: {}".format(remoteAccessMethod) +
              "remoteCopyMethod: {}".format(remoteCopyMethod) +
              "sshUserName: {}".format(sshUserName) +
              "sshPassword: {}".format(sshPassword) +
              "sshPrivateKeyFile: {}".format(sshPrivateKeyFile) +
              "sshPrivateKeyPassword: {}".format(sshPrivateKeyPassword) +
              "networkScratch: {}".format(networkScratch) +
              "localScratch: {}".format(localScratch) +
              "akrrData: {}".format(akrrData) +
              "appKerDir: {}".format(appKerDir) +
              "batchScheduler: {}".format(batchScheduler) +
              "batchJobHeaderTemplate: {}".format(batchJobHeaderTemplate) +
              "\n")

    generate_resource_config(resource_id, resource_name, queuing_system)
    log.info("Initiation of new resource is completed.\n"
             "    Edit batchJobHeaderTemplate variable in {}\n"
             "    and move to resource validation and deployment step.\n"
             "    i.e. execute:\n"
             "        akrr resource deploy -r {}".format(
                 resource_cfg_filename, resource_name))
Exemplo n.º 12
0
def get_remote_access_method():
    global resource_name
    global remoteAccessNode
    global remoteAccessMethod
    global remoteCopyMethod
    global sshUserName
    global sshPassword
    global sshPassword4thisSession
    global sshPrivateKeyFile
    global sshPrivateKeyPassword
    global rsh
    global no_ping

    # set remoteAccessNode
    while True:
        log.log_input(
            "Enter Resource head node (access node) full name (e.g. headnode.somewhere.org):"
        )
        remoteAccessNode = input("[%s] " % resource_name)
        if remoteAccessNode.strip() == "":
            remoteAccessNode = resource_name

        response = os.system("ping -c 1 -w2 " + remoteAccessNode +
                             " > /dev/null 2>&1")

        if response == 0:
            break
        else:
            if no_ping:
                log.warning("Can not ping %s, but asked to ignore it.",
                            remoteAccessNode)
                break
            log.error("Incorrect head node name (can not ping %s), try again",
                      remoteAccessNode)

    # set sshUserName
    current_user = getpass.getuser()
    ask_for_user_name = True

    while True:
        if ask_for_user_name:
            log.log_input("Enter username for resource access:")
            sshUserName = input("[%s] " % current_user)
            if sshUserName.strip() == "":
                sshUserName = current_user
            current_user = sshUserName

        # check password-less access
        if sshPassword is None:
            log.info("Checking for password-less access")
        else:
            log.info("Checking for resource access")
        successfully_connected = check_connection_to_resource()

        if successfully_connected:
            if sshPassword is None:
                log.info("Can access resource without password")
            else:
                log.info("Can access resource")

        if successfully_connected is False:
            log.info("Can not access resource without password")
            action_list = [(
                "TryAgain",
                "The private and public keys was generated manually, right now. Try again."
            )]
            # check private keys
            user_home_dir = os.path.expanduser("~")
            private_keys = [
                os.path.join(user_home_dir, '.ssh', f[:-4])
                for f in os.listdir(os.path.join(user_home_dir, '.ssh'))
                if os.path.isfile(os.path.join(user_home_dir, '.ssh', f))
                and f[-4:] == '.pub'
                and os.path.isfile(os.path.join(user_home_dir, '.ssh', f[:-4]))
            ]

            if len(private_keys) > 0:
                action_list.append(("UseExistingPrivateKey",
                                    "Use existing private and public key."))

            action_list.append(
                ("GenNewKey", "Generate new private and public key."))
            action_list.append(("UsePassword", "Use password directly."))
            log.empty_line()

            log.info("Select authentication method:" + "\n".join([
                "%3d  %s" % (i, desc)
                for i, (_, desc) in enumerate(action_list)
            ]))
            while True:
                log.log_input("Select option from list above:")
                try:
                    action = input("[2] ")
                    if action.strip() == "":
                        action = 2
                    else:
                        action = int(action)

                    if action < 0 or action >= len(action_list):
                        raise ValueError()
                    break
                except (ValueError, TypeError):
                    log.error("Incorrect entry, try again.")

            # do the action
            log.empty_line()
            if action_list[action][0] == "TryAgain":
                continue
            if action_list[action][0] == "UsePassword":
                log.log_input("Enter password for %s@%s:" %
                              (sshUserName, remoteAccessNode))
                sshPassword = getpass.getpass("")
                ask_for_user_name = not ask_for_user_name
                continue
            if action_list[action][0] == "UseExistingPrivateKey":
                log.info("Available private keys:" + "\n".join(
                    ["%3d  %s" % (i, p) for i, p in enumerate(private_keys)]))
                while True:
                    log.log_input("Select key number from list above:")
                    try:
                        i_key = input("")
                        i_key = int(i_key)

                        if i_key < 0 or i_key >= len(private_keys):
                            raise ValueError()
                        break
                    except (ValueError, TypeError):
                        log.error("Incorrect entry, try again.")
                sshPrivateKeyFile = private_keys[i_key]
                ask_for_user_name = not ask_for_user_name
                continue
            if action_list[action][0] == "GenNewKey":
                count = 0
                while True:
                    log.log_input(
                        "Enter password for %s@%s (will be used only during this session):"
                        % (sshUserName, remoteAccessNode))
                    sshPassword4thisSession = getpass.getpass("")
                    sshPassword = sshPassword4thisSession

                    if check_connection_to_resource():
                        break
                    count += 1
                    if count >= 3:
                        break
                sshPassword = None
                # generate keys
                log.log_input("Enter private key name:")
                sshPrivateKeyFile = input("[id_rsa_%s]" % resource_name)
                if sshPrivateKeyFile.strip() == "":
                    sshPrivateKeyFile = "id_rsa_%s" % resource_name
                sshPrivateKeyFile = os.path.join(user_home_dir, '.ssh',
                                                 sshPrivateKeyFile)
                log.log_input(
                    "Enter passphrase for new key (leave empty for passwordless access):"
                )
                sshPrivateKeyPassword = getpass.getpass("")
                os.system("ssh-keygen -t rsa -N \"%s\" -f %s" %
                          (sshPrivateKeyPassword, sshPrivateKeyFile))
                if sshPrivateKeyPassword.strip() == "":
                    sshPrivateKeyPassword = None
                # copy keys
                cfg.sshAccess(remoteAccessNode,
                              ssh='ssh-copy-id',
                              username=sshUserName,
                              password=sshPassword4thisSession,
                              PrivateKeyFile=sshPrivateKeyFile,
                              PrivateKeyPassword=None,
                              logfile=sys.stdout,
                              command='')
                ask_for_user_name = not ask_for_user_name
                continue

        if successfully_connected:
            break
        else:
            log.error("Incorrect resource access credential")

    if successfully_connected:
        log.empty_line()
        log.info("Connecting to " + resource_name)

        str_io = io.StringIO()
        try:
            sys.stdout = sys.stderr = str_io
            rsh = cfg.sshAccess(remoteAccessNode,
                                ssh=remoteAccessMethod,
                                username=sshUserName,
                                password=sshPassword,
                                PrivateKeyFile=sshPrivateKeyFile,
                                PrivateKeyPassword=sshPrivateKeyPassword,
                                logfile=sys.stdout,
                                command=None)
            sys.stdout = sys.__stdout__
            sys.stderr = sys.__stderr__
        except Exception as e:
            sys.stdout = sys.__stdout__
            sys.stderr = sys.__stderr__
            log.debug(str_io.getvalue())
            raise e

        log.info("              Done")
    log.empty_line()
    return successfully_connected
Exemplo n.º 13
0
def check_connection_to_resource():
    """check the connection to remote resource."""
    global remoteAccessNode
    global remoteAccessMethod
    global remoteCopyMethod
    global sshUserName
    global sshPassword
    global sshPassword4thisSession
    global sshPrivateKeyFile
    global sshPrivateKeyPassword

    successfully_connected = False
    passphrase_entrance_count = 0
    authorize_key_count = 0
    while True:
        # Try to connect
        str_io = io.StringIO()
        try:
            sys.stdout = sys.stderr = str_io
            cfg.sshAccess(remoteAccessNode,
                          ssh=remoteAccessMethod,
                          username=sshUserName,
                          password=sshPassword,
                          PrivateKeyFile=sshPrivateKeyFile,
                          PrivateKeyPassword=sshPrivateKeyPassword,
                          logfile=str_io,
                          command='ls')

            sys.stdout = sys.__stdout__
            sys.stderr = sys.__stderr__

            successfully_connected = True
            break
        except Exception:
            sys.stdout = sys.__stdout__
            sys.stderr = sys.__stderr__
            response = str_io.getvalue()

            log.debug(
                "Had attempted to access resource without password and failed, below is resource response"
                + "=" * 80 + str_io.getvalue() + "=" * 80)

            # check if it asking for passphrase
            m = re.search(r"Enter passphrase for key '(.*)':", response)
            if m:
                if passphrase_entrance_count >= 3:
                    sshPrivateKeyPassword = None
                    sshPrivateKeyFile = None
                    break
                if passphrase_entrance_count > 0:
                    log.error("Incorrect passphrase try again")
                sshPrivateKeyFile = m.group(1)
                log.log_input("Enter passphrase for key '%s':" %
                              sshPrivateKeyFile)
                sshPrivateKeyPassword = getpass.getpass("")
                passphrase_entrance_count += 1
                continue
            m2 = re.search(r"[pP]assword:", response)
            if m is None and sshPrivateKeyFile is not None and m2:
                log.warning(
                    "Can not login to head node. "
                    "Probably the public key of private key was not authorized on head node"
                )
                log.info(
                    "Will try to add public key to list of authorized keys on head node"
                )
                while True:
                    try:
                        authorize_key_count += 1
                        log.log_input(
                            "Enter password for %s@%s (will be used only during this session):"
                            % (sshUserName, remoteAccessNode))
                        sshPassword4thisSession = getpass.getpass("")
                        log.empty_line()
                        str_io = io.StringIO()
                        sys.stdout = sys.stderr = str_io
                        cfg.sshAccess(remoteAccessNode,
                                      ssh='ssh-copy-id',
                                      username=sshUserName,
                                      password=sshPassword4thisSession,
                                      PrivateKeyFile=sshPrivateKeyFile,
                                      PrivateKeyPassword=None,
                                      logfile=str_io,
                                      command='')

                        sys.stdout = sys.__stdout__
                        sys.stderr = sys.__stderr__
                        log.info(response)

                        log.info(
                            "Have added public key to list of authorized keys on head node, "
                            "will attempt to connect again.")
                        log.empty_line()
                        break
                    except Exception:
                        sys.stdout = sys.__stdout__
                        sys.stderr = sys.__stderr__
                        if verbose:
                            log.debug(
                                "Had attempted to add public key to list of authorized keys on head node and failed, "
                                + "below is resource response" + "=" * 80 +
                                str_io.getvalue() + "=" * 80)
                        log.error("Incorrect password try again.")
                        if authorize_key_count >= 3:
                            break
                if authorize_key_count < 3:
                    continue
            break
    return successfully_connected
Exemplo n.º 14
0
    def read_db_user_credentials(self):
        ###
        # mod_akrr
        log.info(
            "Before Installation continues we need to setup the database.")

        self.akrr_db_user_name, self.akrr_db_user_password = _read_username_password(
            "Please specify a database user to access mod_akrr database (Used by AKRR)"
            "(This user will be created if it does not already exist):",
            self.akrr_db_user_name, self.akrr_db_user_password,
            self.default_akrr_user)
        log.empty_line()

        # check if user, db already there

        user_exists = False
        db_exists = False
        user_rights_are_correct = False
        try:
            # connect with provided user, Exception will raise if user can not connect
            _, cur = get_con_to_db(self.akrr_db_user_name,
                                   self.akrr_db_user_password,
                                   self.akrr_db_host, self.akrr_db_port)
            client_host = get_db_client_host(cur)
            user_exists = True

            db_exists = db_exist(cur, self.akrr_db_name)
            if not db_exists:
                log.debug("Database {} doesn't exists on {}".format(
                    self.akrr_db_name, self.akrr_db_host))
            user_rights_are_correct = db_check_priv(cur, self.akrr_db_name,
                                                    "ALL",
                                                    self.akrr_db_user_name,
                                                    client_host)
            if not user_rights_are_correct:
                log.debug(
                    "User {} doesn't have right privilege on {}, should be ALL"
                    .format(self.akrr_db_user_name, self.akrr_db_name))
        except MySQLdb.Error:
            user_exists = False
            log.debug("User ({}) does not exists on {}".format(
                self.akrr_db_user_name, self.akrr_db_host))

        # ask for su user on this machine if needed
        if not user_exists or not db_exists or not user_rights_are_correct:
            self.akrr_db_su_user_name, \
            self.akrr_db_su_user_password = _read_sql_su_credentials(self.akrr_db_host, self.akrr_db_port)
        log.empty_line()
        ###
        # mod_appkernel
        same_host_as_ak = self.ak_db_host == self.akrr_db_host and self.ak_db_port == self.akrr_db_port

        self.ak_db_user_name, self.ak_db_user_password = _read_username_password(
            "Please specify a database user to access mod_appkernel database "
            "(Used by XDMoD appkernel module, AKRR creates and syncronize resource and appkernel description)"
            "(This user will be created if it does not already exist):",
            self.ak_db_user_name, self.ak_db_user_password,
            self.akrr_db_user_name,
            self.akrr_db_user_password if same_host_as_ak else None)
        log.empty_line()

        # ask for su user on this machine
        user_exists = False
        db_exists = False
        user_rights_are_correct = False
        try:
            _, cur = get_con_to_db(self.ak_db_user_name,
                                   self.ak_db_user_password, self.ak_db_host,
                                   self.ak_db_port)
            client_host = get_db_client_host(cur)
            user_exists = True

            db_exists = db_exist(cur, self.ak_db_name)
            if not db_exists:
                log.debug("Database {} doesn't exists on {}".format(
                    self.ak_db_name, self.ak_db_host))
            user_rights_are_correct = db_check_priv(cur, self.ak_db_name,
                                                    "ALL",
                                                    self.ak_db_user_name,
                                                    client_host)
            if not user_rights_are_correct:
                log.debug(
                    "User {} doesn't have right privelege on {}, should be ALL"
                    .format(self.ak_db_user_name, self.ak_db_name))
        except Exception as e:
            user_exists = False
            log.debug("User ({}) does not exists on {}".format(
                self.akrr_db_user_name, self.akrr_db_host))

        if not user_exists or not db_exists or not user_rights_are_correct:
            self.ak_db_su_user_name = self.akrr_db_su_user_name
            self.ak_db_su_user_password = self.akrr_db_su_user_password
            try:
                get_con_to_db(self.ak_db_su_user_name,
                              self.ak_db_su_user_password, self.ak_db_host,
                              self.ak_db_port)
            except:
                self.ak_db_su_user_name, \
                self.ak_db_su_user_password = _read_sql_su_credentials(self.ak_db_host, self.ak_db_port)
        log.empty_line()

        ##
        # modw
        same_host_as_xd = self.xd_db_host == self.ak_db_host and self.xd_db_port == self.ak_db_port

        self.xd_db_user_name, \
        self.xd_db_user_password = _read_username_password(
            "Please specify the user that will be connecting to the XDMoD database (modw):",
            self.xd_db_user_name,
            self.xd_db_user_password,
            self.ak_db_user_name,
            self.ak_db_user_password if same_host_as_xd else None
        )
        log.empty_line()

        # ask for su user on this machine
        user_exists = False
        db_exists = False
        user_rights_are_correct = False
        try:

            _, cur = get_con_to_db(self.xd_db_user_name,
                                   self.xd_db_user_password, self.xd_db_host,
                                   self.xd_db_port)
            client_host = get_db_client_host(cur)
            user_exists = True

            db_exists = db_exist(cur, "modw")
            if not db_exists:
                log.debug("Database {} doesn't exists on {}".format(
                    self.xd_db_name, self.xd_db_host))
            user_rights_are_correct = db_check_priv(cur, self.xd_db_name,
                                                    "SELECT",
                                                    self.xd_db_user_name,
                                                    client_host)
            if not user_rights_are_correct:
                log.debug(
                    "User {} doesn't have right privelege on {}, should be at least SELECT"
                    .format(self.xd_db_user_name, self.xd_db_name))
        except Exception as e:
            user_exists = False
            log.debug("User ({}) does not exists on {}".format(
                self.xd_db_user_name, self.xd_db_host))

        if not user_exists or not db_exists or not user_rights_are_correct:
            self.xd_db_su_user_name = self.ak_db_su_user_name
            self.xd_db_su_user_password = self.ak_db_su_user_password
            try:
                get_con_to_db(self.xd_db_su_user_name,
                              self.xd_db_su_user_password, self.xd_db_host,
                              self.xd_db_port)
            except:
                self.ak_db_su_user_name, \
                self.ak_db_su_user_password = _read_sql_su_credentials(self.xd_db_host, self.xd_db_port)
        log.empty_line()