-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPushUpdates.py
159 lines (150 loc) · 5.77 KB
/
PushUpdates.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
"""
Python script used by Git Actions automation to apply changes to SLATE instances
described by a Git repository.
Originally written by Mitchell Steinman
"""
import sys
import requests
PathToChangedFiles = sys.argv[1]
slateToken = sys.argv[2]
try:
ChangedFiles = open(PathToChangedFiles, "r").read().split("\n")
except Exception as e:
print("Failed to open temp file", PathToChangedFiles, e)
for Entry in ChangedFiles:
print(Entry, "\n")
# Parse entry containing file name and change status
if Entry == "":
print("Skipping file", Entry, "\n")
continue
# Status: M = Modified, A = Added, D = Removed
FileStatus = Entry.split()[0]
FileName = Entry.split()[1]
# The "container" is any arbitrary path before the slate details
# 'values.yaml' and 'instance.yaml'
containerName = FileName.split("/values.yaml")[0]
# Skip irrelevant files
if containerName.__contains__("."):
print("Skipping file", Entry, "\n")
continue
if not FileName.__contains__("values.yaml"):
if FileName.__contains__("instance.yaml"):
print("Not implemented: Version update")
else:
print("Skipping file", Entry, "\n")
continue
# Update an instance
if FileStatus == "M":
try:
instanceDetails = open(
containerName + "/" + "instance.yaml", "r"
).readlines()
except Exception as e:
print(
"Failed to open instance file for reading:",
containerName + "/" + "instance.yaml",
e,
)
instanceConfig = {}
for line in instanceDetails:
if (line == ""):
continue
if (not line.__contains__(":")):
print(
"Skipping malformed line", line
)
continue
instanceConfig.update(
{line.split(": ")[0].strip(): line.split(": ")[1].strip()}
)
if not instanceConfig.get("instance"):
print(
"Failed to find instance ID for:", containerName + "/" + "values.yaml"
)
appVersion = ""
if instanceConfig.get("appVersion"):
appVersion = instanceConfig["appVersion"]
instanceID = instanceConfig["instance"]
valuesString = open(containerName + "/" + "values.yaml", "r").read()
uri = "https://api.slateci.io:443/v1alpha3/instances/" + instanceID + "/update"
print(uri)
response = requests.put(
uri,
params={"token": slateToken},
json={"apiVersion": "v1alpha3", "configuration": valuesString},
)
print(response, response.text)
# Create a new instance
elif FileStatus == "A":
try:
instanceDetails = open(
containerName + "/" + "instance.yaml", "r"
).readlines()
except Exception as e:
print(
"Failed to open instance file for reading:",
containerName + "/" + "instance.yaml",
e,
)
instanceConfig = {}
for line in instanceDetails:
# Parse key value pairs from the instance file into a dict
instanceConfig.update(
{line.split(":")[0].strip(): line.split(":")[1].strip()}
)
if (instanceConfig["instance"]):
appVersion = ""
if instanceConfig.get("appVersion"):
appVersion = instanceConfig["appVersion"]
instanceID = instanceConfig["instance"]
valuesString = open(containerName + "/" + "values.yaml", "r").read()
uri = "https://api.slateci.io:443/v1alpha3/instances/" + instanceID + "/update"
print(uri)
response = requests.put(
uri,
params={"token": slateToken},
json={"apiVersion": "v1alpha3", "configuration": valuesString},
)
print(response, response.text)
else:
clusterName = instanceConfig["cluster"]
groupName = instanceConfig["group"]
appName = instanceConfig["app"]
appVersion = ""
if instanceConfig.get("appVersion"):
appVersion = instanceConfig["appVersion"]
valuesString = open(containerName + "/" + "values.yaml", "r").read()
uri = "https://api.slateci.io:443/v1alpha3/apps/" + appName
print(uri)
response = requests.post(
uri,
params={"token": slateToken},
json={
"apiVersion": "v1alpha3",
"group": groupName,
"cluster": clusterName,
"configuration": valuesString,
},
)
print(response, response.text)
if response.status_code == 200:
instanceID = response.json()["metadata"]["id"]
# Open instance.yaml for writing and writeback instance ID
try:
instanceFile = open(containerName + "/" + "instance.yaml", "a")
instanceFile.write("\ninstance: " + instanceID)
# Git add commit push
print("::set-output name=push::true")
except Exception as e:
print(
"Failed to open instance file for ID writeback:",
containerName + "/" + "instance.yaml",
e,
)
# Remove an instance
elif FileStatus == "D":
print(
"Deletion is not implemented. Your instance is still running in SLATE despite file deletion."
)
else:
print("Error: Invalid file status passed by actions")