forked from actions/runner-container-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgha-runner-rpc.py
executable file
·174 lines (148 loc) · 5.36 KB
/
gha-runner-rpc.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
172
173
#!/usr/bin/env python3
# Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
# This implements a very simple RPC server that should be running on the job container of the workflow pod,
# and used by the k8s hook to execute steps in the workflow on the workflow pod.
# It supports a running a single RPC call at a time, and will return an error if a new call is made while
# another one is still running (which is a valid assumption, as the runner is expected to execute one step at a time).
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
import time
from flask import Flask, jsonify, request
from threading import Thread
from waitress import serve
import argparse
import json
import logging
import os
import signal
import subprocess
import logging
import json_logging
app = Flask(__name__)
app.logger.setLevel(logging.DEBUG)
json_logging.init_flask(enable_json=True)
json_logging.init_request_instrument(app)
@dataclass
class Response:
id: str
status: str
pid: int = None
returncode: int = None
error: str = None
def readLines(path, fromLine, maxLines):
try:
with open(path, 'r') as f:
return [x for i, x in enumerate(f) if i >= fromLine and x.endswith('\n') and i < fromLine + maxLines]
except Exception as e:
app.logger.warning(f"Error reading file {path}: {e}")
return []
class State:
def __init__(self):
self.latest_id = None
self.status = Response(id = "", status = "idle")
self.worker = ThreadPoolExecutor(max_workers=1)
self.future = None
self.process = None
self.out = None
def __run(self, id, path):
self.latest_id = id
try:
app.logger.debug(f"Running id {id}")
logsfilename = f"/logs/{id}.out"
self.out = open(logsfilename, "w")
self.process = subprocess.Popen(['sh', '-e', path], start_new_session=True, stdout=self.out, stderr=self.out)
app.logger.debug(f"Process for id {id} started with pid {self.process.pid}")
self.status = Response(
id = id,
status = 'running',
pid = self.process.pid
)
self.process.wait()
self.out.close()
app.logger.debug(f"Process for id {id} finished (return code {self.process.returncode})")
self.status = Response(
id = id,
status = 'completed',
returncode = self.process.returncode,
)
except Exception as e:
app.logger.error(f"Error starting process: {e}")
self.status = Response(
id = id,
status = 'failed',
error = str(e),
returncode = -1,
)
def exec(self, id, path):
if self.future and not self.future.done():
app.logger.error(f"A job is already running (ID {self.latest_id})")
return Response(
id = id,
status = 'failed',
error = f"A job is already running (ID {self.latest_id})",
returncode = -1,
)
app.logger.debug(f"Queueing job {id} with path {path}")
self.status = Response(id = id, status = "pending")
self.future = self.worker.submit(self.__run, id, path)
return self.status
def cancel(self):
if not self.future:
return Response(
id = '',
status = 'failed',
error = 'No job has been started yet',
)
elif self.future.done():
# The job is already done, no need to cancel
return self.status
else:
app.logger.debug(f"Cancelling {self.latest_id} (pid {self.process.pid})")
os.killpg(os.getpgid(self.process.pid), signal.SIGINT)
return Response(
id = self.latest_id,
status = 'cancelling',
pid = self.process.pid
)
state = State()
# Post a new job
@app.route('/', methods=['POST'])
def call():
data = json.loads(request.data)
if 'id' not in data or 'path' not in data:
return jsonify(Response(
id = '',
status = 'failed',
error = 'Missing id or path in request',
))
id = data['id']
path = data['path']
return jsonify(state.exec(id, path))
# Cancel the current job
@app.route('/', methods=['DELETE'])
def cancel():
return jsonify(state.cancel())
# Get the current status
@app.route('/')
def status():
app.logger.debug(f"Status: {state.status}")
return jsonify(state.status)
# Get the logs of a given job
@app.route('/logs')
def logs():
if 'id' not in request.args:
return 'Missing id in request', 400
id = request.args.get('id')
fromLine = int(request.args.get('fromLine', 0))
maxLines = int(request.args.get('maxLines', 1000))
path = f"/logs/{id}.out"
return jsonify(readLines(path, fromLine, maxLines))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--dev', action='store_true', help='Run in Flask development mode')
args = parser.parse_args()
if args.dev:
app.run(host='0.0.0.0', port=8080, debug=True)
else:
serve(app, host='0.0.0.0', port=8080, threads=1)