|
3 | 3 | import requests
|
4 | 4 | import shutil
|
5 | 5 | import json
|
| 6 | +import time |
6 | 7 |
|
7 | 8 | # Global Variables
|
8 | 9 | templateExecutionPayloadJSONPath = "./templates/execution.payload.json"
|
@@ -100,25 +101,78 @@ def execute(requestJSONFile):
|
100 | 101 | print("Failed to upload JSON data: " + r['error'])
|
101 | 102 | exit(1)
|
102 | 103 | elif request.status_code != 200:
|
103 |
| - print("ERROR: API returned status code during execution: ", request.status_code) |
| 104 | + print("ERROR: API returned status code during execution post: ", request.status_code) |
104 | 105 | else:
|
105 | 106 | return r['id']
|
106 | 107 |
|
| 108 | +# Fetches logs for execution and then returns them, or throws an error if the status code != 200 |
| 109 | +def getLogs(executionID): |
| 110 | + buildURL = baseURL + "/orgs/" + orgID + "/engine/executions/" + executionID + "/log?offset=1374&limit=10000" |
| 111 | + request = requests.get(buildURL, auth = ("", apiToken), headers = {"accept": "application/json", "Content-Type": "application/json"}) |
| 112 | + if request.status_code != 200: |
| 113 | + print("ERROR: API returned status code during log fetch: ", request.status_code) |
| 114 | + else: |
| 115 | + return request.text |
| 116 | + |
107 | 117 | def watch(executionID):
|
108 |
| - buildURL = baseURL + "/orgs/" + orgID + "/engine/executions/" + executionID + "/events" |
| 118 | + buildURL = baseURL + "/orgs/" + orgID + "/engine/executions/" + executionID |
109 | 119 | request = requests.get(buildURL, auth = ("", apiToken), headers = {"accept": "application/json", "Content-Type": "application/json"})
|
| 120 | + response = request.text |
110 | 121 | if request.status_code != 200:
|
111 | 122 | print("ERROR: API returned status code during watch: ", request.status_code)
|
112 | 123 | else:
|
113 |
| - print(request.text) |
| 124 | + r = json.loads(response) |
| 125 | + completionStatus = "completed" |
| 126 | + failureState = "failed" |
| 127 | + while r['state'] != completionStatus: |
| 128 | + time.sleep(5) |
| 129 | + request = requests.get(buildURL, auth = ("", apiToken), headers = {"accept": "application/json", "Content-Type": "application/json"}) |
| 130 | + if request.status_code != 200: |
| 131 | + print("ERROR: API returned status code during watch: ", request.status_code) |
| 132 | + else: |
| 133 | + response = request.text |
| 134 | + r = json.loads(response) |
| 135 | + print("ID: " + executionID) |
| 136 | + print("Status: " + r['state']) |
| 137 | + if r['state'] == failureState: |
| 138 | + result(executionID, True) |
| 139 | + else: |
| 140 | + print("-") |
| 141 | + print(getLogs(executionID)) |
| 142 | + print("------------------") |
| 143 | + result(executionID, True) |
114 | 144 |
|
| 145 | +def result(executionID, isFailedStatus): |
| 146 | + print("========= RESULTS ==========") |
| 147 | + if isFailedStatus: |
| 148 | + buildURL = baseURL + "/orgs/" + orgID + "/engine/executions/" + executionID + "/events" |
| 149 | + else: |
| 150 | + buildURL = baseURL + "/orgs/" + orgID + "/engine/executions/" + executionID + "/result" |
| 151 | + request = requests.get(buildURL, auth = ("", apiToken), headers = {"accept": "application/json", "Content-Type": "application/json"}) |
| 152 | + response = request.text |
| 153 | + if request.status_code != 200: |
| 154 | + print("ERROR: API returned status code during result fetch: ", request.status_code) |
| 155 | + else: |
| 156 | + r = json.loads(response) |
| 157 | + successStatus = "ready" |
| 158 | + if isFailedStatus: |
| 159 | + print("ERROR: Execution has failed.") |
| 160 | + print("Please see events below for details") |
| 161 | + print("---------- EVENTS ----------") |
| 162 | + print(response) |
| 163 | + exit(1) |
| 164 | + else: |
| 165 | + print("Image hardened successfully. (" + namespace + "/" + repo + tag + "-slim)") |
| 166 | + exit() |
| 167 | + exit() |
115 | 168 |
|
116 | 169 |
|
117 | 170 | if __name__ == "__main__":
|
118 | 171 | doTmpDir("create")
|
119 | 172 | createFlags()
|
120 | 173 | getRequestJSONFile = generateRequest()
|
121 |
| - #getExecutionID = execute(getRequestJSONFile) |
122 |
| - getExecutionID = "" |
| 174 | + getExecutionID = execute(getRequestJSONFile) |
| 175 | + #getExecutionID = "rknx.2Ny4ii2XQcvFqj3OxjPSaI7eA35" |
123 | 176 | watch(getExecutionID)
|
124 |
| - #doTmpDir("delete") |
| 177 | + result(getExecutionID) |
| 178 | + doTmpDir("delete") |
0 commit comments