Example #1
0
def install(request):
    plugins = []

    if not request.user.is_authenticated() or not request.user.has_perm(
            'smartshark.install_plugin'):
        messages.error(request,
                       'You are not authorized to perform this action.')
        return HttpResponseRedirect('/admin/smartshark/plugin')

    if not request.GET.get('ids'):
        messages.error(request, 'No plugin ids were given to install.')
        return HttpResponseRedirect('/admin/smartshark/plugin')

    for plugin_id in request.GET.get('ids', '').split(','):
        plugin = get_object_or_404(Plugin, pk=plugin_id)
        plugins.append(plugin)

    if request.method == 'POST':
        if 'cancel' in request.POST:
            return HttpResponseRedirect('/admin/smartshark/plugin')

        # create a form instance and populate it with data from the request:
        form = get_form(plugins, request.POST, 'install')
        # check whether it's valid:
        if form.is_valid():
            # Parse the fields and set the corresponding values of the install arguments in the database
            set_argument_values(form.cleaned_data)

            # Install plugins
            installations = PluginManagementInterface.find_correct_plugin_manager(
            ).install_plugins(plugins)

            # Check if plugins successfully installed
            append_success_messages_to_req(installations, plugins, request)

            return HttpResponseRedirect('/admin/smartshark/plugin')

    # if a GET (or any other method) we'll create a blank form
    else:
        form = get_form(plugins, request.POST or None, 'install')

    return render(
        request, 'smartshark/plugin/install.html', {
            'form': form,
            'plugins': plugins,
            'substitutions': create_substitutions_for_display()
        })
Example #2
0
def start_collection(request):
    ak = request.POST.get('ak', None)
    if not ak:
        return HttpResponse('Unauthorized', status=401)
    if ak != settings.API_KEY:
        return HttpResponse('Unauthorized', status=401)

    project_mongo_ids = request.POST.get('project_mongo_ids', None)
    plugin_ids = request.POST.get('plugin_ids', None)

    if not project_mongo_ids:
        return HttpResponse('project mongo ids required', status=400)

    if not plugin_ids:
        return HttpResponse('no plugins selected', status=400)

    interface = PluginManagementInterface.find_correct_plugin_manager()

    plugins = []
    projects = []

    for mongo_id in project_mongo_ids.split(','):
        project = Project.objects.get(mongo_id=mongo_id)
        projects.append(project)

    for plugin_id in plugin_ids.split(','):
        plugin = Plugin.objects.get(pk=plugin_id, active=True, installed=True)
        plugins.append(plugin)

        for project in projects:
            # check plugin requirements
            for req_plugin in plugin.requires.all():
                if not _check_if_at_least_one_execution_was_successful(req_plugin, project):
                    return HttpResponse('not all requirements for plugin {} are met. Plugin {} was not executed successfully for project {} before'.format(plugin, req_plugin, project), status=400)

            # check if plugin alredy runs
            plugin_executions = PluginExecution.objects.all().filter(plugin=plugin, project=project)

            # Get all jobs from all plugin_executions which did not terminate yet
            jobs = []
            for plugin_execution in plugin_executions:
                jobs.extend(Job.objects.filter(plugin_execution=plugin_execution, status='WAIT').all())

            # Update the job stati for these jobs
            job_stati = interface.get_job_stati(jobs)
            i = 0
            for job in jobs:
                job.status = job_stati[i]
                job.save()

            # check if some plugin has unfinished jobs
            has_unfinished_jobs = False
            for plugin_execution in plugin_executions:
                if plugin_execution.has_unfinished_jobs():
                    has_unfinished_jobs = True

            if has_unfinished_jobs:
                return HttpResponse('Plugin {} has unfinished jobs in project {}'.format(plugin, project), status=500)

    form = get_form(plugins, request.POST, 'execute')

    if form.is_valid():
        execution_type = form.cleaned_data.get('execution', None)
        revisions = form.cleaned_data.get('revisions', None)
        repository_url = form.cleaned_data.get('repository_url', None)

        sorted_plugins = order_plugins(plugins)

        for project in projects:

            plugin_executions = []
            for plugin in sorted_plugins:
                # Create Plugin Execution Objects
                plugin_execution = PluginExecution(project=project, plugin=plugin)

                if plugin.plugin_type == 'repo' or plugin.plugin_type == 'rev':
                    plugin_execution.repository_url = repository_url

                if plugin.plugin_type == 'rev':
                    plugin_execution.execution_type = execution_type
                    plugin_execution.revisions = revisions

                plugin_execution.save()
                plugin_executions.append(plugin_execution)

            # Set execution history with execution values for the plugin execution
            set_argument_execution_values(form.cleaned_data, plugin_executions)

            # Create jobs and execute them in a separate thread
            thread = JobSubmissionThread(project, plugin_executions)
            thread.start()
        return HttpResponse(status=202)
    else:
        print(form.errors)
        return HttpResponse('Form Invalid', status=400)
Example #3
0
def start_collection(request):
    projects = []
    plugins = []

    if not request.user.is_authenticated() or not request.user.has_perm(
            'smartshark.start_collection'):
        messages.error(request,
                       'You are not authorized to perform this action.')
        return HttpResponseRedirect('/admin/smartshark/project')

    if request.GET.get('projects'):
        for project_id in request.GET.get('projects', '').split(','):
            projects.append(get_object_or_404(Project, pk=project_id))
    else:
        messages.error(request, 'No project ids were given.')
        return HttpResponseRedirect('/admin/smartshark/project')

    if request.GET.get('plugins'):
        for plugin_id in request.GET.get('plugins', '').split(','):
            plugin = get_object_or_404(Plugin, pk=plugin_id)
            plugins.append(plugin)
    else:
        messages.error(request, 'No plugin ids were given.')
        return HttpResponseRedirect('/admin/smartshark/project')

    if request.method == 'POST':
        if 'cancel' in request.POST:
            return HttpResponseRedirect('/admin/smartshark/project')

        # create a form instance and populate it with data from the request:
        form = get_form(plugins, request.POST, 'execute')

        # check whether it's valid:
        if form.is_valid():
            execution_type = form.cleaned_data.get('execution', None)
            revisions = form.cleaned_data.get('revisions', None)
            repository_url = form.cleaned_data.get('repository_url', None)

            sorted_plugins = order_plugins(plugins)

            for project in projects:

                plugin_executions = []
                for plugin in sorted_plugins:
                    # Create Plugin Execution Objects
                    plugin_execution = PluginExecution(project=project,
                                                       plugin=plugin)

                    if plugin.plugin_type == 'repo' or plugin.plugin_type == 'rev':
                        plugin_execution.repository_url = repository_url

                    if plugin.plugin_type == 'rev':
                        plugin_execution.execution_type = execution_type
                        plugin_execution.revisions = revisions

                    plugin_execution.save()
                    plugin_executions.append(plugin_execution)

                    messages.success(
                        request, 'Started plugin %s on project %s.' %
                        (str(plugin), project.name))

                # Set execution history with execution values for the plugin execution
                set_argument_execution_values(form.cleaned_data,
                                              plugin_executions)

                # Create jobs and execute them in a separate thread
                thread = JobSubmissionThread(project, plugin_executions)
                thread.start()

            return HttpResponseRedirect('/admin/smartshark/project')

    # if a GET (or any other method) we'll create a blank form
    else:
        form = get_form(plugins, request.POST or None, 'execute')

    return render(
        request, 'smartshark/project/execution.html', {
            'form': form,
            'plugins': plugins,
            'projects': projects,
            'substitutions': create_substitutions_for_display()
        })