Example #1
0
    def test_exists(self):
        self.assertFalse(operating_system.exists(tempfile.gettempdir(), is_directory=False))
        self.assertTrue(operating_system.exists(tempfile.gettempdir(), is_directory=True))

        with tempfile.NamedTemporaryFile() as test_file:
            self.assertTrue(operating_system.exists(test_file.name, is_directory=False))
            self.assertFalse(operating_system.exists(test_file.name, is_directory=True))

        self._assert_execute_call(
            [["test -f path && echo 1 || echo 0"]],
            [{"shell": True, "check_exit_code": False, "run_as_root": True, "root_helper": "sudo"}],
            operating_system.exists,
            None,
            "path",
            is_directory=False,
            as_root=True,
        )
        self._assert_execute_call(
            [["test -d path && echo 1 || echo 0"]],
            [{"shell": True, "check_exit_code": False, "run_as_root": True, "root_helper": "sudo"}],
            operating_system.exists,
            None,
            "path",
            is_directory=True,
            as_root=True,
        )
Example #2
0
    def build_log_file_name(self, log_name, owner, datastore_dir=None):
        """Build a log file name based on the log_name and make sure the
        directories exist and are accessible by owner.
        """
        if datastore_dir is None:
            base_dir = self.GUEST_LOG_BASE_DIR
            if not operating_system.exists(base_dir, is_directory=True):
                operating_system.ensure_directory(base_dir,
                                                  user=owner,
                                                  group=owner,
                                                  force=True,
                                                  as_root=True)
            datastore_dir = guestagent_utils.build_file_path(
                base_dir, self.GUEST_LOG_DATASTORE_DIRNAME)

        if not operating_system.exists(datastore_dir, is_directory=True):
            operating_system.ensure_directory(datastore_dir,
                                              user=owner,
                                              group=owner,
                                              force=True,
                                              as_root=True)
        log_file_name = guestagent_utils.build_file_path(
            datastore_dir, '%s-%s.log' % (self.manager, log_name))

        return self.validate_log_file(log_file_name, owner)
Example #3
0
    def init_config(self):
        if not operating_system.exists(MOUNT_POINT, True):
            operating_system.create_directory(MOUNT_POINT,
                                              system.DB2_INSTANCE_OWNER,
                                              system.DB2_INSTANCE_OWNER,
                                              as_root=True)
        """
        The database manager configuration file - db2systm is stored  under the
        /home/db2inst1/sqllib directory. To update the configuration
        parameters, DB2 recommends using the command - UPDATE DBM CONFIGURATION
        commands instead of directly updating the config file.

        The existing PropertiesCodec implementation has been reused to handle
        text-file operations. Configuration overrides are implemented using
        the ImportOverrideStrategy of the guestagent configuration manager.
        """
        LOG.debug("Initialize DB2 configuration")
        revision_dir = (
            guestagent_utils.build_file_path(
                os.path.join(MOUNT_POINT,
                             os.path.dirname(system.DB2_INSTANCE_OWNER)),
                ConfigurationManager.DEFAULT_STRATEGY_OVERRIDES_SUB_DIR)
        )
        if not operating_system.exists(FAKE_CFG):
            operating_system.write_file(FAKE_CFG, '', as_root=True)
            operating_system.chown(FAKE_CFG, system.DB2_INSTANCE_OWNER,
                                   system.DB2_INSTANCE_OWNER, as_root=True)
        self.configuration_manager = (
            ConfigurationManager(FAKE_CFG, system.DB2_INSTANCE_OWNER,
                                 system.DB2_INSTANCE_OWNER,
                                 PropertiesCodec(delimiter='='),
                                 requires_root=True,
                                 override_strategy=ImportOverrideStrategy(
                                     revision_dir, "cnf"))
        )
        '''
        Below we are getting the database manager default configuration and
        saving it to the DB2_DEFAULT_CFG file. This is done to help with
        correctly resetting the configurations to the original values when
        user wants to detach a user-defined configuration group from an
        instance. DB2 provides a command to reset the database manager
        configuration parameters (RESET DBM CONFIGURATION) but this command
        resets all the configuration parameters to the system defaults. When
        we build a DB2 guest image there are certain configurations
        parameters like SVCENAME which we set so that the instance can start
        correctly. Hence resetting this value to the system default will
        render the instance in an unstable state. Instead, the recommended
        way for resetting a subset of configuration parameters is to save
        the output of GET DBM CONFIGURATION of the original configuration
        and then call UPDATE DBM CONFIGURATION to reset the value.
          http://www.ibm.com/support/knowledgecenter/SSEPGG_10.5.0/
        com.ibm.db2.luw.admin.cmd.doc/doc/r0001970.html
        '''
        if not operating_system.exists(DB2_DEFAULT_CFG):
            run_command(system.GET_DBM_CONFIGURATION % {
                "dbm_config": DB2_DEFAULT_CFG})
        self.process_default_dbm_config()
Example #4
0
 def update_spfile(self):
     """Checks if there is a new SPFILE and replaces the old.
     The database must be shutdown before running this.
     """
     if operating_system.exists(self.new_spfile, as_root=True):
         LOG.debug('Found a new SPFILE.')
         operating_system.move(self.new_spfile, self.spfile, force=True)
Example #5
0
 def _read_log_position(self):
     backup_var_file = ('%s/backup_variables.txt' %
                        MySqlApp.get_data_dir())
     if operating_system.exists(backup_var_file):
         try:
             LOG.info(_("Reading log position from %s") % backup_var_file)
             backup_vars = operating_system.read_file(
                 backup_var_file,
                 stream_codecs.PropertiesCodec(delimiter='='),
                 as_root=True)
             binlog_position = backup_vars['binlog_position']
             binlog_file, binlog_pos = binlog_position.split(':')
             return {
                 'log_file': binlog_file,
                 'log_position': int(binlog_pos)
             }
         except Exception as ex:
             LOG.exception(ex)
             raise self.UnableToDetermineBinlogPosition(
                 {'binlog_file': backup_var_file})
     else:
         LOG.info(_("Log position detail not available. "
                    "Using default values."))
         return {'log_file': '',
                 'log_position': 4}
Example #6
0
 def has_revisions(self):
     """Return True if there currently are any revision files.
     """
     return (operating_system.exists(
         self._revision_dir, is_directory=True,
         as_root=self._requires_root) and
         (len(self._collect_revision_files()) > 0))
Example #7
0
    def __init__(self, status):
        self.state_change_wait_time = CONF.state_change_wait_time
        self.status = status
        revision_dir = \
            guestagent_utils.build_file_path(
                os.path.join(MOUNT_POINT,
                             os.path.dirname(system.VERTICA_ADMIN)),
                ConfigurationManager.DEFAULT_STRATEGY_OVERRIDES_SUB_DIR)

        if not operating_system.exists(FAKE_CFG):
            operating_system.write_file(FAKE_CFG, '', as_root=True)
            operating_system.chown(FAKE_CFG,
                                   system.VERTICA_ADMIN,
                                   system.VERTICA_ADMIN_GRP,
                                   as_root=True)
            operating_system.chmod(FAKE_CFG,
                                   FileMode.ADD_GRP_RX_OTH_RX(),
                                   as_root=True)
        self.configuration_manager = \
            ConfigurationManager(FAKE_CFG, system.VERTICA_ADMIN,
                                 system.VERTICA_ADMIN_GRP,
                                 PropertiesCodec(delimiter='='),
                                 requires_root=True,
                                 override_strategy=ImportOverrideStrategy(
                                     revision_dir, "cnf"))
Example #8
0
    def mount_storage(self, storage_info):
        fstab = path.join('/etc', 'fstab')
        default_mount_options = ('rw,bg,hard,nointr,tcp,vers=3,timeo=600,'
                                 'rsize=32768,wsize=32768,actimeo=0')
        data_mount_options = ('user,tcp,rsize=32768,wsize=32768,hard,intr,'
                              'noac,nfsvers=3')
        if storage_info['type'] == 'nfs':
            sources = storage_info['data']
            data = list()
            if operating_system.exists(fstab):
                data.append(operating_system.read_file(fstab, as_root=True))

            def _line(source, target, options=default_mount_options):
                data.append('{source} {target} nfs {options} 0 0'.format(
                    source=source, target=target, options=options))

            _line(sources['votedisk_mount'], SHARED_DISK_PATHS['votedisk'],)
            _line(sources['registry_mount'], SHARED_DISK_PATHS['registry'],)
            _line(sources['database_mount'], SHARED_DISK_PATHS['database'],
                  data_mount_options)
            operating_system.write_file(fstab, '\n'.join(data),
                                        as_root=True)
            utils.execute_with_timeout('mount', '-a',
                                       run_as_root=True,
                                       root_helper='sudo',
                                       timeout=service.ORACLE_TIMEOUT,
                                       log_output_on_error=True)
        else:
            raise exception.GuestError(_(
                "Storage type {t} not valid.").format(t=storage_info['type']))
 def connect_to_master(self, service, snapshot):
     """All that is required in postgresql to connect to a slave is to
     restart with a recovery.conf file in the data dir, which contains
     the connection information for the master.
     """
     assert operating_system.exists(service.pgsql_recovery_config, as_root=True)
     service.restart()
Example #10
0
 def connect_to_master(self, service, snapshot):
     # All that is required in postgresql to connect to a slave is to
     # restart with a recovery.conf file in the data dir, which contains
     # the connection information for the master.
     assert operating_system.exists(self.pgsql_recovery_config,
                                    as_root=True)
     self.restart(context=None)
    def _get_or_create_replication_user(self, service):
        """There are three scenarios we need to deal with here:
        - This is a fresh master, with no replicator user created.
           Generate a new u/p
        - We are attaching a new slave and need to give it the login creds
           Send the creds we have stored in PGDATA/.replpass
        - This is a failed-over-to slave, who will have the replicator user
           but not the credentials file. Recreate the repl user in this case
        """

        LOG.debug("Checking for replicator user")
        pwfile = os.path.join(service.pgsql_data_dir, ".replpass")
        admin = service.build_admin()
        if admin.user_exists(REPL_USER):
            if operating_system.exists(pwfile, as_root=True):
                LOG.debug("Found existing .replpass, returning pw")
                pw = operating_system.read_file(pwfile, as_root=True)
            else:
                LOG.debug("Found user but not .replpass, recreate")
                u = models.PostgreSQLUser(REPL_USER)
                admin._drop_user(context=None, user=u)
                pw = self._create_replication_user(service, admin, pwfile)
        else:
            LOG.debug("Found no replicator user, create one")
            pw = self._create_replication_user(service, admin, pwfile)

        repl_user_info = {"name": REPL_USER, "password": pw}

        return repl_user_info
Example #12
0
 def build_module_dir(cls, module_type, module_id):
     sub_dir = os.path.join(module_type, module_id)
     module_dir = guestagent_utils.build_file_path(
         cls.MODULE_BASE_DIR, sub_dir)
     if not operating_system.exists(module_dir, is_directory=True):
         operating_system.create_directory(module_dir, force=True)
     return module_dir
 def has_revisions(self):
     """Return True if there currently are any revision files.
     """
     return (operating_system.exists(
         self._revision_dir, is_directory=True,
         as_root=self._requires_root) and
         (len(self._collect_revision_files()) > 0))
Example #14
0
    def _update_details(self):
        if operating_system.exists(self._file, as_root=True):
            file_path = Path(self._file)

            # Make sure guest agent can read the log file.
            if not os.access(self._file, os.R_OK):
                operating_system.chmod(self._file,
                                       FileMode.ADD_ALL_R,
                                       as_root=True)
                operating_system.chmod(str(file_path.parent),
                                       FileMode.ADD_GRP_RX_OTH_RX,
                                       as_root=True)

            self._size = file_path.stat().st_size
            self._update_log_header_digest(self._file)

            if self.status != LogStatus.Disabled:
                if self._log_rotated():
                    self.status = LogStatus.Rotated
                # See if we have stuff to publish
                elif self._size > self._published_size:
                    self._set_status(self._published_size, LogStatus.Partial,
                                     LogStatus.Ready)
                # We've published everything so far
                elif self._size == self._published_size:
                    self._set_status(self._published_size, LogStatus.Published,
                                     LogStatus.Enabled)
                # We've already handled this case (log rotated) so what gives?
                else:
                    raise Exception(_("Bug in _log_rotated ?"))
        else:
            LOG.warning(f"File {self._file} does not exist")
            self._published_size = 0
            self._size = 0
Example #15
0
    def _get_or_create_replication_user(self, service):
        """There are three scenarios we need to deal with here:
        - This is a fresh master, with no replicator user created.
           Generate a new u/p
        - We are attaching a new slave and need to give it the login creds
           Send the creds we have stored in PGDATA/.replpass
        - This is a failed-over-to slave, who will have the replicator user
           but not the credentials file. Recreate the repl user in this case
        """

        LOG.debug("Checking for replicator user")
        pwfile = os.path.join(service.pgsql_data_dir, ".replpass")
        admin = service.build_admin()
        if admin.user_exists(REPL_USER):
            if operating_system.exists(pwfile, as_root=True):
                LOG.debug("Found existing .replpass, returning pw")
                pw = operating_system.read_file(pwfile, as_root=True)
            else:
                LOG.debug("Found user but not .replpass, recreate")
                u = models.PostgreSQLUser(REPL_USER)
                admin._drop_user(context=None, user=u)
                pw = self._create_replication_user(service, admin, pwfile)
        else:
            LOG.debug("Found no replicator user, create one")
            pw = self._create_replication_user(service, admin, pwfile)

        repl_user_info = {'name': REPL_USER, 'password': pw}

        return repl_user_info
Example #16
0
    def pre_upgrade(self, context):
        app = self.mysql_app(self.mysql_app_status.get())
        data_dir = app.get_data_dir()
        mount_point, _data = os.path.split(data_dir)
        save_dir = "%s/etc_mysql" % mount_point
        save_etc_dir = "%s/etc" % mount_point
        home_save = "%s/trove_user" % mount_point

        app.status.begin_restart()
        app.stop_db()

        if operating_system.exists("/etc/my.cnf", as_root=True):
            operating_system.create_directory(save_etc_dir, as_root=True)
            operating_system.copy("/etc/my.cnf",
                                  save_etc_dir,
                                  preserve=True,
                                  as_root=True)

        operating_system.copy("/etc/mysql/.",
                              save_dir,
                              preserve=True,
                              as_root=True)

        operating_system.copy("%s/." % os.path.expanduser('~'),
                              home_save,
                              preserve=True,
                              as_root=True)

        self.unmount_volume(context, mount_point=data_dir)
        return {
            'mount_point': mount_point,
            'save_dir': save_dir,
            'save_etc_dir': save_etc_dir,
            'home_save': home_save
        }
Example #17
0
    def post_upgrade(self, context, upgrade_info):
        app = self.mysql_app(self.mysql_app_status.get())
        app.stop_db()
        if 'device' in upgrade_info:
            self.mount_volume(context,
                              mount_point=upgrade_info['mount_point'],
                              device_path=upgrade_info['device'])

        if operating_system.exists(upgrade_info['save_etc_dir'],
                                   is_directory=True,
                                   as_root=True):
            operating_system.copy("%s/." % upgrade_info['save_etc_dir'],
                                  "/etc",
                                  preserve=True,
                                  as_root=True)

        operating_system.copy("%s/." % upgrade_info['save_dir'],
                              "/etc/mysql",
                              preserve=True,
                              as_root=True)
        operating_system.copy("%s/." % upgrade_info['home_save'],
                              os.path.expanduser('~'),
                              preserve=True,
                              as_root=True)
        app.start_mysql()
Example #18
0
 def build_module_dir(cls, module_type, module_id):
     sub_dir = os.path.join(module_type, module_id)
     module_dir = guestagent_utils.build_file_path(cls.MODULE_BASE_DIR,
                                                   sub_dir)
     if not operating_system.exists(module_dir, is_directory=True):
         operating_system.create_directory(module_dir, force=True)
     return module_dir
Example #19
0
    def pre_upgrade(self, context):
        app = self.mysql_app(self.mysql_app_status.get())
        data_dir = app.get_data_dir()
        mount_point, _data = os.path.split(data_dir)
        save_dir = "%s/etc_mysql" % mount_point
        save_etc_dir = "%s/etc" % mount_point
        home_save = "%s/trove_user" % mount_point

        app.status.begin_restart()
        app.stop_db()

        if operating_system.exists("/etc/my.cnf", as_root=True):
            operating_system.create_directory(save_etc_dir, as_root=True)
            operating_system.copy("/etc/my.cnf", save_etc_dir,
                                  preserve=True, as_root=True)

        operating_system.copy("/etc/mysql/.", save_dir,
                              preserve=True, as_root=True)

        operating_system.copy("%s/." % os.path.expanduser('~'), home_save,
                              preserve=True, as_root=True)

        self.unmount_volume(context, mount_point=data_dir)
        return {
            'mount_point': mount_point,
            'save_dir': save_dir,
            'save_etc_dir': save_etc_dir,
            'home_save': home_save
        }
Example #20
0
    def post_upgrade(self, context, upgrade_info):
        app = self.mysql_app(self.mysql_app_status.get())
        app.stop_db()
        if 'device' in upgrade_info:
            self.mount_volume(context,
                              mount_point=upgrade_info['mount_point'],
                              device_path=upgrade_info['device'],
                              write_to_fstab=True)
            operating_system.chown(path=upgrade_info['mount_point'],
                                   user=service.MYSQL_OWNER,
                                   group=service.MYSQL_OWNER,
                                   recursive=True,
                                   as_root=True)

        self._restore_home_directory(upgrade_info['home_save'])

        if operating_system.exists(upgrade_info['save_etc_dir'],
                                   is_directory=True,
                                   as_root=True):
            self._restore_directory(upgrade_info['save_etc_dir'], "/etc")

        self._restore_directory("%s/." % upgrade_info['save_dir'],
                                "/etc/mysql")

        self.configuration_manager.refresh_cache()
        app.start_mysql()
        app.status.end_restart()
Example #21
0
 def connect_to_master(self, service, snapshot):
     """All that is required in postgresql to connect to a slave is to
     restart with a recovery.conf file in the data dir, which contains
     the connection information for the master.
     """
     assert operating_system.exists(self.PGSQL_RECOVERY_CONFIG,
                                    as_root=True)
     self.restart(context=None)
Example #22
0
 def __init__(self, status, state_change_wait_time=None):
     super(OracleVMApp, self).__init__(
         status, OracleVMClient, OracleVMCursor, OracleVMAdmin,
         state_change_wait_time)
     self.paths = OracleVMPaths(self.admin.database_name)
     self.configuration_manager = None
     if operating_system.exists(self.paths.os_pfile):
         self._init_configuration_manager()
Example #23
0
 def __init__(self, status, state_change_wait_time=None):
     super(OracleVMApp,
           self).__init__(status, OracleVMClient, OracleVMCursor,
                          OracleVMAdmin, state_change_wait_time)
     self.paths = OracleVMPaths(self.admin.database_name)
     self.configuration_manager = None
     if operating_system.exists(self.paths.os_pfile):
         self._init_configuration_manager()
Example #24
0
    def clean_wal_archives(self, context):
        """Clean up the wal archives to free up disk space."""
        archive_path = service.WAL_ARCHIVE_DIR
        data_path = cfg.get_configuration_property('mount_point')

        if not operating_system.exists(archive_path, is_directory=True,
                                       as_root=True):
            return

        self._clean_wals(archive_path, data_path)
Example #25
0
 def update_spfile(self):
     """Checks if there is a new SPFILE and replaces the old.
     The database must be shutdown before running this.
     """
     if operating_system.exists(self.new_spfile, as_root=True):
         LOG.debug('Found a new SPFILE.')
         operating_system.move(
             self.new_spfile,
             self.spfile,
             force=True
         )
Example #26
0
def get_conf_dir():
    """Get the config directory for the database related settings.

    For now, the files inside the config dir are mainly for instance rebuild.
    """
    mount_point = CONF.get(CONF.datastore_manager).mount_point
    conf_dir = os.path.join(mount_point, 'conf.d')
    if not operating_system.exists(conf_dir, is_directory=True, as_root=True):
        operating_system.ensure_directory(conf_dir, as_root=True)

    return conf_dir
Example #27
0
    def build_log_file_name(self, log_name, owner, datastore_dir=None):
        """Build a log file name based on the log_name and make sure the
        directories exist and are accessible by owner.
        """
        if datastore_dir is None:
            base_dir = self.GUEST_LOG_BASE_DIR
            if not operating_system.exists(base_dir, is_directory=True):
                operating_system.create_directory(
                    base_dir, user=owner, group=owner, force=True,
                    as_root=True)
            datastore_dir = guestagent_utils.build_file_path(
                base_dir, self.GUEST_LOG_DATASTORE_DIRNAME)

        if not operating_system.exists(datastore_dir, is_directory=True):
            operating_system.create_directory(
                datastore_dir, user=owner, group=owner, force=True,
                as_root=True)
        log_file_name = guestagent_utils.build_file_path(
            datastore_dir, '%s-%s.log' % (self.manager, log_name))

        return self.validate_log_file(log_file_name, owner)
Example #28
0
    def validate_log_file(self, log_file, owner):
        """Make sure the log file exists and is accessible by owner.
        """
        if not operating_system.exists(log_file, as_root=True):
            operating_system.write_file(log_file, '', as_root=True)

        operating_system.chown(log_file, user=owner, group=owner,
                               as_root=True)
        operating_system.chmod(log_file, FileMode.ADD_USR_RW_GRP_RW_OTH_R,
                               as_root=True)
        LOG.debug("Set log file '%s' as readable" % log_file)
        return log_file
Example #29
0
    def validate_log_file(self, log_file, owner):
        """Make sure the log file exists and is accessible by owner.
        """
        if not operating_system.exists(log_file, as_root=True):
            operating_system.write_file(log_file, '', as_root=True)

        operating_system.chown(log_file, user=owner, group=owner, as_root=True)
        operating_system.chmod(log_file,
                               FileMode.ADD_USR_RW_GRP_RW_OTH_R,
                               as_root=True)

        return log_file
Example #30
0
    def validate_log_file(self, log_file, owner, group=None):
        """Make sure the log file exists and is accessible by owner.
        """
        if not group:
            group = owner
        if not operating_system.exists(log_file, as_root=True):
            operating_system.write_file(log_file, '', as_root=True)

        operating_system.chown(log_file, user=owner, group=group, as_root=True)
        operating_system.chmod(log_file,
                               FileMode.ADD_USR_RW_GRP_RW_OTH_R,
                               as_root=True)
        LOG.debug("Set log file '%s' as readable" % log_file)
        return log_file
    def test_exists(self):
        self.assertFalse(
            operating_system.exists(tempfile.gettempdir(), is_directory=False))
        self.assertTrue(
            operating_system.exists(tempfile.gettempdir(), is_directory=True))

        with tempfile.NamedTemporaryFile() as test_file:
            self.assertTrue(
                operating_system.exists(test_file.name, is_directory=False))
            self.assertFalse(
                operating_system.exists(test_file.name, is_directory=True))

        self._assert_execute_call(
            [['test -f path && echo 1 || echo 0']],
            [{'shell': True, 'check_exit_code': False,
              'run_as_root': True, 'root_helper': 'sudo'}],
            operating_system.exists, None, 'path', is_directory=False,
            as_root=True)
        self._assert_execute_call(
            [['test -d path && echo 1 || echo 0']],
            [{'shell': True, 'check_exit_code': False,
              'run_as_root': True, 'root_helper': 'sudo'}],
            operating_system.exists, None, 'path', is_directory=True,
            as_root=True)
Example #32
0
    def __init__(self, status, state_change_wait_time=None):
        LOG.debug("Initialize OracleApp.")
        if state_change_wait_time:
            self.state_change_wait_time = state_change_wait_time
        else:
            self.state_change_wait_time = CONF.state_change_wait_time
        LOG.debug("state_change_wait_time = %s." % self.state_change_wait_time)

        self.pfile = self._param_file_path(system.PFILE_NAME)
        self.spfile = self._param_file_path(system.SPFILE_NAME)
        self.new_spfile = self._param_file_path(system.NEW_SPFILE_NAME)
        self.configuration_manager = None
        if operating_system.exists(self.pfile):
            self._init_configuration_manager()
        self.status = status
    def test_exists(self):
        self.assertFalse(
            operating_system.exists(tempfile.gettempdir(), is_directory=False))
        self.assertTrue(
            operating_system.exists(tempfile.gettempdir(), is_directory=True))

        with tempfile.NamedTemporaryFile() as test_file:
            self.assertTrue(
                operating_system.exists(test_file.name, is_directory=False))
            self.assertFalse(
                operating_system.exists(test_file.name, is_directory=True))

        self._assert_execute_call(
            [['test -f path && echo 1 || echo 0']],
            [{'shell': True, 'check_exit_code': False,
              'run_as_root': True, 'root_helper': 'sudo'}],
            operating_system.exists, None, 'path', is_directory=False,
            as_root=True)
        self._assert_execute_call(
            [['test -d path && echo 1 || echo 0']],
            [{'shell': True, 'check_exit_code': False,
              'run_as_root': True, 'root_helper': 'sudo'}],
            operating_system.exists, None, 'path', is_directory=True,
            as_root=True)
Example #34
0
    def __init__(self, status, state_change_wait_time=None):
        LOG.debug("Initialize OracleApp.")
        if state_change_wait_time:
            self.state_change_wait_time = state_change_wait_time
        else:
            self.state_change_wait_time = CONF.state_change_wait_time
        LOG.debug("state_change_wait_time = %s." % self.state_change_wait_time)

        self.pfile = self._param_file_path(system.PFILE_NAME)
        self.spfile = self._param_file_path(system.SPFILE_NAME)
        self.new_spfile = self._param_file_path(system.NEW_SPFILE_NAME)
        self.configuration_manager = None
        if operating_system.exists(self.pfile):
            self._init_configuration_manager()
        self.status = status
Example #35
0
 def establish_ssh_user_equivalency(self, host_ip_pairs):
     """Establish SSH user equivalency by using ssh-keyscan against the
     cluster hostnames and IPs.
     :arg host_ip_pairs: list of (hostname, ip) tuples to add
     """
     filepath = path.join(self.user_home_dir, '.ssh', 'known_hosts')
     data = []
     if operating_system.exists(filepath, as_root=True):
         data.append(operating_system.read_file(filepath, as_root=True))
     for host_ip_pair in host_ip_pairs:
         host = host_ip_pair[0]
         ip = host_ip_pair[1]
         (stdout, stderr) = run_sys_command(
             'ssh-keyscan {host},{ip}'.format(host=host, ip=ip))
         data.append(stdout.strip())
     self.write_oracle_user_file(filepath, '\n'.join(data))
Example #36
0
 def establish_ssh_user_equivalency(self, host_ip_pairs):
     """Establish SSH user equivalency by using ssh-keyscan against the
     cluster hostnames and IPs.
     :arg host_ip_pairs: list of (hostname, ip) tuples to add
     """
     filepath = path.join(self.user_home_dir, '.ssh', 'known_hosts')
     data = []
     if operating_system.exists(filepath, as_root=True):
         data.append(operating_system.read_file(filepath, as_root=True))
     for host_ip_pair in host_ip_pairs:
         host = host_ip_pair[0]
         ip = host_ip_pair[1]
         (stdout, stderr) = run_sys_command(
             'ssh-keyscan {host},{ip}'.format(host=host, ip=ip))
         data.append(stdout.strip())
     self.write_oracle_user_file(filepath, '\n'.join(data))
Example #37
0
 def mount(self):
     if not operating_system.exists(self.mount_point, is_directory=True,
                                    as_root=True):
         operating_system.create_directory(self.mount_point, as_root=True)
     LOG.debug("Mounting volume. Device path:{0}, mount_point:{1}, "
               "volume_type:{2}, mount options:{3}".format(
                   self.device_path, self.mount_point, self.volume_fstype,
                   self.mount_options))
     try:
         utils.execute("mount", "-t", self.volume_fstype,
                       "-o", self.mount_options,
                       self.device_path, self.mount_point,
                       run_as_root=True, root_helper="sudo")
     except exception.ProcessExecutionError:
         msg = _("Could not mount '%s'.") % self.mount_point
         log_and_raise(msg)
Example #38
0
 def publish_log(self):
     if self.exposed:
         if self._log_rotated():
             LOG.debug(
                 "Log file rotation detected for '%s' - "
                 "discarding old log", self._name)
             self._delete_log_components()
         if operating_system.exists(self._file, as_root=True):
             self._publish_to_container(self._file)
         else:
             raise RuntimeError(
                 _("Cannot publish log file '%s' as it does not exist.") %
                 self._file)
         return self.show()
     else:
         raise exception.LogAccessForbidden(action='publish',
                                            log=self._name)
Example #39
0
    def remove_module(cls, driver, module_type, module_id, name, datastore,
                      ds_version):
        datastore = datastore or cls.MODULE_APPLY_TO_ALL
        ds_version = ds_version or cls.MODULE_APPLY_TO_ALL
        module_dir = cls.build_module_dir(module_type, module_id)
        contents_file = cls.build_contents_filename(module_dir)

        if not operating_system.exists(cls.get_result_filename(module_dir)):
            raise exception.NotFound(
                _("Module '%s' has not been applied") % name)
        try:
            removed, message = driver.remove(name, datastore, ds_version,
                                             contents_file)
            cls.remove_module_result(module_dir)
        except Exception:
            LOG.exception(_("Could not remove module '%s'") % name)
            raise
        return removed, message
Example #40
0
    def remove_module(cls, driver, module_type, module_id, name,
                      datastore, ds_version):
        datastore = datastore or cls.MODULE_APPLY_TO_ALL
        ds_version = ds_version or cls.MODULE_APPLY_TO_ALL
        module_dir = cls.build_module_dir(module_type, module_id)
        contents_file = cls.build_contents_filename(module_dir)

        if not operating_system.exists(cls.get_result_filename(module_dir)):
            raise exception.NotFound(
                _("Module '%s' has not been applied") % name)
        try:
            removed, message = driver.remove(
                name, datastore, ds_version, contents_file)
            cls.remove_module_result(module_dir)
        except Exception:
            LOG.exception(_("Could not remove module '%s'") % name)
            raise
        return removed, message
Example #41
0
    def post_upgrade(self, context, upgrade_info):
        app = self.mysql_app(self.mysql_app_status.get())
        app.stop_db()
        if 'device' in upgrade_info:
            self.mount_volume(context, mount_point=upgrade_info['mount_point'],
                              device_path=upgrade_info['device'])

        if operating_system.exists(upgrade_info['save_etc_dir'],
                                   is_directory=True, as_root=True):
            operating_system.copy("%s/." % upgrade_info['save_etc_dir'],
                                  "/etc", preserve=True, as_root=True)

        operating_system.copy("%s/." % upgrade_info['save_dir'], "/etc/mysql",
                              preserve=True, as_root=True)
        operating_system.copy("%s/." % upgrade_info['home_save'],
                              os.path.expanduser('~'),
                              preserve=True, as_root=True)
        app.start_mysql()
Example #42
0
 def save_files_pre_upgrade(self, mount_point):
     save_dir = path.join(mount_point, 'saves')
     saves = {
         'oratab': self.paths.oratab_file,
         'dbs': self.paths.dbs_dir,
         'oranet': self.paths.oranet_dir,
         'admin': self.paths.admin_dir,
         'conf_file': CONF.get(MANAGER).conf_file
     }
     if not operating_system.exists(save_dir,
                                    is_directory=True, as_root=True):
         operating_system.create_directory(save_dir,
                                           force=True, as_root=True)
     for item in saves.keys():
         operating_system.copy(saves[item], path.join(save_dir, item),
                               recursive=True, preserve=True, as_root=True)
     return {'save_dir': save_dir,
             'saves': saves}
Example #43
0
    def _run_restore(self):
        metadata = self.storage.load_metadata(self.location, self.checksum)
        self.db_name = metadata['db_name']
        self.app.paths.update_db_name(self.db_name)

        new_dirs = [
            self.app.paths.audit_dir, self.app.paths.db_fast_recovery_logs_dir,
            self.app.paths.db_fast_recovery_dir, self.app.paths.db_data_dir
        ]
        for new_dir in new_dirs:
            operating_system.create_directory(
                new_dir,
                user=self.app.instance_owner,
                group=self.app.instance_owner_group,
                force=True,
                as_root=True)

        # the backup set will restore directly to ORADATA/backupset_files
        self._unpack_backup_files(self.location, self.checksum)

        if operating_system.exists(self.app.paths.base_spfile, as_root=True):
            operating_system.copy(self.app.paths.base_spfile,
                                  self.app.paths.spfile,
                                  preserve=True,
                                  as_root=True)

        # the conf file was just restored by the unpack so sync now
        self.app.admin.delete_conf_cache()
        self.app.admin.ora_config.db_name = self.db_name

        chown_dirs = [
            self.app.paths.backup_dir, self.app.paths.fast_recovery_area
        ]
        for chown_dir in chown_dirs:
            operating_system.chown(chown_dir,
                                   self.app.instance_owner,
                                   self.app.instance_owner_group,
                                   recursive=True,
                                   force=True,
                                   as_root=True)

        self._perform_restore()
        self._perform_recover()
        self._open_database()
Example #44
0
 def configure_hosts(self, cluster_name, public_cidr, private_cidr):
     """Configure /etc/hosts file."""
     hosts = path.join('/etc', 'hosts')
     pub_subnet = rac_utils.RACPublicSubnetManager(public_cidr)
     pri_subnet = rac_utils.CommonSubnetManager(private_cidr)
     data = []
     if operating_system.exists(hosts):
         data.append(operating_system.read_file(hosts, as_root=True))
     for scan_ip in pub_subnet.scan_list:
         data.append("{ip} {name}".format(ip=scan_ip, name=SCAN_NAME))
     for i in range(0, pub_subnet.max_instances()):
         hostname = rac_utils.make_instance_hostname(cluster_name, i)
         data.append("{pubip} {hostname}".format(
             pubip=pub_subnet.instance_ip(i), hostname=hostname))
         data.append("{vip} {hostname}-vip".format(
             vip=pub_subnet.instance_vip(i), hostname=hostname))
         data.append("{priip} {hostname}-priv".format(
             priip=pri_subnet.instance_ip(i), hostname=hostname))
     operating_system.write_file(hosts, '\n'.join(data), as_root=True)
Example #45
0
 def save_files_pre_upgrade(self, mount_point):
     mnt_confs_dir = os.path.join(mount_point, 'save_confs/')
     mnt_creds_dir = os.path.join(mount_point, 'save_creds/')
     for save_dir in [mnt_confs_dir, mnt_creds_dir]:
         if not operating_system.exists(save_dir,
                                        is_directory=True,
                                        as_root=True):
             operating_system.create_directory(save_dir,
                                               force=True,
                                               as_root=True)
     operating_system.copy(system.MONGOD_CONFIG_FILE, mnt_confs_dir,
                           preserve=True, as_root=True)
     operating_system.copy(system.MONGOD_CONFIG_OVERRIDES_DIR,
                           mnt_confs_dir,
                           preserve=True, recursive=True, as_root=True)
     operating_system.copy(system.MONGO_ADMIN_CREDS_FILE, mnt_creds_dir,
                           preserve=True, as_root=True)
     return {'save_confs': mnt_confs_dir,
             'save_creds': mnt_creds_dir}
Example #46
0
 def configure_hosts(self, cluster_name, public_cidr, private_cidr):
     """Configure /etc/hosts file."""
     hosts = path.join('/etc', 'hosts')
     pub_subnet = rac_utils.RACPublicSubnetManager(public_cidr)
     pri_subnet = rac_utils.CommonSubnetManager(private_cidr)
     data = []
     if operating_system.exists(hosts):
         data.append(operating_system.read_file(hosts, as_root=True))
     for scan_ip in pub_subnet.scan_list:
         data.append("{ip} {name}".format(ip=scan_ip, name=SCAN_NAME))
     for i in range(0, pub_subnet.max_instances()):
         hostname = rac_utils.make_instance_hostname(cluster_name, i)
         data.append("{pubip} {hostname}".format(
             pubip=pub_subnet.instance_ip(i), hostname=hostname))
         data.append("{vip} {hostname}-vip".format(
             vip=pub_subnet.instance_vip(i), hostname=hostname))
         data.append("{priip} {hostname}-priv".format(
             priip=pri_subnet.instance_ip(i), hostname=hostname))
     operating_system.write_file(hosts, '\n'.join(data),
                                 as_root=True)
    def _run_restore(self):
        metadata = self.storage.load_metadata(self.location, self.checksum)
        self.db_name = metadata["db_name"]
        self.app.paths.update_db_name(self.db_name)

        new_dirs = [
            self.app.paths.audit_dir,
            self.app.paths.db_fast_recovery_logs_dir,
            self.app.paths.db_fast_recovery_dir,
            self.app.paths.db_data_dir,
        ]
        for new_dir in new_dirs:
            operating_system.create_directory(
                new_dir, user=self.app.instance_owner, group=self.app.instance_owner_group, force=True, as_root=True
            )

        # the backup set will restore directly to ORADATA/backupset_files
        self._unpack_backup_files(self.location, self.checksum)

        if operating_system.exists(self.app.paths.base_spfile, as_root=True):
            operating_system.copy(self.app.paths.base_spfile, self.app.paths.spfile, preserve=True, as_root=True)

        # the conf file was just restored by the unpack so sync now
        self.app.admin.delete_conf_cache()
        self.app.admin.ora_config.db_name = self.db_name

        chown_dirs = [self.app.paths.backup_dir, self.app.paths.fast_recovery_area]
        for chown_dir in chown_dirs:
            operating_system.chown(
                chown_dir,
                self.app.instance_owner,
                self.app.instance_owner_group,
                recursive=True,
                force=True,
                as_root=True,
            )

        self._perform_restore()
        self._perform_recover()
        self._open_database()
Example #48
0
    def __init__(self, status):
        self.state_change_wait_time = CONF.state_change_wait_time
        self.status = status
        revision_dir = \
            guestagent_utils.build_file_path(
                os.path.join(MOUNT_POINT,
                             os.path.dirname(system.VERTICA_ADMIN)),
                ConfigurationManager.DEFAULT_STRATEGY_OVERRIDES_SUB_DIR)

        if not operating_system.exists(FAKE_CFG):
            operating_system.write_file(FAKE_CFG, '', as_root=True)
            operating_system.chown(FAKE_CFG, system.VERTICA_ADMIN,
                                   system.VERTICA_ADMIN_GRP, as_root=True)
            operating_system.chmod(FAKE_CFG, FileMode.ADD_GRP_RX_OTH_RX(),
                                   as_root=True)
        self.configuration_manager = \
            ConfigurationManager(FAKE_CFG, system.VERTICA_ADMIN,
                                 system.VERTICA_ADMIN_GRP,
                                 PropertiesCodec(delimiter='='),
                                 requires_root=True,
                                 override_strategy=ImportOverrideStrategy(
                                     revision_dir, "cnf"))
Example #49
0
    def _update_details(self):
        # Make sure we can read the file
        if not self._file_readable or not os.access(self._file, os.R_OK):
            if not os.access(self._file, os.R_OK):
                if operating_system.exists(self._file, as_root=True):
                    operating_system.chmod(self._file,
                                           FileMode.ADD_ALL_R,
                                           as_root=True)
            self._file_readable = True

        if os.path.isfile(self._file):
            logstat = os.stat(self._file)
            self._size = logstat.st_size
            self._update_log_header_digest(self._file)

            if self._log_rotated():
                self.status = LogStatus.Rotated
            # See if we have stuff to publish
            elif logstat.st_size > self._published_size:
                self._set_status(self._published_size, LogStatus.Partial,
                                 LogStatus.Ready)
            # We've published everything so far
            elif logstat.st_size == self._published_size:
                self._set_status(self._published_size, LogStatus.Published,
                                 LogStatus.Enabled)
            # We've already handled this case (log rotated) so what gives?
            else:
                raise ("Bug in _log_rotated ?")
        else:
            self._published_size = 0
            self._size = 0

        if not self._size or not self.enabled:
            user_status = LogStatus.Disabled
            if self.enabled:
                user_status = LogStatus.Enabled
            self._set_status(self._type == LogType.USER, user_status,
                             LogStatus.Unavailable)
Example #50
0
    def _get_or_create_replication_user(self):
        # There are three scenarios we need to deal with here:
        # - This is a fresh master, with no replicator user created.
        #   Generate a new u/p
        # - We are attaching a new slave and need to give it the login creds
        #   Send the creds we have stored in PGDATA/.replpass
        # - This is a failed-over-to slave, who will have the replicator user
        #   but not the credentials file. Recreate the repl user in this case

        pwfile = os.path.join(self.pgsql_data_dir, ".replpass")
        if self.user_exists(REPL_USER):
            if operating_system.exists(pwfile, as_root=True):
                pw = operating_system.read_file(pwfile, as_root=True)
            else:
                u = models.PostgreSQLUser(REPL_USER)
                self._drop_user(context=None, user=u)
                pw = self._create_replication_user(pwfile)
        else:
            pw = self._create_replication_user(pwfile)

        repl_user_info = {'name': REPL_USER, 'password': pw}

        return repl_user_info
Example #51
0
 def save_files_pre_upgrade(self, mount_point):
     mnt_confs_dir = os.path.join(mount_point, 'save_confs/')
     mnt_creds_dir = os.path.join(mount_point, 'save_creds/')
     for save_dir in [mnt_confs_dir, mnt_creds_dir]:
         if not operating_system.exists(
                 save_dir, is_directory=True, as_root=True):
             operating_system.create_directory(save_dir,
                                               force=True,
                                               as_root=True)
     operating_system.copy(system.MONGOD_CONFIG_FILE,
                           mnt_confs_dir,
                           preserve=True,
                           as_root=True)
     operating_system.copy(system.MONGOD_CONFIG_OVERRIDES_DIR,
                           mnt_confs_dir,
                           preserve=True,
                           recursive=True,
                           as_root=True)
     operating_system.copy(system.MONGO_ADMIN_CREDS_FILE,
                           mnt_creds_dir,
                           preserve=True,
                           as_root=True)
     return {'save_confs': mnt_confs_dir, 'save_creds': mnt_creds_dir}
Example #52
0
 def restore_files_post_upgrade(self, upgrade_info):
     saves = upgrade_info.get('saves')
     save_dir = upgrade_info.get('save_dir')
     if not (saves and save_dir):
         raise exception.GuestError(_(
             "Missing upgrade saves and/or save directory info: %s")
             % upgrade_info)
     for item in saves.keys():
         if item == 'conf_file':
             operating_system.copy(path.join(save_dir, item),
                                   CONF.get(MANAGER).conf_file,
                                   force=True, preserve=True, as_root=True)
             continue
         if operating_system.exists(saves[item],
                                    is_directory=True, as_root=True):
             operating_system.remove(saves[item], force=True,
                                     recursive=True, as_root=True)
         operating_system.copy(path.join(save_dir, item), saves[item],
                               recursive=True, preserve=True, as_root=True)
     operating_system.remove(save_dir, force=True, as_root=True)
     self._init_configuration_manager()
     self.configuration_manager.refresh_cache()
     self.status.set_ready()
Example #53
0
    def _update_details(self):
        # Make sure we can read the file
        if not self._file_readable or not os.access(self._file, os.R_OK):
            if not os.access(self._file, os.R_OK):
                if operating_system.exists(self._file, as_root=True):
                    operating_system.chmod(
                        self._file, FileMode.ADD_ALL_R, as_root=True)
            self._file_readable = True

        if os.path.isfile(self._file):
            logstat = os.stat(self._file)
            self._size = logstat.st_size
            self._update_log_header_digest(self._file)

            if self._log_rotated():
                self.status = LogStatus.Rotated
            # See if we have stuff to publish
            elif logstat.st_size > self._published_size:
                self._set_status(self._published_size,
                                 LogStatus.Partial, LogStatus.Ready)
            # We've published everything so far
            elif logstat.st_size == self._published_size:
                self._set_status(self._published_size,
                                 LogStatus.Published, LogStatus.Enabled)
            # We've already handled this case (log rotated) so what gives?
            else:
                raise ("Bug in _log_rotated ?")
        else:
            self._published_size = 0
            self._size = 0

        if not self._size or not self.enabled:
            user_status = LogStatus.Disabled
            if self.enabled:
                user_status = LogStatus.Enabled
            self._set_status(self._type == LogType.USER,
                             user_status, LogStatus.Unavailable)
Example #54
0
def create_db2_dir(dir_name):
    if not operating_system.exists(dir_name, True):
        operating_system.create_directory(dir_name,
                                          system.DB2_INSTANCE_OWNER,
                                          system.DB2_INSTANCE_OWNER,
                                          as_root=True)
Example #55
0
 def _run_pre_backup(self):
     if operating_system.exists(MYSQL_BACKUP_DIR, is_directory=True,
                                as_root=True):
         operating_system.create_directory(MYSQL_BACKUP_DIR,
                                           as_root=True)
Example #56
0
 def pre_restore(self):
     super(MySqlBackup, self).pre_restore()
     if operating_system.exists(MYSQL_BACKUP_DIR, is_directory=True,
                                as_root=True):
         operating_system.create_directory(MYSQL_BACKUP_DIR,
                                           as_root=True)