Пример #1
0
def show_cluster_command(parsed_options):
    cluster = repository.lookup_cluster(parsed_options.cluster)
    if cluster is None:
        raise MongoctlException("Could not find cluster '%s'." %
                                parsed_options.cluster)
    log_info("Configuration for cluster '%s':" % parsed_options.cluster)
    print cluster
Пример #2
0
def show_server_command(parsed_options):
    server = repository.lookup_server(parsed_options.server)
    if server is None:
        raise MongoctlException("Could not find server '%s'." %
                                parsed_options.server)
    log_info("Configuration for server '%s':" % parsed_options.server)
    print server
Пример #3
0
def do_mongo_restore(source,
                     host=None,
                     port=None,
                     dbpath=None,
                     database=None,
                     username=None,
                     password=None,
                     version_info=None,
                     restore_options=None):


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

    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 version_info is less than 2.4.0
    if (restore_options and "authenticationDatabase" in restore_options and
            version_info and version_info < make_version_info("2.4.0")):
        restore_options.pop("authenticationDatabase", None)

    # ignore restoreDbUsersAndRoles option is version_info is less than 2.6.0
    if (restore_options and "restoreDbUsersAndRoles" in restore_options and
            version_info and version_info < make_version_info("2.6.0")):
        restore_options.pop("restoreDbUsersAndRoles", 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)
Пример #4
0
def show_cluster_command(parsed_options):
    cluster = repository.lookup_cluster(parsed_options.cluster)
    if cluster is None:
        raise MongoctlException("Could not find cluster '%s'." %
                                parsed_options.cluster)
    log_info("Configuration for cluster '%s':" % parsed_options.cluster)
    print cluster
Пример #5
0
def do_mongo_restore(source,
                     host=None,
                     port=None,
                     dbpath=None,
                     database=None,
                     username=None,
                     password=None,
                     version_info=None,
                     restore_options=None):

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

    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 version_info is less than 2.4.0
    if (restore_options and "authenticationDatabase" in restore_options
            and version_info and version_info < make_version_info("2.4.0")):
        restore_options.pop("authenticationDatabase", None)

    # ignore restoreDbUsersAndRoles option is version_info is less than 2.6.0
    if (restore_options and "restoreDbUsersAndRoles" in restore_options
            and version_info and version_info < make_version_info("2.6.0")):
        restore_options.pop("restoreDbUsersAndRoles", 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)
Пример #6
0
    def configure_sharded_cluster(self):
        sh_list = self.list_shards()
        if sh_list and sh_list.get("shards"):
            log_info("Shard cluster already configured. Will only be adding"
                     " new shards as needed...")

        for shard_member in self.shards:
            self.add_shard(shard_member.get_member_cluster_or_server())
Пример #7
0
 def set_runtime_parameter_cmd(self, name, value):
     log_info("Setting runtime parameter '%s' to '%s', for server '%s'..." %
              (name, value, self.id))
     cmd = {
         "setParameter": 1,
         name: value
     }
     self.db_command(cmd, "admin")
Пример #8
0
    def configure_sharded_cluster(self):
        sh_list = self.list_shards()
        if sh_list and sh_list.get("shards"):
            log_info("Shard cluster already configured. Will only be adding"
                     " new shards as needed...")

        for shard_member in self.shards:
            self.add_shard(shard_member.get_shard())
Пример #9
0
def do_mongo_dump(host=None,
                  port=None,
                  dbpath=None,
                  database=None,
                  username=None,
                  password=None,
                  version_info=None,
                  dump_options=None):


    # create dump command with host and port
    dump_cmd = [get_mongo_dump_executable(version_info)]

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

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

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

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

    # ignore authenticationDatabase option is version_info is less than 2.4.0
    if (dump_options and "authenticationDatabase" in dump_options and
            version_info and version_info < VersionInfo("2.4.0")):
        dump_options.pop("authenticationDatabase", None)

    # ignore dumpDbUsersAndRoles option is version_info is less than 2.6.0
    if (dump_options and "dumpDbUsersAndRoles" in dump_options and
            version_info and version_info < VersionInfo("2.6.0")):
        dump_options.pop("dumpDbUsersAndRoles", None)

    # append shell options
    if dump_options:
        dump_cmd.extend(options_to_command_args(dump_options))


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



    log_info("Executing command: \n%s" % " ".join(cmd_display))
    call_command(dump_cmd, bubble_exit_code=True)
Пример #10
0
def do_restart_server(server, options_override=None):
    log_info("Restarting server '%s'..." % server.id)

    if server.is_online():
        do_stop_server(server)
    else:
        log_info("Server '%s' is not running." % server.id)

    do_start_server(server, options_override)
Пример #11
0
def do_restart_server(server, options_override=None):
    log_info("Restarting server '%s'..." % server.id)

    if server.is_online():
        do_stop_server(server)
    else:
        log_info("Server '%s' is not running." % server.id)

    do_start_server(server, options_override)
Пример #12
0
    def get_environment_variables(self):
        env_vars = super(MongodServer, self).get_environment_variables() or {}

        # default TCMALLOC_AGGRESSIVE_DECOMMIT as needed if not set
        if "TCMALLOC_AGGRESSIVE_DECOMMIT" not in env_vars and self.needs_tcmalloc_aggressive_decommit():
            log_info("Defaulting TCMALLOC_AGGRESSIVE_DECOMMIT=y")
            env_vars["TCMALLOC_AGGRESSIVE_DECOMMIT"] = "y"

        return env_vars
Пример #13
0
def extract_archive(archive_name):
    log_info("Extracting %s..." % archive_name)
    if not which("tar"):
        msg = ("Cannot extract archive.You need to have 'tar' command in your"
               " path in order to proceed.")
        raise MongoctlException(msg)

    tar_cmd = ['tar', 'xvf', archive_name]
    call_command(tar_cmd)
Пример #14
0
    def get_environment_variables(self):
        env_vars = super(MongodServer, self).get_environment_variables() or {}

        # default TCMALLOC_AGGRESSIVE_DECOMMIT as needed if not set
        if "TCMALLOC_AGGRESSIVE_DECOMMIT" not in env_vars and self.needs_tcmalloc_aggressive_decommit(
        ):
            log_info("Defaulting TCMALLOC_AGGRESSIVE_DECOMMIT=y")
            env_vars["TCMALLOC_AGGRESSIVE_DECOMMIT"] = "y"

        return env_vars
Пример #15
0
def do_mongo_dump(host=None,
                  port=None,
                  dbpath=None,
                  database=None,
                  username=None,
                  password=None,
                  version_info=None,
                  dump_options=None):

    # create dump command with host and port
    dump_cmd = [get_mongo_dump_executable(version_info)]

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

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

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

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

    # ignore authenticationDatabase option is version_info is less than 2.4.0
    if (dump_options and "authenticationDatabase" in dump_options
            and version_info and version_info < MongoDBVersionInfo("2.4.0")):
        dump_options.pop("authenticationDatabase", None)

    # ignore dumpDbUsersAndRoles option is version_info is less than 2.6.0
    if (dump_options and "dumpDbUsersAndRoles" in dump_options and version_info
            and version_info < MongoDBVersionInfo("2.6.0")):
        dump_options.pop("dumpDbUsersAndRoles", None)

    # append shell options
    if dump_options:
        dump_cmd.extend(options_to_command_args(dump_options))

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

    log_info("Executing command: \n%s" % " ".join(cmd_display))
    call_command(dump_cmd, bubble_exit_code=True)
Пример #16
0
    def remove_shard(self, shard, unsharded_data_dest_id=None, synchronized=False):
        log_info("Removing shard '%s' from shardset '%s' " % (shard.id, self.id))

        configured_shards = self.list_shards()
        log_info("Current configured shards: \n%s" % document_pretty_string(configured_shards))

        completed = False
        while not completed:
            result = self._do_remove_shard(shard, unsharded_data_dest_id)
            completed = synchronized and (result["state"] == "completed" or not self.is_shard_configured(shard))
            if not completed:
                time.sleep(2)
Пример #17
0
    def get_environment_variables(self):
        env_vars = super(MongodServer, self).get_environment_variables() or {}

        # default TCMALLOC_AGGRESSIVE_DECOMMIT for wiredTiger if not set
        if self.is_wired_tiger(
        ) and "TCMALLOC_AGGRESSIVE_DECOMMIT" not in env_vars:
            log_info(
                "Server is wiredTiger. Defaulting TCMALLOC_AGGRESSIVE_DECOMMIT=y"
            )
            env_vars["TCMALLOC_AGGRESSIVE_DECOMMIT"] = "y"

        return env_vars
Пример #18
0
    def verify_runtime_parameters(self):
        log_info("Verifying runtime params...")
        wtcs_gb = self.get_cmd_option("wiredTigerCacheSizeGB")
        if wtcs_gb is not None and isinstance(wtcs_gb, float):
            wtcs_bytes = int(wtcs_gb * 1024 * 1024 * 1024)
            server_status = self.server_status()
            if not(server_status and "wiredTiger" in server_status and "cache" in server_status["wiredTiger"] and
                           "maximum bytes configured" in server_status["wiredTiger"]["cache"] and
                           server_status["wiredTiger"]["cache"]["maximum bytes configured"] == wtcs_bytes):
                raise MongoctlException("CONFIG ERROR: wiredTigerCacheSizeGB was not applied")

        log_info("Runtime params verification passed!")
Пример #19
0
def do_open_mongo_shell_to(address,
                           database=None,
                           username=None,
                           password=None,
                           server_version=None,
                           shell_options=None,
                           js_files=None,
                           ssl=False):

    # default database to admin
    database = database if database else "admin"

    shell_options = shell_options or {}
    js_files = js_files or []

    # override port if specified in --port
    if "port" in shell_options:
        address = "%s:%s" % (address.split(":")[0], shell_options["port"])
        # remove port from options since passing address + port is disallowed in mongo
        del shell_options["port"]
    connect_cmd = [
        get_mongo_shell_executable(server_version),
        "%s/%s" % (address, database)
    ]

    if username:
        connect_cmd.extend(["-u", username, "-p"])
        if password:
            connect_cmd.extend([password])

    # append shell options
    if shell_options:
        connect_cmd.extend(options_to_command_args(shell_options))

    # append js files
    if js_files:
        connect_cmd.extend(js_files)

    # ssl options
    if ssl and "--ssl" not in connect_cmd:
        connect_cmd.append("--ssl")

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

    log_info("Executing command: \n%s" % " ".join(cmd_display))
    call_command(connect_cmd, bubble_exit_code=True)
Пример #20
0
def do_open_mongo_shell_to(address,
                           database=None,
                           username=None,
                           password=None,
                           server_version=None,
                           shell_options=None,
                           js_files=None,
                           ssl=False):

    # default database to admin
    database = database if database else "admin"

    shell_options = shell_options or {}
    js_files = js_files or []

    # override port if specified in --port
    if "port" in shell_options:
        address = "%s:%s" % (address.split(":")[0], shell_options["port"])
        # remove port from options since passing address + port is disallowed in mongo
        del shell_options["port"]
    connect_cmd = [get_mongo_shell_executable(server_version),
                   "%s/%s" % (address, database)]

    if username:
        connect_cmd.extend(["-u",username, "-p"])
        if password:
            connect_cmd.extend([password])

    # append shell options
    if shell_options:
        connect_cmd.extend(options_to_command_args(shell_options))

    # append js files
    if js_files:
        connect_cmd.extend(js_files)

    # ssl options
    if ssl and "--ssl" not in connect_cmd:
        connect_cmd.append("--ssl")

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

    log_info("Executing command: \n%s" % " ".join(cmd_display))
    call_command(connect_cmd, bubble_exit_code=True)
Пример #21
0
def dry_run_configure_cluster(cluster, force_primary_server_id=None):
    log_info("\n************ Dry Run ************\n")
    db_command = None
    force = force_primary_server_id is not None
    if cluster.is_replicaset_initialized():
        log_info("Replica set already initialized. "
                 "Making the replSetReconfig command...")
        db_command = cluster.get_replicaset_reconfig_db_command(force=force)
    else:
        log_info("Replica set has not yet been initialized."
                 " Making the replSetInitiate command...")
        db_command = cluster.get_replicaset_init_all_db_command()

    log_info("Executing the following command on the current primary:")
    log_info(document_pretty_string(db_command))
Пример #22
0
def dry_run_configure_cluster(cluster, force_primary_server_id=None):
    log_info("\n************ Dry Run ************\n")
    db_command = None
    force = force_primary_server_id is not None
    if cluster.is_replicaset_initialized():
        log_info("Replica set already initialized. "
                 "Making the replSetReconfig command...")
        db_command = cluster.get_replicaset_reconfig_db_command(force=force)
    else:
        log_info("Replica set has not yet been initialized."
                 " Making the replSetInitiate command...")
        db_command = cluster.get_replicaset_init_all_db_command()

    log_info("Executing the following command on the current primary:")
    log_info(document_pretty_string(db_command))
Пример #23
0
def download(url):
    log_info("Downloading %s..." % url)

    if which("curl"):
        download_cmd = ['curl', '-O']
        if not is_interactive_mode():
            download_cmd.append('-Ss')
    elif which("wget"):
        download_cmd = ['wget']
    else:
        msg = ("Cannot download file.You need to have 'curl' or 'wget"
               "' command in your path in order to proceed.")
        raise MongoctlException(msg)

    download_cmd.append(url)
    call_command(download_cmd)
Пример #24
0
    def verify_runtime_parameters(self):
        log_info("Verifying runtime params...")
        wtcs_gb = self.get_cmd_option("wiredTigerCacheSizeGB")
        if wtcs_gb is not None and isinstance(wtcs_gb, float):
            wtcs_bytes = int(wtcs_gb * 1024 * 1024 * 1024)
            server_status = self.server_status()
            if not (server_status and "wiredTiger" in server_status
                    and "cache" in server_status["wiredTiger"]
                    and "maximum bytes configured"
                    in server_status["wiredTiger"]["cache"]
                    and server_status["wiredTiger"]["cache"]
                    ["maximum bytes configured"] == wtcs_bytes):
                raise MongoctlException(
                    "CONFIG ERROR: wiredTigerCacheSizeGB was not applied")

        log_info("Runtime params verification passed!")
Пример #25
0
def list_servers_command(parsed_options):
    servers = repository.lookup_all_servers()
    if not servers or len(servers) < 1:
        log_info("No servers have been configured.")
        return

    servers = sorted(servers, key=lambda s: s.id)
    bar = "-" * 80
    print bar
    formatter = "%-25s %-40s %s"
    print formatter % ("_ID", "DESCRIPTION", "CONNECT TO")
    print bar

    for server in servers:
        print formatter % (server.id, to_string(server.get_description()), to_string(server.get_address_display()))
    print "\n"
Пример #26
0
def mongo_dump_cluster_primary(cluster,
                               database=None,
                               username=None,
                               password=None,
                               dump_options=None):
    log_info("Locating default server for cluster '%s'..." % cluster.id)
    default_server = cluster.get_default_server()
    if default_server:
        log_info("Dumping default server '%s'..." % default_server.id)
        mongo_dump_server(default_server,
                          database=database,
                          username=username,
                          password=password,
                          dump_options=dump_options)
    else:
        raise MongoctlException("No default server found for cluster '%s'" %
                                cluster.id)
Пример #27
0
def list_servers_command(parsed_options):
    servers = repository.lookup_all_servers()
    if not servers or len(servers) < 1:
        log_info("No servers have been configured.")
        return

    servers = sorted(servers, key=lambda s: s.id)
    bar = "-" * 80
    print bar
    formatter = "%-25s %-40s %s"
    print formatter % ("_ID", "DESCRIPTION", "CONNECT TO")
    print bar

    for server in servers:
        print formatter % (server.id, to_string(
            server.get_description()), to_string(server.get_address_display()))
    print "\n"
Пример #28
0
    def remove_shard(self,
                     shard,
                     unsharded_data_dest_id=None,
                     synchronized=False):
        log_info("Removing shard '%s' from shardset '%s' " %
                 (shard.id, self.id))

        configured_shards = self.list_shards()
        log_info("Current configured shards: \n%s" %
                 document_pretty_string(configured_shards))

        completed = False
        while not completed:
            result = self._do_remove_shard(shard, unsharded_data_dest_id)
            completed = synchronized and (result["state"] == "completed" or
                                          not self.is_shard_configured(shard))
            if not completed:
                time.sleep(2)
Пример #29
0
    def add_shard(self, shard):
        log_info("Adding shard '%s' to shardset '%s' " % (shard.id, self.id))

        if self.is_shard_configured(shard):
            log_info("Shard '%s' already added! Nothing to do..." % shard.id)
            return

        mongos = self.get_any_online_mongos()
        shard_member = self.get_shard_member(shard)
        cmd = self.get_add_shard_command(shard_member)

        configured_shards = self.list_shards()
        log_info("Current configured shards: \n%s" % document_pretty_string(configured_shards))

        log_info("Executing command \n%s\non mongos '%s'" % (document_pretty_string(cmd), mongos.id))
        mongos.db_command(cmd, "admin")

        log_info("Shard '%s' added successfully!" % self.id)
Пример #30
0
def mongo_restore_cluster(cluster, source,
                          database=None,
                          username=None,
                          password=None,
                          restore_options=None):
    repository.validate_cluster(cluster)
    log_info("Locating default server for cluster '%s'..." % cluster.id)
    default_server = cluster.get_default_server()
    if default_server:
        log_info("Restoring default server '%s'" % default_server.id)
        mongo_restore_server(default_server, source,
                             database=database,
                             username=username,
                             password=password,
                             restore_options=restore_options)
    else:
        raise MongoctlException("No default server found for cluster '%s'" %
                                cluster.id)
Пример #31
0
def open_mongo_shell_to_cluster(cluster,
                                database=None,
                                username=None,
                                password=None,
                                shell_options=None,
                                js_files=None):

    log_info("Locating default server for cluster '%s'..." % cluster.id)
    default_server = cluster.get_default_server()
    if default_server:
        log_info("Connecting to server '%s'" % default_server.id)
        open_mongo_shell_to_server(default_server,
                                   database=database,
                                   username=username,
                                   password=password,
                                   shell_options=shell_options,
                                   js_files=js_files)
    else:
        log_error("No default server found for cluster '%s'" % cluster.id)
Пример #32
0
def do_open_mongo_shell_to(address,
                           database=None,
                           username=None,
                           password=None,
                           server_version=None,
                           shell_options={},
                           js_files=[],
                           ssl=False):

    # default database to admin
    database = database if database else "admin"

    connect_cmd = [
        get_mongo_shell_executable(server_version),
        "%s/%s" % (address, database)
    ]

    if username:
        connect_cmd.extend(["-u", username, "-p"])
        if password:
            connect_cmd.extend([password])

    # append shell options
    if shell_options:
        connect_cmd.extend(options_to_command_args(shell_options))

    # append js files
    if js_files:
        connect_cmd.extend(js_files)

    # ssl options
    if ssl:
        connect_cmd.append("--ssl")

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

    log_info("Executing command: \n%s" % " ".join(cmd_display))
    call_command(connect_cmd, bubble_exit_code=True)
Пример #33
0
def do_open_mongo_shell_to(address,
                           database=None,
                           username=None,
                           password=None,
                           server_version=None,
                           shell_options={},
                           js_files=[],
                           ssl=False):

    # default database to admin
    database = database if database else "admin"


    connect_cmd = [get_mongo_shell_executable(server_version),
                   "%s/%s" % (address, database)]

    if username:
        connect_cmd.extend(["-u",username, "-p"])
        if password:
            connect_cmd.extend([password])

    # append shell options
    if shell_options:
        connect_cmd.extend(options_to_command_args(shell_options))

    # append js files
    if js_files:
        connect_cmd.extend(js_files)

    # ssl options
    if ssl:
        connect_cmd.append("--ssl")

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

    log_info("Executing command: \n%s" % " ".join(cmd_display))
    call_command(connect_cmd, bubble_exit_code=True)
Пример #34
0
def mongo_restore_cluster(cluster,
                          source,
                          database=None,
                          username=None,
                          password=None,
                          restore_options=None):
    repository.validate_cluster(cluster)
    log_info("Locating default server for cluster '%s'..." % cluster.id)
    default_server = cluster.get_default_server()
    if default_server:
        log_info("Restoring default server '%s'" % default_server.id)
        mongo_restore_server(default_server,
                             source,
                             database=database,
                             username=username,
                             password=password,
                             restore_options=restore_options)
    else:
        raise MongoctlException("No default server found for cluster '%s'" %
                                cluster.id)
Пример #35
0
    def add_shard(self, shard):
        log_info("Adding shard '%s' to shardset '%s' " % (shard.id, self.id))

        if self.is_shard_configured(shard):
            log_info("Shard '%s' already added! Nothing to do..." % shard.id)
            return

        mongos = self.get_any_online_mongos()
        shard_member = self.get_shard_member(shard)
        cmd = self.get_add_shard_command(shard_member)

        configured_shards = self.list_shards()
        log_info("Current configured shards: \n%s" %
                 document_pretty_string(configured_shards))

        log_info("Executing command \n%s\non mongos '%s'" %
                 (document_pretty_string(cmd), mongos.id))
        mongos.db_command(cmd, "admin")

        log_info("Shard '%s' added successfully!" % self.id)
Пример #36
0
def open_mongo_shell_to_cluster(cluster,
                                database=None,
                                username=None,
                                password=None,
                                shell_options={},
                                js_files=[]):

    log_info("Locating default server for cluster '%s'..." % cluster.id)
    default_server = cluster.get_default_server()
    if default_server:
        log_info("Connecting to server '%s'" % default_server.id)
        open_mongo_shell_to_server(default_server,
                                   database=database,
                                   username=username,
                                   password=password,
                                   shell_options=shell_options,
                                   js_files=js_files)
    else:
        log_error("No default server found for cluster '%s'" %
                  cluster.id)
Пример #37
0
def mongo_dump_cluster_best_secondary(cluster,
                                      max_repl_lag=None,
                                      database=None,
                                      username=None,
                                      password=None,
                                      dump_options=None):

    #max_repl_lag = max_repl_lag or 3600
    log_info("Finding best secondary server for cluster '%s' with replication"
             " lag less than max (%s seconds)..." %
             (cluster.id, max_repl_lag))
    best_secondary = cluster.get_dump_best_secondary(max_repl_lag=max_repl_lag)
    if best_secondary:
        server = best_secondary.get_server()

        log_info("Found secondary server '%s'. Dumping..." % server.id)
        mongo_dump_server(server, database=database, username=username,
                          password=password, dump_options=dump_options)
    else:
        raise MongoctlException("No secondary server found for cluster '%s'" %
                                cluster.id)
Пример #38
0
def list_clusters_command(parsed_options):
    clusters = repository.lookup_all_clusters()
    if not clusters or len(clusters) < 1:
        log_info("No clusters configured")
        return

    # sort clusters by id
    clusters = sorted(clusters, key=lambda c: c.id)
    bar = "-"*80
    print bar
    formatter = "%-25s %-40s %s"
    print formatter % ("_ID", "DESCRIPTION", "MEMBERS")
    print bar

    for cluster in clusters:
        desc = to_string(cluster.get_description())

        members_info = "[ %s ]" % ", ".join(cluster.get_members_info)

        print formatter % (cluster.id, desc, members_info)
    print "\n"
Пример #39
0
def list_clusters_command(parsed_options):
    clusters = repository.lookup_all_clusters()
    if not clusters or len(clusters) < 1:
        log_info("No clusters configured")
        return

    # sort clusters by id
    clusters = sorted(clusters, key=lambda c: c.id)
    bar = "-" * 80
    print bar
    formatter = "%-25s %-40s %s"
    print formatter % ("_ID", "DESCRIPTION", "MEMBERS")
    print bar

    for cluster in clusters:
        desc = to_string(cluster.get_description())

        members_info = "[ %s ]" % ", ".join(cluster.get_members_info())

        print formatter % (cluster.id, desc, members_info)
    print "\n"
Пример #40
0
def resync_secondary(server_id):

    server = repository.lookup_and_validate_server(server_id)
    server.validate_local_op("resync-secondary")

    log_info("Checking if server '%s' is secondary..." % server_id)
    # get the server status
    status = server.get_status(admin=True)
    if not status["connection"]:
        msg = "Server '%s' does not seem to be running. For more details," " run 'mongoctl status %s'" % (
            server_id,
            server_id,
        )
        raise MongoctlException(msg)
    elif "error" in status:
        msg = (
            "There was an error while connecting to server '%s' (error:%s)."
            " For more details, run 'mongoctl status %s'" % (server_id, status["error"], server_id)
        )
        raise MongoctlException(msg)

    rs_state = None
    if "selfReplicaSetStatusSummary" in status:
        rs_state = status["selfReplicaSetStatusSummary"]["stateStr"]

    if rs_state not in ["SECONDARY", "RECOVERING"]:
        msg = (
            "Server '%s' is not a secondary member or cannot be determined"
            " as secondary (stateStr='%s'. For more details, run 'mongoctl"
            " status %s'" % (server_id, rs_state, server_id)
        )
        raise MongoctlException(msg)

    do_stop_server(server)

    log_info("Deleting server's '%s' dbpath '%s'..." % (server_id, server.get_db_path()))

    shutil.rmtree(server.get_db_path())

    do_start_server(server)
Пример #41
0
    def _do_remove_shard(self, shard, unsharded_data_dest_id=None):
        cmd = self.get_validate_remove_shard_command(shard)
        mongos = self.get_any_online_mongos()

        log_info("Executing command \n%s\non mongos '%s'" %
                 (document_pretty_string(cmd), mongos.id))

        result = mongos.db_command(cmd, "admin")

        log_info("Command result: \n%s" % result)

        if "dbsToMove" in result and unsharded_data_dest_id:
            dest_shard_member = self.get_shard_member_by_shard_id(
                unsharded_data_dest_id)

            if not dest_shard_member:
                raise Exception("No such shard '%s' in shardset '%s' " %
                                (unsharded_data_dest_id, self.id))

            dest_shard = dest_shard_member.get_shard()
            self.move_dbs_primary(result["dbsToMove"], dest_shard)

        if result.get('state') == "completed":
            log_info("Shard '%s' removed successfully!" % self.id)

        return result
Пример #42
0
    def _do_remove_shard(self, shard, unsharded_data_dest_id=None):
        cmd = self.get_validate_remove_shard_command(shard)
        mongos = self.get_any_online_mongos()

        log_info("Executing command \n%s\non mongos '%s'" %
                 (document_pretty_string(cmd), mongos.id))

        result = mongos.db_command(cmd, "admin")

        log_info("Command result: \n%s" % result)

        if "dbsToMove" in result and unsharded_data_dest_id:
            dest_shard_member = self.get_shard_member_by_shard_id(
                unsharded_data_dest_id)

            if not dest_shard_member:
                raise Exception("No such shard '%s' in shardset '%s' " %
                                (unsharded_data_dest_id, self.id))

            dest_shard = dest_shard_member.get_shard()
            self.move_dbs_primary(result["dbsToMove"], dest_shard)

        if result.get('state') == "completed":
            log_info("Shard '%s' removed successfully!" % self.id)

        return result
Пример #43
0
def get_mongo_restore_executable(version_info):

    version_check_pref = VersionPreference.EXACT_OR_MINOR

    if version_info and version_info.equals_ignore_edition(make_version_info("3.0.7")):
        log_info("Requiring version 3.0.6 instead of 3.0.7 b/c of mongorestore "
                 "bug TOOLS-939")
        version_info = make_version_info("3.0.6", version_info.edition)
        version_check_pref = VersionPreference.EXACT

    restore_exe = get_mongo_executable(version_info,
                                       'mongorestore',
                                       version_check_pref=
                                       version_check_pref)

    # Warn the user if it is not an exact match (minor match)
    if version_info and version_info != restore_exe.version:
        log_warning("Using mongorestore '%s' that does not exactly match"
                    "server version '%s'" % (restore_exe.version,
                                             version_info))

    return restore_exe.path
Пример #44
0
def dry_run_add_shard(shard, sharded_cluster):
    log_info("\n************ Dry Run ************\n")

    shard_member = sharded_cluster.get_shard_member(shard)
    db_command = sharded_cluster.get_add_shard_command(shard_member)

    log_info("Executing the following command")
    log_info(document_pretty_string(db_command))
Пример #45
0
def dry_run_add_shard(shard, sharded_cluster):
    log_info("\n************ Dry Run ************\n")

    shard_member = sharded_cluster.get_shard_member(shard)
    db_command = sharded_cluster.get_add_shard_command(shard_member)

    log_info("Executing the following command")
    log_info(document_pretty_string(db_command))
Пример #46
0
def dry_run_configure_sharded_cluster(cluster):

    log_info("\n************ Dry Run ************\n")

    db_command = cluster.get_shardset_configure_command()

    log_info("Executing the following command")
    log_info(document_pretty_string(db_command))
Пример #47
0
def dry_run_configure_sharded_cluster(cluster):

    log_info("\n************ Dry Run ************\n")

    db_command = cluster.get_shardset_configure_command()

    log_info("Executing the following command")
    log_info(document_pretty_string(db_command))
Пример #48
0
def uninstall_mongodb(version):

    # validate version string
    if not is_valid_version(version):
        raise MongoctlException("Invalid version '%s'. Please provide a"
                                " valid MongoDB version." % version)

    mongo_installation = get_mongo_installation(version)

    if mongo_installation is None: # no-op
        msg = ("Cannot find a MongoDB installation for version '%s'. Please"
               " use list-versions to see all possible versions " % version)
        log_info(msg)
        return

    log_info("Found MongoDB '%s' in '%s'" % (version, mongo_installation))

    def rm_mongodb():
        log_info("Deleting '%s'" % mongo_installation)
        shutil.rmtree(mongo_installation)
        log_info("MongoDB '%s' Uninstalled successfully!" % version)

    prompt_execute_task("Proceed uninstall?" , rm_mongodb)
Пример #49
0
def get_mongo_restore_executable(version_info):

    version_check_pref = VersionPreference.EXACT_OR_MINOR

    if version_info and version_info.equals_ignore_edition(
            make_version_info("3.0.7")):
        log_info(
            "Requiring version 3.0.6 instead of 3.0.7 b/c of mongorestore "
            "bug TOOLS-939")
        version_info = make_version_info("3.0.6", version_info.edition)
        version_check_pref = VersionPreference.EXACT

    restore_exe = get_mongo_executable(version_info,
                                       'mongorestore',
                                       version_check_pref=version_check_pref)

    # Warn the user if it is not an exact match (minor match)
    if version_info and version_info != restore_exe.version:
        log_warning("Using mongorestore '%s' that does not exactly match"
                    "server version '%s'" %
                    (restore_exe.version, version_info))

    return restore_exe.path
Пример #50
0
def resync_secondary(server_id):

    server = repository.lookup_and_validate_server(server_id)
    server.validate_local_op("resync-secondary")

    log_info("Checking if server '%s' is secondary..." % server_id)
    # get the server status
    status = server.get_status(admin=True)
    if not status['connection']:
        msg = ("Server '%s' does not seem to be running. For more details,"
               " run 'mongoctl status %s'" % (server_id, server_id))
        raise MongoctlException(msg)
    elif 'error' in status:
        msg = ("There was an error while connecting to server '%s' (error:%s)."
               " For more details, run 'mongoctl status %s'" %
               (server_id, status['error'], server_id))
        raise MongoctlException(msg)

    rs_state = None
    if 'selfReplicaSetStatusSummary' in status:
        rs_state = status['selfReplicaSetStatusSummary']['stateStr']

    if rs_state not in ['SECONDARY', 'RECOVERING']:
        msg = ("Server '%s' is not a secondary member or cannot be determined"
               " as secondary (stateStr='%s'. For more details, run 'mongoctl"
               " status %s'" % (server_id, rs_state, server_id))
        raise MongoctlException(msg)

    do_stop_server(server)

    log_info("Deleting server's '%s' dbpath '%s'..." %
             (server_id, server.get_db_path()))

    shutil.rmtree(server.get_db_path())

    do_start_server(server)
Пример #51
0
def push_mongodb(repo_name,
                 mongodb_version,
                 mongodb_edition=None,
                 access_key=None,
                 secret_key=None):
    """

    :param repo_name:
    :param mongodb_version:
    :param mongodb_edition:
    :return:
    """
    mongodb_edition = mongodb_edition or MongoDBEdition.COMMUNITY
    repo = get_binary_repository(repo_name)

    if access_key and isinstance(repo, S3MongoDBBinaryRepository):
        repo.access_key = access_key
        repo.secret_key = secret_key
        repo.validate()

    version_info = make_version_info(mongodb_version, mongodb_edition)
    mongodb_install_dir = get_mongo_installation(version_info)

    if not mongodb_install_dir:
        raise MongoctlException("No mongodb installation found for '%s'" %
                                version_info)

    mongodb_install_home = os.path.dirname(mongodb_install_dir)
    target_archive_name = repo.get_archive_name(mongodb_version,
                                                mongodb_edition)

    target_archive_path = os.path.join(mongodb_install_home,
                                       target_archive_name)

    mongodb_install_dir_name = os.path.basename(mongodb_install_dir)
    log_info("Taring MongoDB at '%s'" % mongodb_install_dir_name)

    tar_exe = which("tar")
    tar_cmd = [tar_exe, "-cvzf", target_archive_name, mongodb_install_dir_name]
    call_command(tar_cmd, cwd=mongodb_install_home)

    log_info("Uploading tar to repo")

    repo.upload_file(mongodb_version, mongodb_edition, target_archive_path)

    # cleanup
    log_info("Cleanup")
    try:
        os.remove(target_archive_path)
    except Exception, e:
        log_error(str(e))
        log_exception(e)
Пример #52
0
    def move_dbs_primary(self, db_names, dest_shard):
        log_info("Moving databases %s primary to shard '%s'" %
                 (db_names, dest_shard.id))
        mongos = self.get_any_online_mongos()

        for db_name in db_names:
            move_cmd = {"movePrimary": db_name, "to": dest_shard.id}
            log_info("Executing movePrimary command:\n%s\non mongos '%s'" %
                     (document_pretty_string(move_cmd), mongos.id))

            result = mongos.db_command(move_cmd, "admin")

            log_info("Move result: %s" % document_pretty_string(result))
Пример #53
0
    def move_dbs_primary(self, db_names, dest_shard):
        log_info("Moving databases %s primary to shard '%s'" % (db_names, dest_shard.id))
        mongos = self.get_any_online_mongos()

        for db_name in db_names:
            move_cmd = {"movePrimary": db_name, "to": dest_shard.id}
            log_info(
                "Executing movePrimary command:\n%s\non mongos '%s'" % (document_pretty_string(move_cmd), mongos.id)
            )

            result = mongos.db_command(move_cmd, "admin")

            log_info("Move result: %s" % document_pretty_string(result))
Пример #54
0
def push_mongodb(repo_name, mongodb_version, mongodb_edition=None,
                 access_key=None, secret_key=None):
    """

    :param repo_name:
    :param mongodb_version:
    :param mongodb_edition:
    :return:
    """
    mongodb_edition = mongodb_edition or MongoDBEdition.COMMUNITY
    repo = get_binary_repository(repo_name)

    if access_key and isinstance(repo, S3MongoDBBinaryRepository):
        repo.access_key = access_key
        repo.secret_key = secret_key
        repo.validate()

    version_info = make_version_info(mongodb_version, mongodb_edition)
    mongodb_install_dir = get_mongo_installation(version_info)


    if not mongodb_install_dir:
        raise MongoctlException("No mongodb installation found for '%s'" %
                                version_info)

    mongodb_install_home = os.path.dirname(mongodb_install_dir)
    target_archive_name = repo.get_archive_name(mongodb_version,
                                                mongodb_edition)

    target_archive_path = os.path.join(mongodb_install_home,
                                       target_archive_name)

    mongodb_install_dir_name = os.path.basename(mongodb_install_dir)
    log_info("Taring MongoDB at '%s'" % mongodb_install_dir_name)

    tar_exe = which("tar")
    tar_cmd = [tar_exe, "-cvzf", target_archive_name, mongodb_install_dir_name]
    call_command(tar_cmd, cwd=mongodb_install_home)

    log_info("Uploading tar to repo")

    repo.upload_file(mongodb_version, mongodb_edition, target_archive_path)

    # cleanup
    log_info("Cleanup")
    try:
        os.remove(target_archive_path)
    except Exception, e:
        log_error(str(e))
        log_exception(e)