This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
jobExecutor.py
515 lines (366 loc) · 16.6 KB
/
jobExecutor.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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
'''
/*
* Copyright 2019 Amazon.com, Inc. and its affiliates. All Rights Reserved.
*
* Licensed under the MIT License. See the LICENSE accompanying this file
* for the specific language governing permissions and limitations under
* the License.
*/
'''
import base64
import boto3
import datetime
import docker
import fnmatch
import json
import os
import platform
import requests
import subprocess
import time
def datetime_handler(x):
if isinstance(x, datetime.datetime):
return x.isoformat()
raise TypeError("Unknown type")
def stringToBool(str):
if str in ['true', 'True']:
return True
else:
return False
class JobExecutor(object):
def __init__(self, config, deviceShadowHandler):
self.ACCESS_KEY = None
self.SECRET_KEY = None
self.SESSION_TOKEN = None
self.session = None
self.thingName = config.thingName
self.certificatePath = config.certificatePath
self.privateKeyPath = config.privateKeyPath
self.rootCaPath = config.rootCAPath
self.credentialsEndpoint = config.credentialsEndpoint
self.roleAlias = config.roleAlias
self.region = config.region
self.deviceShadowHandler = deviceShadowHandler
def formatResults(self, resultArray, didSucceed):
results = {}
processedCount = 0
for result in resultArray:
results[f'command{processedCount}'] = str(result)
processedCount = processedCount + 1
results['didSucceed'] = didSucceed
return results
def downloadFiles(self, execution):
results = []
didSucceed = True
for fileOperation in execution['jobDocument']['files']:
# if the destination file already exists, create a rollback version
exists = os.path.isfile(fileOperation['destination'])
if exists:
os.rename(fileOperation['destination'], fileOperation['destination'] + '.old')
results.append(subprocess.run(["curl", "--output", fileOperation['destination'], fileOperation['url']], shell=False, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE))
return self.formatResults(results, didSucceed)
def rollbackFiles(self, execution):
results = []
didSucceed = True
for fileOperation in execution['jobDocument']['files']:
# check to see if rollback file exists
exists = os.path.isfile(fileOperation['destination'] + '.old')
# if required rollback file does not exist, return failure immediately
if not exists and fileOperation['required'] == True:
errorMessage = 'Unable to roll back file' + fileOperation['destination'] + '.'
results.append({'fileOperation': fileOperation, 'stderr': errorMessage.encode('ascii')})
didSucceed = False
elif exists:
os.rename(fileOperation['destination'] + '.old', fileOperation['destination'])
successMessage = 'Rolled back file' + fileOperation['destination'] + '.'
results.append({'fileOperation': fileOperation, 'stdout': successMessage.encode('ascii')})
return self.formatResults(results, didSucceed)
def executeJob(self, execution):
operations = {
'container-logs': self.uploadContainerLogs,
'download-files': self.downloadFiles,
'install-packages': self.installPackages,
'list-containers': self.listContainers,
'pip-install': self.pipInstall,
'pip-list': self.pipList,
'pip-uninstall': self.pipUninstall,
'pull-container-images': self.pullContainerImages,
'reboot': self.reboot,
'rollback-files': self.rollbackFiles,
'run-commands': self.runCommands,
'start-containers': self.runContainers,
'stop-containers': self.stopContainers,
'uninstall-packages': self.uninstallPackages,
'upload-files': self.uploadS3Files
}
# Get the function from switcher dictionary
func = operations.get(execution['jobDocument']['operation'], self.notSupportedHandler)
try:
# Execute the function
return func(execution)
except Exception as e:
results = [{'error':e}]
return self.formatResults(results, didSucceed=False)
def getTemporaryCredentials(self):
url = 'https://' + self.credentialsEndpoint + \
'/role-aliases/' + self.roleAlias + '/credentials'
headers = {
'x-amzn-iot-thingname': self.thingName,
}
response = requests.get(
url,
headers=headers,
verify=self.rootCaPath,
cert=(
self.certificatePath,
self.privateKeyPath
)
)
json_response = json.loads(response.content)
self.ACCESS_KEY = json_response['credentials']['accessKeyId']
self.SECRET_KEY = json_response['credentials']['secretAccessKey']
self.SESSION_TOKEN = json_response['credentials']['sessionToken']
session = boto3.Session(
aws_access_key_id=self.ACCESS_KEY,
aws_secret_access_key=self.SECRET_KEY,
aws_session_token=self.SESSION_TOKEN,
)
# print(session.get_credentials().access_key)
# print(session.get_credentials().secret_key)
return session
def notSupportedHandler(self, execution):
errorMessage = 'Operation \'' + execution['jobDocument']['operation'] + '\' is currently not supported on this device.'
print(errorMessage)
return subprocess.CompletedProcess(args=[],returncode=1,stderr=errorMessage.encode('ascii'))
def reboot(self, execution):
results = []
didSucceed = True
systems = {
'Darwin': ["sudo", "shutdown", "-r", "+1"],
'Linux': ["sudo", "shutdown", "-r", "+1"]
}
# Get the function from switcher dictionary
commands = systems.get(platform.system())
if commands is None:
self.notSupportedHandler(execution)
else:
# Execute the function
print('reboot!')
results.append(subprocess.run(commands, shell=False, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE))
return self.formatResults(results, didSucceed)
def runCommands(self, execution):
results = []
didSucceed = True
for cmd in execution['jobDocument']['commands']:
result = subprocess.run(cmd, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
results.append(result)
if result.stderr and result.stderr is not None:
didSucceed = False
return self.formatResults(results, didSucceed)
def uploadS3Files(self, execution):
results = []
didSucceed = True
session = self.getTemporaryCredentials()
s3_client = session.client('s3')
for source in execution['jobDocument']['sources']:
filePath = None
key = None
filePath = source['filename']
if 'prefix' in source:
key = source['prefix'] + '/' + filePath
else:
key = filePath
s3_client.upload_file(filePath, source['bucket'], key)
successMessage = 'Uploaded ' + filePath + ' to ' + source['bucket']
results.append(successMessage)
return self.formatResults(results, didSucceed)
# PACKAGE MANAGER
def installPackages(self, execution):
systems = {
'Darwin': ["brew", "install"],
# 'Linux': ["sudo", "apt-get", "install", "-y"], # not for Amazon Linux
'Linux': ["sudo", "yum", "-y", "install"]
}
results = []
didSucceed = True
# Get the function from switcher dictionary
commands = systems.get(platform.system())
if commands is None:
return self.notSupportedHandler(execution)
else:
for package in execution['jobDocument']['packages']:
pkgCommands = commands.copy()
pkgCommands.append(package['name'])
result = subprocess.run(pkgCommands, shell=False, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
results.append(result)
if result.stderr and result.stderr is not None:
didSucceed = False
return self.formatResults(results, didSucceed)
def uninstallPackages(self, execution):
systems = {
'Darwin': ["brew", "uninstall"],
# 'Linux': ["sudo", "apt-get", "install", "-y"], # not for Amazon Linux
'Linux': ["sudo", "yum", "-y", "erase"]
}
results = []
didSucceed = True
# Get the function from switcher dictionary
commands = systems.get(platform.system())
if commands is None:
return self.notSupportedHandler(execution)
else:
for package in execution['jobDocument']['packages']:
pkgCommands = commands.copy()
pkgCommands.append(package['name'])
result = subprocess.run(pkgCommands, shell=False, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
results.append(result)
if result.stderr and result.stderr is not None:
didSucceed = False
return self.formatResults(results, didSucceed)
# PIP
def pipList(self, execution):
systems = {
'Darwin': ["pip3", "list"],
'Linux': ["pip3", "list"]
}
# Get the function from switcher dictionary
commands = systems.get(platform.system())
if commands is None:
return self.notSupportedHandler(execution)
else:
result = subprocess.run(commands, shell=False, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
pkgList = result.stdout
pkgs = filter(None, pkgList.split(b'\n'))
packageList = []
results = []
didSucceed = True
cnt = 0
for pkg in pkgs:
cnt = cnt + 1
if cnt < 3:
continue
pkgSplit = pkg.decode().split()
p = {"name": pkgSplit[0], "version": pkgSplit[1]}
packageList.append(p)
JSONPayload = '{"state":{"reported":{"packages":' + json.dumps(packageList) + '}}}'
self.deviceShadowHandler.shadowUpdate(JSONPayload, customShadowCallback_Update, 5)
return self.formatResults(results, didSucceed)
def pipInstall(self, execution):
systems = {
'Darwin': ["pip3", "install", "--user"],
'Linux': ["pip3", "install", "--user"]
}
results = []
didSucceed = True
# Get the function from switcher dictionary
commands = systems.get(platform.system())
if commands is None:
return self.notSupportedHandler(execution)
else:
for package in execution['jobDocument']['packages']:
pkgCommands = commands.copy()
pkgCommands.append(package['name'])
result = subprocess.run(pkgCommands, shell=False, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
results.append(result)
if result.stderr and result.stderr is not None:
didSucceed = False
return self.formatResults(results, didSucceed)
def pipUninstall(self, execution):
systems = {
'Darwin': ["pip3", "uninstall", "-y"],
'Linux': ["pip3", "uninstall", "-y"]
}
results = []
didSucceed = True
# Get the function from switcher dictionary
commands = systems.get(platform.system())
if commands is None:
return self.notSupportedHandler(execution)
else:
for package in execution['jobDocument']['packages']:
pkgCommands = commands.copy()
pkgCommands.append(package['name'])
result = subprocess.run(pkgCommands, shell=False, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
results.append(result)
if result.stderr and result.stderr is not None:
didSucceed = False
return self.formatResults(results, didSucceed)
# CONTAINERS
def listContainers(self, execution):
client = docker.from_env()
containers = client.containers.list()
containerList = []
didSucceed = True
for container in containers:
print(container.short_id + ', ' + container.name + ', ' + container.status + ', ' + container.image.attrs['RepoTags'][0])
containerList.append(
{
'short_id': container.short_id,
'name': container.name,
'status': container.status,
'repoTag': container.image.attrs['RepoTags'][0]
})
JSONPayload = '{"state":{"reported":{"containers":' + json.dumps(containerList) + '}}}'
self.deviceShadowHandler.shadowUpdate(JSONPayload, customShadowCallback_Update, 5)
return self.formatResults(containerList, didSucceed)
def pullContainerImages(self, execution):
results = []
didSucceed = True
session = self.getTemporaryCredentials()
ecr_client = session.client('ecr')
response = ecr_client.get_authorization_token()
token = response['authorizationData'][0]['authorizationToken']
tokenString = base64.b64decode(token).decode().split(':')[1]
result = subprocess.run(["docker","login","-u","AWS","-p",tokenString,"ACCOUNT.dkr.ecr.us-east-1.amazonaws.com"], shell=False, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
results.append(result)
if result.stderr and result.stderr is not None:
didSucceed = False
for image in execution['jobDocument']['images']:
result = subprocess.run(["docker","pull",image['url'] + ':' + image['version']], shell=False, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
results.append(result)
if result.stderr and result.stderr is not None:
didSucceed = False
return self.formatResults(results, didSucceed)
def runContainers(self, execution):
results = []
didSucceed = True
for image in execution['jobDocument']['images']:
client = docker.from_env()
client.containers.run(image['name'] + ':' + image['version'], detach=True)
return self.formatResults(results, didSucceed)
def stopContainers(self, execution):
results = []
didSucceed = True
for container in execution['jobDocument']['containers']:
client = docker.from_env()
container = client.containers.get(container)
container.stop()
return self.formatResults(results, didSucceed)
def uploadContainerLogs(self, execution):
results = []
didSucceed = True
session = self.getTemporaryCredentials()
s3_client = session.client('s3')
for container in execution['jobDocument']['containers']:
key = None
if 'prefix' in container:
key = container['prefix']
key += str(time.time()) + '_' + container['id'] + '.log'
bucket = container['bucket']
client = docker.from_env()
container = client.containers.get(container['id'])
s3_client.put_object(Body=container.logs(), Bucket=bucket, Key=key)
return self.formatResults(results, didSucceed)
def customShadowCallback_Update(payload, responseStatus, token):
# payload is a JSON string ready to be parsed using json.loads(...)
# in both Py2.x and Py3.x
if responseStatus == "timeout":
print("Update request " + token + " time out!")
if responseStatus == "accepted":
payloadDict = json.loads(payload)
print("~~~~~~~~~~~~~~~~~~~~~~~")
print("Update request with token: " + token + " accepted!")
print(payload)
print("~~~~~~~~~~~~~~~~~~~~~~~\n\n")
if responseStatus == "rejected":
print("Update request " + token + " rejected!")