Ejemplo n.º 1
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)
Ejemplo n.º 2
0
 def get(self, request, *args, **kwargs):
     record = self.get_object()
     serializer = serializers.DetailedRecordSerializer(record)
     return Response({
         "record": serializer.data,
         "static_generation": False,
         "page": "result"
     })
Ejemplo n.º 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)
Ejemplo n.º 4
0
 def get(self, request, *args, **kwargs):
     record = self.get_object()
     serializer = serializers.DetailedRecordSerializer(record)
     return Response({"record": serializer.data})
Ejemplo n.º 5
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)