-
Notifications
You must be signed in to change notification settings - Fork 5
/
helm.py
executable file
·171 lines (122 loc) · 4.7 KB
/
helm.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import base64
import datetime
import hashlib
import json
import os
import yaml
REPONAME = "sample"
PHASE = "alpha"
IMAGENAME = "org/sample"
VERSION = "v0.0.0"
CONTAINER = "app"
ACTION = ""
def parse_args():
p = argparse.ArgumentParser(description="GitOps")
p.add_argument("-r", "--reponame", default=REPONAME, help="reponame")
p.add_argument("-p", "--phase", default=PHASE, help="phase")
p.add_argument("-n", "--imagename", default=IMAGENAME, help="imagename")
p.add_argument("-v", "--version", default=VERSION, help="version")
p.add_argument("-c", "--container", default=CONTAINER, help="container")
p.add_argument("-a", "--action", default=ACTION, help="action")
return p.parse_args()
def update_versions(args):
filepath = "charts/{}/versions-{}.json".format(args.reponame, args.phase)
docs = None
current_time = datetime.datetime.now()
if os.path.exists(filepath):
print("update_versions", filepath)
with open(filepath, "r") as file:
docs = json.load(file)
else:
docs = {}
docs["version"] = ""
docs["items"] = []
if args.action == "approved":
docs["version"] = args.version
docs["approved"] = current_time.isoformat()
isExists = False
for i, doc in enumerate(docs["items"]):
if doc["version"] == args.version:
isExists = True
break
if isExists == False:
docs["items"].append(
{"version": args.version, "updated": current_time.isoformat()}
)
if docs != None:
with open(filepath, "w") as file:
json.dump(docs, file, sort_keys=True, indent=2)
def replace_values(args):
filepath = "charts/{}/values-{}.yaml".format(args.reponame, args.phase)
filehash = ""
docs = None
if os.path.exists(filepath):
print("replace_values", filepath)
with open(filepath, "r") as file:
docs = yaml.safe_load(file)
for i, doc in enumerate(docs):
if args.container != "" and doc != args.container:
continue
print("replace_values", doc, args.version)
# image tag
if "image" in docs[doc]:
docs[doc]["image"]["tag"] = args.version
# configmap
if "configmap" in docs[doc]:
docs[doc]["configmap"]["data"]["VERSION"] = args.version
# secret
if "secret" in docs[doc]:
docs[doc]["secret"]["data"]["SECRET_VERSION"] = base64.b64encode(
args.version.encode("utf-8")
)
if docs != None:
with open(filepath, "w") as file:
yaml.safe_dump(docs, file)
with open(filepath, "rb") as file:
filehash = hashlib.md5(file.read()).hexdigest()
return filehash
def replace_hash(args, hash):
filepath = "charts/{}/values-{}.yaml".format(args.reponame, args.phase)
filehash = ""
docs = None
if os.path.exists(filepath):
print("replace_hash", filepath)
with open(filepath, "r") as file:
docs = yaml.safe_load(file)
for i, doc in enumerate(docs):
if args.container != "" and doc != args.container:
continue
if "env" in docs[doc]:
for i, env in enumerate(docs[doc]["env"]):
has_env_hash = False
has_env_version = False
for i, env in enumerate(docs[doc]["env"]):
print("replace_hash", doc, hash)
if env["name"] == "ENV_HASH":
env["value"] = hash
has_env_hash = True
if env["name"] == "VERSION":
env["value"] = args.version
has_env_version = True
if not has_env_hash:
docs[doc]["env"].append({"name": "ENV_HASH", "value": hash})
if not has_env_version:
docs[doc]["env"].append(
{"name": "VERSION", "value": args.version}
)
if docs != None:
with open(filepath, "w") as file:
yaml.safe_dump(docs, file)
with open(filepath, "rb") as file:
filehash = hashlib.md5(file.read()).hexdigest()
return filehash
def main():
args = parse_args()
update_versions(args)
hash = replace_values(args)
replace_hash(args, hash)
if __name__ == "__main__":
main()