def prepare(self, context, packages, databases, memory_mb, users, device_path=None, mount_point=None, backup_info=None, config_contents=None, root_password=None, overrides=None, cluster_config=None, snapshot=None): """Makes ready DBAAS on a Guest container.""" MySqlAppStatus.get().begin_install() # status end_mysql_install set with secure() app = MySqlApp(MySqlAppStatus.get()) app.install_if_needed(packages) if device_path: # stop and do not update database app.stop_db() device = volume.VolumeDevice(device_path) # unmount if device is already mounted device.unmount_device(device_path) device.format() if os.path.exists(mount_point): # rsync existing data to a "data" sub-directory # on the new volume device.migrate_data(mount_point, target_subdir="data") # mount the volume device.mount(mount_point) operating_system.chown(mount_point, 'mysql', 'mysql', recursive=False, as_root=True) LOG.debug("Mounted the volume at %s." % mount_point) # We need to temporarily update the default my.cnf so that # mysql will start after the volume is mounted. Later on it # will be changed based on the config template and restart. app.update_overrides("[mysqld]\ndatadir=%s/data\n" % mount_point) app.start_mysql() if backup_info: self._perform_restore(backup_info, context, mount_point + "/data", app) LOG.debug("Securing MySQL now.") app.secure(config_contents, overrides) enable_root_on_restore = (backup_info and MySqlAdmin().is_root_enabled()) if root_password and not backup_info: app.secure_root(secure_remote_root=True) MySqlAdmin().enable_root(root_password) elif enable_root_on_restore: app.secure_root(secure_remote_root=False) MySqlAppStatus.get().report_root(context, 'root') else: app.secure_root(secure_remote_root=True) app.complete_install_or_restart() if databases: self.create_database(context, databases) if users: self.create_user(context, users) if snapshot: self.attach_replica(context, snapshot, snapshot['config']) LOG.info(_('Completed setup of MySQL database instance.'))
class MySqlAppStatusTest(testtools.TestCase): def setUp(self): super(MySqlAppStatusTest, self).setUp() util.init_db() self.orig_utils_execute_with_timeout = dbaas.utils.execute_with_timeout self.orig_load_mysqld_options = dbaas.load_mysqld_options self.orig_dbaas_os_path_exists = dbaas.os.path.exists self.orig_dbaas_time_sleep = dbaas.time.sleep self.FAKE_ID = randint(1, 10000) InstanceServiceStatus.create(instance_id=self.FAKE_ID, status=rd_instance.ServiceStatuses.NEW) dbaas.CONF.guest_id = self.FAKE_ID def tearDown(self): super(MySqlAppStatusTest, self).tearDown() dbaas.utils.execute_with_timeout = self.orig_utils_execute_with_timeout dbaas.load_mysqld_options = self.orig_load_mysqld_options dbaas.os.path.exists = self.orig_dbaas_os_path_exists dbaas.time.sleep = self.orig_dbaas_time_sleep InstanceServiceStatus.find_by(instance_id=self.FAKE_ID).delete() dbaas.CONF.guest_id = None def test_get_actual_db_status(self): dbaas.utils.execute_with_timeout = Mock(return_value=(None, None)) self.mySqlAppStatus = MySqlAppStatus() status = self.mySqlAppStatus._get_actual_db_status() self.assertEqual(rd_instance.ServiceStatuses.RUNNING, status) def test_get_actual_db_status_error_shutdown(self): from trove.common.exception import ProcessExecutionError mocked = Mock(side_effect=ProcessExecutionError()) dbaas.utils.execute_with_timeout = mocked dbaas.load_mysqld_options = Mock() dbaas.os.path.exists = Mock(return_value=False) self.mySqlAppStatus = MySqlAppStatus() status = self.mySqlAppStatus._get_actual_db_status() self.assertEqual(rd_instance.ServiceStatuses.SHUTDOWN, status) def test_get_actual_db_status_error_crashed(self): from trove.common.exception import ProcessExecutionError dbaas.utils.execute_with_timeout = MagicMock( side_effect=[ProcessExecutionError(), ("some output", None)]) dbaas.load_mysqld_options = Mock() dbaas.os.path.exists = Mock(return_value=True) self.mySqlAppStatus = MySqlAppStatus() status = self.mySqlAppStatus._get_actual_db_status() self.assertEqual(rd_instance.ServiceStatuses.BLOCKED, status)
def prepare(self, context, packages, databases, memory_mb, users, device_path=None, mount_point=None, backup_info=None, config_contents=None, root_password=None, overrides=None, cluster_config=None, snapshot=None): """Makes ready DBAAS on a Guest container.""" MySqlAppStatus.get().begin_install() # status end_mysql_install set with secure() app = MySqlApp(MySqlAppStatus.get()) app.install_if_needed(packages) if device_path: #stop and do not update database app.stop_db() device = volume.VolumeDevice(device_path) # unmount if device is already mounted device.unmount_device(device_path) device.format() if os.path.exists(mount_point): #rsync exiting data device.migrate_data(mount_point) #mount the volume device.mount(mount_point) LOG.debug("Mounted the volume.") app.start_mysql() if backup_info: self._perform_restore(backup_info, context, mount_point, app) LOG.debug("Securing MySQL now.") app.secure(config_contents, overrides) enable_root_on_restore = (backup_info and MySqlAdmin().is_root_enabled()) if root_password and not backup_info: app.secure_root(secure_remote_root=True) MySqlAdmin().enable_root(root_password) elif enable_root_on_restore: app.secure_root(secure_remote_root=False) MySqlAppStatus.get().report_root('root') else: app.secure_root(secure_remote_root=True) app.complete_install_or_restart() if databases: self.create_database(context, databases) if users: self.create_user(context, users) if snapshot: self.attach_replica(context, snapshot, snapshot['config']) LOG.info(_('Completed setup of MySQL database instance.'))
def prepare(self, context, databases, memory_mb, users, device_path=None, mount_point=None, backup_id=None, config_contents=None, root_password=None): """Makes ready DBAAS on a Guest container.""" MySqlAppStatus.get().begin_install() # status end_mysql_install set with secure() app = MySqlApp(MySqlAppStatus.get()) restart_mysql = False if device_path: device = volume.VolumeDevice(device_path) device.format() #if a /var/lib/mysql folder exists, back it up. if os.path.exists(CONF.mount_point): #stop and do not update database app.stop_db() #rsync exiting data if not backup_id: restart_mysql = True device.migrate_data(CONF.mount_point) #mount the volume device.mount(mount_point) LOG.debug(_("Mounted the volume.")) #check mysql was installed and stopped if restart_mysql: app.start_mysql() app.install_if_needed() if backup_id: self._perform_restore(backup_id, context, CONF.mount_point, app) LOG.info(_("Securing mysql now.")) app.secure(config_contents) enable_root_on_restore = (backup_id and MySqlAdmin().is_root_enabled()) if root_password and not backup_id: app.secure_root(secure_remote_root=True) MySqlAdmin().enable_root(root_password) MySqlAdmin().report_root_enabled(context) elif enable_root_on_restore: app.secure_root(secure_remote_root=False) MySqlAdmin().report_root_enabled(context) else: app.secure_root(secure_remote_root=True) app.complete_install_or_restart() if databases: self.create_database(context, databases) if users: self.create_user(context, users) LOG.info('"prepare" call has finished.')
def get_replication_snapshot(self, context, snapshot_info, replica_source_config=None): LOG.debug("Getting replication snapshot.") app = MySqlApp(MySqlAppStatus.get()) replication = REPLICATION_STRATEGY_CLASS(context) replication.enable_as_master(app, replica_source_config) snapshot_id, log_position = ( replication.snapshot_for_replication(context, app, None, snapshot_info)) mount_point = CONF.get(MANAGER).mount_point volume_stats = dbaas.get_filesystem_volume_stats(mount_point) replication_snapshot = { 'dataset': { 'datastore_manager': MANAGER, 'dataset_size': volume_stats.get('used', 0.0), 'volume_size': volume_stats.get('total', 0.0), 'snapshot_id': snapshot_id }, 'replication_strategy': REPLICATION_STRATEGY, 'master': replication.get_master_ref(app, snapshot_info), 'log_position': log_position } return replication_snapshot
def test_get_actual_db_status(self): dbaas.utils.execute_with_timeout = Mock(return_value=(None, None)) self.mySqlAppStatus = MySqlAppStatus() status = self.mySqlAppStatus._get_actual_db_status() self.assertEqual(rd_instance.ServiceStatuses.RUNNING, status)
def attach_replication_slave(self, context, snapshot, slave_config): LOG.debug("Attaching replication snapshot.") app = MySqlApp(MySqlAppStatus.get()) try: self._validate_slave_for_replication(context, snapshot) replication = REPLICATION_STRATEGY_CLASS(context) replication.enable_as_slave(app, snapshot, slave_config) except Exception: LOG.exception("Error enabling replication.") app.status.set_status(rd_instance.ServiceStatuses.FAILED) raise
def test_get_actual_db_status_error_crashed(self): from trove.common.exception import ProcessExecutionError dbaas.utils.execute_with_timeout = MagicMock( side_effect=[ProcessExecutionError(), ("some output", None)]) dbaas.load_mysqld_options = Mock() dbaas.os.path.exists = Mock(return_value=True) self.mySqlAppStatus = MySqlAppStatus() status = self.mySqlAppStatus._get_actual_db_status() self.assertEqual(rd_instance.ServiceStatuses.BLOCKED, status)
def test_get_actual_db_status_error_shutdown(self): from trove.common.exception import ProcessExecutionError mocked = Mock(side_effect=ProcessExecutionError()) dbaas.utils.execute_with_timeout = mocked dbaas.load_mysqld_options = Mock() dbaas.os.path.exists = Mock(return_value=False) self.mySqlAppStatus = MySqlAppStatus() status = self.mySqlAppStatus._get_actual_db_status() self.assertEqual(rd_instance.ServiceStatuses.SHUTDOWN, status)
def attach_replica(self, context, replica_info, slave_config): LOG.debug("Attaching replica.") app = MySqlApp(MySqlAppStatus.get()) try: if 'replication_strategy' in replica_info: self._validate_slave_for_replication(context, replica_info) replication = REPLICATION_STRATEGY_CLASS(context) replication.enable_as_slave(app, replica_info, slave_config) except Exception: LOG.exception("Error enabling replication.") app.status.set_status(rd_instance.ServiceStatuses.FAILED) raise
def detach_replica(self, context): LOG.debug("Detaching replica.") app = MySqlApp(MySqlAppStatus.get()) replication = REPLICATION_STRATEGY_CLASS(context) replication.detach_slave(app)
def make_read_only(self, context, read_only): LOG.debug("Executing make_read_only(%s)" % read_only) app = MySqlApp(MySqlAppStatus.get()) app.make_read_only(read_only)
def update_status(self, context): """Update the status of the MySQL service.""" MySqlAppStatus.get().update()
def get_latest_txn_id(self, context): LOG.debug("Calling get_latest_txn_id.") return MySqlApp(MySqlAppStatus.get()).get_latest_txn_id()
def detach_replica(self, context, for_failover=False): LOG.debug("Detaching replica.") app = MySqlApp(MySqlAppStatus.get()) replication = REPLICATION_STRATEGY_CLASS(context) replica_info = replication.detach_slave(app, for_failover) return replica_info
def detach_replica(self, context): LOG.debug("Detaching replica.") app = MySqlApp(MySqlAppStatus.get()) replication = REPLICATION_STRATEGY_CLASS(context) replication.detach_slave(app)
def get_txn_count(self, context): LOG.debug("Calling get_txn_count") return MySqlApp(MySqlAppStatus.get()).get_txn_count()
def start_db_with_conf_changes(self, context, config_contents): app = MySqlApp(MySqlAppStatus.get()) app.start_db_with_conf_changes(config_contents)
def update_overrides(self, context, overrides, remove=False): app = MySqlApp(MySqlAppStatus.get()) if remove: app.remove_overrides() app.update_overrides(overrides)
def reset_configuration(self, context, configuration): app = MySqlApp(MySqlAppStatus.get()) app.reset_configuration(configuration)
def _build_app(self): return MariaDBApp(MySqlAppStatus.get())
def demote_replication_master(self, context): LOG.debug("Demoting replication master.") app = MySqlApp(MySqlAppStatus.get()) replication = REPLICATION_STRATEGY_CLASS(context) replication.demote_master(app)
def get_replica_context(self, context): LOG.debug("Getting replica context.") app = MySqlApp(MySqlAppStatus.get()) replication = REPLICATION_STRATEGY_CLASS(context) replica_info = replication.get_replica_context(app) return replica_info
(action_text, log_name)) if gl_cache[log_name].type == guest_log.LogType.USER: requires_change = ((gl_cache[log_name].enabled and disable) or (not gl_cache[log_name].enabled and enable)) if requires_change: restart_required = self.guest_log_enable( context, log_name, disable) if restart_required: self.set_guest_log_status( guest_log.LogStatus.Restart_Required, log_name) gl_cache[log_name].enabled = enable log_details = gl_cache[log_name].show() if discard: log_details = gl_cache[log_name].discard_log() if publish: log_details = gl_cache[log_name].publish_log() LOG.info( _("Details for log '%(log)s': %(det)s") % { 'log': log_name, 'det': log_details }) return log_details raise exception.NotFound("Log '%s' is not defined." % log_name) #---------------------xtzhang-----------------add----------------------- if __name__ == "__main__": app = MySqlApp(MySqlAppStatus.get()) app.get_master_status()
def apply_overrides(self, context, overrides): app = MySqlApp(MySqlAppStatus.get()) app.apply_overrides(overrides)
def update_overrides(self, context, overrides, remove=False): LOG.debug("Updating overrides (%s)." % overrides) app = MySqlApp(MySqlAppStatus.get()) app.update_overrides(overrides, remove=remove)
def make_read_only(self, context, read_only): LOG.debug("Executing make_read_only(%s)" % read_only) app = MySqlApp(MySqlAppStatus.get()) app.make_read_only(read_only)
def prepare(self, context, packages, databases, memory_mb, users, device_path=None, mount_point=None, backup_info=None, config_contents=None, root_password=None, overrides=None, cluster_config=None, snapshot=None): """Makes ready DBAAS on a Guest container.""" MySqlAppStatus.get().begin_install() # status end_mysql_install set with secure() app = MySqlApp(MySqlAppStatus.get()) app.install_if_needed(packages) if device_path: # stop and do not update database app.stop_db() device = volume.VolumeDevice(device_path) # unmount if device is already mounted device.unmount_device(device_path) device.format() if os.path.exists(mount_point): # rsync existing data to a "data" sub-directory # on the new volume device.migrate_data(mount_point, target_subdir="data") # mount the volume device.mount(mount_point) operating_system.chown(mount_point, 'mysql', 'mysql', recursive=False, as_root=True) LOG.debug("Mounted the volume at %s." % mount_point) # We need to temporarily update the default my.cnf so that # mysql will start after the volume is mounted. Later on it # will be changed based on the config template and restart. app.update_overrides("[mysqld]\ndatadir=%s/data\n" % mount_point) app.start_mysql() if backup_info: self._perform_restore(backup_info, context, mount_point + "/data", app) LOG.debug("Securing MySQL now.") app.secure(config_contents, overrides) enable_root_on_restore = (backup_info and MySqlAdmin().is_root_enabled()) if root_password and not backup_info: app.secure_root(secure_remote_root=True) MySqlAdmin().enable_root(root_password) elif enable_root_on_restore: app.secure_root(secure_remote_root=False) MySqlAppStatus.get().report_root(context, 'root') else: app.secure_root(secure_remote_root=True) app.complete_install_or_restart() if databases: self.create_database(context, databases) if users: self.create_user(context, users) if snapshot: self.attach_replica(context, snapshot, snapshot['config']) LOG.info(_('Completed setup of MySQL database instance.'))
def update_status(self, context): """Update the status of the MySQL service.""" MySqlAppStatus.get().update()
def restart(self, context): app = MySqlApp(MySqlAppStatus.get()) app.restart()
def apply_overrides(self, context, overrides): app = MySqlApp(MySqlAppStatus.get()) app.apply_overrides(overrides)
def start_db_with_conf_changes(self, context, config_contents): app = MySqlApp(MySqlAppStatus.get()) app.start_db_with_conf_changes(config_contents)
def restart(self, context): app = MySqlApp(MySqlAppStatus.get()) app.restart()
def stop_db(self, context, do_not_start_on_reboot=False): app = MySqlApp(MySqlAppStatus.get()) app.stop_db(do_not_start_on_reboot=do_not_start_on_reboot)
def stop_db(self, context, do_not_start_on_reboot=False): app = MySqlApp(MySqlAppStatus.get()) app.stop_db(do_not_start_on_reboot=do_not_start_on_reboot)
def update_overrides(self, context, overrides, remove=False): app = MySqlApp(MySqlAppStatus.get()) if remove: app.remove_overrides() app.update_overrides(overrides)
def apply_overrides(self, context, overrides): LOG.debug("Applying overrides (%s)." % overrides) app = MySqlApp(MySqlAppStatus.get()) app.apply_overrides(overrides)
def apply_overrides(self, context, overrides): LOG.debug("Applying overrides (%s)." % overrides) app = MySqlApp(MySqlAppStatus.get()) app.apply_overrides(overrides)
def enable_as_master(self, context, replica_source_config): LOG.debug("Calling enable_as_master.") app = MySqlApp(MySqlAppStatus.get()) replication = REPLICATION_STRATEGY_CLASS(context) replication.enable_as_master(app, replica_source_config)
def enable_as_master(self, context, replica_source_config): LOG.debug("Calling enable_as_master.") app = MySqlApp(MySqlAppStatus.get()) replication = REPLICATION_STRATEGY_CLASS(context) replication.enable_as_master(app, replica_source_config)
def get_last_txn(self, context): LOG.debug("Calling get_last_txn") return MySqlApp(MySqlAppStatus.get()).get_last_txn()
def get_txn_count(self, context): LOG.debug("Calling get_txn_count") return MySqlApp(MySqlAppStatus.get()).get_txn_count()
def wait_for_txn(self, context, txn): LOG.debug("Calling wait_for_txn.") MySqlApp(MySqlAppStatus.get()).wait_for_txn(txn)
def get_last_txn(self, context): LOG.debug("Calling get_last_txn") return MySqlApp(MySqlAppStatus.get()).get_last_txn()
def get_replica_context(self, context): LOG.debug("Getting replica context.") app = MySqlApp(MySqlAppStatus.get()) replication = REPLICATION_STRATEGY_CLASS(context) replica_info = replication.get_replica_context(app) return replica_info
def get_latest_txn_id(self, context): LOG.debug("Calling get_latest_txn_id.") return MySqlApp(MySqlAppStatus.get()).get_latest_txn_id()
def demote_replication_master(self, context): LOG.debug("Demoting replication master.") app = MySqlApp(MySqlAppStatus.get()) replication = REPLICATION_STRATEGY_CLASS(context) replication.demote_master(app)
def wait_for_txn(self, context, txn): LOG.debug("Calling wait_for_txn.") MySqlApp(MySqlAppStatus.get()).wait_for_txn(txn)
def reset_configuration(self, context, configuration): app = MySqlApp(MySqlAppStatus.get()) app.reset_configuration(configuration)
def detach_replica(self, context, for_failover=False): LOG.debug("Detaching replica.") app = MySqlApp(MySqlAppStatus.get()) replication = REPLICATION_STRATEGY_CLASS(context) replica_info = replication.detach_slave(app, for_failover) return replica_info