|
| 1 | +import logging |
| 2 | + |
| 3 | +from django.core.management.base import BaseCommand |
| 4 | +from coldfront.core.resource.models import ( |
| 5 | + ResourceAttribute, |
| 6 | + ResourceAttributeType, |
| 7 | + Resource, |
| 8 | +) |
| 9 | +from coldfront.plugins.slurm.utils import SLURM_CLUSTER_ATTRIBUTE_NAME |
| 10 | +from coldfront.core.utils.common import import_from_settings |
| 11 | + |
| 12 | +GENERAL_RESOURCE_NAME = import_from_settings("GENERAL_RESOURCE_NAME") |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class Command(BaseCommand): |
| 18 | + help = "Create ResourceAttribute for default SLURM cluster resousce" |
| 19 | + |
| 20 | + def handle(self, *args, **options): |
| 21 | + logger.setLevel(logging.INFO) |
| 22 | + |
| 23 | + clusters = Resource.objects.filter(resource_type__name="Cluster") |
| 24 | + if clusters.count() != 1: |
| 25 | + logger.warning( |
| 26 | + "More than one cluster is present, \ |
| 27 | + will not set ResourceAttribute" |
| 28 | + ) |
| 29 | + return |
| 30 | + |
| 31 | + cluster = clusters[0] |
| 32 | + if cluster.name != GENERAL_RESOURCE_NAME: |
| 33 | + logger.warning( |
| 34 | + f"Cluster is present, but it is {cluster.name},\ |
| 35 | + differernt from {GENERAL_RESOURCE_NAME} \ |
| 36 | + will not set ResourceAttribute" |
| 37 | + ) |
| 38 | + return |
| 39 | + |
| 40 | + _, created = ResourceAttribute.objects.get_or_create( |
| 41 | + resource_attribute_type=ResourceAttributeType.objects.filter( |
| 42 | + name=SLURM_CLUSTER_ATTRIBUTE_NAME |
| 43 | + )[0], |
| 44 | + resource=cluster, |
| 45 | + value=GENERAL_RESOURCE_NAME, |
| 46 | + ) |
| 47 | + |
| 48 | + if created: |
| 49 | + logger.info( |
| 50 | + f"Resource Attribute for {GENERAL_RESOURCE_NAME} \ |
| 51 | + HPC cluster resource added." |
| 52 | + ) |
| 53 | + else: |
| 54 | + logger.info( |
| 55 | + f"ResourceAttribute for {GENERAL_RESOURCE_NAME}\ |
| 56 | + HPC cluster resource already available in DB." |
| 57 | + ) |
| 58 | + |
| 59 | + return |
0 commit comments