-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws_batch.py
More file actions
executable file
·289 lines (266 loc) · 9.51 KB
/
aws_batch.py
File metadata and controls
executable file
·289 lines (266 loc) · 9.51 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
#!/usr/bin/env python3
'''
Given an AWS registered job-definition and optional command with uploads and
downloads, this script moves data to and from a local filesystem into an AWS S3
Bucket and Batch container for processing. All data written to the AWS Batch
logging system is monitored and displayed to the user.
Requirements:
1. An AWS S3 Bucket
2. awscli must be available in the container for moving data to and from
the AWS S3 bucket
Extended from https://github.com/awslabs/aws-batch-helpers/
blob/master/gpu-example/submit-job.py
---
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import argparse
import boto3
import datetime
import logging
import os
import pkg_resources
import subprocess
import sys
import time
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
"jobDefinition",
help="name of the job job definition")
parser.add_argument(
"--command",
help="command to run",
type=str)
parser.add_argument(
"--job-name",
default='default',
help="name of the job (default: \"%(default)s\")",
type=str)
parser.add_argument(
"--job-queue",
default='optimal',
help="name of the job queue to submit this job (default: \"%(default)s)\"",
type=str)
parser.add_argument(
"--region-name",
default='us-west-2',
help="(default: %(default)s)",
type=str)
s3_parser = parser.add_argument_group('s3 configuration')
s3_parser.add_argument(
"--bucket",
help='s3 bucket url to stage uploads and downloads')
s3_parser.add_argument(
"--dirty",
action='store_false',
dest='teardown',
help='leave files in work bucket')
s3_parser.add_argument(
"--uploads",
help='')
s3_parser.add_argument(
"--downloads",
help='')
container_parser = parser.add_argument_group('container options')
container_parser.add_argument(
"--awscli",
default='/home/ec2-user/miniconda/bin/aws',
help=("awscli tool in batch job definition "
"container/ami (default: %(default)s)"))
container_parser.add_argument(
"--workdir",
default='/tmp',
help='container folder to execute command (default: %(default)s)')
container_parser.add_argument(
'--cpus',
type=int,
help='number of vCPUs to reserve for the container')
container_parser.add_argument(
'--memory',
type=int,
metavar='GiB',
help='number of GiB (~GB) of memory reserved for the job')
logging_parser = parser.add_argument_group(
title='logging and version options')
logging_parser.add_argument(
'-V', '--version',
action='version',
version=pkg_resources.get_distribution('aws_batch').version,
help='Print the version number and exit')
logging_parser.add_argument(
'-l', '--log',
metavar='',
default=sys.stdout,
type=argparse.FileType('a'),
help='Send logging to a file')
logging_parser.add_argument(
'-v', '--verbose',
action='count',
dest='verbosity',
default=0,
help='Increase verbosity of screen output '
'(eg, -v is verbose, -vv more so)')
logging_parser.add_argument(
'-q', '--quiet',
action='store_const',
dest='verbosity',
const=0,
help='Suppress output')
args = parser.parse_args()
def printLogs(cloudwatch, logStreamName, startTime):
kwargs = {'logGroupName': '/aws/batch/job',
'logStreamName': logStreamName,
'startTime': startTime,
'startFromHead': True}
lastTimestamp = 0
while True:
logEvents = cloudwatch.get_log_events(**kwargs)
for event in logEvents['events']:
lastTimestamp = event['timestamp']
lastTime = lastTimestamp / 1000.0
timestamp = datetime.datetime.utcfromtimestamp(lastTime)
msg = event['message'].strip()
print(msg)
logging.debug('[{:%Y-%m-%d %H:%M:%S}] {}'.format(timestamp, msg))
nextToken = logEvents['nextForwardToken']
if nextToken and kwargs.get('nextToken') != nextToken:
kwargs['nextToken'] = nextToken
else:
break
return lastTimestamp
# TODO: raise error if no awscli in container
# TODO: figure out aws batch/container error handling
def container_sh(cli, bucket, workdir, cmd, uploads, downloads):
commands = ['mkdir -p ' + workdir, 'cd ' + workdir]
for i in uploads + downloads:
dname = os.path.dirname(i)
if dname:
commands.append('mkdir -p ' + dname)
for i in uploads:
s3_path = os.path.join(bucket, i.lstrip('/'))
commands.append('{} s3 cp --only-show-errors {} {}'.format(
cli, s3_path, i))
commands.append('chmod 777 ' + i)
commands.append(cmd)
for i in downloads:
s3_path = os.path.join(bucket, i.lstrip('/'))
commands.append('{} s3 cp --only-show-errors {} {}'.format(
cli, i, s3_path))
return '; '.join(commands)
def s3_download(bucket, downloads):
for i in downloads:
path = os.path.join(bucket, i.lstrip('/'))
cmd = 'aws s3 cp {} {}'.format(path, i)
out = subprocess.run(
cmd.split(),
check=True,
encoding='utf-8',
stdout=subprocess.PIPE).stdout
logging.info(out.strip())
# TODO: check for local awscli
def s3_upload(bucket, uploads):
for i in uploads:
path = os.path.join(bucket, i.lstrip('/'))
cmd = 'aws s3 cp {} {}'.format(i, path)
out = subprocess.run(
cmd.split(),
check=True,
encoding='utf-8',
stdout=subprocess.PIPE).stdout
logging.info(out.strip())
def setup_logging(namespace):
log = namespace.log
loglevel = {
0: logging.ERROR,
1: logging.INFO,
2: logging.DEBUG,
}.get(namespace.verbosity, logging.DEBUG)
logging.basicConfig(stream=log, format='%(message)s', level=loglevel)
def check_args(args):
if not args.bucket and (args.uploads or args.downloads):
raise ValueError('--uploads and --downloads requires --bucket')
if args.bucket and not (args.uploads or args.downloads):
logging.warn('--bucket specified without --uploads or --downloads')
def main():
setup_logging(args)
check_args(args)
batch = boto3.client(
service_name='batch',
region_name=args.region_name,
endpoint_url='https://batch.{}.amazonaws.com'.format(args.region_name))
cloudwatch = boto3.client(
service_name='logs',
region_name=args.region_name,
endpoint_url='https://logs.{}.amazonaws.com'.format(args.region_name))
uploads = args.uploads.split(',') if args.uploads else []
downloads = args.downloads.split(',') if args.downloads else []
workdir = args.workdir.lstrip('/')
if args.bucket:
s3_upload(args.bucket, uploads)
sh = container_sh(
args.awscli, args.bucket, workdir, args.command, uploads, downloads)
containerOverrides = {'command': ['/bin/bash', '-c', sh]}
if args.cpus:
containerOverrides.update({'vcpus': args.cpus})
if args.memory:
containerOverrides.update({'memory': args.memory * 1024})
logging.debug(sh)
submitJobResponse = batch.submit_job(
jobName=args.job_name,
jobQueue=args.job_queue,
jobDefinition=args.jobDefinition,
containerOverrides=containerOverrides)
jobId = submitJobResponse['jobId']
logStreamName = None
startTime = 0
status = None
logging.info('({}) {}: {}'.format(
jobId, args.jobDefinition, args.command))
try:
while True:
time.sleep(1)
describeJobsResponse = batch.describe_jobs(jobs=[jobId])
job = describeJobsResponse['jobs'][0]
if status != job['status']:
status = job['status'].strip()
if status not in ['SUCCEEDED', 'FAILED']:
logging.info(status)
if status in ['RUNNING', 'SUCCEEDED', 'FAILED']:
if (logStreamName is None and
'logStreamName' in job['container']):
logStreamName = job['container']['logStreamName']
if logStreamName:
startTime = printLogs(
cloudwatch, logStreamName, startTime)
startTime += 1
if status in ['SUCCEEDED', 'FAILED']:
logging.info(status)
break
except BaseException as e:
logging.info('FAILED')
batch.cancel_job(jobId=jobId, reason=repr(e))
raise e
finally:
if args.bucket:
s3_download(args.bucket, downloads)
if args.teardown:
cloud_path = os.path.join(args.bucket, workdir)
cmd = 'aws s3 rm --recursive ' + cloud_path
out = subprocess.run(
cmd.split(),
check=True,
encoding='utf-8',
stdout=subprocess.PIPE).stdout.strip()
if out:
logging.info(out)
if __name__ == "__main__":
main()