Beispiel #1
0
def best_executable_match(executable_name,
                          exe_version_tuples,
                          version_str,
                          version_check_pref=VERSION_PREF_EXACT):
    version = version_obj(version_str)
    match_func = exact_exe_version_match

    exe_versions_str = exe_version_tuples_to_strs(exe_version_tuples)

    log_verbose("Found the following %s's. Selecting best match "
                "for version %s\n%s" %(executable_name, version_str,
                                       exe_versions_str))

    if version is None:
        log_verbose("mongoVersion is null. "
                    "Selecting default %s" % executable_name)
        match_func = default_match
    elif version_check_pref == VERSION_PREF_LATEST_STABLE:
        match_func = latest_stable_exe
    elif version_check_pref == VERSION_PREF_MAJOR_GE:
        match_func = major_ge_exe_version_match
    elif version_check_pref == VERSION_PREF_EXACT_OR_MINOR:
        match_func = exact_or_minor_exe_version_match

    return match_func(executable_name, exe_version_tuples, version)
Beispiel #2
0
 def supports_repl_key(self):
     """
      We need a repl key if you are auth + a cluster member +
      version is None or >= 2.0.0
     """
     version = self.get_mongo_version_obj()
     return (version is None or
             version >= version_obj(REPL_KEY_SUPPORTED_VERSION))
Beispiel #3
0
 def supports_repl_key(self):
     """
      We need a repl key if you are auth + a cluster member +
      version is None or >= 2.0.0
     """
     version = self.get_mongo_version_obj()
     return (version is None or
             version >= version_obj(REPL_KEY_SUPPORTED_VERSION))
Beispiel #4
0
def get_mongo_installation(version_str):
    # get all mongod installation dirs and return the one
    # whose version == specified version. If any...
    version = version_obj(version_str)
    for install_dir, install_version in find__all_mongo_installations():
        if install_version == version:
            return install_dir

    return None
Beispiel #5
0
def do_mongo_restore(source,
                     host=None,
                     port=None,
                     dbpath=None,
                     database=None,
                     username=None,
                     password=None,
                     server_version=None,
                     restore_options=None):


    # create restore command with host and port
    restore_cmd = [get_mongo_restore_executable(server_version)]

    if host:
        restore_cmd.extend(["--host", host])
    if port:
        restore_cmd.extend(["--port", str(port)])

    # dbpath
    if dbpath:
        restore_cmd.extend(["--dbpath", dbpath])

    # database
    if database:
        restore_cmd.extend(["-d", database])

    # username and password
    if username:
        restore_cmd.extend(["-u", username, "-p"])
        if password:
            restore_cmd.append(password)

    # ignore authenticationDatabase option is server_version is less than 2.4.0
    if (restore_options and "authenticationDatabase" in restore_options and
            server_version and
                version_obj(server_version) < MongoctlNormalizedVersion("2.4.0")):
        restore_options.pop("authenticationDatabase", None)

    # append shell options
    if restore_options:
        restore_cmd.extend(options_to_command_args(restore_options))

    # pass source arg
    restore_cmd.append(source)

    cmd_display =  restore_cmd[:]
    # mask user/password
    if username:
        cmd_display[cmd_display.index("-u") + 1] =  "****"
        if password:
            cmd_display[cmd_display.index("-p") + 1] =  "****"

    # execute!
    log_info("Executing command: \n%s" % " ".join(cmd_display))
    call_command(restore_cmd, bubble_exit_code=True)
Beispiel #6
0
def do_mongo_restore(source,
                     host=None,
                     port=None,
                     dbpath=None,
                     database=None,
                     username=None,
                     password=None,
                     server_version=None,
                     restore_options=None):

    # create restore command with host and port
    restore_cmd = [get_mongo_restore_executable(server_version)]

    if host:
        restore_cmd.extend(["--host", host])
    if port:
        restore_cmd.extend(["--port", str(port)])

    # dbpath
    if dbpath:
        restore_cmd.extend(["--dbpath", dbpath])

    # database
    if database:
        restore_cmd.extend(["-d", database])

    # username and password
    if username:
        restore_cmd.extend(["-u", username, "-p"])
        if password:
            restore_cmd.append(password)

    # ignore authenticationDatabase option is server_version is less than 2.4.0
    if (restore_options and "authenticationDatabase" in restore_options
            and server_version and
            version_obj(server_version) < MongoctlNormalizedVersion("2.4.0")):
        restore_options.pop("authenticationDatabase", None)

    # append shell options
    if restore_options:
        restore_cmd.extend(options_to_command_args(restore_options))

    # pass source arg
    restore_cmd.append(source)

    cmd_display = restore_cmd[:]
    # mask user/password
    if username:
        cmd_display[cmd_display.index("-u") + 1] = "****"
        if password:
            cmd_display[cmd_display.index("-p") + 1] = "****"

    # execute!
    log_info("Executing command: \n%s" % " ".join(cmd_display))
    call_command(restore_cmd, bubble_exit_code=True)
Beispiel #7
0
def get_mongo_dump_executable(server_version):
    dump_exe = get_mongo_executable(server_version,
                                    'mongodump',
                                    version_check_pref=
                                    VERSION_PREF_EXACT_OR_MINOR)
    # Warn the user if it is not an exact match (minor match)
    if server_version and version_obj(server_version) != dump_exe.version:
        log_warning("Using mongodump '%s' that does not exactly match "
                    "server version '%s'" % (dump_exe.version, server_version))

    return dump_exe.path
Beispiel #8
0
def get_mongo_dump_executable(server_version):
    dump_exe = get_mongo_executable(
        server_version,
        'mongodump',
        version_check_pref=VERSION_PREF_EXACT_OR_MINOR)
    # Warn the user if it is not an exact match (minor match)
    if server_version and version_obj(server_version) != dump_exe.version:
        log_warning("Using mongodump '%s' that does not exactly match "
                    "server version '%s'" % (dump_exe.version, server_version))

    return dump_exe.path
Beispiel #9
0
def mongo_exe_version(mongo_exe):
    try:
        re_expr = "v?((([0-9]+)\.([0-9]+)\.([0-9]+))([^, ]*))"
        vers_spew = execute_command([mongo_exe, "--version"])
        # only take first line of spew
        vers_spew = vers_spew.split('\n')[0]
        vers_grep = re.findall(re_expr, vers_spew)
        full_version = vers_grep[-1][0]
        result = version_obj(full_version)
        if result is not None:
            return result
        else:
            raise MongoctlException("Cannot parse mongo version from the"
                                    " output of '%s --version'" % mongo_exe)
    except Exception, e:
        log_exception(e)
        raise MongoctlException("Unable to get mongo version of '%s'."
                                " Cause: %s" % (mongo_exe, e))
Beispiel #10
0
 def get_mongo_version_obj(self):
     version_str = self.get_mongo_version()
     if version_str is not None:
         return version_obj(version_str)
     else:
         return None
Beispiel #11
0
    def test_version_functions(self):
        self.assertTrue(version_obj("1.2.0") < version_obj("1.2.1"))
        self.assertFalse(version_obj("1.2.0") < version_obj("1.2.0"))
        self.assertFalse(version_obj("1.2.0") < version_obj("1.2.0C"))
        self.assertFalse(version_obj("1.2.0-rc1") < version_obj("1.2.0-rc0"))
        self.assertTrue(version_obj("1.2.0-rc0") < version_obj("1.2.0-rc1"))
        self.assertTrue(version_obj("1.2.0-rc0") < version_obj("1.2.0"))
        self.assertTrue(version_obj("1.2.0-rc0") < version_obj("1.2.1"))

        self.assertTrue(is_valid_version("1.0.1"))
        self.assertTrue(is_valid_version("0.1"))
        self.assertFalse(is_valid_version("1"))
        self.assertTrue(is_valid_version("0.1a"))
        self.assertTrue(is_valid_version("0.1c"))
        self.assertTrue(is_valid_version("1.8.9-rc0"))
        self.assertFalse(is_valid_version("a.1.2.3.4"))
Beispiel #12
0
 def get_mongo_version_obj(self):
     version_str = self.get_mongo_version()
     if version_str is not None:
         return version_obj(version_str)
     else:
         return None