File: render-template.py

package info (click to toggle)
lava 2026.01-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 30,796 kB
  • sloc: python: 82,790; javascript: 16,658; sh: 1,364; makefile: 335
file content (80 lines) | stat: -rwxr-xr-x 2,317 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#! /usr/bin/python3

"""
This script is particularly intended for those adding new devices to LAVA
and developing new jinja templates to create the per-device configuration
in the Device Dictionary database table.

In a production system, the devices directory will be stored in
/etc/lava-server/dispatcher-config/devices/<hostname>.jinja2

When developers are working on new support directly from the command line
lava-dispatch or developing new templates, this script can be used to match the
template output with existing templates.

The path used needs to contain both the jinja2 device-type template in a
device-types/ directory *and* the jinja2 device dictionary file for the device
to review in a devices/ directory.

(This script will go into the lava-dev binary package.)

"""

#  Copyright 2014 Linaro Limited
# Author: Remi Duraffort <remi.duraffort@linaro.org>
#
# SPDX-License-Identifier: GPL-2.0-or-later

# pylint gets confused: commands have no shebang, but the file is not a module.


import argparse
import os

import yaml
from jinja2 import FileSystemLoader
from jinja2.sandbox import SandboxedEnvironment


def main():
    parser = argparse.ArgumentParser(description="LAVA Dispatcher template helper")
    parser.add_argument(
        "--device", type=str, required=True, help="Path to the device template file"
    )
    parser.add_argument(
        "--path",
        default="/etc/lava-server/dispatcher-config/",
        type=str,
        help="Path to the device-types template folder",
    )
    args = parser.parse_args()

    env = SandboxedEnvironment(
        loader=FileSystemLoader(
            [
                os.path.join(args.path, "devices"),
                os.path.join(args.path, "device-types"),
            ]
        ),
        trim_blocks=True,
        autoescape=False,
    )
    if not os.path.exists(
        os.path.join(args.path, "devices", "%s.jinja2" % args.device)
    ):
        print("Cannot find %s device configuration file" % args.device)
        return
    template = env.get_template("%s.jinja2" % args.device)
    ctx = {}
    config = template.render(**ctx)

    print("YAML config")
    print("===========")
    print(config)
    print("Parsed config")
    print("=============")
    print(yaml.safe_load(config))


if __name__ == "__main__":
    main()