diff options
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/migrations/0019_auto_20210907_1448.py | 29 | ||||
-rw-r--r-- | src/api/models.py | 26 | ||||
-rw-r--r-- | src/api/urls.py | 8 | ||||
-rw-r--r-- | src/api/views.py | 25 |
4 files changed, 69 insertions, 19 deletions
diff --git a/src/api/migrations/0019_auto_20210907_1448.py b/src/api/migrations/0019_auto_20210907_1448.py new file mode 100644 index 0000000..92140fb --- /dev/null +++ b/src/api/migrations/0019_auto_20210907_1448.py @@ -0,0 +1,29 @@ +# Generated by Django 2.2 on 2021-09-07 14:48 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('booking', '0008_auto_20201109_1947'), + ('resource_inventory', '0020_cloudinitfile'), + ('api', '0018_cloudinitfile'), + ] + + operations = [ + migrations.CreateModel( + name='GeneratedCloudConfig', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('resource_id', models.CharField(max_length=200)), + ('text', models.TextField(blank=True, null=True)), + ('booking', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='booking.Booking')), + ('rconfig', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='resource_inventory.ResourceConfiguration')), + ], + ), + migrations.DeleteModel( + name='CloudInitFile', + ), + ] diff --git a/src/api/models.py b/src/api/models.py index 3098111..ec163a1 100644 --- a/src/api/models.py +++ b/src/api/models.py @@ -32,7 +32,8 @@ from resource_inventory.models import ( OPNFVConfig, ConfigState, ResourceQuery, - ResourceConfiguration + ResourceConfiguration, + CloudInitFile ) from resource_inventory.idf_templater import IDFTemplater from resource_inventory.pdf_templater import PDFTemplater @@ -351,10 +352,11 @@ class LabManager(object): profile_ser.append(p) return profile_ser -class CloudInitFile(models.Model): +class GeneratedCloudConfig(models.Model): resource_id = models.CharField(max_length=200) booking = models.ForeignKey(Booking, on_delete=models.CASCADE) rconfig = models.ForeignKey(ResourceConfiguration, on_delete=models.CASCADE) + text = models.TextField(null=True, blank=True) def _normalize_username(self, username: str) -> str: # TODO: make usernames posix compliant @@ -447,8 +449,8 @@ class CloudInitFile(models.Model): return full_dict @classmethod - def get(cls, booking_id: int, resource_lab_id: str): - return CloudInitFile.objects.get(resource_id=resource_lab_id, booking__id=booking_id) + def get(cls, booking_id: int, resource_lab_id: str, file_id: int): + return GeneratedCloudConfig.objects.get(resource_id=resource_lab_id, booking__id=booking_id, file_id=file_id) def _resource(self): return ResourceQuery.get(labid=self.resource_id, lab=self.booking.lab) @@ -469,7 +471,7 @@ class CloudInitFile(models.Model): return main_dict def serialize(self) -> str: - return str("#cloud-config\n") + yaml.dump(self._to_dict()) + return yaml.dump(self._to_dict()) class Job(models.Model): """ @@ -804,7 +806,7 @@ class HardwareConfig(TaskConfig): return self.get_delta() def get_delta(self): - # TODO: grab the CloudInitFile urls from self.hosthardwarerelation.get_resource() + # TODO: grab the GeneratedCloudConfig urls from self.hosthardwarerelation.get_resource() return self.format_delta( self.hosthardwarerelation.get_resource().get_configuration(self.state), self.hosthardwarerelation.lab_token) @@ -1148,7 +1150,7 @@ class JobFactory(object): booking=booking, job=job ) - cls.makeCloudInitFiles( + cls.makeGeneratedCloudConfigs( resources=resources, job=job ) @@ -1176,11 +1178,17 @@ class JobFactory(object): continue @classmethod - def makeCloudInitFiles(cls, resources=[], job=Job()): + def makeGeneratedCloudConfigs(cls, resources=[], job=Job()): for res in resources: - cif = CloudInitFile.objects.create(resource_id=res.labid, booking=job.booking, rconfig=res.config) + cif = GeneratedCloudConfig.objects.create(resource_id=res.labid, booking=job.booking, rconfig=res.config) cif.save() + cif = CloudInitFile.create(priority=0, text=cif.serialize()) + cif.save() + + res.config.cloud_init_files.add(cif) + res.config.save() + @classmethod def makeHardwareConfigs(cls, resources=[], job=Job()): """ diff --git a/src/api/urls.py b/src/api/urls.py index 970ecf2..8dcfafe 100644 --- a/src/api/urls.py +++ b/src/api/urls.py @@ -48,10 +48,11 @@ from api.views import ( analytics_job, resource_ci_metadata, resource_ci_userdata, + resource_ci_userdata_directory, all_images, all_opsyss, single_image, - single_opsys + single_opsys, ) urlpatterns = [ @@ -69,8 +70,9 @@ urlpatterns = [ path('labs/<slug:lab_name>/booking/<int:booking_id>/idf', get_idf, name="get-idf"), path('labs/<slug:lab_name>/jobs/<int:job_id>', specific_job), path('labs/<slug:lab_name>/jobs/<int:job_id>/<slug:task_id>', specific_task), - path('labs/<slug:lab_name>/jobs/<int:job_id>/cidata/<slug:resource_id>/user-data', resource_ci_userdata), - path('labs/<slug:lab_name>/jobs/<int:job_id>/cidata/<slug:resource_id>/meta-data', resource_ci_metadata), + path('labs/<slug:lab_name>/jobs/<int:job_id>/cidata/<slug:resource_id>/user-data', resource_ci_userdata_directory, name="specific-user-data"), + path('labs/<slug:lab_name>/jobs/<int:job_id>/cidata/<slug:resource_id>/meta-data', resource_ci_metadata, name="specific-meta-data"), + path('labs/<slug:lab_name>/jobs/<int:job_id>/cidata/<slug:resource_id>/<int:file_id>/user-data', resource_ci_userdata, name="user-data-dir"), path('labs/<slug:lab_name>/jobs/new', new_jobs), path('labs/<slug:lab_name>/jobs/current', current_jobs), path('labs/<slug:lab_name>/jobs/done', done_jobs), diff --git a/src/api/views.py b/src/api/views.py index 7add23e..79da84c 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -24,14 +24,16 @@ from django.core.exceptions import ObjectDoesNotExist from api.serializers.booking_serializer import BookingSerializer from api.serializers.old_serializers import UserSerializer from api.forms import DowntimeForm -from account.models import UserProfile +from account.models import UserProfile, Lab from booking.models import Booking -from api.models import LabManagerTracker, get_task, CloudInitFile, Job +from api.models import LabManagerTracker, get_task, Job from notifier.manager import NotificationHandler from analytics.models import ActiveVPNUser from resource_inventory.models import ( Image, - Opsys + Opsys, + CloudInitFile, + ResourceQuery, ) import json @@ -248,7 +250,7 @@ def specific_job(request, lab_name="", job_id=""): return JsonResponse(lab_manager.get_job(job_id), safe=False) @csrf_exempt -def resource_ci_userdata(request, lab_name="", job_id="", resource_id=""): +def resource_ci_userdata(request, lab_name="", job_id="", resource_id="", file_id=0): #lab_token = request.META.get('HTTP_AUTH_TOKEN') #lab_manager = LabManagerTracker.get(lab_name, lab_token) @@ -257,16 +259,25 @@ def resource_ci_userdata(request, lab_name="", job_id="", resource_id=""): cifile = None try: - cifile = CloudInitFile.get(job.booking.id, resource_id) + cifile = CloudInitFile.objects.get(id=file_id) except ObjectDoesNotExist: return HttpResponseNotFound("Could not find a matching resource by id " + str(resource_id)) - return HttpResponse(cifile.serialize(), status=200) + return HttpResponse(cifile.text, status=200) @csrf_exempt -def resource_ci_metadata(request, lab_name="", job_id="", resource_id=""): +def resource_ci_metadata(request, lab_name="", job_id="", resource_id="", file_id=0): return HttpResponse("#cloud-config", status=200) +@csrf_exempt +def resource_ci_userdata_directory(request, lab_name="", job_id="", resource_id=""): + #files = [{"id": file.file_id, "priority": file.priority} for file in CloudInitFile.objects.filter(job__id=job_id, resource_id=resource_id).order_by("priority").all()] + resource = ResourceQuery.get(labid=resource_id, lab=Lab.objects.get(name=lab_name)) + files = resource.config.cloud_init_files + files = [{"id": file.id, "priority": file.priority} for file in files.order_by("priority").all()] + + return HttpResponse(json.dumps(files), status=200) + def new_jobs(request, lab_name=""): lab_token = request.META.get('HTTP_AUTH_TOKEN') |