def delete(self, request, user_id, hpc=None, project_name=None): if hpc: hpc = hpc.upper() if hpc_exists(hpc): if project_name: try: project = Project.objects.get(hpc=hpc, name=project_name) quota = Quota.objects.get(user=user_id, project=project) quota.delete() return Response('Quota deleted', status=status.HTTP_204_NO_CONTENT) except Project.DoesNotExist: return Response('Project not found!', status=status.HTTP_404_NOT_FOUND) except Quota.DoesNotExist: return Response('Quota not found!', status=status.HTTP_404_NOT_FOUND) projects = Project.objects.filter(hpc=hpc) quotas = Quota.objects.filter(user=user_id, project=projects) quotas.delete() return Response('Quotas deleted', status=status.HTTP_204_NO_CONTENT) return Response('HPC not found!', status=status.HTTP_404_NOT_FOUND) quota = Quota.objects.filter(user=user_id) quota.delete() return Response('Quotas deleted!', status=status.HTTP_204_NO_CONTENT)
def get(self, request, user_id, hpc=None, project_name=None): if hpc: hpc = hpc.upper() if hpc_exists(hpc): if project_name: try: project = Project.objects.get(hpc=hpc, name=project_name) quota = Quota.objects.get(user=user_id, project=project) serializer = QuotaSerializer(quota) return Response(serializer.data, status=status.HTTP_200_OK) except Project.DoesNotExist: return Response('Project not found!', status=status.HTTP_404_NOT_FOUND) except Quota.DoesNotExist: return Response('Quota not found!', status=status.HTTP_404_NOT_FOUND) projects = Project.objects.filter(hpc=hpc) quotas = Quota.objects.filter(project=projects, user=user_id) serializer = QuotaSerializer(quotas, many=True) return Response(serializer.data, status=status.HTTP_200_OK) return Response('HPC not found!', status=status.HTTP_404_NOT_FOUND) else: quotas = Quota.objects.filter(user=user_id) serializer = QuotaSerializer(quotas, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def get(self, request, hpc=None, project_name=None): if hpc and hpc_exists(hpc): hpc = hpc.upper() if project_name: try: project = Project.objects.get(hpc=hpc, name=project_name) quotas = Quota.objects.filter(project=project) serializer = QuotaSerializer(quotas, many=True) return Response(serializer.data, status=status.HTTP_200_OK) except Quota.DoesNotExist: return Response('Quota not found!', status=status.HTTP_404_NOT_FOUND) try: projects = Project.objects.filter(hpc=hpc) quotas = Quota.objects.filter(project=projects) serializer = QuotaSerializer(quotas, many=True) return Response(serializer.data, status=status.HTTP_200_OK) except Project.DoesNotFound: return Response('No quotas found!', status=status.HTTP_404_NOT_FOUND) try: quotas = Quota.objects.all() serializer = QuotaSerializer(quotas, many=True) return Response(serializer.data, status=status.HTTP_200_OK) except Quota.DoesNotExist: return Response('No quotas found!', status=status.HTTP_404_NOT_FOUND)
def delete(self, request, hpc, project_name): hpc = hpc.upper() if hpc_exists(hpc): try: project = Project.objects.get(hpc=hpc, name=project_name) project.delete() return Response('Project deleted!', status=status.HTTP_204_NO_CONTENT) except Project.DoesNotExist: return Response('Project not found!', status=status.HTTP_404_NOT_FOUND) else: return Response('HPC not found!', status=status.HTTP_404_NOT_FOUND)
def post(self, request, hpc, project_name): if hpc_exists(hpc): hpc = hpc.upper() if project_name: try: project = Project.objects.get(hpc=hpc, name=project_name) quota = Quota(project=project, user=request.data['user']) serializer = QuotaSerializer(instance=quota) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_404_NOT_FOUND) except Project.DoesNotExist: return Response('Project not found!', status=status.HTTP_404_NOT_FOUND) return Response('HPC not found!', status=status.HTTP_404_NOT_FOUND)
def get(self, request, hpc=None, project_name=None): if hpc: if hpc_exists(hpc): hpc = hpc.upper() if project_name: try: print hpc, project_name project = Project.objects.get(hpc=hpc, name=project_name) serializer = ProjectSerializer(project) return Response(serializer.data, status=status.HTTP_200_OK) except Project.DoesNotExist: return Response('Project not found!', status=status.HTTP_404_NOT_FOUND) projects = Project.objects.filter(hpc=hpc) serializer = ProjectSerializer(projects, many=True) return Response(serializer.data, status=status.HTTP_200_OK) return Response('HPC not exists!', status=status.HTTP_400_BAD_REQUEST) projects = Project.objects.all() serializer = ProjectSerializer(projects, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def get(self, request, hpc, project_name): logger.debug('JobsViewExample--->GET: Called.') user = get_user(request) if not isinstance(user, User): logger.warning( 'JobsViewExample--->GET: User not recognized.\n' + ' =================== USER ERRORS =================\n' + user + ' =================================================') return Response(user, status=status.HTTP_403_FORBIDDEN) hpc = hpc.upper() if hpc_exists(hpc): try: project = Project.objects.get(name=project_name) # run example on NSG if hpc == 'NSG': job_file_example = open( BASE_DIR + '/job_examples/JonesEtAl2009_r31.zip', 'r') payload = {"tool": "NEURON74_PY_TG", "Runtime": 0.5} data, status_code = nsg.submit_job( enduser=get_nsg_enduser(user), payload=payload, infile=job_file_example) if status_code != 201 and status_code != 200: return Response(data, status=status.HTTP_400_BAD_REQUEST) # run example on PIZDAINT elif hpc == 'PIZDAINT': pizdaint_job_example = { "Executable": "/bin/date", "Resources": { "Project": "ich011", "NodeConstraints": "mc", "Runtime": "60" } } data, status_code = pizdaint.submit( job=pizdaint_job_example, headers={}) if status_code != 201 and status_code != 200: return Response( status=status.HTTP_503_SERVICE_UNAVAILABLE) data.update({ 'owner': user.id, 'project': project.id, 'runtime': 0.5, 'title': 'check job submission for hpc-monitor', }) serializer = JobSerializer(data=data) if serializer.is_valid(): job = serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) except Project.DoesNotExist: return Response('Project not found!', status=status.HTTP_404_NOT_FOUND)
def delete(self, request, hpc, project_name, job_id): """ Delete or Cancel a job. Deleting a job means that the job was completed and user choose to delete it from the HPC or it can happen that the job goes in timeout on HPC system and it is deleted. Canceling a job means that the job is not completed yet and it will be canceled restoring all the user quota allocated to run the job, it can happen because the job was submitted by mistake. """ logger.debug('JobsView--->DELETE: Called.') user = get_user(request) if not isinstance(user, User): logger.warning( 'JobsView--->DELETE: User not recognized.\n' + ' ================= USER ERRORS ====================\n' + user + ' ==================================================') return Response(user, status=status.HTTP_403_FORBIDDEN) hpc = hpc.upper() if not hpc_exists(hpc): return Response('HPC not found!', status=status.HTTP_404_NOT_FOUND) try: project = Project.objects.get(hpc=hpc, name=project_name) job = Job.objects.get(job_id=job_id, project=project) if job.stage == 'DELETED' or job.stage == 'CANCELED': return Response('Job is already deleted/canceled', status=status.HTTP_200_OK) if hpc == 'NSG': content, status_code = nsg.delete_job( enduser=get_nsg_enduser(user), jobid=job_id) if status_code == 204: # update job if job.terminal_stage: # delete job if is completed job.stage = 'DELETED' else: # cancel job if it is not completed yet and restore quota job.stage = 'CANCELED' quota = Quota.objects.get(user=user, project=job.project) quota.add(time=job.runtime) else: # if the delete request goes wrong returns the error and the status_code return Response(content, status=status_code) elif hpc == 'PIZDAINT': content, status_code = pizdaint.abort_job(job_id=job_id) if status_code != 200: # if the delete request goes wrong returns the error and the status_code return Response(content, status=status_code) if content == 'Aborted': # update job if job_terminal_stage: # deleted job if is completed job.stage = 'DELETED' else: # cancel job if it is not completed yet and restore quota job.stage = 'CANCELED' quota = Quota.objects.get(user=user, project=job.project) quota.add(time=job.runtime) job.save() serializer = JobSerializer(job) return Response(serializer.data, status=status.HTTP_200_OK) except Project.DoesNotExist: return Response('Project not found!', status=status.HTTP_404_NOT_FOUND) except Job.DoesNotExist: return Response('Job not found!', status=status.HTTP_404_NOT_FOUND)
def get(self, request, hpc=None, project_name=None, job_id=None): """ The request queries for job or jobs object to the local database. If the request is send from an AdminUser, the response returns all jobs object. If all parameters are omitted, then the method returns all user's jobs. If only hpc_name is specified, then the method all user's jobs submitted on that HPC. If the project_name is specified, then the method returns all user's jobs submitted on that project of that HPC. If also the job_id is specified, then the method returns the single job with that id submitted on that project of that HPC. """ logger.debug('JobsView--->GET: Called.') user = get_user(request) if not isinstance(user, User): logger.warning( 'JobsView--->GET: User not recognized.\n' + ' ================= USER ERRORS ====================\n' + user + ' ==================================================') return Response(user, status=status.HTTP_403_FORBIDDEN) # if hpc is set, check if it exists and then returns all user's job submitted on that HPC if hpc: hpc = hpc.upper() if hpc_exists(hpc): # if project_name is set returns all user's job submitted on that project of that HPC if project_name: try: project = Project.objects.get(name=project_name) # if job_id is set returns the job instance with that job_id on that project on that HPC if job_id: try: # update and return the single job update_job_status_and_quota(user=user, project=project, job_id=job_id) job = Job.objects.get(owner=user, project=project, job_id=job_id) serializer = JobSerializer(job) return Response(serializer.data, status=status.HTTP_200_OK) except Job.DoesNotExist: return Response( 'Job not found!', status=status.HTTP_404_NOT_FOUND) # update and return all jobs for the specified project update_job_status_and_quota(user=user, project=project) jobs = Job.objects.filter(owner=user, project=project) serializer = JobSerializer(jobs, many=True) return Response(serializer.data, status=status.HTTP_200_OK) except Project.DoesNotExist: return Response('Project not found!', status=status.HTTP_404_NOT_FOUND) # update and return all jobs submitted into the specified hpc update_job_status_and_quota(user=user, hpc=hpc) projects = Project.objects.filter(hpc=hpc) jobs = Job.objects.filter(owner=user, project=projects) serializer = JobSerializer(jobs, many=True) return Response(serializer.data, status=status.HTTP_200_OK) else: # if hpc is wrong returns 404 error code return Response('HPC not found!', status=status.HTTP_404_NOT_FOUND) # update and return all user's jobs update_job_status_and_quota(user=user) jobs = Job.objects.filter(owner=user) serializer = JobSerializer(jobs, many=True) return Response(serializer.data, status=status.HTTP_200_OK)