generated from Sage-Bionetworks-Challenges/model-to-data-challenge-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_docker.py
234 lines (201 loc) · 8.35 KB
/
run_docker.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
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
"""Run training synthetic docker models"""
from __future__ import print_function
import argparse
import getpass
import os
import tarfile
import time
import docker
import synapseclient
def create_log_file(log_filename, log_text=None):
"""Create log file"""
with open(log_filename, 'w') as log_file:
if log_text is not None:
if isinstance(log_text, bytes):
log_text = log_text.decode("utf-8")
log_file.write(log_text.encode("ascii", "ignore").decode("ascii"))
else:
log_file.write("No Logs")
def get_last_lines(log_filename, n=5):
"""Get last N lines of log file (default=5)."""
lines = 0
with open(log_filename, "rb") as f:
try:
f.seek(-2, os.SEEK_END)
# Keep reading, starting at the end, until n lines is read.
while lines < n:
f.seek(-2, os.SEEK_CUR)
if f.read(1) == b"\n":
lines += 1
except OSError:
# If file only contains one line, then only read that one
# line. This exception will probably never occur, but
# adding it in, just in case.
f.seek(0)
last_lines = f.read().decode()
return last_lines
def store_log_file(syn, log_filename, parentid, store=True):
"""Store log file"""
statinfo = os.stat(log_filename)
if statinfo.st_size > 0:
# If log file is larger than 50Kb, only save last 5 lines.
if statinfo.st_size/1000.0 > 50:
log_tail = get_last_lines(log_filename)
create_log_file(log_filename, log_tail)
ent = synapseclient.File(log_filename, parent=parentid)
if store:
try:
syn.store(ent)
except synapseclient.exceptions.SynapseHTTPError as err:
print(err)
def remove_docker_container(container_name):
"""Remove docker container"""
client = docker.from_env()
try:
cont = client.containers.get(container_name)
cont.stop()
cont.remove()
except Exception:
print("Unable to remove container")
def remove_docker_image(image_name):
"""Remove docker image"""
client = docker.from_env()
try:
client.images.remove(image_name, force=True)
except Exception:
print("Unable to remove image")
def tar(directory, tar_filename):
"""Tar all files in a directory
Args:
directory: Directory path to files to tar
tar_filename: tar file path
"""
with tarfile.open(tar_filename, "w") as tar_o:
tar_o.add(directory)
# TODO: Potentially add code to remove all files that were zipped.
def untar(directory, tar_filename):
"""Untar a tar file into a directory
Args:
directory: Path to directory to untar files
tar_filename: tar file path
"""
with tarfile.open(tar_filename, "r") as tar_o:
tar_o.extractall(path=directory)
def main(syn, args):
"""Run docker model"""
if args.status == "INVALID":
raise Exception("Docker image is invalid")
# The new toil version doesn't seem to pull the docker config file from
# .docker/config.json...
# client = docker.from_env()
client = docker.DockerClient(base_url='unix://var/run/docker.sock')
config = synapseclient.Synapse().getConfigFile(
configPath=args.synapse_config
)
authen = dict(config.items("authentication"))
client.login(username=authen['username'],
password=authen['password'],
registry="https://docker.synapse.org")
# dockercfg_path=".docker/config.json")
print(getpass.getuser())
# Add docker.config file
docker_image = args.docker_repository + "@" + args.docker_digest
# These are the volumes that you want to mount onto your docker container
#output_dir = os.path.join(os.getcwd(), "output")
output_dir = os.getcwd()
input_dir = args.input_dir
print("mounting volumes")
# These are the locations on the docker that you want your mounted
# volumes to be + permissions in docker (ro, rw)
# It has to be in this format '/output:rw'
mounted_volumes = {output_dir: '/output:rw',
input_dir: '/input:ro'}
# All mounted volumes here in a list
all_volumes = [output_dir, input_dir]
# Mount volumes
volumes = {}
for vol in all_volumes:
volumes[vol] = {'bind': mounted_volumes[vol].split(":")[0],
'mode': mounted_volumes[vol].split(":")[1]}
# Look for if the container exists already, if so, reconnect
print("checking for containers")
container = None
errors = None
for cont in client.containers.list(all=True, ignore_removed=True):
if args.submissionid in cont.name:
# Must remove container if the container wasn't killed properly
if cont.status == "exited":
cont.remove()
else:
container = cont
# If the container doesn't exist, make sure to run the docker image
if container is None:
# Run as detached, logs will stream below
print("running container")
try:
container = client.containers.run(docker_image,
detach=True, volumes=volumes,
name=args.submissionid,
network_disabled=True,
mem_limit='6g', stderr=True)
except docker.errors.APIError as err:
remove_docker_container(args.submissionid)
errors = str(err) + "\n"
print("creating logfile")
# Create the logfile
log_filename = args.submissionid + "_log.txt"
# Open log file first
open(log_filename, 'w').close()
# If the container doesn't exist, there are no logs to write out and
# no container to remove
if container is not None:
# Check if container is still running
while container in client.containers.list(ignore_removed=True):
log_text = container.logs()
create_log_file(log_filename, log_text=log_text)
store_log_file(syn, log_filename, args.parentid, store=args.store)
time.sleep(60)
# Must run again to make sure all the logs are captured
log_text = container.logs()
create_log_file(log_filename, log_text=log_text)
store_log_file(syn, log_filename, args.parentid, store=args.store)
# Remove container and image after being done
container.remove()
statinfo = os.stat(log_filename)
if statinfo.st_size == 0:
create_log_file(log_filename, log_text=errors)
store_log_file(syn, log_filename, args.parentid, store=args.store)
print("finished training")
# Try to remove the image
remove_docker_image(docker_image)
output_folder = os.listdir(output_dir)
if not output_folder:
raise Exception("No 'predictions.csv' file written to /output, "
"please check inference docker")
elif "predictions.csv" not in output_folder:
raise Exception("No 'predictions.csv' file written to /output, "
"please check inference docker")
# CWL has a limit of the array of files it can accept in a folder
# therefore creating a tarball is sometimes necessary
# tar(output_dir, 'outputs.tar.gz')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--submissionid", required=True,
help="Submission Id")
parser.add_argument("-p", "--docker_repository", required=True,
help="Docker Repository")
parser.add_argument("-d", "--docker_digest", required=True,
help="Docker Digest")
parser.add_argument("-i", "--input_dir", required=True,
help="Input Directory")
parser.add_argument("-c", "--synapse_config", required=True,
help="credentials file")
parser.add_argument("--store", action='store_true',
help="to store logs")
parser.add_argument("--parentid", required=True,
help="Parent Id of submitter directory")
parser.add_argument("--status", required=True, help="Docker image status")
args = parser.parse_args()
syn = synapseclient.Synapse(configPath=args.synapse_config)
syn.login()
main(syn, args)