-
Notifications
You must be signed in to change notification settings - Fork 0
/
AwsLinuxRemoteMachine.py
492 lines (445 loc) · 21.6 KB
/
AwsLinuxRemoteMachine.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
import os, sys, re, time
import boto3
import logging
import botocore
#from RemoteMachine import RemoteMachine
## not exactly extending remote machine?
class AwsLinuxRemoteMachine():
@staticmethod
def create_aws_linux_machine(image_id, access_key_id, secret_access_key, region,
instance_type='t2.micro'):
"""
Creates an ami isntance in AWS, returns an AwsLinuxremoteMachine object
:param instance_type:
:param image_id:
:param access_key_id:
:param secret_access_key:
:param region:
:return:
"""
ec2 = boto3.resource('ec2',
aws_access_key_id=access_key_id,
aws_secret_access_key=secret_access_key,
region_name=region)
ec2_client = boto3.client('ec2',
aws_access_key_id=access_key_id,
aws_secret_access_key=secret_access_key,
region_name=region)
instances = ec2.create_instances(
ImageId=image_id,
MinCount=1,
MaxCount=1,
InstanceType=instance_type,
IamInstanceProfile={'Name': 'SimpleSystemManagerRole'}
)
instance_id = str(instances[0].id)
waiter = ec2_client.get_waiter('instance_status_ok')
waiter.wait(InstanceIds=[instance_id])
return AwsLinuxRemoteMachine(instance_id, access_key_id, secret_access_key, region)
def __init__(self, instance_id, access_key_id, secret_access_key, region, log_level='INFO'):
"""
constructor
:param machine_ip: ip address of remote machine
:param machine_name: host name of remote machine
:param machine_username: login username for remote machine
:param machine_password: login password for remote machine
:param psexec_exe: path to psexec exe
:param log_level: set log level
"""
# use 'super' to call a method defined in the parent class.
# The call below calls the constructor from parent class.
#super(AwsLinuxRemoteMachine, self).__init__(machine_ip, machine_name, machine_username, machine_password)
self.machine_type = 'aws'
self.instance_id = instance_id
self.access_key_id = access_key_id
self.secret_access_key = secret_access_key
self.region = region
self.bucket_name = 'dxlauto-bucket'
self.ec2_resource = boto3.resource('ec2',
aws_access_key_id=self.access_key_id,
aws_secret_access_key=self.secret_access_key,
region_name=self.region)
self.ec2_client = boto3.client('ec2',
aws_access_key_id=self.access_key_id,
aws_secret_access_key=self.secret_access_key,
region_name=self.region)
self.ec2_ssm_client = boto3.client('ssm',
aws_access_key_id=self.access_key_id,
aws_secret_access_key=self.secret_access_key,
region_name=self.region)
self.s3_client = boto3.client('s3',
aws_access_key_id=self.access_key_id,
aws_secret_access_key=self.secret_access_key,
region_name=self.region)
self.s3_resource = boto3.resource('s3',
aws_access_key_id=self.access_key_id,
aws_secret_access_key=self.secret_access_key,
region_name=self.region)
aws_region_list = ['us-west-1', 'us-west-2', 'ca-central-1', 'eu-west-1', 'eu-west-2', 'eu-central-1',
'ap-south-1', 'ap-southeast-1', 'ap-southeast-2', 'ap-northeast-1', 'ap-northeast-2',
'sa-east-1', 'sa-east-2']
# setup logging - change level to DEBUG to view all the activity
self.logger = logging.getLogger(__name__)
formatter = logging.Formatter('%(asctime)s: %(levelname)s: %(message)s', datefmt="%Y%m%d-%H%M%S")
handler = logging.StreamHandler()
handler.setFormatter(formatter)
self.logger.addHandler(handler)
self.logger.debug("Instance {0} init done".format(self.instance_id))
# Store instance
self.instance = self.ec2_resource.Instance(self.instance_id)
# Configure to use AWS CLI on the instance
self.configure_aws_cli(access_key_id, secret_access_key, region)
# create bucket folder for machine upload/download
response = self.s3_client.list_buckets()
bucket_list = [bucket['Name'] for bucket in response['Buckets']]
self.logger.debug(bucket_list)
if self.bucket_name not in bucket_list:
#self.s3.create_bucket(Bucket=self.bucket_name, CreateBucketConfiguration=CreateBucketConfiguration)
bucket_created = False
for region in aws_region_list:
try:
self.logger.debug("Trying to create bucket in region: {}".format(region))
bucket_config = {'LocationConstraint': region}
self.s3_client.create_bucket(Bucket=self.bucket_name, CreateBucketConfiguration=bucket_config)
bucket_created = True
break
except botocore.exceptions.ClientError:
self.logger.error("Unable to create bucket in region: {}".format(region))
continue
if not bucket_created:
raise Exception, "ERROR: Unable to create bucket in any of the regions: {}".format(aws_region_list)
# create bucket folder for machine
print self.s3_client.put_object(Bucket=self.bucket_name, Key=instance_id+'/')
if log_level:
self.set_log_level(log_level)
else:
self.set_log_level('INFO')
# --------------------------------------------------
# PROPERTIES
# --------------------------------------------------
# https://www.programiz.com/python-programming/property
@property
def ip(self):
return self.instance.public_ip_address
@property
def name(self):
return self.instance.public_dns_name
@property
def type(self):
return self.machine_type
# --------------------------------------------------
# SETUP
# --------------------------------------------------
def set_log_level(self, level):
"""
set the logging level of the class
:param level: log level to set for the object
:return: None
"""
if level == 'DEBUG':
self.logger.setLevel(logging.DEBUG)
elif level == 'INFO':
self.logger.setLevel(logging.INFO)
else:
self.logger.setLevel(logging.INFO)
# --------------------------------------------------
# CHECK METHODS
# --------------------------------------------------
def check_file_exists(self, file_path):
"""
check if the file exists on remote machine
:param file_path: path on the remote machine
:return: True/False
"""
try:
self.execute_command("ls {}".format(file_path), timeout=2)
return True
except TimeoutError as ex:
self.logger.debug("File {} does not exist!".format(file_path))
return False
def check_folder_exists(self, folder_path):
"""
check if the folder exists on remote machine
:param folder_path: path on the remote machine
:return: True/False
"""
try:
self.execute_command("ls {}".format(folder_path), timeout=2)
return True
except TimeoutError as ex:
self.logger.debug("Folder {} does not exist!".format(folder_path))
return False
# --------------------------------------------------
# DOWNLOAD
# --------------------------------------------------
def download_file(self, local_destination, remote_destination, file_name):
"""
download file from remote destination path to destination on local machine
:param local_destination: folder path on local machine
:param remote_destination: folder path on remote machine
:param file_name: file name to download
:return: True/False
"""
self.logger.debug("Downloading file {} FROM={}, TO={}".format(file_name, remote_destination, local_destination))
"""
# check if local folder exists
if not os.path.exists(local_destination):
os.makedirs(local_destination)
# check if remote file exists
remote_file_path = os.path.join(remote_destination, file_name)
local_file_path = os.path.join(local_destination, file_name)
if self.check_file_exists(remote_file_path):
self.ftp.get(remote_file_path, local_file_path)
return os.path.exists(os.path.join(local_destination, file_name))
else:
return False
"""
def download_folder(self, local_destination, remote_destination):
"""
download folder from remote destination to local destination
:param local_destination: path of folder on local machine
:param remote_destination: path of folder on remote machine
:return: true/false
"""
self.logger.debug("Downloading folder FROM={}, TO={}".format(remote_destination, local_destination))
"""
# check if local folder exists
if not os.path.exists(local_destination):
os.makedirs(local_destination)
# check if remote folder exists
if not self.check_folder_exists(remote_destination):
return False
# download each file one by one from remote location
self.ftp.chdir(remote_destination)
for file in self.ftp.listdir():
if not self.download_file(local_destination, remote_destination, file):
self.logger.error("Problem while downloading file {}!".format(os.path.join(remote_destination, file)))
return False
return True
"""
# --------------------------------------------------
# UPLOAD
# --------------------------------------------------
def upload_file(self, local_destination, remote_destination, file_name):
"""
upload file from local destination to remote machine destination
:param local_destination: location on local machine
:param remote_destination: location on remote machine
:param file_name: name of file to upload
:return: true/false
"""
self.logger.debug("Uploading file {0} FROM={1}, TO={2}".format(file_name, local_destination, remote_destination))
local_file_path=os.path.join(local_destination, file_name)
# check local file exists
if not os.path.exists(local_file_path):
self.logger.error("No file exists in local machine at path {}!".format(local_file_path))
return False
# check remote path exists
if not self.check_folder_exists(remote_destination):
self.logger.debug("No folder exists in remote machine at path {}!".format(remote_destination))
self._mkdir_path(remote_destination)
uploaded_file = self.instance_id+'/'+file_name
bucket_path = self.bucket_name+'/'+self.instance_id
# upload file to bucket
self.s3_resource.meta.client.upload_file(local_file_path, self.bucket_name, uploaded_file)
# sync file to machine from bucket
self.execute_command("sudo aws s3 sync s3://{0} {1} --exclude \"*\" --include \"{2}\"".format(bucket_path, remote_destination, file_name))
return self.check_file_exists(os.path.join(remote_destination, file_name))
def upload_folder(self, local_destination, remote_destination):
"""
upload folder from local destination to remote folder destination
:param local_destination: location on local machine
:param remote_destination: location on remote machine
:return: true/false
"""
self.logger.debug("Uploading folder FROM={0}, TO={1}".format(local_destination, remote_destination))
# check local folder exists
if not os.path.exists(local_destination):
self.logger.error("No folder exists in local machine at path {}!".format(local_destination))
return False
# check remote path exists
if not self.check_folder_exists(remote_destination):
self.logger.debug("No folder exists in remote machine at path {}!".format(remote_destination))
self._mkdir_path(remote_destination)
bucket_path = self.bucket_name+'/'+self.instance_id
for file_name in os.listdir(local_destination):
local_file_path=os.path.join(local_destination, file_name)
uploaded_file = self.instance_id+'/'+file_name
# upload file to bucket
self.s3_resource.meta.client.upload_file(local_file_path, self.bucket_name, uploaded_file)
# sync file to machine from bucket
self.execute_command("sudo aws s3 sync s3://{0} {1} --exclude \"*\" --include \"{2}\"".format(bucket_path, remote_destination, file_name))
if not self.check_file_exists(os.path.join(remote_destination, file_name)):
self.logger.error("Problem while uploading file {}!".format(os.path.join(local_destination, file_name)))
return False
return True
"""
# check local folder exists
if not os.path.exists(local_destination):
self.logger.error("No file exists in local machine at path {}!".format(local_destination))
return False
# check remote dir exists
if not self.check_folder_exists(remote_destination):
self.logger.debug("No folder exists in remote machine {}. Creating directory tree".format(remote_destination))
self._mkdir_path(remote_destination)
file_list=os.listdir(local_destination)
for file in file_list:
if not self.upload_file(local_destination, remote_destination, file):
self.logger.error("Problem while uploading file {}!".format(os.path.join(local_destination, file)))
return False
return True
"""
# --------------------------------------------------
# SERVICE
# --------------------------------------------------
def service_status(self, service_name):
"""
check if the service by name is running on remote machine
:param service_name: name of service to check the status of
:return: true/false
"""
try:
output = self.execute_command("service {} status".format(service_name), timeout=10)
if not output:
self.logger.debug("No service found with name '{}'!".format(service_name))
return None
if 'stopped' in output:
self.logger.debug("Service '{}' is not running".format(service_name))
return False
if 'running' in output:
self.logger.debug("Service '{}' is running".format(service_name))
return True
except TimeoutError as ex:
self.logger.debug("Service '{}' is not running".format(service_name))
return False
def service_start(self, service_name, timeout=5):
"""
start service by name on remote machine
:param service_name: name of service to start
:param timeout: timeout for attempt to start
:return: true/false
"""
if not self.service_status(service_name):
self.logger.debug("Starting service {}".format(service_name))
self.execute_command('service {} start'.format(service_name))
else:
return True
# wait for time out
end_time = time.time()+timeout
while end_time > time.time():
if not self.service_status(service_name):
self.logger.debug("Waiting for service {} to start".format(service_name))
time.sleep(1)
else:
return True
# final check
if not self.service_status(service_name):
self.logger.debug("Unable to start {} after timeout {} seconds expired".format(service_name, timeout))
return False
else:
return True
def service_stop(self, service_name, timeout=5):
"""
stop the service by name on remote machine
:param service_name: name of service to stop
:param timeout: timeout for service to stop
:return: true/false
"""
if self.service_status(service_name):
self.logger.debug("Stopping service {}".format(service_name))
self.execute_command('service {} stop'.format(service_name))
else:
return True
# wait for time out
end_time = time.time()+timeout
while end_time > time.time():
if self.service_status(service_name):
self.logger.debug("Waiting for service {} to stop".format(service_name))
time.sleep(1)
else:
return True
# final check
if self.service_status(service_name):
self.logger.debug("Unable to stop {} after timeout {} seconds expired".format(service_name, timeout))
return False
else:
return True
# --------------------------------------------------
# EXECUTE
# --------------------------------------------------
def execute_command(self, command, timeout=10):
"""
execute command on remote machine
:param command: command to execute on remote machine
:param timeout: timeout
:return: output of command execution
"""
self.logger.debug("Executing Command: '{}'".format(command))
cmd = [command]
command_meta = self.ec2_ssm_client.send_command(DocumentName="AWS-RunShellScript", Parameters={'commands': cmd},
InstanceIds=[self.instance_id])
time_initial = time.time()
now = time.time()
while now - time_initial < timeout:
command_status = self.ec2_ssm_client.list_commands(CommandId=
command_meta["Command"]["CommandId"])["Commands"][0]["Status"]
if command_status == "Success":
command_result = self.ec2_ssm_client.get_command_invocation(InstanceId=self.instance_id,
CommandId=command_meta["Command"]["CommandId"])["StandardOutputContent"]
self.logger.debug("Command Output: '{}'".format(command_result))
return command_result
else:
time.sleep(0.1)
now = time.time()
if now - time_initial > timeout:
command_status = self.ec2_ssm_client.list_commands(CommandId=
command_meta["Command"]["CommandId"])["Commands"][0]["Status"]
self.logger.debug("Command Status = {0}".format(command_status))
if command_status != "Success":
raise TimeoutError, "function timed out"
# this method does not work properly - suggest using sync method for async/sync both
def execute_command_async(self, command):
"""
execute command on remote machine asynchronously
:param command: command to execute on remote machine
:return: output of command execution
"""
cmd = [command]
self.ec2_ssm_client.send_command(DocumentName="AWS-RunShellScript", Parameters={'commands': cmd},
InstanceIds=[self.instance_id])
# --------------------------------------------------
# UTILS
# --------------------------------------------------
def _mkdir_path(self, remote_directory):
"""
makes the directory tree recursively, if it does not exist
:param remote_directory:
:return: None
"""
self.logger.debug("remote directory = {}".format(remote_directory))
directory_tree = remote_directory.split('/')
directory_tree = directory_tree[1:len(directory_tree)-1]
for tree_depth in range(1, len(directory_tree)):
self.logger.debug("depth={}".format(tree_depth))
self.logger.debug("DT={}".format(directory_tree))
self.logger.debug("DT_sub={}".format(directory_tree[:tree_depth]))
sub_tree = '/'.join(directory_tree[:tree_depth])
self.logger.debug("ST={}".format(sub_tree))
if not self.check_folder_exists("/{}/".format(sub_tree)):
self.execute_command("sudo mkdir /{}/".format(sub_tree), timeout=5)
def configure_aws_cli(self, access_key_id, secret_access_key, region):
"""
configure AWS environment to be able to execute CLI commands
:param access_key_id:
:param secret_access_key:
:param region:
:return:
"""
self.execute_command("export AWS_ACCESS_KEY_ID={}".format(access_key_id), timeout=5)
self.execute_command("export AWS_SECRET_ACCESS_KEY={}".format(secret_access_key), timeout=5)
self.execute_command("export AWS_DEFAULT_REGION={}".format(region), timeout=5)
# --------------------------------------------------
# CUSTOM EXCEPTIONS
# --------------------------------------------------
class TimeoutError(Exception):
pass