From 16c65d9f119901b127ed0f2ccd2aa590d5b343b0 Mon Sep 17 00:00:00 2001 From: David Moreau Simard Date: Fri, 19 Jul 2019 10:15:23 -0400 Subject: [PATCH] Add an "ara-manage generate" command to export to static HTML This new command renders the templates of the built-in web UI to static html files. Change-Id: I66a346bcee188a194a7a2dd300a41d23906bbe5f --- ara/ui/management/commands/generate.py | 64 ++++++++++++++++++++++++++ tests/role-integration-post.yaml | 3 ++ tests/test_tasks.yaml | 7 +++ 3 files changed, 74 insertions(+) create mode 100644 ara/ui/management/commands/generate.py diff --git a/ara/ui/management/commands/generate.py b/ara/ui/management/commands/generate.py new file mode 100644 index 00000000..21a34c4d --- /dev/null +++ b/ara/ui/management/commands/generate.py @@ -0,0 +1,64 @@ +import os +import shutil + +from django.core.management.base import BaseCommand +from django.template.loader import render_to_string + +from ara.clients.offline import AraOfflineClient + + +class Command(BaseCommand): + help = "Generates a static tree of the web application" + + @staticmethod + def create_dirs(path): + # create main output dir + if not os.path.exists(path): + os.mkdir(path) + + # create subdirs + dirs = ["playbook", "file", "host", "result"] + for dir in dirs: + if not os.path.exists(os.path.join(path, dir)): + os.mkdir(os.path.join(path, dir)) + + # Retrieve static assets (../../static) + shutil.rmtree(os.path.join(path, "static"), ignore_errors=True) + ui_path = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) + shutil.copytree(os.path.join(ui_path, "static"), os.path.join(path, "static")) + + def add_arguments(self, parser): + parser.add_argument("path", help="Path where the static files will be built in", type=str) + + def handle(self, *args, **options): + path = options.get("path") + print("Generating static files at %s..." % path) + self.create_dirs(path) + + client = AraOfflineClient(run_sql_migrations=False) + playbooks = client.get("/api/v1/playbooks") + + with open(os.path.join(path, "index.html"), "w") as f: + f.write(render_to_string("index.html", {"page": "index", "playbooks": playbooks["results"]})) + + for playbook in playbooks["results"]: + detailed_playbook = client.get("/api/v1/playbooks/%s" % playbook["id"]) + with open(os.path.join(path, "playbook/%s.html" % detailed_playbook["id"]), "w") as f: + f.write(render_to_string("playbook.html", {"playbook": detailed_playbook})) + + for file in detailed_playbook["files"]: + detailed_file = client.get("/api/v1/files/%s" % file["id"]) + with open(os.path.join(path, "file/%s.html" % detailed_file["id"]), "w") as f: + f.write(render_to_string("file.html", {"file": detailed_file})) + + for host in detailed_playbook["hosts"]: + detailed_host = client.get("/api/v1/hosts/%s" % host["id"]) + with open(os.path.join(path, "host/%s.html" % detailed_host["id"]), "w") as f: + f.write(render_to_string("host.html", {"host": detailed_host})) + + # This is inefficient but the results are currently nested in plays -> tasks -> results + results = client.get("/api/v1/results", playbook=detailed_playbook["id"]) + for result in results["results"]: + detailed_result = client.get("/api/v1/results/%s" % result["id"]) + with open(os.path.join(path, "result/%s.html" % detailed_result["id"]), "w") as f: + f.write(render_to_string("result.html", {"result": detailed_result})) diff --git a/tests/role-integration-post.yaml b/tests/role-integration-post.yaml index 0a753b24..ec1e4d54 100644 --- a/tests/role-integration-post.yaml +++ b/tests/role-integration-post.yaml @@ -29,6 +29,9 @@ - name: Recover integration test data command: cp -rp {{ ara_api_root_dir }}/server {{ ansible_user_dir }}/workspace/logs/server + - name: Recover static report + command: cp -rp {{ ara_api_root_dir }}/static {{ ansible_user_dir }}/workspace/logs/static + - name: Upload log artifacts synchronize: src: "{{ ansible_user_dir }}/workspace/logs" diff --git a/tests/test_tasks.yaml b/tests/test_tasks.yaml index c8508f0d..345b1ac1 100644 --- a/tests/test_tasks.yaml +++ b/tests/test_tasks.yaml @@ -90,3 +90,10 @@ args: executable: /bin/bash ignore_errors: yes + + - name: Generate static report + command: > + {{ ara_api_venv_path }}/bin/ara-manage generate {{ ara_api_root_dir }}/static + +- name: List static report files + command: ls -alR {{ ara_api_root_dir }}/static