def index(self, req): """Return a list of existing file shares.""" filesystems = self.fs_driver.list_fs() # Only return filesystems in the db. context = req.environ['nova.context'] if self.has_db_support: db_list = sharedfs_db.filesystem_list(context) fs_list = [] for fs in filesystems: name = fs.get('name') if name in db_list: db_entry = sharedfs_db.filesystem_get(context, name) fs_list.append({'name': fs.get('name'), 'size': fs.get('size'), 'scope': db_entry.get('scope'), 'project': db_entry.get('project_id')}) db_list.remove(name) else: LOG.warn(_("Found filesystem %s that is not recored " "in the database. Ignoring.") % name) if db_list: LOG.warn(_("Possible database integrity issue. The following " "filesystems are recorded in the database but cannot " "be located: %s") % db_list) else: fs_list = [{'name': fs.get('name'), 'size': fs.get('size'), 'scope': 'unknown', 'project': 'unknown'} for fs in filesystems] return _translate_fs_entries_view(fs_list)
def notify(self, message): event_type = message.get('event_type') if event_type not in ['compute.instance.delete.start', 'compute.instance.create.end']: return payload = message['payload'] instance = payload['instance_id'] tenant = payload['tenant_id'] user = payload['user_id'] ctxt = context.RequestContext(user, tenant) # Find all global scope filesystems # and all project-scope systems that are # associated with this project. fs_list = [] db_fs_list = sharedfs_db.filesystem_list(ctxt) for fs_name in db_fs_list: fs = sharedfs_db.filesystem_get(ctxt, fs_name) if not fs: LOG.debug(_("Database inconsistency: no record for FS %s") % fs_name) continue if fs.scope == 'global': fs_list.append(fs_name) elif fs.scope == 'project' and fs.project_id == tenant: fs_list.append(fs_name) for fs_name in fs_list: if event_type == 'compute.instance.delete.start': self.unattach(ctxt, instance, fs_name) elif event_type == 'compute.instance.create.end': self.attach(ctxt, instance, fs_name)
def delete(self, req, id): """Delete the filesystem identified by id.""" name = id if self.has_db_support: # Unattach global or project-wide shares immediately. context = req.environ['nova.context'] fs_entry = sharedfs_db.filesystem_get(context, name) if not fs_entry: msg = _("Filesystem %s not found.") % name raise webob.exc.HTTPNotFound(msg) scope = fs_entry.scope project = fs_entry.project_id instance_list = [] if scope == 'global': instance_list = db.instance_get_all(context) elif scope == 'project': instance_list = db.instance_get_all_by_project(context, project) for instance in instance_list: try: fixed_ips = db.fixed_ip_get_by_instance(context, instance.id) for ip in fixed_ips: LOG.debug(_("unattaching %(ip)s from fs %(fs)s.") % {'ip': ip['address'], 'fs': name}) try: self.fs_driver.unattach(name, ip['address']) except exception.NotAuthorized: LOG.warning(_("Insufficient permission to unattach" " %(instance)s from filesystem %(fs)s.") % {'instance': instance.name, 'fs': name}) except exception.FixedIpNotFound: LOG.warning(_("Unable to get IP address for %s.") % instance.id) sharedfs_db.filesystem_delete(context, name) try: self.fs_driver.delete_fs(name, project) except exception.NotAuthorized: msg = _("Filesystem deletion requires admin permissions.") raise webob.exc.HTTPForbidden(msg) except exception.NotFound: msg = _("Filesystem %s does not exist.") % name raise webob.exc.HTTPNotFound(msg) return webob.Response(status_int=202)