def service_run(request, name, crits_type, identifier): """ Run a service. """ response = {'success': False} if crits_type not in DETAIL_VIEWS: response['html'] = "Unknown CRITs type." return HttpResponse(json.dumps(response), mimetype="application/json") env = crits.service_env.environment username = str(request.user.username) # We can assume this is a DatabaseAnalysisEnvironment if name not in env.manager.enabled_services: response['html'] = "Service %s is unknown or not enabled." % name return HttpResponse(json.dumps(response), mimetype="application/json") try: context = env.create_context(crits_type, identifier, username) except Exception as e: logger.exception("Could not build context") response['html'] = "Error: %s" % e return HttpResponse(json.dumps(response), mimetype="application/json") service_class = env.manager.get_service_class(name) ServiceRunConfigForm = make_run_config_form(service_class) # Set defaults config = None force = False if ServiceRunConfigForm: if request.method == "POST": #Populate the form with values from the POST request form = ServiceRunConfigForm(request.POST) if form.is_valid(): # parse_config will remove the "force" option from cleaned_data config = service_class.parse_config(form.cleaned_data, exclude_private=True) force = form.cleaned_data.get("force") else: # TODO: return corrected form via AJAX response[ 'html'] = "Invalid configuration, please try again :-(" return HttpResponse(json.dumps(response), mimetype="application/json") try: env.run_service(name, context, execute=settings.SERVICE_MODEL, custom_config=config, force=force) except ServiceAnalysisError as e: logger.exception("Error when running service") response['html'] = "Error when running service: %s" % e return HttpResponse(json.dumps(response), mimetype="application/json") return refresh_services(request, crits_type, identifier)
def service_run(request, name, crits_type, identifier): """ Run a service. """ response = {'success': False} if crits_type not in DETAIL_VIEWS: response['html'] = "Unknown CRITs type." return HttpResponse(json.dumps(response), mimetype="application/json") env = crits.service_env.environment username = str(request.user.username) # We can assume this is a DatabaseAnalysisEnvironment if name not in env.manager.enabled_services: response['html'] = "Service %s is unknown or not enabled." % name return HttpResponse(json.dumps(response), mimetype="application/json") try: context = env.create_context(crits_type, identifier, username) except Exception as e: logger.exception("Could not build context") response['html'] = "Error: %s" % e return HttpResponse(json.dumps(response), mimetype="application/json") service_class = env.manager.get_service_class(name) ServiceRunConfigForm = make_run_config_form(service_class) # Set defaults config = None force = False if ServiceRunConfigForm: if request.method == "POST": #Populate the form with values from the POST request form = ServiceRunConfigForm(request.POST) if form.is_valid(): # parse_config will remove the "force" option from cleaned_data config = service_class.parse_config(form.cleaned_data, exclude_private=True) force = form.cleaned_data.get("force") else: # TODO: return corrected form via AJAX response['html'] = "Invalid configuration, please try again :-(" return HttpResponse(json.dumps(response), mimetype="application/json") try: env.run_service(name, context, execute=settings.SERVICE_MODEL, custom_config=config, force=force) except ServiceAnalysisError as e: logger.exception("Error when running service") response['html'] = "Error when running service: %s" % e return HttpResponse(json.dumps(response), mimetype="application/json") return refresh_services(request, crits_type, identifier)
def test_default_config(self): FormClass = make_edit_config_form(ServiceWithDefaultConfig) self.assertNotEqual(None, FormClass) FormClass2 = make_run_config_form(ServiceWithDefaultConfig) self.assertNotEqual(None, FormClass) fields = _get_config_fields(ServiceWithDefaultConfig, True) self.assertEqual(1, len(fields)) private_fields = _get_config_fields(ServiceWithDefaultConfig, False) self.assertEqual(2, len(private_fields))
def get_form(request, name, crits_type, identifier): """ Get a configuration form for a service. """ response = {} response['name'] = name service = CRITsService.objects(name=name, status__ne="unavailable").first() # TODO: return an AJAX error instead if not service: msg = 'Service "%s" is unavailable. Please review error logs.' % name response['error'] = msg return HttpResponse(json.dumps(response), mimetype="application/json") # Get the class that implements this service. service_class = crits.service_env.manager.get_service_class(name) # format_config returns a list of tuples config = service_class.format_config(service.config, printable=False) ServiceRunConfigForm = make_run_config_form(service_class) if not ServiceRunConfigForm: # this should only happen if there are no config options and the # service is rerunnable. response['redirect'] = reverse('crits.services.views.service_run', args=[name, crits_type, identifier]) else: form = ServiceRunConfigForm(dict(config)) response['form'] = render_to_string( "services_run_form.html", { 'name': name, 'form': form, 'crits_type': crits_type, 'identifier': identifier }, RequestContext(request)) return HttpResponse(json.dumps(response), mimetype="application/json")
def get_form(request, name, crits_type, identifier): """ Get a configuration form for a service. """ response = {} response['name'] = name service = CRITsService.objects(name=name, status__ne="unavailable").first() # TODO: return an AJAX error instead if not service: msg = 'Service "%s" is unavailable. Please review error logs.' % name response['error'] = msg return HttpResponse(json.dumps(response), mimetype="application/json") # Get the class that implements this service. service_class = crits.service_env.manager.get_service_class(name) # format_config returns a list of tuples config = service_class.format_config(service.config, printable=False) ServiceRunConfigForm = make_run_config_form(service_class) if not ServiceRunConfigForm: # this should only happen if there are no config options and the # service is rerunnable. response['redirect'] = reverse('crits.services.views.service_run', args=[name, crits_type, identifier]) else: form = ServiceRunConfigForm(dict(config)) response['form'] = render_to_string("services_run_form.html", {'name': name, 'form': form, 'crits_type': crits_type, 'identifier': identifier}, RequestContext(request)) return HttpResponse(json.dumps(response), mimetype="application/json")
def test_no_default_config(self): FormClass = make_edit_config_form(ServiceWithNoDefaultConfig) self.assertEqual(None, FormClass) FormClass2 = make_run_config_form(ServiceWithNoDefaultConfig) self.assertNotEqual(None, FormClass2)