-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathokd_helper.py
More file actions
executable file
·247 lines (188 loc) · 6.91 KB
/
okd_helper.py
File metadata and controls
executable file
·247 lines (188 loc) · 6.91 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Script to help in templating and creating/updating OKD objects
"""
import argparse
import base64
import difflib
import json
import os
import random
import string
import subprocess
import sys
import boto3
from jinja2 import Environment, FileSystemLoader, StrictUndefined
import yaml
loader = yaml.Loader
def random_digit_string(length):
return ''.join(
random.SystemRandom().choice(
string.ascii_uppercase + string.ascii_lowercase + string.digits)
for i in range(length))
def ensure_bytes(bytes_or_str):
try:
out = bytes_or_str.encode('utf-8')
except AttributeError:
out = bytes_or_str
return out
def ensure_str(bytes_or_str):
try:
out = bytes_or_str.decode('utf-8')
except AttributeError:
out = bytes_or_str
return out
def b64encode(value):
return ensure_str(base64.b64encode(ensure_bytes(value)))
def to_json(value):
return json.dumps(value)
def to_env_file(value):
out = ''
for k, v in value.items():
if isinstance(v, str):
v = '"{}"'.format(v)
out += '{k}={v}\n'.format(k=k, v=v)
return ensure_str(out)
def random_constructor(loader, node):
value = loader.construct_scalar(node)
return random_digit_string(int(value))
def ssm_constructor(loader, node):
value = loader.construct_scalar(node)
client = boto3.client('ssm')
resp = client.get_parameter(Name=value, WithDecryption=True)
return resp['Parameter']['Value']
yaml.add_constructor('!random', random_constructor, Loader=loader)
yaml.add_constructor('!ssm', ssm_constructor, Loader=loader)
def make_parser():
parser = argparse.ArgumentParser(
description=(
'Provide helpers to template OpenShift yaml config files,'
' apply changes, and see differences'),
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
def print_help(args):
parser.print_help()
return ''
parser.set_defaults(func=print_help)
subparsers = parser.add_subparsers(help='Sub-commands')
apply_parser = subparsers.add_parser(
'apply', help='Apply the new config',
description='Apply the config to OpenShift (create or update objects)')
apply_parser.set_defaults(func=apply_config)
diff_parser = subparsers.add_parser(
'diff', help='Show differences',
description='Show the difference between the current and new configs')
diff_parser.set_defaults(func=diff_config)
template_parser = subparsers.add_parser(
'template', help='Template the config files',
description='Only template and print the configs')
template_parser.set_defaults(func=template_config)
for p in (diff_parser, apply_parser, template_parser):
p.add_argument('path',
help='File or folder to template and apply/diff')
p.add_argument('-n', '--namespace',
help='OpenShift project namespace')
random_parser = subparsers.add_parser(
'random', help='Generate a random string',
description='Generate a random alphanumeric string')
random_parser.add_argument('length', type=int,
help='Length of random string')
random_parser.set_defaults(func=random_print)
args = parser.parse_args()
return args
def load_vars(path):
fargs = {}
if os.path.isfile(path):
args = yaml.load(open(path, 'r'), Loader=loader)
if 'include' in args:
for apath in args['include']:
# each succesive includes overwrites previous
fargs.update(yaml.load(
open(os.path.join(
os.path.dirname(path),
apath), 'r'), Loader=loader))
fargs.update(args)
return fargs
class RelEnvironment(Environment):
def join_path(self, template, parent):
return os.path.join(os.path.dirname(parent), template)
def render_templates(args):
path = os.path.expanduser(args.path)
if os.path.isdir(path):
basedir = path
recurse = True
else:
basedir = os.path.dirname(path)
recurse = False
env = RelEnvironment(
loader=FileSystemLoader(basedir, followlinks=True),
undefined=StrictUndefined,
autoescape=False)
env.filters['b64encode'] = b64encode
env.filters['to_json'] = to_json
env.filters['to_env_file'] = to_env_file
if recurse:
all_templates = env.list_templates(
filter_func=lambda x: x.endswith('.yml'))
else:
all_templates = [os.path.basename(path),]
templated_configs = []
for template_file in all_templates:
template = env.get_template(template_file)
vars_path = os.path.join(os.path.dirname(template.filename), 'vars')
rendered = template.render(**load_vars(vars_path))
templated_configs.append((rendered, template_file))
return templated_configs
def template_config(args):
templated_configs = render_templates(args)
return '\n---\n'.join(list(zip(*templated_configs))[0])
def get_last_applied(kind, name, namespace=None):
cmd = ['oc', 'apply', 'view-last-applied', kind, name]
if namespace is not None:
cmd += ['-n', namespace]
try:
out = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
if b'NotFound' in e.output:
out = ''
else:
print(ensure_str(e.output))
sys.exit(e.returncode)
return ensure_str(out)
def diff_config(args):
templated_configs = render_templates(args)
def make_diff(templated_config, filename):
config_yaml = yaml.load(templated_config, Loader=loader)
last_applied = get_last_applied(
config_yaml['kind'], config_yaml['metadata']['name'],
args.namespace)
config_sorted_text = yaml.dump(config_yaml, default_flow_style=False)
diff = ''.join(
difflib.unified_diff(
last_applied.splitlines(1),
config_sorted_text.splitlines(1),
fromfile='Current',
tofile=filename))
return diff
return ('\n'+'='*40+'\n').join([make_diff(tc, f)
for tc, f in templated_configs])
def apply_template(template, namespace=None):
cmd = ['oc', 'apply', '-f', '-']
if namespace is not None:
cmd.extend(['-n', namespace])
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
out, err = proc.communicate(input=ensure_bytes(template))
return ensure_str(out)
def apply_config(args):
one_big_template = template_config(args)
return apply_template(one_big_template, args.namespace)
def random_print(args):
return random_digit_string(args.length)
def main():
args = make_parser()
out = args.func(args)
print(out)
if __name__ == '__main__':
main()