15
15
# Get Required Info from Instance
16
16
for each_item in ec2_client .describe_instances ()['Reservations' ]:
17
17
for each_instance in each_item ['Instances' ]:
18
- if each_instance ['State' ] == "Running" :
19
- public_ip = each_instance ['PublicIpAddress' ]
20
- public_dns_name = each_instance ['PublicDnsName' ]
21
- else :
22
- continue
18
+ instance_state = each_instance ['State' ]['Name' ]
19
+ if instance_state == 'running' and each_instance .get ('PublicIpAddress' ) is not None :
20
+ public_ip = each_instance .get ('PublicIpAddress' )
21
+ print ("Public_Ip:" ,public_ip )
23
22
24
23
# Get list of Volume ID and its associated Instance ID
25
24
response = ec2_client .describe_volumes ()['Volumes' ]
50
49
print ("Current Disk Size:" , current_size )
51
50
52
51
53
- if current_size == 10 :
52
+ if current_size == 12 :
54
53
print ("Disk already same size as target size.. exiting" )
55
54
sys .exit (0 )
56
55
else :
57
56
print ("Attempting to increasing volume size from AWS ..." )
58
57
volumemodify = ec2_client .modify_volume (
59
58
DryRun = False ,
60
59
VolumeId = volume_id ,
61
- Size = 10 ,
60
+ Size = 12 ,
62
61
VolumeType = volume_type
63
62
)
64
- waiter = ec2_client . get_waiter ( 'volume_in_use ' )
65
- waiter . wait ( VolumeIds = [ volume_id ] )
63
+ print ( 'Extending volume from AWS ...... ' )
64
+ sleep ( 300 )
66
65
print ("Volume has been modified from AWS System.." )
67
66
68
67
# Logging into the instance to get disk partitions using paramiko
85
84
# print(stdout.read())
86
85
87
86
# # close the client connection once the job is done
88
- # client.close()
87
+ # client.close()
88
+
89
+ # ====== Windows testing =======
90
+
91
+ key = paramiko .RSAKey .from_private_key_file ("E:\\ aws-keys\ebsdemo.pem" )
92
+ client = paramiko .SSHClient ()
93
+ client .set_missing_host_key_policy (paramiko .AutoAddPolicy ())
94
+ client .connect (hostname = public_ip , username = "ec2-user" , pkey = key )
95
+
96
+ localfile = "E:\\ projects\\ aws-python\getDiskInfo.py"
97
+ remotefile = "/tmp/getDiskInfo.py"
98
+
99
+ # Copy file locally to remote host
100
+ sftp = client .open_sftp ()
101
+ sftp .put (localfile , remotefile )
102
+ sftp .close ()
103
+
104
+ #Execute a command(cmd) after connecting/ssh to an instance
105
+ stdin ,stdout ,stderr = client .exec_command ("chmod +x /tmp/getDiskInfo.py; python /tmp/getDiskInfo.py" )
106
+ print (stdout .read ())
107
+
108
+ # close the client connection once the job is done
109
+ client .close ()
0 commit comments