def handle(self, *args, **options): # pylint:disable=too-many-branches username = options[self.UserModel.USERNAME_FIELD].strip() password = options['password'].strip() email = options['email'].strip() force = to_bool(options['force']) is_superuser = to_bool(options['is_superuser']) try: username = self.username_field.clean(username, None) except exceptions.ValidationError as e: raise CommandError('; '.join(e.messages)) try: self.email_field.clean(email, None) except exceptions.ValidationError as e: raise CommandError('; '.join(e.messages)) try: self.UserModel.objects.get_by_natural_key(username) except self.UserModel.DoesNotExist: pass else: raise CommandError("Error: That username {} is already taken.".format(username)) try: self.UserModel.objects.get(email=email) except self.UserModel.DoesNotExist: pass except exceptions.MultipleObjectsReturned: raise CommandError("Error: That %s is already taken." % email) else: raise CommandError("Error: That %s is already taken." % email) if not username: raise CommandError("Error: Blank username aren't allowed.") if not password: raise CommandError("Error: Blank passwords aren't allowed.") if not email: raise CommandError("Error: Blank email aren't allowed.") user_data = { self.UserModel.USERNAME_FIELD: username, 'email': email, } self.validate_password(password=password, user_data=user_data, force=force) user_data['password'] = password if is_superuser: self.UserModel.objects.create_superuser(**user_data) else: self.UserModel.objects.create_user(**user_data) if options['verbosity'] >= 1: self.stdout.write("{} created successfully.".format( 'Superuser' if is_superuser else 'User'))
def filter_queryset(self, queryset): independent = self.request.query_params.get('independent', None) filters = {} if independent is not None and to_bool(independent): filters['experiment_group__isnull'] = True return queryset.filter(project=get_permissible_project(view=self), **filters)
def handle(self, *args, **options): pod_id = options['pod_id'] log_sleep_interval = options['log_sleep_interval'] persist = to_bool(options['persist']) self.stdout.write( "Started a new jobs logs / sidecar monitor with, pod_id: `{}` container_job_name: `{}`" "log sleep interval: `{}` and persist: `{}`".format( pod_id, settings.JOB_CONTAINER_NAME, log_sleep_interval, persist), ending='\n') k8s_manager = K8SManager(namespace=settings.K8S_NAMESPACE, in_cluster=True) is_running, labels = sidecar.can_log(k8s_manager, pod_id, log_sleep_interval) if not is_running: sidecar.logger.info('Jobs is not running anymore.') return sidecar.run(k8s_manager=k8s_manager, pod_id=pod_id, experiment_uuid=labels.experiment_uuid.hex, experiment_name=labels.experiment_name, job_uuid=labels.job_uuid.hex, task_type=labels.task_type, task_idx=labels.task_idx, container_job_name=settings.JOB_CONTAINER_NAME, persist=persist) sidecar.logger.info('Finished logging')
def post(self, request, *args, **kwargs): obj = self.get_object() if obj.has_notebook: commit = request.data.get('commit') commit = to_bool(commit) if commit is not None else True if commit: # Commit changes git.commit(obj.repo.path, request.user.email, request.user.username) else: # Reset changes git.undo(obj.repo.path) celery_app.send_task(SchedulerCeleryTasks.PROJECTS_NOTEBOOK_STOP, kwargs={ 'project_name': obj.unique_name, 'project_uuid': obj.uuid.hex, 'notebook_job_name': obj.notebook.unique_name, 'notebook_job_uuid': obj.notebook.uuid.hex, 'update_status': True }) auditor.record(event_type=NOTEBOOK_STOPPED_TRIGGERED, instance=obj.notebook, target='project', actor_id=self.request.user.id, countdown=1) elif obj.notebook and obj.notebook.is_running: obj.notebook.set_status(status=ExperimentLifeCycle.STOPPED, message='Notebook was stopped') return Response(status=status.HTTP_200_OK)
def post(self, request, *args, **kwargs): obj = self.get_object() auditor.record(event_type=self.event_type, instance=obj, actor_id=self.request.user.id) description = None config = None declarations = None update_code_reference = False if 'config' in request.data: spec = validate_experiment_spec_config( [obj.specification.parsed_data, request.data['config']], raise_for_rest=True) config = spec.parsed_data declarations = spec.declarations if 'update_code' in request.data: try: update_code_reference = to_bool(request.data['update_code']) except TypeError: raise ValidationError('update_code should be a boolean') if 'description' in request.data: description = request.data['description'] new_obj = self.clone(obj=obj, config=config, declarations=declarations, update_code_reference=update_code_reference, description=description) serializer = self.get_serializer(new_obj) return Response(status=status.HTTP_201_CREATED, data=serializer.data)
def handle(self, *args, **options): log_sleep_interval = options['log_sleep_interval'] persist = to_bool(options['persist']) node = self.get_node_or_wait(log_sleep_interval) self.stdout.write("Started a new resources monitor with, " "log sleep interval: `{}` and persist: `{}`".format( log_sleep_interval, persist), ending='\n') containers = {} while True: try: if node: monitor.run(containers, node, persist) except Exception as e: monitor.logger.exception("Unhandled exception occurred %s\n", e) time.sleep(log_sleep_interval) try: if node: node.refresh_from_db() else: node = self.get_node() except (InterfaceError, ProgrammingError, OperationalError) as e: monitor.logger.exception( "Database connection is probably already closed %s\n", e) return
def post(self, request, *args, **kwargs): obj = self.get_object() pending = request.data.get('pending') pending = to_bool(pending) if pending is not None else False stop_group_experiments.delay(experiment_group_id=obj.id, pending=pending, message='User stopped experiment group') return Response(status=status.HTTP_200_OK)
def handle(self, *args, **options): username = options[self.UserModel.USERNAME_FIELD].strip() password = options['password'].strip() email = options['email'].strip() is_superuser = to_bool(options['is_superuser']) try: username = self.username_field.clean(username, None) except exceptions.ValidationError as e: raise CommandError('; '.join(e.messages)) try: self.email_field.clean(email, None) except exceptions.ValidationError as e: raise CommandError('; '.join(e.messages)) try: self.UserModel.objects.get_by_natural_key(username) except self.UserModel.DoesNotExist: pass else: raise CommandError( "Error: That username {} is already taken.".format(username)) try: self.UserModel.objects.get(email=email) except self.UserModel.DoesNotExist: pass except exceptions.MultipleObjectsReturned: raise CommandError("Error: That %s is already taken." % email) else: raise CommandError("Error: That %s is already taken." % email) if not username: raise CommandError("Error: Blank username aren't allowed.") if not password: raise CommandError("Error: Blank passwords aren't allowed.") if not email: raise CommandError("Error: Blank email aren't allowed.") user_data = { self.UserModel.USERNAME_FIELD: username, 'email': email, } validate_password(password, self.UserModel(**user_data)) user_data['password'] = password if is_superuser: self.UserModel.objects.create_superuser(**user_data) else: self.UserModel.objects.create_user(**user_data) if options['verbosity'] >= 1: self.stdout.write("{} created successfully.".format( 'Superuser' if is_superuser else 'User'))
def get_serializer_class(self): if self.create_serializer_class and self.request.method.lower( ) == 'post': return self.create_serializer_class metrics_only = to_bool(self.request.query_params.get('metrics', None), handle_none=True, exception=ValidationError) if metrics_only: return self.metrics_serializer_class declarations_only = to_bool(self.request.query_params.get( 'declarations', None), handle_none=True, exception=ValidationError) if declarations_only: return self.declarations_serializer_class return self.serializer_class
def filter_queryset(self, queryset): independent = self.request.query_params.get('independent', None) filters = {} if independent is not None and to_bool(independent): filters['experiment_group__isnull'] = True project = get_permissible_project(view=self) auditor.record(event_type=PROJECT_EXPERIMENTS_VIEWED, instance=project, actor_id=self.request.user.id) return queryset.filter(project=project, **filters)
def post(self, request, *args, **kwargs): obj = self.get_object() pending = request.data.get('pending') pending = to_bool(pending) if pending is not None else False auditor.record(event_type=EXPERIMENT_GROUP_STOPPED_TRIGGERED, instance=obj, actor_id=request.user.id, pending=pending) stop_group_experiments.delay(experiment_group_id=obj.id, pending=pending, message='User stopped experiment group') return Response(status=status.HTTP_200_OK)
def filter_queryset(self, queryset): independent = self.request.query_params.get('independent', None) if independent is not None: independent = to_bool(independent) else: independent = False group_id = self.request.query_params.get('group', None) if independent and group_id: raise ValidationError( 'You cannot filter for independent experiments and ' 'group experiments at the same time.') project = get_permissible_project(view=self) queryset = queryset.filter(project=project) if independent is not None and to_bool(independent): queryset = queryset.filter(experiment_group__isnull=True) if group_id: group = self.get_group(project=project, group_id=group_id) queryset = queryset.filter(experiment_group=group) auditor.record(event_type=PROJECT_EXPERIMENTS_VIEWED, instance=project, actor_id=self.request.user.id) return super().filter_queryset(queryset=queryset)
def post(self, request, *args, **kwargs): obj = self.get_object() if obj.has_notebook: commit = request.data.get('commit') commit = to_bool(commit) if commit is not None else True if commit: # Commit changes git.commit(obj.repo.path, request.user.email, request.user.username) else: # Reset changes git.undo(obj.repo.path) stop_notebook.delay(project_id=obj.id) return Response(status=status.HTTP_200_OK)
def post(self, request, *args, **kwargs): obj = self.get_object() pending = request.data.get('pending') pending = to_bool(pending) if pending is not None else False auditor.record(event_type=EXPERIMENT_GROUP_STOPPED_TRIGGERED, instance=obj, actor_id=request.user.id, actor_name=request.user.username, pending=pending) celery_app.send_task( SchedulerCeleryTasks.EXPERIMENTS_GROUP_STOP_EXPERIMENTS, kwargs={'experiment_group_id': obj.id, 'pending': pending, 'message': 'User stopped experiment group'}) return Response(status=status.HTTP_200_OK)
def post(self, request, *args, **kwargs): obj = self.get_object() if obj.has_notebook: commit = request.data.get('commit') commit = to_bool(commit) if commit is not None else True if commit: # Commit changes git.commit(obj.repo.path, request.user.email, request.user.username) else: # Reset changes git.undo(obj.repo.path) stop_notebook.delay(project_id=obj.id) elif obj.notebook and obj.notebook.is_running: obj.notebook.set_status(status=ExperimentLifeCycle.STOPPED, message='Notebook was stopped') return Response(status=status.HTTP_200_OK)
def handle(self, *args, **options): log_sleep_interval = options['log_sleep_interval'] persist = to_bool(options['persist']) node = self.get_node() self.stdout.write( "Started a new resources monitor with, " "log sleep interval: `{}` and persist: `{}`".format(log_sleep_interval, persist), ending='\n') containers = {} while True: try: resources.run(containers, node, persist) except Exception as e: resources.logger.exception("Unhandled exception occurred %s\n" % e) time.sleep(log_sleep_interval) if node: node.refresh_from_db() else: node = self.get_node()
def post(self, request, *args, **kwargs): ttl = self.request.data.get(RedisTTL.TTL_KEY) if ttl: try: ttl = RedisTTL.validate_ttl(ttl) except ValueError: raise ValidationError('ttl must be an integer.') obj = self.get_object() auditor.record(event_type=self.event_type, instance=obj, actor_id=self.request.user.id, actor_name=self.request.user.username) description = None config = None declarations = None update_code_reference = False if 'config' in request.data: spec = validate_experiment_spec_config( [obj.specification.parsed_data, request.data['config']], raise_for_rest=True) config = spec.parsed_data declarations = spec.declarations if 'update_code' in request.data: update_code_reference = to_bool(request.data['update_code'], handle_none=True, exception=ValidationError) if 'description' in request.data: description = request.data['description'] new_obj = self.clone(obj=obj, config=config, declarations=declarations, update_code_reference=update_code_reference, description=description) if ttl: RedisTTL.set_for_experiment(experiment_id=new_obj.id, value=ttl) serializer = self.get_serializer(new_obj) return Response(status=status.HTTP_201_CREATED, data=serializer.data)
def _indepenent_condition(queryset, params, negation): params = to_list(params) if len(params) == 1 and to_bool(params[0]) is True: return queryset.filter(experiment_group__isnull=True) return queryset