Skip to content

Commit

Permalink
update zone addition and removal conditions, improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
claire-peters committed Feb 8, 2024
1 parent 93da0c1 commit 6861682
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
<div class="mb-3">
<h2>Allocation Detail</h2>
<button class="btn btn-primary" id="download"> Download PDF </button>
{% if allocation.project.sf_zone %}
<a class="btn btn-success" href="https://starfish.rc.fas.harvard.edu/#/browser?zone={{allocation.project.sf_zone}}" role="button" id="starfish"> View more information about this allocation on Starfish </a>
{% if allocation.project.sf_zone and "tier" in allocation.get_parent_resource.name %}
<a class="btn btn-success" href="https://starfish.rc.fas.harvard.edu/#/browser?zone={{allocation.project.sf_zone}}" role="button" id="starfish"> View more information about your storage on Starfish </a>
{% endif %}
<hr>
</div>
Expand Down
64 changes: 40 additions & 24 deletions coldfront/plugins/sftocf/management/commands/update_zones.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ def add_arguments(self, parser):
def handle(self, *args, **options):
dry_run = options['dry_run']
if dry_run:
print("DRY RUN")
print('DRY RUN')

report = {
'dry_run': dry_run,
'deleted_zones': [],
'created_zones': [],
'allocations_missing_paths': [],
'added_zone_ids': [],
'updated_zone_paths': [],
'updated_zone_groups': []
}
Expand All @@ -47,24 +48,19 @@ def handle(self, *args, **options):
starfish_zone_attr_type = ProjectAttributeType.objects.get(name='Starfish Zone')
# collect all projects that have active allocations on Starfish
projects_with_allocations = Project.objects.filter(
status__name='Active',
allocation__status__name='Active',
allocation__resources__in=sf.get_corresponding_coldfront_resources(),
title__in=sf.get_groups() # confirm the projects have groups in Starfish
).distinct()

# also confirm that the projects are available as groups in Starfish
projects_with_allocations = projects_with_allocations.filter(
title__in=sf.get_groups(),
)

projects_with_zones = projects_with_allocations.filter(
projectattribute__proj_attr_type=starfish_zone_attr_type,
)
# for the projects that do have zones, ensure that its zone:
sf_cf_vols = sf.get_volumes_in_coldfront()
for project in projects_with_zones:
zone_id = project.projectattribute_set.get(
proj_attr_type__name='Starfish Zone',
).value
zone_id = project.sf_zone
zone = sf.get_zones(zone_id)

# has all the allocation paths associated with the project
Expand All @@ -85,16 +81,6 @@ def handle(self, *args, **options):
if missing_paths:
continue
paths = [f'{a.resources.first().name.split("/")[0]}:{a.path}' for a in storage_allocations] + zone_paths_not_in_cf
# delete any zones that have no paths
if not paths:
if not dry_run:
sf.delete_zone(zone['id'])
# delete projectattribute
project.projectattribute_set.get(
proj_attr_type=starfish_zone_attr_type,
).delete()
report['deleted_zones'].append(zone['name'])
continue
if not set(paths) == set(zone['paths']):
if not dry_run:
update_paths['paths'] = paths
Expand All @@ -107,7 +93,7 @@ def handle(self, *args, **options):

# has the project AD group in “managing_groups”
update_groups = zone['managing_groups']
zone_group_names = [g['groupname'] for g in zone['managing_groups']]
zone_group_names = [g['groupname'] for g in update_groups]
if project.title not in zone_group_names:
if not dry_run:
update_groups.append({'groupname': project.title})
Expand All @@ -131,16 +117,46 @@ def handle(self, *args, **options):
if e.response.status_code == 409:
logger.warning('zone for %s already exists; adding zoneid to Project and breaking', project.title)
zone = sf.get_zone_by_name(project.title)
report['added_zone_ids'].append([project.title, zone['id']])
elif e.response.status_code == 402:
logger.error('zone quota reached; can no longer add any zones.')
continue
else:
logger.error(
'unclear error prevented creation of zone for project %s. error: %s',
project.title, e
)
err = f'unclear error prevented creation of zone for project {project.title}. error: {e.response}'
logger.error(err)
print(err)
continue
except ValueError as e:
err = f"error encountered. If no groups returned, LDAP group doesn't exist: {e}, {project.title}"
logger.error(err)
print(err)
continue
project.projectattribute_set.get_or_create(
proj_attr_type=starfish_zone_attr_type,
value=zone['id'],
)
else:
report['created_zones'].append(project.title)

# check whether to delete zones of projects with no active SF storage allocations
potential_delete_zone_attr_projs = Project.objects.filter(
projectattribute__proj_attr_type__name='Starfish Zone'
).exclude(
title__in=[p.title for p in projects_with_allocations]
)
for project in potential_delete_zone_attr_projs:
print(project, project.pk)
zone = sf.get_zones(project.sf_zone)
zone_paths_not_in_cf = [p for p in zone['paths'] if p.split(':')[0] not in sf_cf_vols]
# delete any zones that have no paths
if not zone_paths_not_in_cf:
if not dry_run:
sf.delete_zone(zone['id'])
# delete projectattribute
project.projectattribute_set.get(
proj_attr_type=starfish_zone_attr_type,
).delete()
report['deleted_zones'].append(zone['name'])
continue
print(report)
logger.warning(report)
12 changes: 5 additions & 7 deletions coldfront/plugins/sftocf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def add_zone_group_to_ad(group_name):
error = f'Error adding {group_name} to starfish_users: {e}'
print(error)
logger.warning(error)
raise


class StarFishServer:
Expand Down Expand Up @@ -192,14 +193,11 @@ def get_zone_by_name(self, zone_name):
def create_zone(self, zone_name, paths, managers, managing_groups):
"""Create a zone via the API"""
url = self.api_url + 'zone/'
zone_paths = paths if paths else []
zone_managers = managers if managers else []
zone_managing_groups = managing_groups if managing_groups else []
data = {
"name": zone_name,
"paths": zone_paths,
"managers": zone_managers,
"managing_groups": zone_managing_groups,
"paths": paths,
"managers": managers,
"managing_groups": managing_groups,
}
logger.debug(data)
response = return_post_json(url, data=data, headers=self.headers)
Expand Down Expand Up @@ -243,7 +241,7 @@ def zone_from_project(self, project_obj):
managers = [f'{project_obj.pi.username}']
managing_groups = [{'groupname': project_obj.title}]
add_zone_group_to_ad(project_obj.title)
return self.create_zone(zone_name, paths, managers, managing_groups)
return self.create_zone(zone_name, paths, [], managing_groups)

def get_corresponding_coldfront_resources(self):
resources = Resource.objects.filter(
Expand Down

0 comments on commit 6861682

Please sign in to comment.