Example #1
0
    def get(self, request, *args, **kwargs):
        playbook = serializers.DetailedPlaybookSerializer(self.get_object())
        hosts = serializers.ListHostSerializer(models.Host.objects.filter(
            playbook=playbook.data["id"]).order_by("name").all(),
                                               many=True)
        files = serializers.ListFileSerializer(
            models.File.objects.filter(playbook=playbook.data["id"]).all(),
            many=True)
        records = serializers.ListRecordSerializer(
            models.Record.objects.filter(playbook=playbook.data["id"]).all(),
            many=True)

        search_form = forms.ResultSearchForm(request.GET)
        order = "-started"
        if "order" in request.GET:
            order = request.GET["order"]
        result_queryset = models.Result.objects.filter(
            playbook=playbook.data["id"]).order_by(order).all()
        result_filter = filters.ResultFilter(request.GET,
                                             queryset=result_queryset)

        page = self.paginate_queryset(result_filter.qs)
        if page is not None:
            serializer = serializers.ListResultSerializer(page, many=True)
        else:
            serializer = serializers.ListResultSerializer(result_filter,
                                                          many=True)

        for result in serializer.data:
            task_id = result["task"]
            result["task"] = serializers.SimpleTaskSerializer(
                models.Task.objects.get(pk=task_id)).data
            host_id = result["host"]
            result["host"] = serializers.SimpleHostSerializer(
                models.Host.objects.get(pk=host_id)).data
        paginated_results = self.get_paginated_response(serializer.data)

        if self.paginator.count > (self.paginator.offset +
                                   self.paginator.limit):
            max_current = self.paginator.offset + self.paginator.limit
        else:
            max_current = self.paginator.count
        current_page_results = "%s-%s" % (self.paginator.offset + 1,
                                          max_current)

        # fmt: off
        return Response({
            "playbook": playbook.data,
            "hosts": hosts.data,
            "files": files.data,
            "records": records.data,
            "results": paginated_results.data,
            "current_page_results": current_page_results,
            "search_form": search_form
        })
Example #2
0
    def get(self, request, *args, **kwargs):
        host = self.get_object()
        host_serializer = serializers.DetailedHostSerializer(host)

        order = "-started"
        if "order" in request.GET:
            order = request.GET["order"]
        result_queryset = models.Result.objects.filter(
            host=host_serializer.data["id"]).order_by(order).all()
        result_filter = filters.ResultFilter(request.GET,
                                             queryset=result_queryset)

        page = self.paginate_queryset(result_filter.qs)
        if page is not None:
            result_serializer = serializers.ListResultSerializer(page,
                                                                 many=True)
        else:
            result_serializer = serializers.ListResultSerializer(result_filter,
                                                                 many=True)

        for result in result_serializer.data:
            task_id = result["task"]
            result["task"] = serializers.SimpleTaskSerializer(
                models.Task.objects.get(pk=task_id)).data
        paginated_results = self.get_paginated_response(result_serializer.data)

        if self.paginator.count > (self.paginator.offset +
                                   self.paginator.limit):
            max_current = self.paginator.offset + self.paginator.limit
        else:
            max_current = self.paginator.count
        current_page_results = "%s-%s" % (self.paginator.offset + 1,
                                          max_current)

        # We need to expand the search card if there is a search query, not considering pagination args
        search_args = [
            arg for arg in request.GET.keys()
            if arg not in ["limit", "offset"]
        ]
        expand_search = True if search_args else False

        search_form = forms.ResultSearchForm(request.GET)

        # fmt: off
        return Response(
            dict(
                current_page_results=current_page_results,
                expand_search=expand_search,
                host=host_serializer.data,
                page="host",
                results=paginated_results.data,
                search_form=search_form,
                static_generation=False,
            ))
Example #3
0
    def handle(self, *args, **options):
        path = options.get("path")
        self.create_dirs(path)

        # TODO: Leverage ui views directly instead of duplicating logic here
        query = models.Playbook.objects.all().order_by("-id")
        serializer = serializers.ListPlaybookSerializer(query, many=True)

        print("[ara] Generating static files for %s playbooks at %s..." %
              (query.count(), path))

        # Index
        destination = os.path.join(path, "index.html")
        data = {
            "data": {
                "results": serializer.data
            },
            "static_generation": True,
            "page": "index"
        }
        self.render("index.html", destination, **data)

        # Escape surrogates to prevent UnicodeEncodeError exceptions
        codecs.register_error("strict", codecs.lookup_error("surrogateescape"))

        # Playbooks
        for pb in query:
            playbook = serializers.DetailedPlaybookSerializer(pb)
            hosts = serializers.ListHostSerializer(
                models.Host.objects.filter(playbook=playbook.data["id"]).all(),
                many=True)
            files = serializers.ListFileSerializer(
                models.File.objects.filter(playbook=playbook.data["id"]).all(),
                many=True)
            records = serializers.ListRecordSerializer(
                models.Record.objects.filter(
                    playbook=playbook.data["id"]).all(),
                many=True)
            results = serializers.ListResultSerializer(
                models.Result.objects.filter(
                    playbook=playbook.data["id"]).all(),
                many=True)

            # Backfill task and host data into results
            for result in results.data:
                task_id = result["task"]
                result["task"] = serializers.SimpleTaskSerializer(
                    models.Task.objects.get(pk=task_id)).data
                host_id = result["host"]
                result["host"] = serializers.SimpleHostSerializer(
                    models.Host.objects.get(pk=host_id)).data

            # Results are paginated in the dynamic version and the template expects data in a specific format
            formatted_results = {
                "count": len(results.data),
                "results": results.data
            }

            destination = os.path.join(
                path, "playbooks/%s.html" % playbook.data["id"])
            self.render(
                "playbook.html",
                destination,
                static_generation=True,
                playbook=playbook.data,
                hosts=hosts.data,
                files=files.data,
                records=records.data,
                results=formatted_results,
                current_page_results=None,
                search_form=None,
            )

        # Files
        query = models.File.objects.all()
        for file in query.all():
            destination = os.path.join(path, "files/%s.html" % file.id)
            serializer = serializers.DetailedFileSerializer(file)
            data = {"file": serializer.data, "static_generation": True}
            self.render("file.html", destination, **data)

        # Hosts
        query = models.Host.objects.all()
        for host in query.all():
            destination = os.path.join(path, "hosts/%s.html" % host.id)
            serializer = serializers.DetailedHostSerializer(host)
            data = {"host": serializer.data, "static_generation": True}
            self.render("host.html", destination, **data)

        # Results
        query = models.Result.objects.all()
        for result in query.all():
            destination = os.path.join(path, "results/%s.html" % result.id)
            serializer = serializers.DetailedResultSerializer(result)
            data = {"result": serializer.data, "static_generation": True}
            self.render("result.html", destination, **data)

        # Records
        query = models.Record.objects.all()
        for record in query.all():
            destination = os.path.join(path, "records/%s.html" % record.id)
            serializer = serializers.DetailedRecordSerializer(record)
            data = {"record": serializer.data, "static_generation": True}
            self.render("record.html", destination, **data)

        print("[ara] %s files generated." % self.rendered)
Example #4
0
    def handle(self, *args, **options):
        path = options.get("path")
        self.create_dirs(path)

        # TODO: Leverage ui views directly instead of duplicating logic here
        query = models.Playbook.objects.all().order_by("-id")
        serializer = serializers.ListPlaybookSerializer(query, many=True)

        print("[ara] Generating static files for %s playbooks at %s..." %
              (query.count(), path))

        # Playbook index
        destination = os.path.join(path, "index.html")
        data = {
            "data": {
                "results": serializer.data
            },
            "page": "index",
            **self.DEFAULT_PARAMS
        }
        self.render("index.html", destination, **data)

        # Escape surrogates to prevent UnicodeEncodeError exceptions
        codecs.register_error("strict", codecs.lookup_error("surrogateescape"))

        # Playbooks
        for pb in query:
            playbook = serializers.DetailedPlaybookSerializer(pb)
            hosts = serializers.ListHostSerializer(models.Host.objects.filter(
                playbook=playbook.data["id"]).order_by("name").all(),
                                                   many=True)
            files = serializers.ListFileSerializer(
                models.File.objects.filter(playbook=playbook.data["id"]).all(),
                many=True)
            records = serializers.ListRecordSerializer(
                models.Record.objects.filter(
                    playbook=playbook.data["id"]).all(),
                many=True)
            results = serializers.ListResultSerializer(
                models.Result.objects.filter(
                    playbook=playbook.data["id"]).all(),
                many=True)

            # Backfill task and host data into results
            for result in results.data:
                task_id = result["task"]
                result["task"] = serializers.SimpleTaskSerializer(
                    models.Task.objects.get(pk=task_id)).data
                host_id = result["host"]
                result["host"] = serializers.SimpleHostSerializer(
                    models.Host.objects.get(pk=host_id)).data
                if result["delegated_to"]:
                    delegated_to = [
                        models.Host.objects.get(pk=delegated)
                        for delegated in result["delegated_to"]
                    ]
                    result["delegated_to"] = serializers.SimpleHostSerializer(
                        delegated_to, many=True).data

            # Results are paginated in the dynamic version and the template expects data in a specific format
            formatted_results = {
                "count": len(results.data),
                "results": results.data
            }

            destination = os.path.join(
                path, "playbooks/%s.html" % playbook.data["id"])
            self.render("playbook.html",
                        destination,
                        playbook=playbook.data,
                        hosts=hosts.data,
                        files=files.data,
                        records=records.data,
                        results=formatted_results,
                        current_page_results=None,
                        search_form=None,
                        page="playbook",
                        **self.DEFAULT_PARAMS)

        # Files
        query = models.File.objects.all()
        for file in query.all():
            destination = os.path.join(path, "files/%s.html" % file.id)
            serializer = serializers.DetailedFileSerializer(file)
            data = {
                "file": serializer.data,
                "page": "file",
                **self.DEFAULT_PARAMS
            }
            self.render("file.html", destination, **data)

        # Hosts
        query = models.Host.objects.all()
        for host in query.all():
            destination = os.path.join(path, "hosts/%s.html" % host.id)
            serializer = serializers.DetailedHostSerializer(host)

            # fmt: off
            host_results = serializers.ListResultSerializer(
                models.Result.objects.filter(host=host.id).all(), many=True)
            # fmt: on

            # Backfill task data into results
            for result in host_results.data:
                task_id = result["task"]
                result["task"] = serializers.SimpleTaskSerializer(
                    models.Task.objects.get(pk=task_id)).data

            # Results are paginated in the dynamic version and the template expects data in a specific format
            formatted_results = {
                "count": len(host_results.data),
                "results": host_results.data
            }

            self.render("host.html",
                        destination,
                        current_page_results=None,
                        host=serializer.data,
                        page="host",
                        results=formatted_results,
                        search_form=None,
                        **self.DEFAULT_PARAMS)

        # Results
        query = models.Result.objects.all()
        for result in query.all():
            destination = os.path.join(path, "results/%s.html" % result.id)
            serializer = serializers.DetailedResultSerializer(result)
            data = {
                "result": serializer.data,
                "page": "result",
                **self.DEFAULT_PARAMS
            }
            self.render("result.html", destination, **data)

        # Records
        query = models.Record.objects.all()
        for record in query.all():
            destination = os.path.join(path, "records/%s.html" % record.id)
            serializer = serializers.DetailedRecordSerializer(record)
            data = {
                "record": serializer.data,
                "page": "record",
                **self.DEFAULT_PARAMS
            }
            self.render("record.html", destination, **data)

        # Host index
        # The toggle between latests hosts and all hosts is dynamic with a toggle in the UI
        # Provide all hosts for the static version, we can consider adding a CLI flag if there is a use case for it
        query = models.Host.objects.all().order_by("-updated")
        serializer = serializers.DetailedHostSerializer(query, many=True)

        destination = os.path.join(path, "hosts/index.html")
        data = {
            "data": {
                "results": serializer.data
            },
            "page": "host_index",
            **self.DEFAULT_PARAMS
        }
        self.render("host_index.html", destination, **data)

        print("[ara] %s files generated." % self.rendered)
Example #5
0
File: views.py Project: srgvg/ara
    def get(self, request, *args, **kwargs):
        playbook = serializers.DetailedPlaybookSerializer(self.get_object())
        hosts = serializers.ListHostSerializer(models.Host.objects.filter(
            playbook=playbook.data["id"]).order_by("name").all(),
                                               many=True)
        files = serializers.ListFileSerializer(
            models.File.objects.filter(playbook=playbook.data["id"]).all(),
            many=True)
        records = serializers.ListRecordSerializer(
            models.Record.objects.filter(playbook=playbook.data["id"]).all(),
            many=True)

        order = "-started"
        if "order" in request.GET:
            order = request.GET["order"]
        result_queryset = models.Result.objects.filter(
            playbook=playbook.data["id"]).order_by(order).all()
        result_filter = filters.ResultFilter(request.GET,
                                             queryset=result_queryset)

        page = self.paginate_queryset(result_filter.qs)
        if page is not None:
            serializer = serializers.ListResultSerializer(page, many=True)
        else:
            serializer = serializers.ListResultSerializer(result_filter,
                                                          many=True)

        # TODO: We should have a serializer that takes care of this automatically instead of backfilling "manually"
        for result in serializer.data:
            task = models.Task.objects.get(pk=result["task"])
            result["task"] = serializers.SimpleTaskSerializer(task).data
            host = models.Host.objects.get(pk=result["host"])
            result["host"] = serializers.SimpleHostSerializer(host).data
            if result["delegated_to"]:
                delegated_to = [
                    models.Host.objects.get(pk=delegated)
                    for delegated in result["delegated_to"]
                ]
                result["delegated_to"] = serializers.SimpleHostSerializer(
                    delegated_to, many=True).data
        paginated_results = self.get_paginated_response(serializer.data)

        if self.paginator.count > (self.paginator.offset +
                                   self.paginator.limit):
            max_current = self.paginator.offset + self.paginator.limit
        else:
            max_current = self.paginator.count
        current_page_results = "%s-%s" % (self.paginator.offset + 1,
                                          max_current)

        # We need to expand the search card if there is a search query, not considering pagination args
        search_args = [
            arg for arg in request.GET.keys()
            if arg not in ["limit", "offset"]
        ]
        expand_search = True if search_args else False

        search_form = forms.ResultSearchForm(request.GET)

        # fmt: off
        return Response(
            dict(
                current_page_results=current_page_results,
                expand_search=expand_search,
                files=files.data,
                hosts=hosts.data,
                page="playbook",
                playbook=playbook.data,
                records=records.data,
                results=paginated_results.data,
                search_form=search_form,
                static_generation=False,
            ))