Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion coldfront/core/allocation/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,10 @@ def test_allocationdetail_approval_post(self):
self.assertEqual(self.new_allocation.status.name, 'Active')

def test_allocationdetail_tape_approval_saves_path_and_globus_uuid(self):
"""approving a tape allocation stores the entered path and Globus UUID"""
"""approving a tape allocation stores the entered path and Globus UUID.

Pushing the UUID to a Starfish zone is FASRC-specific and lives in
coldfront.plugins.fasrc.signals, not here (see its own tests)."""
tape_resource = ResourceFactory(
name='nesetape/tier3', resource_type=ResourceTypeFactory(name='Storage')
)
Expand Down
3 changes: 3 additions & 0 deletions coldfront/plugins/fasrc/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@

class FASRCConfig(AppConfig):
name = 'coldfront.plugins.fasrc'

def ready(self):
import coldfront.plugins.fasrc.signals # noqa: F401
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
from coldfront.core.allocation.utils import check_l3_tag
from coldfront.core.utils.fasrc import update_csv, select_one_project_allocation, save_json
from coldfront.core.resource.models import Resource
from sftocf.utils import StarFishRedash, RedashDataPipeline
from sftocf.client import StarFishRedash
from sftocf.pipelines import RedashDataPipeline
from coldfront.plugins.fasrc.utils import (
AllTheThingsConn,
match_entries_with_projects,
Expand Down
75 changes: 16 additions & 59 deletions coldfront/plugins/fasrc/management/commands/pull_resource_data.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import logging
from django.core.management.base import BaseCommand

from coldfront.core.resource.models import Resource, ResourceAttributeType
from coldfront.core.resource.models import Resource
from coldfront.core.resource.signals import update_volume_information
from sftocf.utils import StarFishServer, StarFishRedash
from sftocf.signals import starfish_update_volume_information


logger = logging.getLogger(__name__)

def update_resource_attr_types_from_dict(resource, res_attr_type_dict):
for attr_name, attr_val in res_attr_type_dict.items():
if attr_val:
attr_type_obj = ResourceAttributeType.objects.get(name=attr_name)
resource.resourceattribute_set.update_or_create(
resource_attribute_type=attr_type_obj,
defaults={'value': attr_val}
)

class Command(BaseCommand):
"""Pull data from starfish and save to ResourceAttribute objects
"""Pull storage capacity/usage data and save it to ResourceAttribute objects.

Dispatches update_volume_information for every Storage resource; each
storage-backend plugin living in this same coldfront repo (e.g. isilon;
see coldfront.plugins.isilon.signals) owns a receiver that updates the
resources it recognizes and ignores the rest, so this command doesn't need
to import those directly. A separate signal gets sent to the sftocf plugin.
"""

def add_arguments(self, parser):
Expand All @@ -30,51 +27,11 @@ def add_arguments(self, parser):
)

def handle(self, *args, **options):
source = options['source']
if source == 'rest_api':
sf = StarFishServer()
volumes = sf.get_volume_attributes()
for vol in volumes:
vol['capacity_TB'] = vol['total_capacity']/(1024**4)
vol['free_TB'] = vol['free_space']/(1024**4)
vol['used_physical_TB'] = (vol['total_capacity'] - vol['free_space'])/(1024**4)
elif source == 'redash':
sf = StarFishRedash()
volumes = sf.get_vol_stats()
else:
raise ValueError('source must be "rest_api" or "redash"')

volumes = [
{
'name': vol['vol'],
'attrs': {
'capacity_tb': vol['capacity_TB'],
'free_tb': vol['free_TB'],
'used_tb': vol['used_physical_TB'],
'file_count': vol['number_of_files'],
}
}
for vol in volumes
]

# collect user and lab counts, allocation sizes for each volume
resources = Resource.objects.filter(resource_type__name='Storage', is_available=True)
# update all tier 0 resources
for resource in resources.filter(
resourceattribute__value__in=('isilon', 'powerscale')
):
update_volume_information.send(sender=self.__class__, resource=resource)
for resource in resources.exclude(
resourceattribute__value__in=('isilon', 'powerscale')):
resource_name = resource.name.split('/')[0]
try:
volume = [v for v in volumes if v['name'] == resource_name][0]
except IndexError:
logger.warning('no data found for resource %s from source %s', resource.name, source)
continue
except Exception as e:
logger.warning('problem updating resource %s with data from source %s: %s',
resource.name, source, e
)
continue
update_resource_attr_types_from_dict(resource, volume['attrs'])
for resource in resources:
update_volume_information.send(
sender=self.__class__, resource=resource, source=options['source']
)
starfish_update_volume_information.send(
sender=self.__class__, resource=resource, source=options['source']
)
70 changes: 70 additions & 0 deletions coldfront/plugins/fasrc/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""FASRC-specific signal receivers.

Organization-specific logic lives here, not in the generic plugins
(sftocf, isilon, ...) those plugins stay reusable by other institutions.
"""
import logging

from django.db.models.signals import post_save
from django.dispatch import receiver

from coldfront.core.allocation.models import AllocationAttribute
from sftocf.signals import starfish_set_zone_userparam, starfish_zone_created

logger = logging.getLogger(__name__)


def _push_archive_globus_collection(allocation, globus_uuid):
zone_id = allocation.project.sf_zone
if not zone_id:
logger.warning(
'Project has no Starfish zone yet; archive_globus_collection will be '
'pushed once update_zones creates one. project=%s allocation_pk=%s',
allocation.project.title, allocation.pk
)
return
starfish_set_zone_userparam.send(
sender=allocation.__class__,
zone_id=zone_id,
param_name='archive_globus_collection',
param_value=globus_uuid,
)


@receiver(post_save, sender=AllocationAttribute)
def push_globus_uuid_on_approval(sender, instance, created, **kwargs):
"""When a Globus UUID attribute is set on an allocation, push it to the
allocation's project's Starfish zone as archive_globus_collection.

If the project has no zone yet, this is skipped; push_globus_uuid_to_new_zone
below catches it up once update_zones creates one.
"""
if not created or instance.allocation_attribute_type.name != 'Globus UUID':
return
_push_archive_globus_collection(instance.allocation, instance.value)


@receiver(starfish_zone_created)
def push_globus_uuid_to_new_zone(sender, **kwargs):
"""Catch up archive_globus_collection on a project's newly created zone.

push_globus_uuid_on_approval above pushes it immediately when a project
already has a Starfish zone; when it doesn't yet, this catches up for any
of the project's active allocations that have a Globus UUID, once
update_zones creates the zone. Uses the zone_id the signal already
provides rather than re-deriving project.sf_zone, since that attribute's
save is only guaranteed to precede this signal, not any particular
read-after-write visibility.
"""
project = kwargs['project']
zone_id = kwargs['zone_id']
for allocation in project.allocation_set.filter(status__name='Active'):
globus_uuid = allocation.get_attribute('Globus UUID')
if not globus_uuid:
continue
starfish_set_zone_userparam.send(
sender=sender,
zone_id=zone_id,
param_name='archive_globus_collection',
param_value=globus_uuid,
)
139 changes: 139 additions & 0 deletions coldfront/plugins/fasrc/tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
from django.test import TestCase

from coldfront.core.test_helpers.utils import require_plugin

require_plugin('coldfront.plugins.fasrc')

from coldfront.core.test_helpers.factories import (
AAttributeTypeFactory,
AllocationAttributeFactory,
AllocationAttributeTypeFactory,
AllocationFactory,
AllocationStatusChoiceFactory,
PAttributeTypeFactory,
ProjectAttributeFactory,
ProjectAttributeTypeFactory,
ProjectFactory,
ProjectStatusChoiceFactory,
UserFactory,
)
from sftocf.signals import starfish_set_zone_userparam, starfish_zone_created


class PushGlobusUuidToNewZoneTests(TestCase):
"""push_globus_uuid_to_new_zone reacts to sftocf's starfish_zone_created by
re-sending the already-generic starfish_set_zone_userparam signal for any
of the project's active allocations that have a Globus UUID attribute."""

def setUp(self):
ProjectStatusChoiceFactory(name='Active')
AllocationStatusChoiceFactory(name='Active')
pi = UserFactory()
self.project = ProjectFactory(pi=pi, status__name='Active')
self.allocation = AllocationFactory(project=self.project, status__name='Active')

self._received = []

def handler(sender, **kwargs):
self._received.append(kwargs)

self._handler = handler
starfish_set_zone_userparam.connect(handler, weak=False)
self.addCleanup(starfish_set_zone_userparam.disconnect, handler)

def test_pushes_globus_uuid_for_allocation_with_one(self):
globus_attr_type = AllocationAttributeTypeFactory(
name='Globus UUID', attribute_type=AAttributeTypeFactory(name='Text')
)
AllocationAttributeFactory(
allocation=self.allocation,
allocation_attribute_type=globus_attr_type,
value='the-uuid',
)

starfish_zone_created.send(
sender=self.__class__, project=self.project, zone_id='zone-123'
)

self.assertEqual(len(self._received), 1)
self.assertEqual(self._received[0]['zone_id'], 'zone-123')
self.assertEqual(self._received[0]['param_name'], 'archive_globus_collection')
self.assertEqual(self._received[0]['param_value'], 'the-uuid')

def test_does_nothing_without_a_globus_uuid(self):
starfish_zone_created.send(
sender=self.__class__, project=self.project, zone_id='zone-123'
)

self.assertEqual(self._received, [])


class PushGlobusUuidOnApprovalTests(TestCase):
"""push_globus_uuid_on_approval reacts to the built-in post_save signal on
AllocationAttribute, filtered to the 'Globus UUID' attribute type, instead
of coldfront.core.allocation.views needing to know anything about this."""

def setUp(self):
ProjectStatusChoiceFactory(name='Active')
AllocationStatusChoiceFactory(name='Active')
pi = UserFactory()
self.project = ProjectFactory(pi=pi, status__name='Active')
self.allocation = AllocationFactory(project=self.project, status__name='Active')
self.globus_attr_type = AllocationAttributeTypeFactory(
name='Globus UUID', attribute_type=AAttributeTypeFactory(name='Text')
)

self._received = []

def handler(sender, **kwargs):
self._received.append(kwargs)

starfish_set_zone_userparam.connect(handler, weak=False)
self.addCleanup(starfish_set_zone_userparam.disconnect, handler)

def test_pushes_when_project_already_has_a_zone(self):
zone_attr_type = ProjectAttributeTypeFactory(
name='Starfish Zone', attribute_type=PAttributeTypeFactory(name='Text')
)
ProjectAttributeFactory(
project=self.project, proj_attr_type=zone_attr_type, value='42'
)

AllocationAttributeFactory(
allocation=self.allocation,
allocation_attribute_type=self.globus_attr_type,
value='the-uuid',
)

self.assertEqual(len(self._received), 1)
self.assertEqual(self._received[0]['zone_id'], '42')
self.assertEqual(self._received[0]['param_name'], 'archive_globus_collection')
self.assertEqual(self._received[0]['param_value'], 'the-uuid')

def test_does_nothing_when_project_has_no_zone_yet(self):
AllocationAttributeFactory(
allocation=self.allocation,
allocation_attribute_type=self.globus_attr_type,
value='the-uuid',
)

self.assertEqual(self._received, [])

def test_ignores_other_attribute_types(self):
other_attr_type = AllocationAttributeTypeFactory(
name='Subdirectory', attribute_type=AAttributeTypeFactory(name='Text')
)
zone_attr_type = ProjectAttributeTypeFactory(
name='Starfish Zone', attribute_type=PAttributeTypeFactory(name='Text')
)
ProjectAttributeFactory(
project=self.project, proj_attr_type=zone_attr_type, value='42'
)

AllocationAttributeFactory(
allocation=self.allocation,
allocation_attribute_type=other_attr_type,
value='some/path',
)

self.assertEqual(self._received, [])
4 changes: 2 additions & 2 deletions coldfront/plugins/fasrc_monitoring/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'PENDING_ACTIVE_ALLOCATION_STATUSES', ['Active', 'New', 'Updated', 'Ready for Review'])

if 'sftocf' in import_from_settings('INSTALLED_APPS', []):
from sftocf.utils import StarFishServer
from sftocf.client import StarfishClient



Expand All @@ -40,7 +40,7 @@ def get_context_data(self, **kwargs):
scan_data_processed = None
if 'sftocf' in import_from_settings('INSTALLED_APPS', []):
try:
sf = StarFishServer()
sf = StarfishClient()
scan_data = sf.get_most_recent_scans()
scan_data_processed = [
{'volume': s['volume'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def add_arguments(self, parser):

def handle(self, *args, **options):
isilon_resources = Resource.objects.filter(
resource_type__name='Storage',
resourceattribute__value__in=('isilon', 'powerscale'),
is_available=True,
)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ reportlab==4.0.5
xhtml2pdf==0.2.15
XlsxWriter
git+https://github.com/coldfront/slurm-python-sdk.git
git+https://github.com/fasrc/sftocf.git
git+https://github.com/fasrc/sftocf.git@1.0.1
git+https://github.com/theam/coldfront_notifications.git
vastpy
django-prometheus
Expand Down
Loading