コード例 #1
0
ファイル: repository.py プロジェクト: zenglzh/mongoctl
def build_cluster_from_uri(uri):
    uri_wrapper = parse_mongo_uri(uri)
    database = uri_wrapper.database or "admin"
    username = uri_wrapper.username
    password = uri_wrapper.password

    nodes = uri_wrapper.node_list
    cluster_doc = {"_id": mask_mongo_uri(uri)}
    member_doc_list = []

    for node in nodes:
        host = node[0]
        port = node[1]
        member_doc = {"host": "%s:%s" % (host, port)}
        member_doc_list.append(member_doc)

    cluster_doc["members"] = member_doc_list

    cluster = new_cluster(cluster_doc)

    # set login user if specified
    if username:
        for member in cluster.get_members():
            member.get_server().set_login_user(database, username, password)

    return cluster
コード例 #2
0
ファイル: repository.py プロジェクト: Mat-Loz/mongoctl
def build_cluster_from_uri(uri):
    uri_wrapper = parse_mongo_uri(uri)
    database = uri_wrapper.database or "admin"
    username = uri_wrapper.username
    password = uri_wrapper.password

    nodes = uri_wrapper.node_list
    cluster_doc = {
        "_id": mask_mongo_uri(uri)
    }
    member_doc_list = []

    for node in nodes:
        host = node[0]
        port = node[1]
        member_doc = {
            "host": "%s:%s" % (host, port)
        }
        member_doc_list.append(member_doc)

    cluster_doc["members"] = member_doc_list

    cluster = new_cluster(cluster_doc)

    # set login user if specified
    if username:
        for member in cluster.get_members():
            member.get_server().set_login_user(database, username, password)

    return cluster
コード例 #3
0
    def dump_backup(self, backup, uri, destination, log_file_name, options=None):
        mongoctl_exe = which("mongoctl")
        if not mongoctl_exe:
            raise MBSError("mongoctl exe not found in PATH")

        dump_cmd = [mongoctl_exe, "--noninteractive", "dump", uri, "-o", destination]

        if options:
            dump_cmd.extend(options)

        dump_cmd_display = dump_cmd[:]
        # mask mongo uri
        dump_cmd_display[3] = mask_mongo_uri(uri)

        logger.info("Running dump command: %s" % " ".join(dump_cmd_display))

        workspace = self.get_task_workspace_dir(backup)
        log_path = os.path.join(workspace, destination, log_file_name)
        last_error_line = {"line": ""}

        def capture_last_error(line):
            if is_mongo_error_log_line(line):
                last_error_line["line"] = line
        # execute dump command
        return_code = execute_command_wrapper(dump_cmd, cwd=workspace, output_path=log_path,
                                             on_output=capture_last_error)

        # raise an error if return code is not 0
        if return_code:
            errors.raise_dump_error(return_code, last_error_line["line"])
コード例 #4
0
    def run_mongo_restore(self,
                          restore,
                          destination_uri,
                          dump_dir,
                          source_database_name,
                          log_file_name,
                          dump_log_file_name,
                          exclude_system_users=None,
                          exclude_admin_system_users=None,
                          exclude_system_roles=None,
                          options=None):

        if source_database_name:
            source_dir = os.path.join(dump_dir, source_database_name)
        else:
            source_dir = dump_dir

        workspace = self.get_task_workspace_dir(restore)
        # IMPORTANT delete dump log file so the restore command would not break
        dump_log_path = os.path.join(workspace, dump_dir, dump_log_file_name)
        if os.path.exists(dump_log_path):
            os.remove(dump_log_path)

        if exclude_system_users:
            self._delete_system_users_from_dump(restore, source_dir)

        if exclude_admin_system_users:
            self._delete_admin_system_users_from_dump(restore, source_dir)

        if exclude_system_roles:
            self._delete_roles_from_dump(restore, source_dir)

        working_dir = workspace
        log_path = os.path.join(workspace, log_file_name)

        restore_cmd = [
            which("mongoctl"), "restore", destination_uri, source_dir
        ]

        if options:
            restore_cmd.extend(options)

        restore_cmd_display = restore_cmd[:]

        restore_cmd_display[restore_cmd_display.index("restore") +
                            1] = mask_mongo_uri(destination_uri)

        logger.info("Running mongoctl restore command: %s" %
                    " ".join(restore_cmd_display))

        returncode = execute_command_wrapper(restore_cmd,
                                             output_path=log_path,
                                             cwd=working_dir)

        # read the last dump log line
        last_line_tail_cmd = [which('tail'), '-1', log_path]
        last_log_line = execute_command(last_line_tail_cmd)

        if returncode:
            raise RestoreError(returncode, last_log_line)
コード例 #5
0
    def run_mongo_restore(self, restore, destination_uri, dump_dir, source_database_name,
                          log_file_name, dump_log_file_name,
                          exclude_system_users=None,
                          exclude_admin_system_users=None,
                          exclude_system_roles=None,
                          options=None):

        if source_database_name:
            source_dir = os.path.join(dump_dir, source_database_name)
        else:
            source_dir = dump_dir

        workspace = self.get_task_workspace_dir(restore)
        # IMPORTANT delete dump log file so the restore command would not break
        dump_log_path = os.path.join(workspace, dump_dir, dump_log_file_name)
        if os.path.exists(dump_log_path):
            os.remove(dump_log_path)

        if exclude_system_users:
            self._delete_system_users_from_dump(restore, source_dir)

        if exclude_admin_system_users:
            self._delete_admin_system_users_from_dump(restore, source_dir)

        if exclude_system_roles:
            self._delete_roles_from_dump(restore, source_dir)

        working_dir = workspace
        log_path = os.path.join(workspace, log_file_name)

        restore_cmd = [
            which("mongoctl"),
            "restore",
            destination_uri,
            source_dir
        ]

        if options:
            restore_cmd.extend(options)

        restore_cmd_display = restore_cmd[:]

        restore_cmd_display[restore_cmd_display.index("restore") + 1] = mask_mongo_uri(destination_uri)

        logger.info("Running mongoctl restore command: %s" %
                    " ".join(restore_cmd_display))

        returncode = execute_command_wrapper(restore_cmd,
                                             output_path=log_path,
                                             cwd=working_dir)

        # read the last dump log line
        last_line_tail_cmd = [which('tail'), '-1', log_path]
        last_log_line = execute_command(last_line_tail_cmd)

        if returncode:
            raise RestoreError(returncode, last_log_line)
コード例 #6
0
    def to_document(self, display_only=False):
        doc =  super(MongoSource, self).to_document()
        doc.update ({
            "_type": "MongoSource",
            "uri": (mongo_uri_tools.mask_mongo_uri(self.uri) if display_only
                    else self.uri)
        })

        return doc
コード例 #7
0
    def __init__(self, uri=None, msg=None, rs_status=None, rs_conf=None):
        if uri:
            details = ("No eligible members in '%s' found to take backup from" %
                       mongo_uri_tools.mask_mongo_uri(uri))
        else:
            details = None

        self._rs_status = rs_status
        self._rs_conf = rs_conf

        super(NoEligibleMembersFound, self).__init__(details=details, msg=msg)
コード例 #8
0
    def __init__(self, uri=None, msg=None, rs_status=None, rs_conf=None):
        if uri:
            details = ("No eligible members in '%s' found to take backup from" %
                       mongo_uri_tools.mask_mongo_uri(uri))
        else:
            details = None

        self._rs_status = rs_status
        self._rs_conf = rs_conf

        super(NoEligibleMembersFound, self).__init__(details=details, msg=msg)
コード例 #9
0
    def dump_backup(self,
                    backup,
                    uri,
                    destination,
                    log_file_name,
                    options=None):
        mongoctl_exe = which("mongoctl")
        if not mongoctl_exe:
            raise MBSError("mongoctl exe not found in PATH")

        dump_cmd = [
            mongoctl_exe, "--noninteractive", "dump", uri, "-o", destination
        ]

        if options:
            dump_cmd.extend(options)

        dump_cmd_display = dump_cmd[:]
        # mask mongo uri
        dump_cmd_display[3] = mask_mongo_uri(uri)

        logger.info("Running dump command: %s" % " ".join(dump_cmd_display))

        workspace = self.get_task_workspace_dir(backup)
        log_path = os.path.join(workspace, destination, log_file_name)
        last_error_line = {"line": ""}

        def capture_last_error(line):
            if is_mongo_error_log_line(line):
                last_error_line["line"] = line

        # execute dump command
        return_code = execute_command_wrapper(dump_cmd,
                                              cwd=workspace,
                                              output_path=log_path,
                                              on_output=capture_last_error)

        # raise an error if return code is not 0
        if return_code:
            errors.raise_dump_error(return_code, last_error_line["line"])
コード例 #10
0
 def __init__(self, uri=None):
     if uri:
         details = "Unable to determine primary for cluster '%s'" % mongo_uri_tools.mask_mongo_uri(uri)
     else:
         details = None
     super(PrimaryNotFoundError, self).__init__(details=details)
コード例 #11
0
 def __init__(self, uri=None, cause=None):
     if uri:
         msg = "Failed to authenticate to '%s'" % mongo_uri_tools.mask_mongo_uri(uri)
     else:
         msg = ""
     super(AuthenticationFailedError, self).__init__(msg=msg, cause=cause)
コード例 #12
0
 def __init__(self, uri=None):
     if uri:
         details = "Unable to determine primary for cluster '%s'" % mongo_uri_tools.mask_mongo_uri(uri)
     else:
         details = None
     super(PrimaryNotFoundError, self).__init__(details=details)
コード例 #13
0
 def __init__(self, uri=None, details=None, cause=None):
     if uri:
         msg = "Could not establish a database connection to '%s'" % mongo_uri_tools.mask_mongo_uri(uri)
     else:
         msg = ""
     super(ConnectionError, self).__init__(msg=msg, details=details, cause=cause)
コード例 #14
0
 def __init__(self, uri):
     details = ("No eligible members in '%s' found to take dump from" %
                mongo_uri_tools.mask_mongo_uri(uri))
     super(NoEligibleMembersFound, self).__init__(details=details)
コード例 #15
0
 def __init__(self, uri=None, details=None, cause=None):
     if uri:
         msg = "Could not establish a database connection to '%s'" % mongo_uri_tools.mask_mongo_uri(uri)
     else:
         msg = ""
     super(ConnectionError, self).__init__(msg=msg, details=details, cause=cause)
コード例 #16
0
 def __init__(self, uri):
     details = ("No eligible members in '%s' found to take dump from" %
                mongo_uri_tools.mask_mongo_uri(uri))
     super(NoEligibleMembersFound, self).__init__(details=details)
コード例 #17
0
 def __init__(self, uri=None, cause=None):
     if uri:
         msg = "Failed to authenticate to '%s'" % mongo_uri_tools.mask_mongo_uri(uri)
     else:
         msg = ""
     super(AuthenticationFailedError, self).__init__(msg=msg, cause=cause)