コード例 #1
0
    def _initializeReplicaSet(
            cluster_object: V1MongoClusterConfiguration) -> None:
        """
        Initializes the replica set by sending an `initiate` command to the 1st Mongo pod.
        :param cluster_object: The cluster object from the YAML file.
        :raise ValueError: In case we receive an unexpected response from Mongo.
        :raise ApiException: In case we receive an unexpected response from Kubernetes.
        """
        cluster_name = cluster_object.metadata.name
        namespace = cluster_object.metadata.namespace

        logging.debug("Will initialize replicaset now.")
        master_connection = MongoClient(
            MongoResources.getMemberHostname(0, cluster_name, namespace),
            username='******',
            password=cluster_object.spec.users.admin_password,
            authSource='admin')
        create_replica_command, create_replica_args = MongoResources.createReplicaInitiateCommand(
            cluster_object)
        create_replica_response = master_connection.admin.command(
            create_replica_command, create_replica_args)

        if create_replica_response["ok"] == 1:
            logging.info("Initialized replica set %s @ ns/%s", cluster_name,
                         namespace)
            return

        logging.error("Initializing replica set failed, received %s",
                      repr(create_replica_response))
        raise ValueError(
            "Unexpected response initializing replica set {} @ ns/{}:\n{}".
            format(cluster_name, namespace, create_replica_response))
コード例 #2
0
    def backup(self, cluster_object: V1MongoClusterConfiguration, now: datetime):
        """
        Creates a new backup for the given cluster saving it in the cloud storage.
        :param cluster_object: The cluster object from the YAML file.
        :param now: The current date, used in the date format.
        """
        backup_file = "/tmp/" + self.BACKUP_FILE_FORMAT.format(namespace=cluster_object.metadata.namespace,
                                                               name=cluster_object.metadata.name,
                                                               date=now.strftime("%Y-%m-%d_%H%M%S"))

        pod_index = cluster_object.spec.mongodb.replicas - 1  # take last pod
        hostname = MongoResources.getMemberHostname(pod_index, cluster_object.metadata.name,
                                                    cluster_object.metadata.namespace)

        logging.info("Backing up cluster %s @ ns/%s from %s to %s.", cluster_object.metadata.name,
                     cluster_object.metadata.namespace, hostname, backup_file)

        try:
            backup_output = check_output(["mongodump", "--host", hostname, "--gzip", "--archive=" + backup_file])
        except CalledProcessError as err:
            raise SubprocessError("Could not backup '{}' to '{}'. Return code: {}\n stderr: '{}'\n stdout: '{}'"
                                  .format(hostname, backup_file, err.returncode, err.stderr, err.stdout))

        logging.debug("Backup output: %s", backup_output)

        self._uploadBackup(cluster_object, backup_file)
        os.remove(backup_file)