-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdockerContainer.py
More file actions
81 lines (71 loc) · 2.43 KB
/
dockerContainer.py
File metadata and controls
81 lines (71 loc) · 2.43 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
#-*- coding: utf8 -*-
import subprocess
import time
import os
def execute(command, timeLimit = 5, logger=None):
try:
command = 'OUTPUT=$(docker run -d ' + command + '); echo $OUTPUT'
kwargs = {
"stdout": subprocess.PIPE,
"stderr": subprocess.PIPE,
"shell": True
}
popen = subprocess.Popen(command, **kwargs)
start = time.time()
while time.time() < start + timeLimit and popen.poll() is None:
time.sleep(0.1)
wait = popen.poll()
if wait is None:
popen.kill()
if logger is not None:
logger.info('TLE')
return {
'state': 'tle',
'exitcode': '9',
'stdout': '',
'stderr': 'Time Limit Exceeded'
}
containerID = popen.stdout.read().strip()
popen = subprocess.Popen('docker wait ' + containerID, **kwargs)
start = time.time()
while time.time() < start + timeLimit and popen.poll() is None:
time.sleep(0.1)
wait = popen.poll()
if wait is None:
popen.kill()
subprocess.call(['docker', 'kill', containerID])
if logger is not None:
logger.info('TLE')
return {
'state': 'tle',
'exitcode': '9',
'stdout': '',
'stderr': 'Time Limit Exceeded'
}
exitCode = int(popen.stdout.read().strip())
popen = subprocess.Popen('docker logs ' + containerID, **kwargs)
start = time.time()
while time.time() < start + timeLimit and popen.poll() is None:
time.sleep(0.1)
wait = popen.poll()
if wait is None:
popen.kill()
return {
'state': 'tle',
'exitcode': '9',
'stdout': '',
'stderr': 'Time Limit Exceeded'
}
FNULL = open(os.devnull, 'w')
subprocess.call(['docker', 'rm', containerID], stdout=FNULL, stderr=FNULL)
return {
'state': 'success',
'exitcode': exitCode,
'stdout': popen.stdout.read(),
'stderr': popen.stderr.read()
}
except Exception as e:
if logger is not None:
logger.critical('Error while docker run. ' + str(e))
else:
print 'Error while docker run. ' + str(e)