-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathexecutor2.py
More file actions
369 lines (330 loc) · 12.8 KB
/
executor2.py
File metadata and controls
369 lines (330 loc) · 12.8 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
"""
Executor executes a script, parses consoles and posts logs
"""
from datetime import datetime
import json
import shutil
import subprocess
import tempfile
import threading
import time
import traceback
import uuid
import os
from shippable_adapter import ShippableAdapter
class Executor2(object):
"""
Sets up attributes that will be used to execute the build
"""
def __init__(self, config):
# -------
# Private
# -------
# Configs obtained from the job.env file
self._config = config
self._shippable_adapter = ShippableAdapter(config)
# Threads
self._logger_thread = None
self._script_runner_thread = None
# Error buffer state
self._has_errors = False
# Log directory and file
self._temporary_log_directory = tempfile.mkdtemp()
self._log_file_path = \
os.path.join(self._temporary_log_directory, 'logs')
buffer_size = 0
self._write_log_file = open(self._log_file_path, 'w', buffer_size)
self._read_log_file = open(self._log_file_path, 'r')
# Console state
self._current_group_info = None
self._current_group_name = None
self._current_cmd_info = None
self._show_group = None
# Execution error consoles
self._error_grp = {
'consoleId': str(uuid.uuid4()),
'parentConsoleId': 'root',
'type': 'grp',
'message': 'Error',
'timestamp': Executor2._get_timestamp(),
'isSuccess': False
}
self._error_buffer = [self._error_grp]
# ------
# Public
# ------
# Assume failure by default
self.exit_code = 1
def __del__(self):
shutil.rmtree(self._temporary_log_directory, ignore_errors=True)
def execute(self):
"""
Starts the script runner and logger threads and waits for
them to finish.
"""
# Instantiate script runner and logger threads
self._script_runner_thread = \
threading.Thread(target=self._script_runner)
self._logger_thread = threading.Thread(target=self.logger)
# Start both the threads.
self._script_runner_thread.start()
self._logger_thread.start()
# Wait until the threads are completed
self._script_runner_thread.join()
self._logger_thread.join()
if self._has_errors:
self._flush_error_buffer()
def _script_runner(self):
"""
Runs the script, handles console output and finally sets the exit code
"""
# We need to unset the LD_LIBRARY_PATH set by pyinstaller. This
# will ensure the script prefers libraries on system rather
# than the ones bundled during build time.
env = dict(os.environ)
env.pop('LD_LIBRARY_PATH', None)
cmd = self._config['SCRIPT_PATH']
if self._config.get('REQEXEC_SHELL'):
cmd = [self._config['REQEXEC_SHELL'], self._config['SCRIPT_PATH']]
try:
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=self._config['BUILD_DIR'],
env=env
)
except Exception as ex:
trace = traceback.format_exc()
error = '{0}: {1}'.format(str(ex), trace)
self._append_to_error_buffer(error)
return
try:
for line in iter(proc.stdout.readline, ''):
is_script_success, is_complete = self._handle_console_line(line)
if is_script_success:
self.exit_code = 0
if is_complete:
break
except Exception as ex:
trace = traceback.format_exc()
error = '{0}: {1}'.format(str(ex), trace)
self._append_to_error_buffer(error)
proc.kill()
def logger(self):
"""
Reads from the log file and flushes consoles periodically or
if a limit is hit
"""
logs_to_post = {
'buildJobId': self._config['BUILD_JOB_ID'],
'buildJobConsoles': []
}
# Add a hidden version notice.
# NOTE: Remove this once we switch to this executor completely.
notice_console_id = str(uuid.uuid4())
notice_message = 'Notice: Executor v2'
logs_to_post['buildJobConsoles'].append({
'consoleId': notice_console_id,
'parentConsoleId': 'root',
'type': 'grp',
'message': notice_message,
'timestamp': Executor2._get_timestamp(),
'isShown': False
})
logs_to_post['buildJobConsoles'].append({
'consoleId': notice_console_id,
'parentConsoleId': 'root',
'type': 'grp',
'message': notice_message,
'timestamp': Executor2._get_timestamp(),
'timestampEndedAt': Executor2._get_timestamp(),
'isSuccess': True,
'isShown': False
})
logs_last_posted_at = datetime.now()
while True:
post_logs = False
and_break = False
log_line = self._read_log_file.readline()
if log_line:
try:
parsed_log_line = json.loads(log_line)
logs_to_post['buildJobConsoles'].append(parsed_log_line)
except Exception as ex:
trace = traceback.format_exc()
error = '{0}: {1}'.format(str(ex), trace)
self._append_to_error_buffer(error)
# We added a new line, if the new array length exceeds max
# log lines, post logs.
if len(logs_to_post['buildJobConsoles']) \
>= self._config['MAX_LOG_LINES_TO_FLUSH']:
post_logs = True
else:
# If the script runner is dead and there are no more logs to
# read, attempt to post any remaining logs and break.
if not self._script_runner_thread.isAlive():
post_logs = True
and_break = True
# If its been a while since we posted logs, post.
elif (datetime.now() - logs_last_posted_at).total_seconds() \
> self._config['MAX_LOGS_FLUSH_WAIT_TIME_IN_S'] \
and logs_to_post['buildJobConsoles']:
post_logs = True
# Sleep a bit if there hasn't been any activity.
else:
time.sleep(self._config['LOGS_FILE_READ_WAIT_TIME_IN_S'])
# Post logs if asked and there is something to post.
if post_logs and logs_to_post['buildJobConsoles']:
logs_last_posted_at = datetime.now()
data = json.dumps(logs_to_post)
self._shippable_adapter.post_build_job_consoles(data)
logs_to_post['buildJobConsoles'] = []
if and_break:
break
def _handle_console_line(self, line):
"""
Parses a single line of console output and pushes it to a file
This also returns whether the console line is successful and the
script is complete
"""
is_script_success = False
is_complete = False
timestamp = Executor2._get_timestamp()
line_split = line.split('|')
if line.startswith('__SH__GROUP__START__'):
self._current_group_info = line_split[1]
self._current_group_name = '|'.join(line_split[2:])
self._current_group_info = json.loads(self._current_group_info)
self._show_group = self._current_group_info.get('is_shown', True)
if self._show_group == 'false':
self._show_group = False
console_out = {
'consoleId': self._current_group_info.get('id'),
'parentConsoleId': 'root',
'type': 'grp',
'message': self._current_group_name,
'timestamp': timestamp,
'isShown': self._show_group
}
self._append_to_log_file(console_out)
elif line.startswith('__SH__CMD__START__'):
self._current_cmd_info = line_split[1]
current_cmd_name = '|'.join(line_split[2:])
self._current_cmd_info = json.loads(self._current_cmd_info)
parent_id = self._current_group_info.get('id') if \
self._current_group_info else None
console_out = {
'consoleId': self._current_cmd_info.get('id'),
'parentConsoleId': parent_id,
'type': 'cmd',
'message': current_cmd_name,
'timestamp': timestamp,
}
if parent_id:
self._append_to_log_file(console_out)
elif line.startswith('__SH__CMD__END__'):
current_cmd_end_info = line_split[1]
current_cmd_end_name = '|'.join(line_split[2:])
current_cmd_end_info = json.loads(current_cmd_end_info)
parent_id = self._current_group_info.get('id') if \
self._current_group_info else None
is_cmd_success = False
if current_cmd_end_info.get('exitcode') == '0':
is_cmd_success = True
console_out = {
'consoleId': self._current_cmd_info.get('id'),
'parentConsoleId': parent_id,
'type': 'cmd',
'message': current_cmd_end_name,
'timestamp': timestamp,
'timestampEndedAt': timestamp,
'isSuccess': is_cmd_success,
'isShown': self._show_group
}
if parent_id:
self._append_to_log_file(console_out)
elif line.startswith('__SH__GROUP__END__'):
current_grp_end_info = line_split[1]
current_grp_end_name = '|'.join(line_split[2:])
current_grp_end_info = json.loads(current_grp_end_info)
is_cmd_success = False
if current_grp_end_info.get('exitcode') == '0':
is_cmd_success = True
console_out = {
'consoleId': self._current_group_info.get('id'),
'parentConsoleId': 'root',
'type': 'grp',
'message': current_grp_end_name,
'timestamp': timestamp,
'timestampEndedAt': timestamp,
'isSuccess': is_cmd_success,
'isShown': self._show_group
}
self._append_to_log_file(console_out)
elif line.startswith('__SH__SCRIPT_END_SUCCESS__'):
is_script_success = True
is_complete = True
elif line.startswith('__SH__SCRIPT_END_FAILURE__'):
is_script_success = False
is_complete = True
else:
parent_id = self._current_cmd_info.get('id') if \
self._current_cmd_info else None
console_out = {
'consoleId': str(uuid.uuid4()),
'parentConsoleId': parent_id,
'type': 'msg',
'message': line,
'timestamp': timestamp,
}
if parent_id:
self._append_to_log_file(console_out)
else:
self._append_to_error_buffer(line)
return is_script_success, is_complete
def _append_to_log_file(self, console_out):
"""
Pushes a console line to the log file
"""
try:
self._write_log_file.write(json.dumps(console_out) + '\n')
except Exception as ex:
trace = traceback.format_exc()
error = '{0}: {1}'.format(str(ex), trace)
self._append_to_error_buffer(error)
def _append_to_error_buffer(self, error):
"""
Appends an error into errors buffer after ensuring it is not empty
"""
if not error.strip():
return
self._has_errors = True
error_msg = {
'consoleId': str(uuid.uuid4()),
'parentConsoleId': self._error_grp['consoleId'],
'type': 'msg',
'message': error,
'timestamp': Executor2._get_timestamp(),
'isSuccess': False
}
self._error_buffer.append(error_msg)
def _flush_error_buffer(self):
"""
Flushes error buffer
"""
req_body = {
'buildJobId': self._config['BUILD_JOB_ID'],
'buildJobConsoles': self._error_buffer
}
data = json.dumps(req_body)
self._shippable_adapter.post_build_job_consoles(data)
del self._error_buffer
self._error_buffer = []
@staticmethod
def _get_timestamp():
"""
Helper method to return timestamp in a format which is acceptable
for consoles
"""
return int(time.time() * 1000000)