10
from http import HTTPStatus
12
from art import text2art
15
RESULT_FILE_KEY = "farm/ob_results/"
16
TASK_QUEUE_FILE_KEY = "farm/ob_jobs/{}.json"
19
def _range(start, last):
26
return to_str(start) + '-' + to_str(last)
29
def _make_range_string(range):
36
if start is None and last is None:
39
return 'bytes=' + _range(start, last)
44
def __init__(self, endpoint=""):
45
self.endpoint = endpoint
47
def get_object(self, key, _range=None):
48
url = "{}/{}".format(self.endpoint, key)
50
if _range is not None:
51
_range = (_range, None)
52
headers.update({"range": _make_range_string(_range)})
53
res = requests.get(url, headers=headers)
54
if res.status_code < 400:
55
result = res.content.decode()
59
def get_object_meta(self, key):
60
url = "{}/{}".format(self.endpoint, key)
62
res = requests.head(url, headers=headers)
65
def exists_object(self, key):
71
def __init__(self, host="api.github.com"):
74
def get_job_by_id(self, project, pipeline_id):
75
url = "https://{}/repos/{}/actions/runs/{}".format(
76
self.host, project, pipeline_id
81
"Accept": "application/vnd.github+json"
84
status_code = res.status_code
85
if status_code == HTTPStatus.NOT_FOUND:
93
class TaskStatusEnum(Enum):
102
submit_task_fail = -4
105
def request(method, url, params=None, payload=None, timeout=10, data=None, without_check_status=False):
106
params = params or {}
108
response = requests.request(
116
if not without_check_status and response.status_code >= 300:
118
msg = response.json()["msg"]
121
print("[ERROR] MSG:{}".format(msg))
126
traceback.print_exc()
127
print("Please contact the management personnel for assistance !")
128
if not without_check_status:
132
def monitor_tasks(oss_proxy: OssProxy, github_pipeline_id, timeout):
133
end_time = time.time() + int(timeout)
135
while time.time() <= end_time:
138
task_data = get_task_res(oss_proxy, github_pipeline_id)
143
if task_data is not None:
144
task_status = int(task_data["status"])
145
if task_status <= TaskStatusEnum.fail.value:
146
print(TaskStatusEnum._value2member_map_[task_status])
147
print("there is the output url: {}".format(
148
"https://ce-farm.oceanbase-dev.com/farm2/ci/?id={}".format(task_data["task_id"])))
150
elif task_status >= TaskStatusEnum.success.value:
151
print(TaskStatusEnum._value2member_map_[task_status])
152
print("there is the output url: {}".format(
153
"https://ce-farm.oceanbase-dev.com/farm2/ci/?id={}".format(task_data["task_id"])))
161
def get_task_res(oss_proxy: OssProxy, github_pipeline_id):
163
result_key = RESULT_FILE_KEY + "{}.json".format(github_pipeline_id)
164
origin_task_data = oss_proxy.get_object(result_key)
165
return json.loads(origin_task_data)
170
def main(pipeline_id, project, timeout):
171
print("create a new task")
173
logo = text2art('OceanBase Farm')
175
oss_proxy = OssProxy("https://obfarm-ce.oss-cn-hongkong.aliyuncs.com")
176
github_proxy = GithubProxy()
177
job_info = github_proxy.get_job_by_id(project, pipeline_id)
178
attempt_number = job_info["run_attempt"]
179
run_pipeline_id = "{}-{}".format(pipeline_id, attempt_number)
180
result = monitor_tasks(oss_proxy, run_pipeline_id, timeout)
185
if __name__ == "__main__":
187
if len(sys.argv) < 4:
188
print("Missing relevant parameters !")
189
OUTPUT.update({"success": -1})
191
main(sys.argv[1], sys.argv[2], sys.argv[3])