def create(cls, context, instance_id, user, root_password, cluster_instances_list=None): load_and_verify(context, instance_id) if root_password: root = create_guest_client(context, instance_id).enable_root_with_password(root_password) else: root = create_guest_client(context, instance_id).enable_root() root_user = guest_models.RootUser() root_user.deserialize(root) # if cluster_instances_list none, then root create is called for # single instance, adding an RootHistory entry for the instance_id if cluster_instances_list is None: RootHistory.create(context, instance_id, user) return root_user
def update_attributes(cls, context, instance_id, username, hostname, user_attrs): load_and_verify(context, instance_id) client = create_guest_client(context, instance_id) user_changed = user_attrs.get('name') host_changed = user_attrs.get('host') validate = guest_models.MySQLUser() if host_changed: validate.host = host_changed if user_changed: validate.name = user_changed user = user_changed or username host = host_changed or hostname userhost = "%s@%s" % (user, host) if user_changed or host_changed: existing_users, _nadda = Users.load_with_client( client, limit=1, marker=userhost, include_marker=True) if (len(existing_users) > 0 and existing_users[0].name == user and existing_users[0].host == host): raise exception.UserAlreadyExists(name=user, host=host) client.update_attributes(username, hostname, user_attrs)
def create(cls, context, instance_id, user): load_and_verify(context, instance_id) root = create_guest_client(context, instance_id).enable_root() root_user = guest_models.RootUser() root_user.deserialize(root) RootHistory.create(context, instance_id, user) return root_user
def guest_log_list(self, req, tenant_id, id): """Return all information about all logs for an instance.""" LOG.debug("Listing logs for tenant %s" % tenant_id) context = req.environ[wsgi.CONTEXT_KEY] client = create_guest_client(context, id) guest_log_list = client.guest_log_list() return wsgi.Result({'logs': guest_log_list}, 200)
def access(cls, context, instance_id, username, hostname): load_and_verify(context, instance_id) client = create_guest_client(context, instance_id) databases = client.list_access(username, hostname) dbs = [] for db in databases: dbs.append(Schema(name=db["_name"], collate=db["_collate"], character_set=db["_character_set"])) return UserAccess(dbs)
def load_via_context(cls, context, instance_id): """Creates guest and fetches pagination arguments from the context.""" load_and_verify(context, instance_id) limit = utils.pagination_limit(context.limit, cls.DEFAULT_LIMIT) client = create_guest_client(context, instance_id) # The REST API standard dictates that we *NEVER* include the marker. return cls.load_with_client(client=client, limit=limit, marker=context.marker, include_marker=False)
def change_password(cls, context, instance_id, users): load_and_verify(context, instance_id) client = create_guest_client(context, instance_id) change_users = [] for user in users: change_user = {"name": user.name, "host": user.host, "password": user.password} change_users.append(change_user) client.change_passwords(change_users)
def load_guest_info(instance, context, id): if instance.status not in AGENT_INVALID_STATUSES: guest = create_guest_client(context, id) try: instance.volume_used = guest.get_volume_info()['used'] except Exception as e: LOG.error(e) return instance
def create(cls, context, instance_id, schemas): load_and_verify(context, instance_id) client = create_guest_client(context, instance_id) for schema in schemas: schema_name = schema["_name"] existing_schema, _nadda = Schemas.load_with_client(client, limit=1, marker=schema_name, include_marker=True) if len(existing_schema) > 0 and str(existing_schema[0].name) == str(schema_name): raise exception.DatabaseAlreadyExists(name=schema_name) return client.create_database(schemas)
def check_rpl_delay(slave_id): LOG.info("\n\n\n============= check rpl delay ====================== %s" % slave_id) guestagent_api = create_guest_client(get_context(),slave_id) _obj = guestagent_api.ksc_rpl_delay() if int(_obj) <=1: LOG.info('master-slave rpl ok') else: msg = 'master-slave rpl occur delay' raise Exception(msg)
def guest_log_list(self, req, tenant_id, id): """Return all information about all logs for an instance.""" LOG.debug("Listing logs for tenant %s" % tenant_id) context = req.environ[wsgi.CONTEXT_KEY] instance = models.Instance.load(context, id) if not instance: raise exception.NotFound(uuid=id) client = create_guest_client(context, id) guest_log_list = client.guest_log_list() return wsgi.Result({'logs': guest_log_list}, 200)
def load(cls, context, instance_id, username, hostname): load_and_verify(context, instance_id) validate = guest_models.MySQLUser() validate.name = username validate.host = hostname client = create_guest_client(context, instance_id) found_user = client.get_user(username=username, hostname=hostname) if not found_user: return None database_names = [{"name": db["_name"]} for db in found_user["_databases"]] return cls(found_user["_name"], found_user["_host"], found_user["_password"], database_names)
def change_password(cls, context, instance_id, users): load_and_verify(context, instance_id) client = create_guest_client(context, instance_id) change_users = [] for user in users: change_user = {'name': user.name, 'host': user.host, 'password': user.password, } change_users.append(change_user) client.change_passwords(change_users)
def create(cls, context, instance_id, schemas): load_and_verify(context, instance_id) client = create_guest_client(context, instance_id) for schema in schemas: schema_name = schema['_name'] existing_schema, _nadda = Schemas.load_with_client( client, limit=1, marker=schema_name, include_marker=True) if (len(existing_schema) > 0 and str(existing_schema[0].name) == str(schema_name)): raise exception.DatabaseAlreadyExists(name=schema_name) return client.create_database(schemas)
def load(cls, context, instance_id, username, hostname): load_and_verify(context, instance_id) validate = guest_models.MySQLUser() validate.name = username validate.host = hostname client = create_guest_client(context, instance_id) found_user = client.get_user(username=username, hostname=hostname) if not found_user: return None database_names = [{ 'name': db['_name'] } for db in found_user['_databases']] return cls(found_user['_name'], found_user['_host'], found_user['_password'], database_names)
def check_rpl_consist(master_id,slave_ids,master_ip,slave_ips): global is_check_rpl_consit if is_check_rpl_consit: LOG.info("\n\n\n============= check rpl consistense ======================") username = "******" password = "******" guestagent_api = create_guest_client(get_context(),master_id) guestagent_api.admin_create_replica_checking_user(chkUser = username, chkPsw = password) for _id in slave_ids: guestagent_api = create_guest_client(get_context(),_id) guestagent_api.admin_create_replica_checking_user(chkUser = username, chkPsw = password) #print _obj mysqlip = "mysql://%s:%s@%s:3306/" rplcheck = ReplicaChecker( mysqlip % (username,password,master_ip), [mysqlip % (username,password,ip) for ip in slave_ips]) _ret_dict = rplcheck.replica_consistence_check_by_master_slave() for k,v in _ret_dict.iteritems(): LOG.info("%s %s" % (k,v)) for k,v in _ret_dict.iteritems(): if v>0: raise Exception("replica consistence find error on %s" % k)
def guest_log_action(self, req, body, tenant_id, id): """Processes a guest log.""" LOG.info(_("Processing log for tenant %s"), tenant_id) context = req.environ[wsgi.CONTEXT_KEY] log_name = body['name'] enable = body.get('enable', None) disable = body.get('disable', None) publish = body.get('publish', None) discard = body.get('discard', None) if enable and disable: raise exception.BadRequest(_("Cannot enable and disable log.")) client = create_guest_client(context, id) guest_log = client.guest_log_action(log_name, enable, disable, publish, discard) return wsgi.Result({'log': guest_log}, 200)
def update_all(self, context): num_i = len(self.instances) LOG.debug("Host %s has %s instances to update" % (self.name, num_i)) failed_instances = [] for instance in self.instances: client = create_guest_client(context, instance['id']) try: client.update_guest() except exception.TroveError as re: LOG.error(re) LOG.error("Unable to update instance: %s" % instance['id']) failed_instances.append(instance['id']) if len(failed_instances) > 0: msg = "Failed to update instances: %s" % failed_instances raise exception.UpdateGuestError(msg)
def module_remove(self, req, tenant_id, id, module_id): """Remove module from an instance.""" context = req.environ[wsgi.CONTEXT_KEY] instance = models.Instance.load(context, id) if not instance: raise exception.NotFound(uuid=id) module = module_models.Module.load(context, module_id) module_info = module_views.DetailedModuleView(module).data() client = create_guest_client(context, id) client.module_remove(module_info) instance_module = module_models.InstanceModule.load( context, instance_id=id, module_id=module_id) if instance_module: module_models.InstanceModule.delete(context, instance_module) return wsgi.Result(None, 200)
def create(cls, context, instance_id, users): # Load InstanceServiceStatus to verify if it's running load_and_verify(context, instance_id) client = create_guest_client(context, instance_id) for user in users: user_name = user["_name"] host_name = user["_host"] userhost = "%s@%s" % (user_name, host_name) existing_users, _nadda = Users.load_with_client(client, limit=1, marker=userhost, include_marker=True) if ( len(existing_users) > 0 and str(existing_users[0].name) == str(user_name) and str(existing_users[0].host) == str(host_name) ): raise exception.UserAlreadyExists(name=user_name, host=host_name) return client.create_user(users)
def create(cls, context, instance_id, users): # Load InstanceServiceStatus to verify if it's running load_and_verify(context, instance_id) client = create_guest_client(context, instance_id) for user in users: user_name = user['_name'] host_name = user['_host'] userhost = "%s@%s" % (user_name, host_name) existing_users, _nadda = Users.load_with_client( client, limit=1, marker=userhost, include_marker=True) if (len(existing_users) > 0 and str(existing_users[0].name) == str(user_name) and str(existing_users[0].host) == str(host_name)): raise exception.UserAlreadyExists(name=user_name, host=host_name) return client.create_user(users)
def update_attributes(cls, context, instance_id, username, hostname, user_attrs): load_and_verify(context, instance_id) client = create_guest_client(context, instance_id) user_name = user_attrs.get('name') host_name = user_attrs.get('host') user = user_name or username host = host_name or hostname userhost = "%s@%s" % (user, host) if user_name or host_name: existing_users, _nadda = Users.load_with_client( client, limit=1, marker=userhost, include_marker=True) if (len(existing_users) > 0 and existing_users[0].name == user and existing_users[0].host == host): raise exception.UserAlreadyExists(name=user, host=host) client.update_attributes(username, hostname, user_attrs)
def module_apply(self, req, body, tenant_id, id): """Apply modules to an instance.""" context = req.environ[wsgi.CONTEXT_KEY] instance = models.Instance.load(context, id) if not instance: raise exception.NotFound(uuid=id) self.authorize_instance_action(context, 'module_apply', instance) module_ids = [mod['id'] for mod in body.get('modules', [])] modules = module_models.Modules.load_by_ids(context, module_ids) models.validate_modules_for_apply( modules, instance.datastore.id, instance.datastore_version.id) module_list = module_views.get_module_list(modules) client = create_guest_client(context, id) result_list = client.module_apply(module_list) models.Instance.add_instance_modules(context, id, modules) return wsgi.Result({'modules': result_list}, 200)
def module_apply(self, req, body, tenant_id, id): """Apply modules to an instance.""" context = req.environ[wsgi.CONTEXT_KEY] instance = models.Instance.load(context, id) if not instance: raise exception.NotFound(uuid=id) self.authorize_instance_action(context, 'module_apply', instance) module_ids = [mod['id'] for mod in body.get('modules', [])] modules = module_models.Modules.load_by_ids(context, module_ids) models.validate_modules_for_apply(modules, instance.datastore.id, instance.datastore_version.id) module_list = module_views.get_module_list(modules) client = create_guest_client(context, id) result_list = client.module_apply(module_list) models.Instance.add_instance_modules(context, id, modules) return wsgi.Result({'modules': result_list}, 200)
def guest_log_list(self, req, tenant_id, id): """Return all information about all logs for an instance.""" LOG.debug("Listing logs for tenant %s", tenant_id) context = req.environ[wsgi.CONTEXT_KEY] try: backup_model.verify_swift_auth_token(context) except exception.SwiftNotFound: raise exception.LogsNotAvailable() instance = models.Instance.load(context, id) if not instance: raise exception.NotFound(uuid=id) self.authorize_instance_action(context, 'guest_log_list', instance) client = create_guest_client(context, id) guest_log_list = client.guest_log_list() return wsgi.Result({'logs': guest_log_list}, 200)
def update_all(self, context): num_i = len(self.instances) LOG.debug("Host %(name)s has %(num)s instances to update.", { 'name': self.name, 'num': num_i }) failed_instances = [] for instance in self.instances: client = create_guest_client(context, instance['id']) try: client.update_guest() except exception.TroveError as re: LOG.error(re) LOG.error(_("Unable to update instance: %s."), instance['id']) failed_instances.append(instance['id']) if len(failed_instances) > 0: msg = _("Failed to update instances: %s.") % failed_instances raise exception.UpdateGuestError(msg)
def module_remove(self, req, tenant_id, id, module_id): """Remove module from an instance.""" context = req.environ[wsgi.CONTEXT_KEY] instance = models.Instance.load(context, id) if not instance: raise exception.NotFound(uuid=id) self.authorize_instance_action(context, 'module_remove', instance) module = module_models.Module.load(context, module_id) module_info = module_views.DetailedModuleView(module).data() client = create_guest_client(context, id) client.module_remove(module_info) instance_modules = module_models.InstanceModules.load_all( context, instance_id=id, module_id=module_id) for instance_module in instance_modules: module_models.InstanceModule.delete(context, instance_module) LOG.debug("Deleted IM record %s (instance %s, module %s)." % (instance_module.id, id, module_id)) return wsgi.Result(None, 200)
def load(cls, context, instance_id, username, hostname, root_user=False): load_and_verify(context, instance_id) if root_user: validate = guest_models.RootUser() else: validate = guest_models.MySQLUser() validate.name = username validate.host = hostname client = create_guest_client(context, instance_id) found_user = client.get_user(username=username, hostname=hostname) if not found_user: return None database_names = [{'name': db['_name']} for db in found_user['_databases']] return cls(found_user['_name'], found_user['_host'], found_user['_password'], database_names)
def module_apply(self, req, body, tenant_id, id): """Apply modules to an instance.""" context = req.environ[wsgi.CONTEXT_KEY] instance = models.Instance.load(context, id) if not instance: raise exception.NotFound(uuid=id) module_ids = [mod['id'] for mod in body.get('modules', [])] modules = module_models.Modules.load_by_ids(context, module_ids) module_list = [] for module in modules: module.contents = module_models.Module.deprocess_contents( module.contents) module_info = module_views.DetailedModuleView(module).data( include_contents=True) module_list.append(module_info) client = create_guest_client(context, id) result_list = client.module_apply(module_list) models.Instance.add_instance_modules(context, id, modules) return wsgi.Result({'modules': result_list}, 200)
def guest_log_action(self, req, body, tenant_id, id): """Processes a guest log.""" LOG.info("Processing log for tenant %s", tenant_id) context = req.environ[wsgi.CONTEXT_KEY] try: backup_model.verify_swift_auth_token(context) except exception.SwiftNotFound: raise exception.LogsNotAvailable() instance = models.Instance.load(context, id) if not instance: raise exception.NotFound(uuid=id) log_name = body['name'] enable = body.get('enable', None) disable = body.get('disable', None) publish = body.get('publish', None) discard = body.get('discard', None) if enable and disable: raise exception.BadRequest(_("Cannot enable and disable log.")) client = create_guest_client(context, id) guest_log = client.guest_log_action(log_name, enable, disable, publish, discard) return wsgi.Result({'log': guest_log}, 200)
def get_auth_password(cls, context, instance_id): load_and_verify(context, instance_id) password = create_guest_client(context, instance_id).get_root_password() return password
def delete(cls, context, instance_id): load_and_verify(context, instance_id) create_guest_client(context, instance_id).disable_root()
def _module_list(self, context, id, include_contents): """Return information about instnace modules.""" client = create_guest_client(context, id) result_list = client.module_list(include_contents) return wsgi.Result({'modules': result_list}, 200)
def delete(cls, context, instance_id, user): load_and_verify(context, instance_id) with StartNotification(context, instance_id=instance_id, username=user): create_guest_client(context, instance_id).delete_user(user)
def delete(cls, context, instance_id, user): load_and_verify(context, instance_id) create_guest_client(context, instance_id).delete_user(user)
def grant(cls, context, instance_id, username, hostname, databases): load_and_verify(context, instance_id) client = create_guest_client(context, instance_id) client.grant_access(username, hostname, databases)
def fake_deleted_instance_delete(cls, context, instance_id): base_msg = " instance_id: %s " % instance_id success = True msg = " fake_deleted_instance_delete %s " % base_msg deleted_at = utils.utcnow() db_info = None try: db_info = DBInstance.find_by(context=context, id=instance_id, task_id=InstanceTasks.FAKE_DELETED.code, deleted=True) db_info.update(task_status=InstanceTasks.DELETING) LOG.debug("fake_deleted_instance_delete, load instance ok, %s " % base_msg) except Exception: LOG.debug("fake_deleted_instance_delete failed, deleted instance not found, %s " % base_msg) if db_info is None: success = False msg = " fake_deleted_instance_delete failed, load instance error %s " % base_msg return success, msg try: server = load_server(context, db_info.id, db_info.compute_instance_id) LOG.debug("fake_deleted_instance_delete, load server: %s ok, %s ", db_info.compute_instance_id, base_msg) nova_client = create_nova_client(context) def server_is_finished(): try: server_id = db_info.compute_instance_id _server = nova_client.servers.get(server_id) if _server.status not in ['SHUTDOWN', 'ACTIVE']: _msg = "Server %s got into %s status during delete " \ "of instance %s!" % (server.id, server.status, instance_id) LOG.error(_msg) return False except nova_exceptions.NotFound: return True try: LOG.debug("Delete compute server %s" % server.id) server.delete() poll_until(server_is_finished, sleep_time=1, time_out=CONF.server_delete_time_out) guest = create_guest_client(context, db_info.id) guest.delete_queue() LOG.debug("fake_deleted_instance_delete, delete server: %s ok, %s ", db_info.compute_instance_id, base_msg) except Exception as ex: LOG.error(utils.get_traceback_stack()) success = False msg += " ,deleted server error, compute_instance_id: %s, ex:%s, %s " \ % (db_info.compute_instance_id, str(ex), base_msg) except Exception as ex: LOG.error("COMPUTE ID = %s" % db_info.compute_instance_id) success = False msg += " ,load server error, compute_instance_id: %s, %s " % (db_info.compute_instance_id, base_msg) if CONF.trove_vip_support: try: db_info.update(task_status=InstanceTasks.RELEASE_VIP) instance_vip = DBInstanceVip.find_by(context, instance_id=instance_id, deleted=False) vip_info = DBVips.find_by(context, id=instance_vip.vip_id, deleted=False) InstanceVip.release_vip(context, vip_info.vip) LOG.debug("fake_deleted_instance_delete, release_vip: %s ok, %s " % (vip_info.vip, base_msg)) except Exception as ex: LOG.error(utils.get_traceback_stack()) success = False msg += " ,release_vip error, ex:%s, %s " % (str(ex), base_msg) if CONF.trove_security_groups_support: db_info.update(task_status=InstanceTasks.DELETEING_SECURITY_GROUP) try: SecurityGroup.delete_for_group(db_info.group_id, context) LOG.debug( "fake_deleted_instance_delete, delete SecurityGroup: %s ok, %s " % (db_info.group_id, base_msg)) except Exception as ex: LOG.error(utils.get_traceback_stack()) success = False msg += " ,delete SecurityGroup error, ex:%s, %s " % (str(ex), base_msg) db_info.update(deleted_at=deleted_at, task_status=InstanceTasks.NONE) if success is True: msg = "fake_deleted_instance_delete finished, %s " % base_msg return success, msg
def revoke(cls, context, instance_id, username, hostname, database): load_and_verify(context, instance_id) client = create_guest_client(context, instance_id) client.revoke_access(username, hostname, database)
def get_guest(cls, instance): return remote.create_guest_client(instance.context, instance.db_info.id, instance.datastore_version.manager)
def get_guest(self): return create_guest_client(self.context, self.db_info.id)
def _create_replication_slave(self, context, instance_id, name, flavor, image_id, databases, users, datastore_manager, packages, volume_size, availability_zone, root_password, nics, overrides, slave_of_id, backup_id, volume_type, modules): if type(instance_id) in [list]: ids = instance_id root_passwords = root_password else: ids = [instance_id] root_passwords = [root_password] replica_number = 0 replica_backup_id = backup_id replica_backup_created = False replicas = [] master_instance_tasks = BuiltInstanceTasks.load(context, slave_of_id) server_group = master_instance_tasks.server_group scheduler_hints = srv_grp.ServerGroup.convert_to_hint(server_group) LOG.debug("Using scheduler hints for locality: %s" % scheduler_hints) try: for replica_index in range(0, len(ids)): try: replica_number += 1 LOG.debug("Creating replica %d of %d." % (replica_number, len(ids))) instance_tasks = FreshInstanceTasks.load( context, ids[replica_index]) snapshot = instance_tasks.get_replication_master_snapshot( context, slave_of_id, flavor, replica_backup_id, replica_number=replica_number) replica_backup_id = snapshot['dataset']['snapshot_id'] replica_backup_created = (replica_backup_id is not None) instance_tasks.create_instance( flavor, image_id, databases, users, datastore_manager, packages, volume_size, replica_backup_id, availability_zone, root_passwords[replica_index], nics, overrides, None, snapshot, volume_type, modules, scheduler_hints) replicas.append(instance_tasks) except Exception: # if it's the first replica, then we shouldn't continue LOG.exception( _("Could not create replica %(num)d of %(count)d.") % { 'num': replica_number, 'count': len(ids) }) if replica_number == 1: raise for replica in replicas: replica.wait_for_instance(CONF.restore_usage_timeout, flavor) # Some datastores requires completing configuration of replication # nodes with information that is only available after all the # instances has been started. if (master_instance_tasks.post_processing_required_for_replication( )): slave_instances = [ BuiltInstanceTasks.load(context, slave.id) for slave in master_instance_tasks.slaves ] # Collect info from each slave post instance launch slave_detail = [ slave_instance.get_replication_detail() for slave_instance in slave_instances ] # Pass info of all replication nodes to the master for # replication setup completion master_detail = master_instance_tasks.get_replication_detail() master_instance_tasks.complete_master_setup(slave_detail) # Pass info of all replication nodes to each slave for # replication setup completion for slave_instance in slave_instances: slave_instance.complete_slave_setup( master_detail, slave_detail) # Push pending data/transactions from master to slaves master_instance_tasks.sync_data_to_slaves() # Set the status of all slave nodes to ACTIVE for slave_instance in slave_instances: slave_guest = remote.create_guest_client( slave_instance.context, slave_instance.db_info.id, slave_instance.datastore_version.manager) slave_guest.cluster_complete() finally: if replica_backup_created: Backup.delete(context, replica_backup_id)
def delete(cls, context, instance_id, schema): load_and_verify(context, instance_id) create_guest_client(context, instance_id).delete_database(schema)