This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
manifest.py
61 lines (48 loc) · 1.67 KB
/
manifest.py
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
# vim:set sw=2 ts=2 et:
#
# Copyright (c) 2016-2017 Torchbox Ltd.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely. This software is provided 'as-is', without any express or implied
# warranty.
from os import environ
from base64 import b64encode
from sys import stdout, stderr, exit
import re, yaml
# Load a YAML manifest from disk and perform environment substitution on it,
# based on the environment and our arguments. Returns an array of items loaded
# from the YAML; this needs to be converted to a List before sending it to
# kubectl.
def load_manifest(args, filename):
# Avoid modifying the system environment.
menv = environ.copy()
with open(filename, 'r') as f:
spec = f.read()
menv['IMAGE'] = args.image
menv['NAME'] = args.name
menv['NAMESPACE'] = args.namespace
for env in args.env:
(var, value) = env.split('=', 1)
menv[var] = value
def envrep(m):
funcs = {
'b64encode': lambda v: b64encode(v.encode('utf-8')).decode('utf-8'),
}
bits = m.group(2).split(':')
try:
var = menv[bits[0]]
except KeyError:
stderr.write(args.manifest+ ": $" + bits[0] + " not in environment.\n")
exit(1)
if len(bits) > 1:
if bits[1] not in funcs:
stderr.write(args.manifest + ": function " + bits[1] + " unknown.\n")
return funcs[bits[1]](var, *bits[2:])
else:
return var
spec = re.sub(r"\$({)?([A-Za-z_][A-Za-z0-9_:]+)(?(1)})", envrep, spec)
items = []
for item in yaml.load_all(spec):
items.append(item)
return items