def create(self, description=None): cinder = get_cinder() snapshot_ids = [] for vol in self.addon.real_addon.get_plugin_volumes(): name = vol.split(":")[0] try: volume = cinder.volumes.find(name=name) except Exception as e: LOG.error(str(e)) else: snapshot_name = "{0}-snapshot-s{1}".format( volume.name, self.short_id) snapshot_description = "snapshot at {0}".format(now()) if description: snapshot_description = "{0} for {1}".format( snapshot_description, description) snapshot = cinder.volume_snapshots.create( volume.id, force=True, name=snapshot_name, description=snapshot_description) snapshot_ids.append(snapshot.id) if snapshot_ids: self.snapshot_ids = ','.join(snapshot_ids) self.save()
def recover_volumes(addon): cinder = get_cinder() for vol in addon.real_addon.get_plugin_volumes(): name = vol.split(":")[0] try: volume = cinder.volumes.find(name=name) volume.delete() except Exception, e: logger.error(str(e))
def exists_volume(self, name, storage=False): cinder = get_cinder() try: volumes = cinder.volumes.list(search_opts={"name": name}) except Exception as e: volumes = None LOG.error(str(e)) if volumes and storage: volume_ids = [volume.id for volume in volumes] self.storage_volume_ids(volume_ids=volume_ids) return True if volumes else False
def restore(self, snapshot_ids=None, run_async=True): cinder = get_cinder() addon = self.addon.real_addon snapshot_ids = snapshot_ids if snapshot_ids else self.snapshot_ids snapshot_ids = snapshot_ids.split(",") volume_ids = [] success_ids = [] for snapshot_id in snapshot_ids: try: snapshot = cinder.volume_snapshots.get(snapshot_id) name = snapshot.name.split("-snapshot-s")[0] volume = cinder.volumes.create(addon.volume_size, name=name, snapshot_id=snapshot_id) volume_ids.append(volume.id) success_ids.append(snapshot_id) except Exception as e: LOG.error(str(e)) if not run_async: while True: if not addon.check_volume_status( volume_ids=",".join(volume_ids)): time.sleep(1) continue break if len(snapshot_ids) == len(volume_ids): if self.volume_ids: self.volume_ids = str(self.volume_ids) + "," + str( addon.volume_ids) else: self.volume_ids = addon.volume_ids self.save() addon.volume_ids = ",".join(volume_ids) addon.save() retries = 5 while retries: try: create_or_update_marathon_app(addon) except MarathonHttpError as e: retries -= 1 LOG.error(str(e)) time.sleep(1) continue else: return True addon.status = Addon.STATUS.Failed addon.save() return False lost_ids = list(set(snapshot_ids) - set(success_ids)) return ",".join(lost_ids)
def destroy(self): cinder = get_cinder() for snapshot_id in self.snapshot_ids.split(','): try: snapshot = cinder.volume_snapshots.get(snapshot_id) except Exception as e: LOG.error(str(e)) else: cinder.volume_snapshots.delete(snapshot) self.addon.real_addon.delete_volume(self.volume_ids) self.delete()
def check_volume_ids(self, coverage=False): cinder = get_cinder() volume_ids = self.volume_ids.split(",") expect_ids = [] for vol in self.real_addon.get_plugin_volumes(): name = vol.split(":")[0] volumes = cinder.volumes.list(search_opts={"name": name}) expect_ids = expect_ids + [v.id for v in volumes] if len(set(volume_ids + expect_ids)) == len(volume_ids): return True elif coverage: self.volume_ids = ",".join(expect_ids) self.save() return False
def create_volume(self): cinder = get_cinder() volume_ids = [] for vol in self.real_addon.get_plugin_volumes(): name = vol.split(":")[0] if self.exists_volume(name, storage=True): continue try: volume = cinder.volumes.create(self.volume_size, name=name) volume_ids.append(volume.id) except Exception as e: LOG.error(str(e)) self.storage_volume_ids(volume_ids=volume_ids)
def rename_volume(self, suffix=""): cinder = get_cinder() volume_ids = self.volume_ids.split(",") success_ids = [] for volume_id in volume_ids: try: volume = cinder.volumes.get(volume_id) name = volume.name + "." + suffix + ".history" if suffix else volume.name + ".history" if not volume.name.endswith(".history"): volume.update(name=name) success_ids.append(volume.id) except Exception as e: LOG.error(str(e)) if len(volume_ids) == len(success_ids): return True return False
def check_volume_status(self, volume_ids=None): cinder = get_cinder() volume_ids = volume_ids if volume_ids else self.volume_ids volume_ids = volume_ids.split(",") success_ids = [] for volume_id in volume_ids: try: volume = cinder.volumes.get(volume_id) if volume.status == "available": success_ids.append(volume.id) except Exception as e: LOG.error(str(e)) if len(volume_ids) == len(success_ids): return True return False
def check_volume_attach(self, volume_ids=None): cinder = get_cinder() volume_ids = volume_ids if volume_ids else self.volume_ids volume_ids = volume_ids.split(",") success_ids = [] for volume_id in volume_ids: try: volume = cinder.volumes.get(volume_id) if not volume.multiattach: success_ids.append(volume.id) except Exception as e: LOG.error(str(e)) if len(volume_ids) == len(success_ids): return False return True
def delete_volume(self, volumes_ids=""): cinder = get_cinder() volume_ids = volumes_ids if volumes_ids else self.volume_ids volume_ids = volume_ids.split(",") success_ids = [] for volume_id in volume_ids: try: volume = cinder.volumes.get(volume_id) volume.delete() success_ids.append(volume.id) except Exception as e: LOG.error(str(e)) if not volume_ids and len(volume_ids) == len(success_ids): self.volume_ids = "" self.save() return True return False