def remove_host_resources(self, host_id): log.info("Removing resources for host %s, plugin %s" % (host_id, self._plugin_name)) # Stop the session, and block it from starting again with self._processing_lock: try: del self._sessions[host_id] except KeyError: log.warning("remove_host_resources: No session for host %s" % host_id) try: record = ResourceQuery().get_record_by_attributes( "linux", "PluginAgentResources", host_id=host_id, plugin_name=self._plugin_name) except StorageResourceRecord.DoesNotExist: pass else: self._resource_manager.global_remove_resource(record.id) log.info("AgentDaemon: finished removing resources for host %s" % host_id)
def setUp(self): super(TestCallbacks, self).setUp() import chroma_core.lib.storage_plugin.manager self.orig_manager = chroma_core.lib.storage_plugin.manager.storage_plugin_manager chroma_core.lib.storage_plugin.manager.storage_plugin_manager = ( chroma_core.lib.storage_plugin.manager.StoragePluginManager() ) from chroma_core.lib.storage_plugin.manager import storage_plugin_manager storage_plugin_manager._load_plugin(sys.modules[__name__], "test_mod", TestPlugin) from chroma_core.models import StorageResourceRecord resource_class, resource_class_id = storage_plugin_manager.get_plugin_resource_class( "test_mod", "TestGlobalResource" ) record, created = StorageResourceRecord.get_or_create_root(resource_class, resource_class_id, {"name": "test1"}) from chroma_core.lib.storage_plugin.query import ResourceQuery scannable_record = StorageResourceRecord.objects.get() self.scannable_resource = ResourceQuery().get_resource(scannable_record) self.scannable_global_id = scannable_record.pk self.resource_manager = mock.Mock(_sessions={}) self.plugin = TestPlugin(self.resource_manager, self.scannable_global_id) self.resource_manager._sessions[self.scannable_global_id] = PluginSession( self.plugin, self.scannable_global_id, 0 )
def get_list(self, request, **kwargs): if "ancestor_of" in request.GET: record = StorageResourceRecord.objects.get(id=request.GET["ancestor_of"]) ancestor_records = set(ResourceQuery().record_all_ancestors(record)) bundles = [self.build_bundle(obj=obj, request=request) for obj in ancestor_records] dicts = [self.full_dehydrate(bundle) for bundle in bundles] return self.create_response(request, {"meta": None, "objects": dicts}) else: return super(StorageResourceResource, self).get_list(request, **kwargs)
def __init__(self, resource_manager, scannable_id=None): from chroma_core.lib.storage_plugin.manager import storage_plugin_manager storage_plugin_manager.register_plugin(self) self._initialized = False self._resource_manager = resource_manager self._handle_lock = threading.Lock() self._instance_lock = threading.Lock() self._handle_counter = 0 self._index = ResourceIndex() self._scannable_id = scannable_id self._resource_lock = threading.Lock() self._delta_new_resources = [] self._delta_delete_local_resources = [] self._delta_delete_global_resources = [] self._alerts_lock = threading.Lock() self._delta_alerts = set() self._alerts = {} # Should changes to resources be delta'd so that only changes are reported to the resource manager. This # required because it may be that at boot time (for example) we want all the changes everytime but once the # system is quiescent we only want the deltas. self._calc_changes_delta = True self._session_open = False self._update_period = settings.PLUGIN_DEFAULT_UPDATE_PERIOD from chroma_core.lib.storage_plugin.query import ResourceQuery root_resource = ResourceQuery().get_resource(scannable_id) root_resource._handle = self._generate_handle() root_resource._handle_global = False self._root_resource = root_resource
def test_initial_host_lun(self): child_node_resource = self._make_local_resource( 'linux', 'LinuxDeviceNode', path="/dev/foobar", parents=[self.node_resource], host_id=self.host.id) self.resource_manager.session_open( self.plugin, self.scannable_resource_pk, [ self.scannable_resource, self.dev_resource, self.node_resource, child_node_resource ], 60) # Check we got a Volume and a VolumeNode self.assertEqual(Volume.objects.count(), 1) self.assertEqual(VolumeNode.objects.count(), 1) self.assertEqual(Volume.objects.get().label, self.dev_resource.get_label()) # Check the VolumeNode got the correct path self.assertEqual(VolumeNode.objects.get().path, "/dev/foobar") self.assertEqual(VolumeNode.objects.get().host, self.host) # Check the created Volume has a link back to the UnsharedDevice from chroma_core.lib.storage_plugin.query import ResourceQuery from chroma_core.models import StorageResourceRecord resource_record = StorageResourceRecord.objects.get( pk=self.scannable_resource_pk) dev_record = ResourceQuery().get_record_by_attributes('linux', 'ScsiDevice', serial="foobar") self.assertEqual(Volume.objects.get().storage_resource_id, dev_record.pk) self.assertEqual(Volume.objects.get().size, 4096) # Try closing and re-opening the session, this time without the resources, the Volume/VolumeNode objects # should be removed self.resource_manager.session_close(resource_record.pk) self.resource_manager.session_open(self.plugin, resource_record.pk, [self.scannable_resource], 60) self.assertEqual(VolumeNode.objects.count(), 0) self.assertEqual(Volume.objects.count(), 0)
def execute(self, *args, **kwargs): try: import pygraphviz as pgv except ImportError: print("This command requires pygraphviz") return if len(args) == 0: resources = ResourceQuery().get_all_resources() else: resources = [] def iterate(record): res = record.to_resource() resources.append(res) for p in record.parents.all(): p_res = iterate(p) res._parents.append(p_res) return res start_id = int(args[0]) start_record = StorageResourceRecord.objects.get(pk=start_id) iterate(start_record) G = pgv.AGraph(directed=True) for r in resources: G.add_node(r._handle, label="%s:%s:%s" % (r._handle, r._meta.label, r.get_label())) for r in resources: for p in r.get_parents(): G.add_edge(r._handle, p._handle) G.layout(prog="dot") output_file = "resources.png" G.draw(output_file) print("Wrote graph to %s" % output_file)
def setUp(self): super(TestAddRemove, self).setUp() import chroma_core.lib.storage_plugin.manager self.orig_manager = chroma_core.lib.storage_plugin.manager.storage_plugin_manager chroma_core.lib.storage_plugin.manager.storage_plugin_manager = chroma_core.lib.storage_plugin.manager.StoragePluginManager( ) from chroma_core.lib.storage_plugin.manager import storage_plugin_manager storage_plugin_manager._load_plugin(sys.modules[__name__], 'test_mod', TestPlugin) from chroma_core.models import StorageResourceRecord resource_class, resource_class_id = storage_plugin_manager.get_plugin_resource_class( 'test_mod', 'TestGlobalResource') record, created = StorageResourceRecord.get_or_create_root( resource_class, resource_class_id, {'name': 'test1'}) from chroma_core.lib.storage_plugin.query import ResourceQuery scannable_record = StorageResourceRecord.objects.get() self.scannable_resource = ResourceQuery().get_resource( scannable_record) self.scannable_global_id = scannable_record.pk
def dehydrate_alerts(self, bundle): return [ a.to_dict() for a in ResourceQuery().resource_get_alerts( bundle.obj.to_resource()) ]
def alert_message(self): from chroma_core.lib.storage_plugin.query import ResourceQuery class_name, instance_name = ResourceQuery( ).record_class_and_instance_string(self.storage_resource) return "Discovered %s '%s'" % (class_name, instance_name)
def alert_message(self): from chroma_core.lib.storage_plugin.query import ResourceQuery msg = ResourceQuery().record_alert_message(self.alert_item.pk, self.alert_class) return msg
def get_resource_graph(self, request, **kwargs): target = self.cached_obj_get(request=request, **self.remove_api_resource_names(kwargs)) from chroma_core.models import AlertState ancestor_records = set() parent_records = set() storage_alerts = set() lustre_alerts = set(AlertState.filter_by_item(target)) from collections import defaultdict rows = defaultdict(list) id_edges = [] for tm in target.managedtargetmount_set.all(): lustre_alerts |= set(AlertState.filter_by_item(tm)) lun_node = tm.volume_node if lun_node.storage_resource: parent_record = lun_node.storage_resource from chroma_core.lib.storage_plugin.query import ResourceQuery parent_records.add(parent_record) storage_alerts |= ResourceQuery().record_all_alerts( parent_record) ancestor_records |= set( ResourceQuery().record_all_ancestors(parent_record)) def row_iterate(parent_record, i): if not parent_record in rows[i]: rows[i].append(parent_record) for p in parent_record.parents.all(): id_edges.append((parent_record.id, p.id)) row_iterate(p, i + 1) row_iterate(parent_record, 0) for i in range(0, len(rows) - 1): this_row = rows[i] next_row = rows[i + 1] def nextrow_affinity(obj): # if this has a link to anything in the next row, what # index in the next row? for j in range(0, len(next_row)): notional_edge = (obj.id, next_row[j].id) if notional_edge in id_edges: return j return None this_row.sort( lambda a, b: cmp(nextrow_affinity(a), nextrow_affinity(b))) box_width = 120 box_height = 80 xborder = 40 yborder = 40 xpad = 20 ypad = 20 height = 0 width = len(rows) * box_width + (len(rows) - 1) * xpad for i, items in rows.items(): total_height = len(items) * box_height + (len(items) - 1) * ypad height = max(total_height, height) height = height + yborder * 2 width = width + xborder * 2 edges = [e for e in id_edges] nodes = [] x = 0 for i, items in rows.items(): total_height = len(items) * box_height + (len(items) - 1) * ypad y = (height - total_height) / 2 for record in items: resource = record.to_resource() alert_count = len( ResourceQuery().resource_get_alerts(resource)) if alert_count != 0: highlight = "#ff0000" else: highlight = "#000000" nodes.append({ 'left': x, 'top': y, 'title': record.alias_or_name(), 'icon': "%simages/storage_plugin/%s.png" % (settings.STATIC_URL, resource.icon), 'type': resource.get_class_label(), 'id': record.id, 'highlight': highlight }) y += box_height + ypad x += box_width + xpad graph = { 'edges': edges, 'nodes': nodes, 'item_width': box_width, 'item_height': box_height, 'width': width, 'height': height } return self.create_response(request, {'graph': graph})