Example #1
0
def prepare_mongod_server(server):
    """
     Contains post start server operations
    """
    log_info("Preparing server '%s' for use as configured..." % server.id)

    cluster = server.get_cluster()
    # setup the local users if server supports that
    if server.supports_local_users():
        users.setup_server_local_users(server)

    if not server.is_cluster_member() or server.is_standalone_config_server():
        users.setup_server_users(server)

    if cluster and server.is_primary():
        users.setup_cluster_users(cluster, server)
Example #2
0
def prepare_mongod_server(server):
    """
     Contains post start server operations
    """
    log_info("Preparing server '%s' for use as configured..." %
             server.id)

    cluster = server.get_cluster()
    # setup the local users if server supports that
    if server.supports_local_users():
        users.setup_server_local_users(server)

    if not server.is_cluster_member() or server.is_config_server():
        users.setup_server_users(server)

    if cluster and server.is_primary():
        users.setup_cluster_users(cluster, server)
    def initialize_replicaset(self, suggested_primary_server=None):
        log_info("Initializing replica set cluster '%s' %s..." %
                 (self.id,
                  "" if suggested_primary_server is None else
                  "to contain only server '%s'" %
                  suggested_primary_server.id))

        ##### Determine primary server
        log_info("Determining which server should be primary...")
        primary_server = suggested_primary_server
        if primary_server is None:
            primary_member = self.suggest_primary_member()
            if primary_member is not None:
                primary_server = primary_member.get_server()

        if primary_server is None:
            raise MongoctlException("Unable to determine primary server."
                                    " At least one member server has"
                                    " to be online.")
        log_info("Selected server '%s' as primary." % primary_server.id)

        init_cmd = self.get_replicaset_init_all_db_command(
            suggested_primary_server)

        try:

            log_db_command(init_cmd)
            primary_server.timeout_maybe_db_command(init_cmd, "admin")

            # wait for replset to init
            def is_init():
                return self.is_replicaset_initialized()

            log_info("Will now wait for the replica set to initialize.")
            wait_for(is_init,timeout=60, sleep_duration=1)

            if self.is_replicaset_initialized():
                log_info("Successfully initiated replica set cluster '%s'!" %
                         self.id)
            else:
                msg = ("Timeout error: Initializing replicaset '%s' took "
                       "longer than expected. This does not necessarily"
                       " mean that it failed but it could have failed. " %
                       self.id)
                raise MongoctlException(msg)
                ## add the admin user after the set has been initiated
            ## Wait for the server to become primary though (at MongoDB's end)

            def is_primary_for_real():
                return primary_server.is_primary()

            log_info("Will now wait for the intended primary server to "
                     "become primary.")
            wait_for(is_primary_for_real,timeout=60, sleep_duration=1)

            if not is_primary_for_real():
                msg = ("Timeout error: Waiting for server '%s' to become "
                       "primary took longer than expected. "
                       "Please try again later." % primary_server.id)
                raise MongoctlException(msg)

            log_info("Server '%s' is primary now!" % primary_server.id)

            # setup cluster users
            users.setup_cluster_users(self, primary_server)

            log_info("New replica set configuration:\n%s" %
                     document_pretty_string(self.read_rs_config()))
            return True
        except Exception, e:
            log_exception(e)
            raise MongoctlException("Unable to initialize "
                                    "replica set cluster '%s'. Cause: %s" %
                                    (self.id,e) )
Example #4
0
    def initialize_replicaset(self, suggested_primary_server=None):
        log_info("Initializing replica set cluster '%s' %s..." %
                 (self.id, "" if suggested_primary_server is None else
                  "to contain only server '%s'" % suggested_primary_server.id))

        ##### Determine primary server
        log_info("Determining which server should be primary...")
        primary_server = suggested_primary_server
        if primary_server is None:
            primary_member = self.suggest_primary_member()
            if primary_member is not None:
                primary_server = primary_member.get_server()

        if primary_server is None:
            raise MongoctlException("Unable to determine primary server."
                                    " At least one member server has"
                                    " to be online.")
        log_info("Selected server '%s' as primary." % primary_server.id)

        init_cmd = self.get_replicaset_init_all_db_command(
            suggested_primary_server)

        try:

            log_db_command(init_cmd)
            primary_server.timeout_maybe_db_command(init_cmd, "admin")

            # wait for replset to init
            def is_init():
                return self.is_replicaset_initialized()

            log_info("Will now wait for the replica set to initialize.")
            wait_for(is_init, timeout=60 * 10, sleep_duration=1)

            if self.is_replicaset_initialized():
                log_info("Successfully initiated replica set cluster '%s'!" %
                         self.id)
            else:
                msg = ("Timeout error: Initializing replicaset '%s' took "
                       "longer than expected. This does not necessarily"
                       " mean that it failed but it could have failed. " %
                       self.id)
                raise MongoctlException(msg)
                ## add the admin user after the set has been initiated
            ## Wait for the server to become primary though (at MongoDB's end)

            def is_primary_for_real():
                return primary_server.is_primary()

            log_info("Will now wait for the intended primary server to "
                     "become primary.")
            wait_for(is_primary_for_real, timeout=60, sleep_duration=1)

            if not is_primary_for_real():
                msg = ("Timeout error: Waiting for server '%s' to become "
                       "primary took longer than expected. "
                       "Please try again later." % primary_server.id)
                raise MongoctlException(msg)

            log_info("Server '%s' is primary now!" % primary_server.id)

            # setup cluster users
            users.setup_cluster_users(self, primary_server)

            log_info("New replica set configuration:\n%s" %
                     document_pretty_string(self.read_rs_config()))
            return True
        except Exception, e:
            log_exception(e)
            raise MongoctlException("Unable to initialize "
                                    "replica set cluster '%s'. Cause: %s" %
                                    (self.id, e))