From 2ab45887ea86223854fd1a74b886254a9042fecb Mon Sep 17 00:00:00 2001 From: Craig Vyvial Date: Tue, 1 May 2012 13:21:22 -0500 Subject: [PATCH] clean up and change the volume desription to have the instance id fake mode works with all the tests mounting a volume is causing a database name of #mysql50#lost+found to be created pep8 fixes --- etc/reddwarf/reddwarf.conf.sample | 2 +- etc/reddwarf/reddwarf.conf.test | 2 +- reddwarf/extensions/mgmt.py | 3 +- reddwarf/extensions/mgmt/service.py | 1 - reddwarf/extensions/mgmt/views.py | 1 + reddwarf/guestagent/dbaas.py | 5 +- reddwarf/instance/models.py | 21 +++--- reddwarf/tests/fakes/nova.py | 111 ++++++++++++++++++++++++++-- 8 files changed, 123 insertions(+), 23 deletions(-) diff --git a/etc/reddwarf/reddwarf.conf.sample b/etc/reddwarf/reddwarf.conf.sample index acc09bf447..69a1a69f0c 100644 --- a/etc/reddwarf/reddwarf.conf.sample +++ b/etc/reddwarf/reddwarf.conf.sample @@ -53,7 +53,7 @@ nova_service_name = Compute Service add_addresses = True # Config options for enabling volume service -reddwarf_volume_support = False +reddwarf_volume_support = True nova_volume_service_type = volume nova_volume_service_name = Volume Service device_path = /dev/vdb diff --git a/etc/reddwarf/reddwarf.conf.test b/etc/reddwarf/reddwarf.conf.test index 301751b523..374dd9a137 100644 --- a/etc/reddwarf/reddwarf.conf.test +++ b/etc/reddwarf/reddwarf.conf.test @@ -56,7 +56,7 @@ nova_service_name = Compute Service add_addresses = True # Config options for enabling volume service -reddwarf_volume_support = False +reddwarf_volume_support = True nova_volume_service_type = volume nova_volume_service_name = Volume Service device_path = /dev/vdb diff --git a/reddwarf/extensions/mgmt.py b/reddwarf/extensions/mgmt.py index c16127ac86..e76f5e0082 100644 --- a/reddwarf/extensions/mgmt.py +++ b/reddwarf/extensions/mgmt.py @@ -52,5 +52,4 @@ class Mgmt(extensions.ExtensionsDescriptor): deserializer=wsgi.RequestDeserializer(), serializer=serializer) resources.append(resource) - - return resources \ No newline at end of file + return resources diff --git a/reddwarf/extensions/mgmt/service.py b/reddwarf/extensions/mgmt/service.py index 6adbdb2526..b23a6ad4b9 100644 --- a/reddwarf/extensions/mgmt/service.py +++ b/reddwarf/extensions/mgmt/service.py @@ -56,4 +56,3 @@ class MgmtInstanceController(InstanceController): return wsgi.Result(str(e), 404) return wsgi.Result(views.InstanceView(server, add_addresses=self.add_addresses).data(), 200) - diff --git a/reddwarf/extensions/mgmt/views.py b/reddwarf/extensions/mgmt/views.py index 577b3deaf8..ec276c8e2b 100644 --- a/reddwarf/extensions/mgmt/views.py +++ b/reddwarf/extensions/mgmt/views.py @@ -45,6 +45,7 @@ class InstanceView(object): instance_dict['ip'] = ip return {"instance": instance_dict} + class InstancesView(InstanceView): def __init__(self, instances, add_addresses=False): diff --git a/reddwarf/guestagent/dbaas.py b/reddwarf/guestagent/dbaas.py index e8189f6809..7a5bca84ab 100644 --- a/reddwarf/guestagent/dbaas.py +++ b/reddwarf/guestagent/dbaas.py @@ -411,7 +411,8 @@ class MySqlAdmin(object): information_schema.schemata WHERE schema_name not in - ('mysql', 'information_schema', 'lost+found') + ('mysql', 'information_schema', + 'lost+found', '#mysql50#lost+found') ORDER BY schema_name ASC; ''') @@ -503,7 +504,7 @@ class DBaaSAgent(object): # status end_mysql_install set with install_and_secure() app = MySqlApp(self.status) restart_mysql = False - if not device_path is None: + if device_path: VolumeHelper.format(device_path) if app.is_installed(pkg): #stop and do not update database diff --git a/reddwarf/instance/models.py b/reddwarf/instance/models.py index b8709cf56b..97fb9602f6 100644 --- a/reddwarf/instance/models.py +++ b/reddwarf/instance/models.py @@ -64,15 +64,14 @@ def load_volumes(context, server_id, client=None): volume_client = create_nova_volume_client(context) try: volumes = [] - if utils.bool_from_string(volume_support): - volumes_info = client.volumes.get_server_volumes(server_id) - volume_ids = [attachments.volumeId for attachments in - volumes_info] - for volume_id in volume_ids: - volume_info = volume_client.volumes.get(volume_id) - volume = {'id': volume_info.id, - 'size': volume_info.size} - volumes.append(volume) + volumes_info = client.volumes.get_server_volumes(server_id) + volume_ids = [attachments.volumeId for attachments in + volumes_info] + for volume_id in volume_ids: + volume_info = volume_client.volumes.get(volume_id) + volume = {'id': volume_info.id, + 'size': volume_info.size} + volumes.append(volume) except nova_exceptions.NotFound, e: LOG.debug("Could not find nova server_id(%s)" % server_id) raise rd_exceptions.VolumeAttachmentsNotFound(server_id=server_id) @@ -174,11 +173,11 @@ class Instance(object): @classmethod def _create_volume(cls, context, db_info, volume_size): volume_support = config.Config.get("reddwarf_volume_support", 'False') - LOG.debug(_("Volume support = %s") % volume_support) + LOG.debug(_("reddwarf volume support = %s") % volume_support) if utils.bool_from_string(volume_support): LOG.debug(_("Starting to create the volume for the instance")) volume_client = create_nova_volume_client(context) - volume_desc = ("mysql volume for %s" % context.tenant) + volume_desc = ("mysql volume for %s" % db_info.id) volume_ref = volume_client.volumes.create( volume_size, display_name="mysql-%s" % db_info.id, diff --git a/reddwarf/tests/fakes/nova.py b/reddwarf/tests/fakes/nova.py index 9aa2981f7d..170e307ea8 100644 --- a/reddwarf/tests/fakes/nova.py +++ b/reddwarf/tests/fakes/nova.py @@ -84,7 +84,8 @@ class FakeFlavors(object): class FakeServer(object): - def __init__(self, parent, owner, id, name, image_id, flavor_ref): + def __init__(self, parent, owner, id, name, image_id, flavor_ref, + block_device_mapping): self.owner = owner # This is a context. self.id = id self.parent = parent @@ -93,6 +94,8 @@ class FakeServer(object): self.flavor_ref = flavor_ref self.events = EventSimulator() self.schedule_status("BUILD", 0.0) + LOG.debug("block_device_mapping = %s" % block_device_mapping) + self.block_device_mapping = block_device_mapping @property def addresses(self): @@ -153,17 +156,20 @@ class FakeServers(object): return self.context.is_admin or \ server.owner.tenant == self.context.tenant - def create(self, name, image_id, flavor_ref, files): + def create(self, name, image_id, flavor_ref, files, block_device_mapping): id = "FAKE_%d" % self.next_id self.next_id += 1 - server = FakeServer(self, self.context, id, name, image_id, flavor_ref) + server = FakeServer(self, self.context, id, name, image_id, flavor_ref, + block_device_mapping) self.db[id] = server server.schedule_status("ACTIVE", 1) + LOG.info("FAKE_SERVERS_DB : %s" % str(FAKE_SERVERS_DB)) return server def get(self, id): if id not in self.db: - LOG.error("Couldn't find id %s, collection=%s" % (id, self.db)) + LOG.error("Couldn't find server id %s, collection=%s" % (id, + self.db)) raise nova_exceptions.NotFound(404, "Not found") else: if self.can_see(id): @@ -181,6 +187,25 @@ class FakeServers(object): self.events.add_event(time_from_now, delete_server) +class FakeServerVolumes(object): + + def __init__(self, context): + self.context = context + + def get_server_volumes(self, server_id): + class ServerVolumes(object): + def __init__(self, block_device_mapping): + LOG.debug("block_device_mapping = %s" % block_device_mapping) + device = block_device_mapping['vdb'] + (self.volumeId, + self.type, + self.size, + self.delete_on_terminate) = device.split(":") + fake_servers = FakeServers(self.context, FLAVORS) + server = fake_servers.get(server_id) + return [ServerVolumes(server.block_device_mapping)] + + FLAVORS = FakeFlavors() @@ -190,11 +215,87 @@ class FakeClient(object): self.context = context self.flavors = FLAVORS self.servers = FakeServers(context, self.flavors) + self.volumes = FakeServerVolumes(context) def fake_create_nova_client(context): return FakeClient(context) +class FakeVolume(object): + + def __init__(self, parent, owner, id, size, display_name, + display_description): + self.parent = parent + self.owner = owner # This is a context. + self.id = id + self.size = size + self.display_name = display_name + self.display_description = display_description + self.events = EventSimulator() + self.schedule_status("BUILD", 0.0) + + def __repr__(self): + return ("FakeVolume(id=%s, size=%s, " + "display_name=%s, display_description=%s)") % (self.id, + self.size, self.display_name, self.display_description) + + def schedule_status(self, new_status, time_from_now): + """Makes a new status take effect at the given time.""" + def set_status(): + self._current_status = new_status + self.events.add_event(time_from_now, set_status) + + @property + def status(self): + return self._current_status + + +FAKE_VOLUMES_DB = {} + + +class FakeVolumes(object): + + def __init__(self, context): + self.context = context + self.db = FAKE_VOLUMES_DB + self.next_id = 10 + self.events = EventSimulator() + + def can_see(self, id): + """Can this FakeVolumes, with its context, see some resource?""" + server = self.db[id] + return self.context.is_admin or \ + server.owner.tenant == self.context.tenant + + def get(self, id): + if id not in self.db: + LOG.error("Couldn't find volume id %s, collection=%s" % (id, + self.db)) + raise nova_exceptions.NotFound(404, "Not found") + else: + if self.can_see(id): + return self.db[id] + else: + raise nova_exceptions.NotFound(404, "Bad permissions") + + def create(self, size, display_name=None, display_description=None): + id = "FAKE_VOL_%d" % self.next_id + self.next_id += 1 + volume = FakeVolume(self, self.context, id, size, display_name, + display_description) + self.db[id] = volume + volume.schedule_status("available", 2) + LOG.info("FAKE_VOLUMES_DB : %s" % FAKE_VOLUMES_DB) + return volume + + +class FakeVolumeClient(object): + + def __init__(self, context): + self.context = context + self.volumes = FakeVolumes(context) + + def fake_create_nova_volume_client(context): - return FakeClient(context) + return FakeVolumeClient(context)