Пример #1
0
    def get(self, request, *args, **kwargs):
        query = self.filter_queryset(self.queryset.all().order_by("-id"))
        page = self.paginate_queryset(query)
        if page is not None:
            serializer = serializers.ListPlaybookSerializer(page, many=True)
        else:
            serializer = serializers.ListPlaybookSerializer(query, many=True)
        response = 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.PlaybookSearchForm(request.GET)

        # fmt: off
        return Response(
            dict(current_page_results=current_page_results,
                 data=response.data,
                 expand_search=expand_search,
                 page="index",
                 search_form=search_form,
                 static_generation=False))
Пример #2
0
    def get(self, request, *args, **kwargs):
        search_query = False
        if request.GET:
            search_query = True
        search_form = forms.PlaybookSearchForm(request.GET)

        query = self.filter_queryset(self.queryset.all().order_by("-id"))
        page = self.paginate_queryset(query)
        if page is not None:
            serializer = serializers.ListPlaybookSerializer(page, many=True)
        else:
            serializer = serializers.ListPlaybookSerializer(query, many=True)
        response = 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)

        return Response({
            "page": "index",
            "data": response.data,
            "search_form": search_form,
            "search_query": search_query,
            "current_page_results": current_page_results,
        })
Пример #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()
        serializer = serializers.ListPlaybookSerializer(query.all(), many=True)

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

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

        # Playbooks
        for playbook in query.all():
            destination = os.path.join(path, "playbook/%s.html" % playbook.id)
            serializer = serializers.DetailedPlaybookSerializer(playbook)
            data = {"playbook": serializer.data, "static_generation": True}
            self.render("playbook.html", destination, **data)

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

        # Hosts
        query = models.Host.objects.all()
        for host in query.all():
            destination = os.path.join(path, "host/%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, "result/%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, "record/%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)
Пример #4
0
    def get(self, request, *args, **kwargs):
        # TODO: Can we retrieve those fields automatically ?
        fields = [
            "order", "controller", "name", "started_after", "status", "label"
        ]
        search_query = False
        for field in fields:
            if field in request.GET:
                search_query = True

        if search_query:
            search_form = forms.PlaybookSearchForm(request.GET)
        else:
            search_form = forms.PlaybookSearchForm()

        query = self.filter_queryset(self.queryset.all().order_by("-id"))
        page = self.paginate_queryset(query)
        if page is not None:
            serializer = serializers.ListPlaybookSerializer(page, many=True)
        else:
            serializer = serializers.ListPlaybookSerializer(query, many=True)
        response = 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)

        return Response({
            "page": "index",
            "data": response.data,
            "search_form": search_form,
            "search_query": search_query,
            "current_page_results": current_page_results,
        })
Пример #5
0
    def get(self, request, *args, **kwargs):
        # TODO: Can we retrieve those fields automatically ?
        fields = ["order", "name", "started_after", "status"]
        search_query = False
        for field in fields:
            if field in request.GET:
                search_query = True

        if search_query:
            search_form = forms.PlaybookSearchForm(request.GET)
        else:
            search_form = forms.PlaybookSearchForm()

        serializer = serializers.ListPlaybookSerializer(self.filter_queryset(
            self.queryset.all()),
                                                        many=True)
        return Response({
            "page": "index",
            "playbooks": serializer.data,
            "search_form": search_form,
            "search_query": search_query
        })
Пример #6
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)
Пример #7
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)
Пример #8
0
 def get(self, request, *args, **kwargs):
     serializer = serializers.ListPlaybookSerializer(self.queryset.all(),
                                                     many=True)
     return Response({"page": "index", "playbooks": serializer.data})