1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
|
3
|
|
|
# -*- coding: utf-8 -*- |
4
|
|
|
|
5
|
|
|
# hold global variables and functions for direct use in templates |
6
|
|
|
# https://jinja.palletsprojects.com/en/2.11.x/api/#the-global-namespace |
7
|
|
|
# https://jinja.palletsprojects.com/en/2.11.x/templates/#list-of-global-functions |
8
|
|
|
|
9
|
1 |
|
from datetime import datetime, timezone |
10
|
1 |
|
import inspect |
11
|
1 |
|
import markdown as _markdown |
12
|
1 |
|
import platform as _platform |
13
|
1 |
|
import socket |
14
|
1 |
|
import sys |
15
|
|
|
|
16
|
1 |
|
from . import utils |
17
|
|
|
|
18
|
1 |
|
def _os_release(k=None): |
19
|
1 |
|
ret = {} |
20
|
1 |
|
for line in open('/etc/os-release').read().strip().split('\n'): |
21
|
1 |
|
k,v = line.split('=', 1) |
22
|
1 |
|
ret[k] = v.strip('"') |
23
|
1 |
|
return ret |
24
|
|
|
|
25
|
|
|
""" |
26
|
|
|
_globals contains the dictionary of functions (implemented as lambda functions) |
27
|
|
|
or variables (regular values) that are accessible inside template expressions. |
28
|
|
|
""" |
29
|
1 |
|
_globals = dict( |
30
|
|
|
|
31
|
|
|
date = ( """ Return the timestamp for datetime.now() """, |
32
|
|
|
datetime.now() # variable |
33
|
|
|
), |
34
|
|
|
|
35
|
|
|
host_id = ( """ Return the host's ID """, |
36
|
|
|
lambda : _cmd('hostid') |
|
|
|
|
37
|
|
|
), |
38
|
|
|
|
39
|
|
|
fqdn = ( """ Return the current host's fqdn """, |
40
|
|
|
lambda : socket.getfqdn() |
41
|
|
|
), |
42
|
|
|
|
43
|
|
|
hostname = ( """ Return the current host's name """, |
44
|
|
|
lambda : socket.gethostname() |
45
|
|
|
), |
46
|
|
|
|
47
|
|
|
machine_id = ( """ Return the machine's ID """, |
48
|
|
|
lambda : open('/etc/machine-id').read().strip() |
49
|
|
|
), |
50
|
|
|
|
51
|
|
|
markdown = ( """ Load content from a markdown file and convert it to html """, |
52
|
|
|
lambda f, |
53
|
|
|
output_format='html5', |
54
|
|
|
extensions=[ |
55
|
|
|
'admonition', |
56
|
|
|
'extra', |
57
|
|
|
'meta', |
58
|
|
|
'sane_lists', |
59
|
|
|
'smarty', |
60
|
|
|
'toc', |
61
|
|
|
'wikilinks', |
62
|
|
|
'wikilinks', |
63
|
|
|
], |
64
|
|
|
extension_configs = { |
65
|
|
|
'codehilite': { |
66
|
|
|
'linenums': True, |
67
|
|
|
'guess_lang': False, |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
: _markdown.markdown( |
71
|
|
|
utils.load_file(f), |
72
|
|
|
extensions=extensions, |
73
|
|
|
output_format=output_format |
74
|
|
|
) |
75
|
|
|
), |
76
|
|
|
|
77
|
|
|
now = ( """ Return the timestamp for datetime.now() """, |
78
|
|
|
lambda : datetime.now() |
79
|
|
|
), |
80
|
|
|
|
81
|
|
|
os = ( """ Dictionary holding the contents of /etc/os-release """, |
82
|
|
|
_os_release() |
83
|
|
|
), |
84
|
|
|
|
85
|
|
|
os_release = ( """ Lookup key in /etc/os-release and return its value """, |
86
|
|
|
lambda k: _os_release()[k] |
87
|
|
|
), |
88
|
|
|
|
89
|
|
|
platform = ( """ Access functions in the platform module """, |
90
|
|
|
dict(set(inspect.getmembers(_platform, inspect.isfunction))) |
91
|
|
|
), |
92
|
|
|
|
93
|
|
|
run = ( |
94
|
|
|
""" |
95
|
|
|
Run a command and return its STDOUT |
96
|
|
|
e.g. hello {{ run('id -un') }} |
97
|
|
|
""", |
98
|
|
|
lambda v: utils.cmd(v) |
99
|
|
|
), |
100
|
|
|
|
101
|
|
|
) |
102
|
|
|
|
103
|
|
|
|
104
|
1 |
|
for k,v in _globals.items(): |
105
|
|
|
setattr(sys.modules[__name__], k, v[1]) |
106
|
|
|
|