Пример #1
0
    def _write_standby_recovery_file(self,
                                     service,
                                     snapshot,
                                     sslmode='prefer'):
        LOG.info("Snapshot data received:" + str(snapshot))

        logging_config = snapshot['log_position']
        conninfo_params = \
            {'host': snapshot['master']['host'],
             'port': snapshot['master']['port'],
             'repl_user': logging_config['replication_user']['name'],
             'password': logging_config['replication_user']['password'],
             'sslmode': sslmode}

        conninfo = 'host=%(host)s ' \
                   'port=%(port)s ' \
                   'dbname=os_admin ' \
                   'user=%(repl_user)s ' \
                   'password=%(password)s ' \
                   'sslmode=%(sslmode)s ' % conninfo_params

        recovery_conf = "standby_mode = 'on'\n"
        recovery_conf += "primary_conninfo = '" + conninfo + "'\n"
        recovery_conf += "trigger_file = '/tmp/postgresql.trigger'\n"
        recovery_conf += "recovery_target_timeline='latest'\n"

        operating_system.write_file(service.pgsql_recovery_config,
                                    recovery_conf,
                                    codec=stream_codecs.IdentityCodec(),
                                    as_root=True)
        operating_system.chown(service.pgsql_recovery_config,
                               user=service.pgsql_owner,
                               group=service.pgsql_owner,
                               as_root=True)
Пример #2
0
def read_file(path, codec=stream_codecs.IdentityCodec(), as_root=False):
    """
    Read a file into a Python data structure
    digestible by 'write_file'.

    :param path             Path to the read config file.
    :type path              string

    :param codec:           A codec used to deserialize the data.
    :type codec:            StreamCodec

    :returns:               A dictionary of key-value pairs.

    :param as_root:         Execute as root.
    :type as_root:          boolean

    :raises:                :class:`UnprocessableEntity` if file doesn't exist.
    :raises:                :class:`UnprocessableEntity` if codec not given.
    """
    if path and exists(path, is_directory=False, as_root=as_root):
        if as_root:
            return _read_file_as_root(path, codec)

        with open(path, 'r') as fp:
            return codec.deserialize(fp.read())

    raise exception.UnprocessableEntity(_("File does not exist: %s") % path)
Пример #3
0
def write_file(path, data, codec=stream_codecs.IdentityCodec(), as_root=False):
    """Write data into file using a given codec.
    Overwrite any existing contents.
    The written file can be read back into its original
    form by 'read_file'.

    :param path                Path to the written config file.
    :type path                 string

    :param data:               An object representing the file contents.
    :type data:                object

    :param codec:              A codec used to serialize the data.
    :type codec:               StreamCodec

    :param as_root:            Execute as root.
    :type as_root:             boolean

    :raises:                   :class:`UnprocessableEntity` if path not given.
    """
    if path:
        if as_root:
            _write_file_as_root(path, data, codec)
        else:
            with open(path, 'w', 0) as fp:
                fp.write(codec.serialize(data))
    else:
        raise exception.UnprocessableEntity(_("Invalid path: %s") % path)
Пример #4
0
    def write_recovery_file(self, restore=False):
        metadata = self.storage.load_metadata(self.location, self.checksum)
        recovery_conf = ""
        recovery_conf += "recovery_target_name = '%s' \n" % metadata['label']
        recovery_conf += "recovery_target_timeline = '%s' \n" % 1

        if restore:
            recovery_conf += ("restore_command = '" +
                              self.pgsql_restore_cmd + "'\n")

        recovery_file = os.path.join(self.app.pgsql_data_dir, 'recovery.conf')
        operating_system.write_file(recovery_file, recovery_conf,
                                    codec=stream_codecs.IdentityCodec(),
                                    as_root=True)
        operating_system.chown(recovery_file, user=self.app.pgsql_owner,
                               group=self.app.pgsql_owner, as_root=True)
Пример #5
0
    def write_recovery_file(self, restore=False):
        metadata = self.storage.load_metadata(self.location, self.checksum)
        LOG.info(_("Metadata for backup: %s") % str(metadata))
        recovery_conf = ""
        recovery_conf += "recovery_target_name = '%s' \n" % metadata['label']
        recovery_conf += "recovery_target_timeline = '%s' \n" % 1

        if restore:
            recovery_conf += "restore_command = '" + \
                             self.pgsql_restore_cmd + "'\n"

        recovery_file = os.path.join(self.PGSQL_DATA_DIR, 'recovery.conf')
        operating_system.write_file(recovery_file,
                                    recovery_conf,
                                    codec=stream_codecs.IdentityCodec(),
                                    as_root=True)
        operating_system.chown(recovery_file,
                               user=self.PGSQL_OWNER,
                               group=self.PGSQL_OWNER,
                               as_root=True)