def run_container(request, username, experiment, public_name): experiment_session = experiment.split("~") if len(experiment_session) == 2: experiment_proposal_id = experiment_session[0] session_number = experiment_session[1] else: experiment_proposal_id = experiment session_number = experiment logger.debug("Running session:%s from experiment:%s" % (experiment_proposal_id, session_number)) if username != request.user.username: return JSONResponse("username mismatch", status=status.HTTP_403_FORBIDDEN) try: container = container_service.run_container(username, session_number, public_name) except Exception as e: logger.debug("Error after run_container : %s " % e) return JSONResponse({'error': errorFormatting.format(e)}, status=status.HTTP_204_NO_CONTENT) image_selected = image_service.get_available_image(public_name=public_name) logger.debug("Searching... port") try: port = int(container.container_info['NetworkSettings']['Ports'][image_selected.port_hook][0]['HostPort']) except Exception as e: port = 0 container.container_info = experiment_proposal_id container.save() serializer = CalipsoContainerSerializer(container) logger.debug("Selected port: %d" % port) try: params = {'guacamole_username': container.guacamole_username, 'guacamole_password': container.guacamole_password, 'guacamole_connection_name': container.container_name, 'guacamole_protocol': image_selected.protocol, 'vnc_password': container.vnc_password, 'container_ip': settings.REMOTE_MACHINE_IP, 'container_port': port} guacamole_service.create_connection(params) except Exception as e: logger.debug(errorFormatting.format(e)) return JSONResponse({'error': errorFormatting.format(e)}, status=status.HTTP_400_BAD_REQUEST) return JSONResponse(serializer.data, status=status.HTTP_201_CREATED)
def stop_container(request, username, container_name): if username != request.user.username: return JSONResponse("username mismatch", status=status.HTTP_403_FORBIDDEN) try: container_data = container_service.stop_container(container_name) try: serializer = CalipsoContainerSerializer(container_data) return JSONResponse(serializer.data, status=status.HTTP_200_OK) except Exception as e: logger.debug(errorFormatting.format(e)) return JSONResponse([], status=status.HTTP_200_OK) except Exception as e: logger.debug(errorFormatting.format(e)) return JSONResponse({'error': errorFormatting.format(e)}, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, *args, **kwargs): if DYNAMIC_EXPERIMENTS_DATA_RETRIEVAL == 0: return super(GetExperimentsByUserName, self).get(self, request, *args, **kwargs) else: username = self.kwargs.get('username') if username == self.request.user.username: query = { "page_size": PAGE_SIZE_EXPERIMENTS, "page": request.GET.get('page'), "ordering": request.GET.get('ordering'), "search": request.GET.get('search'), "calipsouserexperiment__favorite": request.GET.get('calipsouserexperiment__favorite') } experiments_list = service.get_external_user_experiments( username, query) experiments_list['page_size'] = PAGE_SIZE_EXPERIMENTS return JSONResponse(experiments_list, status=status.HTTP_200_OK) else: raise PermissionDenied
def login_umbrella_by_hash(request): logger.debug("post, login_umbrella_by_hash") try: eaa_hash = request.data['EAAHash'] uid = request.data['uid'] except Exception as e: return JSONResponse( "Expected 'EAAHash' error:%s" % e, status=status.HTTP_400_BAD_REQUEST) user = authenticate(request, uid=uid, eaa_hash=eaa_hash) if user is None: return JsonResponse({'msg': 'user not found'}, status=404) else: logout(request) login(request, user, backend='apprest.views.auth_umbrella.ExternalUmbrellaServiceAuthenticationBackend') return JSONResponse('Login OK', status=status.HTTP_200_OK)
def login_user(request): logout(request) try: username = request.data['username'] password = request.data['password'] except Exception as e: return JSONResponse("Expected 'username' and 'password'", status=status.HTTP_400_BAD_REQUEST) user = authenticate(request, username=username, password=password) if user is not None: login( request, user, backend='apprest.views.auth.ExternalServiceAuthenticationBackend') return JSONResponse('Login OK', status=status.HTTP_200_OK) else: return JSONResponse('Unable to authenticate', status=status.HTTP_401_UNAUTHORIZED)
def get_umbrella_logout_local(request): logout(request) return JSONResponse("ok logout", status=status.HTTP_200_OK)
def logout_user(request): logout(request) return JSONResponse('Logout OK', status=status.HTTP_200_OK)