def run()

in index-tool/migrationtools/documentdb_index_tool.py [0:0]


    def run(self):
        """Entry point
        """
        metadata = None
        compatibility_issues = None
        connection = None

        # get a connection to our source mongodb or destination DocumentDb
        if self.args.dump_indexes is True or self.args.restore_indexes is True:
            try:
                connection = self._get_db_connection(
                    host=self.args.host,
                    port=self.args.port,
                    tls=self.args.tls,
                    tls_ca_file=self.args.tls_ca_file,
                    username=self.args.username,
                    password=self.args.password,
                    auth_db=self.args.auth_db)
            except (ConnectionFailure, ServerSelectionTimeoutError,
                    OperationFailure) as cex:
                logging.error("Connection to instance %s:%s failed: %s",
                              self.args.host, self.args.port, cex)
                sys.exit()

        # dump indexes from a MongoDB server
        if self.args.dump_indexes is True:
            self._dump_indexes_from_server(connection, self.args.dir,
                                           self.args.dry_run)
            sys.exit()

        # all non-dump operations require valid source metadata
        try:
            metadata = self.get_metadata(self.args.dir)
            compatibility_issues = self.find_compatibility_issues(metadata)
        except Exception as ex:
            logging.error("Failed to load collection metadata: %s", ex)
            sys.exit()

        # Apply indexes to a DocumentDB instance
        if self.args.restore_indexes is True:
            metadata_to_restore = metadata

            if self.args.skip_incompatible is not True:
                if compatibility_issues:
                    logging.error(
                        "incompatible indexes exist and --skip-incompatible not specified."
                    )
                    sys.exit()
            else:
                metadata_to_restore = self._get_compatible_metadata(
                    metadata, compatibility_issues)

            self._restore_indexes(connection, metadata_to_restore)
            sys.exit()

        # find and print a summary or detail or compatibility issues
        if self.args.show_issues is True:
            if not compatibility_issues:
                logging.info("No incompatibilities found.")
            else:
                logging.info(
                    json.dumps(compatibility_issues,
                               sort_keys=True,
                               indent=4,
                               separators=(',', ': ')))
            sys.exit()

        # print all compatible (restorable) collections and indexes
        if self.args.show_compatible is True:
            compatible_metadata = self._get_compatible_metadata(
                metadata, compatibility_issues)
            logging.info(
                json.dumps(compatible_metadata,
                           sort_keys=True,
                           indent=4,
                           separators=(',', ': ')))