Ejemplo n.º 1
0
    def reset_password_for_restore(self,
                                   ds_version=None,
                                   data_dir='/var/lib/mysql/data'):
        """Reset the root password after restore the db data.

        We create a temporary database container by running mysqld_safe to
        reset the root password.
        """
        LOG.info('Starting to reset password for restore')

        try:
            root_pass = self.app.get_auth_password(file="root.cnf")
        except exception.UnprocessableEntity:
            root_pass = utils.generate_random_password()
            self.app.save_password('root', root_pass)

        with tempfile.NamedTemporaryFile(mode='w') as init_file, \
            tempfile.NamedTemporaryFile(suffix='.err') as err_file:
            operating_system.write_file(
                init_file.name,
                f"ALTER USER 'root'@'localhost' IDENTIFIED BY '{root_pass}';")
            command = (f'mysqld_safe --init-file={init_file.name} '
                       f'--log-error={err_file.name} '
                       f'--datadir={data_dir}')
            extra_volumes = {
                init_file.name: {
                    "bind": init_file.name,
                    "mode": "rw"
                },
                err_file.name: {
                    "bind": err_file.name,
                    "mode": "rw"
                },
            }

            # Allow database service user to access the temporary files.
            for file in [init_file.name, err_file.name]:
                operating_system.chmod(file,
                                       operating_system.FileMode.SET_ALL_RWX(),
                                       force=True,
                                       as_root=True)

            try:
                self.app.start_db(ds_version=ds_version,
                                  command=command,
                                  extra_volumes=extra_volumes)
            except Exception as err:
                LOG.error('Failed to reset password for restore, error: %s',
                          str(err))
                LOG.debug('Content in init error log file: %s',
                          err_file.read())
                raise err
            finally:
                LOG.debug(
                    'The init container log: %s',
                    docker_util.get_container_logs(self.app.docker_client))
                docker_util.remove_container(self.app.docker_client)

        LOG.info('Finished to reset password for restore')
Ejemplo n.º 2
0
    def upgrade(self, upgrade_info):
        """Upgrade the database."""
        new_version = upgrade_info.get('datastore_version')

        LOG.info('Stopping db container for upgrade')
        self.stop_db()

        LOG.info('Deleting db container for upgrade')
        docker_util.remove_container(self.docker_client)

        LOG.info('Starting new db container with version %s for upgrade',
                 new_version)
        self.start_db(update_db=True, ds_version=new_version)
Ejemplo n.º 3
0
    def upgrade(self, upgrade_info):
        """Upgrade the database."""
        new_version = upgrade_info.get('datastore_version')
        if new_version == CONF.datastore_version:
            return

        LOG.info('Stopping db container for upgrade')
        self.stop_db()

        LOG.info('Deleting db container for upgrade')
        docker_util.remove_container(self.docker_client)

        LOG.info('Remove unused images before starting new db container')
        docker_util.prune_images(self.docker_client)

        LOG.info('Starting new db container with version %s for upgrade',
                 new_version)
        self.start_db(update_db=True, ds_version=new_version)
Ejemplo n.º 4
0
    def pg_rewind(self, conn_info):
        docker_image = CONF.get(CONF.datastore_manager).docker_image
        image = f'{docker_image}:{CONF.datastore_version}'
        user = "******" % (CONF.database_service_uid, CONF.database_service_uid)
        volumes = {
            "/var/run/postgresql": {
                "bind": "/var/run/postgresql",
                "mode": "rw"
            },
            "/var/lib/postgresql": {
                "bind": "/var/lib/postgresql",
                "mode": "rw"
            },
            "/var/lib/postgresql/data": {
                "bind": "/var/lib/postgresql/data",
                "mode": "rw"
            },
        }
        command = (f"pg_rewind --target-pgdata={self.datadir} "
                   f"--source-server='{conn_info}'")

        docker_util.remove_container(self.docker_client, name='pg_rewind')

        LOG.info('Running pg_rewind in container')
        output, ret = docker_util.run_container(self.docker_client,
                                                image,
                                                'pg_rewind',
                                                volumes=volumes,
                                                command=command,
                                                user=user)
        result = output[-1]
        LOG.debug(f"Finished running pg_rewind, last output: {result}")
        if not ret:
            msg = f'Failed to run pg_rewind in container, error: {result}'
            LOG.error(msg)
            raise Exception(msg)