-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvars.py
executable file
·137 lines (121 loc) · 4.62 KB
/
vars.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
import jinja2
import pathlib
import os
import io
import argparse
import yaml
import re
import kubernetes
import base64
from openshift.dynamic import DynamicClient
from distutils.util import strtobool
def split(str, separator):
return str.split(separator)
def basename(path):
return os.path.basename(path)
def dirname(path):
return os.path.dirname(path)
def realpath(path):
return os.path.realpath(path)
def b64decode(str):
return base64.b64decode(str).decode("utf-8")
def to_bool(s):
if s == None:
return False
else:
return bool(strtobool(str(s)))
def lookup(lookup, arg1, errors='strict'):
if lookup == 'env':
return os.getenv(arg1)
elif lookup == 'file':
if os.path.exists(arg1):
try:
with open(arg1, 'r') as file:
file_content = file.read()
return file_content
except Exception as ex:
return None
else:
return None
def query(type, kind, resource_name, namespace, api_version=None):
if type == 'kubernetes.core.k8s':
if kind == 'Secret':
try:
v1 = kubernetes.client.CoreV1Api()
secret = v1.read_namespaced_secret(resource_name, namespace)
return [secret]
except Exception as ex:
#raise Exception('%s: %s - %s' % (k, v, ex), ex)
return [{"data":{}}]
elif kind == 'Ingress':
try:
v1 = kubernetes.client.NetworkingV1Api()
ingress = v1.read_namespaced_ingress(resource_name, namespace)
return [ingress]
except Exception as ex:
#raise Exception('%s: %s - %s' % (k, v, ex), ex)
return [{"spec":{}}]
elif api_version != None:
try:
route = openshift_client.resources.get(api_version='route.openshift.io/v1', kind='Route').get(name=resource_name, namespace=namespace)
return [route]
except Exception as ex:
#raise Exception('%s: %s - %s' % (k, v, ex), ex)
return [{"spec":{}}]
return [{"spec":{}}]
# define a custom representer for strings
def quoted_presenter(dumper, data):
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style="'")
if __name__ == '__main__':
if os.path.exists('/var/run/secrets/kubernetes.io/serviceaccount/namespace'):
kubernetes.config.load_incluster_config()
else:
kubernetes.config.load_kube_config()
k8s_client = kubernetes.client.ApiClient()
openshift_client = DynamicClient(k8s_client)
parser = argparse.ArgumentParser(description='Parse vars.yaml file for a computate project')
parser.add_argument('path', nargs='?', default='vars.yaml', help='the path to the vars.yaml file for the project to generate')
args = parser.parse_args()
filters = {
"basename": basename
, "dirname": dirname
}
config = {}
vars_path = pathlib.Path(args.path).resolve()
vars_str = vars_path.read_text()
original_data = yaml.safe_load(vars_str)
new_data = {}
new_data["VARS_PATH"] = str(vars_path)
config["VARS_PATH"] = str(vars_path)
for k, v in original_data.items():
if isinstance(v, str) \
or isinstance(v, int) \
or isinstance(v, float) \
or isinstance(v, complex):
try:
t = jinja2.Template(v)
t.environment.filters['split'] = split
t.environment.filters['basename'] = basename
t.environment.filters['dirname'] = dirname
t.environment.filters['realpath'] = realpath
t.environment.filters['b64decode'] = b64decode
t.environment.filters['bool'] = to_bool
t.environment.globals.update(lookup = lookup)
t.environment.globals.update(query = query)
new_data[k] = t.render(**new_data)
config[k] = str(new_data[k])
except TypeError as ex:
if str(ex) == "Can't compile non template nodes":
new_data[k] = v
config[k] = str(new_data[k])
# else:
# raise Exception('%s: %s - %s' % (k, v, ex), ex)
str_io = io.StringIO()
yaml.add_representer(str, quoted_presenter)
yaml.dump(config, str_io, width=float('inf'), default_flow_style=False)
str = str_io.getvalue()
str = str.replace("': '", "'='")
str = re.sub(r"'([^']+)'=", r"\1=", str)
str = re.sub(r"([^=])''", r'\1' + "'\"'\"'", str)
print(str)