Esempio n. 1
0
def start_shard_cluster(cluster):
    #List of list of required servers
    list_of_member_list = [cluster.shards, cluster.config_members, cluster.mongos_members]
    #Record the list of started members
    started_members = []

    # We first start the servers
    for member_list in list_of_member_list:
        for server_descriptor in member_list:
            shard = server_descriptor.get_shard()

            try:
                if not isinstance(shard, ReplicaSetCluster):
                    if not shard.is_online():
                        # We set the server working in background compulsorily
                        shard.set_cmd_option("fork", True)
                        start_server(shard)
                else:
                    # TODO: Instructions for starting a replicaset cluster
                    pass

                started_members.append(shard)
            except MongoctlException, e:
                shutdown_severs(started_members)
                log_error(e.message)
                return False
 def is_valid(self):
     try:
         self.validate()
         return True
     except Exception, e:
         log_error("%s" % e)
         log_exception(e)
         return False
Esempio n. 3
0
 def is_local(self):
     try:
         server_host = self.get_host_address()
         return server_host is None or is_host_local(server_host)
     except Exception, e:
         log_exception(e)
         log_error("Unable to resolve address '%s' for server '%s'."
                   " Cause: %s" %
                   (self.get_host_address(), self.id, e))
    def get_validate_remove_shard_command(self, shard):
        if not self.is_shard_configured(shard):
            log_error('Bad remove shard attempt. Shard \'%s\' has not been added yet' % shard.id)

            # TODO: re-enable this when  is_last_shard works properly
            # check if its last shard and raise error if so
            ##if self.is_last_shard(shard):
            ##  raise Exception("Bad remove shard attempt. Shard '%s' is the last"
            ##                " shard" % shard.id)

        #shard_member = self.get_shard_member(shard)
        return self.get_remove_shard_command(shard)
Esempio n. 5
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)
Esempio n. 6
0
def shutdown_severs(started_members):
    """
    Close the sharding members given in the list
    @param started_members: A list of correctly opened sharding members
    @return:
    """
    for shard in started_members:
        try:
            if not isinstance(shard, ReplicaSetCluster):
                stop_server(shard)
            else:
                pass

        except MongoctlException, e:
            log_error(e.message)
Esempio n. 7
0
    def authenticate_db(self, db, dbname, retry=True):
        """
        Returns True if we manage to auth to the given db, else False.
        """
        log_verbose("Server '%s' attempting to authenticate to db '%s'" % (self.id, dbname))
        login_user = self.get_login_user(dbname)
        username = None
        password = None


        auth_success = False

        if login_user:
            username = login_user["username"]
            if "password" in login_user:
                password = login_user["password"]

        # have three attempts to authenticate
        no_tries = 0

        while not auth_success and no_tries < 3:
            if not username:
                username = read_username(dbname)
            if not password:
                password = self.lookup_password(dbname, username)
                if not password:
                    password = read_password("Enter password for user '%s\%s'"%
                                             (dbname, username))

            # if auth success then exit loop and memoize login
            try:
                auth_success = db.authenticate(username, password)
                log_verbose("Authentication attempt #%s to db '%s' result: %s" % (no_tries, dbname, auth_success))
            except OperationFailure, ofe:
                if "auth fails" in str(ofe):
                    auth_success = False

            if auth_success or not retry:
                break
            else:
                log_error("Invalid login!")
                username = None
                password = None

            no_tries += 1
Esempio n. 8
0
    def authenticate_db(self, db, dbname, retry=True):
        """
        Returns True if we manage to auth to the given db, else False.
        """
        login_user = self.get_login_user(dbname)
        username = None
        password = None


        auth_success = False

        if login_user:
            username = login_user["username"]
            if "password" in login_user:
                password = login_user["password"]

        # have three attempts to authenticate
        no_tries = 0

        while not auth_success and no_tries < 3:
            if not username:
                username = read_username(dbname)
            if not password:
                password = self.lookup_password(dbname, username)
                if not password:
                    password = read_password("Enter password for user '%s\%s'"%
                                             (dbname, username))

            # if auth success then exit loop and memoize login
            auth_success = db.authenticate(username, password)
            if auth_success or not retry:
                break
            else:
                log_error("Invalid login!")
                username = None
                password = None

            no_tries += 1

        if auth_success:
            self.set_login_user(dbname, username, password)

        return auth_success
Esempio n. 9
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)
Esempio n. 10
0
def do_install_mongodb(os_name, bits, version):

    if version is None:
        version = fetch_latest_stable_version()
        log_info("Installing latest stable MongoDB version '%s'..." % version)
    # validate version string
    elif not is_valid_version(version):
        raise MongoctlException("Invalid version '%s'. Please provide a"
                                " valid MongoDB version." % version)

    mongodb_installs_dir = config.get_mongodb_installs_dir()
    if not mongodb_installs_dir:
        raise MongoctlException("No mongoDBInstallationsDirectory configured"
                                " in mongoctl.config")

    platform_spec = get_validate_platform_spec(os_name, bits)

    log_info("Running install for %s %sbit to "
             "mongoDBInstallationsDirectory (%s)..." % (os_name, bits,
                                                        mongodb_installs_dir))


    mongo_installation = get_mongo_installation(version)

    if mongo_installation is not None: # no-op
        log_info("You already have MongoDB %s installed ('%s'). "
                 "Nothing to do." % (version, mongo_installation))
        return mongo_installation

    archive_name = "mongodb-%s-%s.tgz" % (platform_spec, version)
    url = "http://fastdl.mongodb.org/%s/%s" % (os_name, archive_name)

    # Validate if the version exists
    response = urllib.urlopen(url)

    if response.getcode() != 200:
        msg = ("Unable to download from url '%s' (response code '%s'). "
               "It could be that version '%s' you specified does not exist."
               " Please double check the version you provide" %
               (url, response.getcode(), version))
        raise MongoctlException(msg)

    mongo_dir_name = "mongodb-%s-%s" % (platform_spec, version)
    install_dir = os.path.join(mongodb_installs_dir, mongo_dir_name)

    ensure_dir(mongodb_installs_dir)

    # XXX LOOK OUT! Two processes installing same version simultaneously => BAD.
    # TODO: mutex to protect the following

    if not dir_exists(install_dir):
        try:
            ## download the url
            download(url)
            extract_archive(archive_name)

            log_info("Moving extracted folder to %s" % mongodb_installs_dir)
            shutil.move(mongo_dir_name, mongodb_installs_dir)

            os.remove(archive_name)
            log_info("Deleting archive %s" % archive_name)

            log_info("MongoDB %s installed successfully!" % version)
            return install_dir
        except Exception, e:
            log_exception(e)
            log_error("Failed to install MongoDB '%s'. Cause: %s" %
                      (version, e))