5
5
import paramiko
6
6
import sys
7
7
from pprint import pprint
8
-
9
- class Error (Exception ):
10
- pass
11
-
12
- class VolumeModificationRateExceeded (Error ):
13
- pass
14
-
8
+ from time import sleep
15
9
16
10
aws_mgmt_console = boto3 .session .Session (profile_name = "default" ,region_name = "ap-south-1" )
17
11
@@ -21,8 +15,11 @@ class VolumeModificationRateExceeded(Error):
21
15
# Get Required Info from Instance
22
16
for each_item in ec2_client .describe_instances ()['Reservations' ]:
23
17
for each_instance in each_item ['Instances' ]:
24
- public_ip = each_instance ['PublicIpAddress' ]
25
- public_dns_name = each_instance ['PublicDnsName' ]
18
+ if each_instance ['State' ] == "Running" :
19
+ public_ip = each_instance ['PublicIpAddress' ]
20
+ public_dns_name = each_instance ['PublicDnsName' ]
21
+ else :
22
+ continue
26
23
27
24
# Get list of Volume ID and its associated Instance ID
28
25
response = ec2_client .describe_volumes ()['Volumes' ]
@@ -34,6 +31,11 @@ class VolumeModificationRateExceeded(Error):
34
31
az = each_item ['AvailabilityZone' ]
35
32
current_state = each_item ['State' ]
36
33
volume_type = each_item ['VolumeType' ]
34
+
35
+ print ("Current Size: " ,current_size )
36
+ print ("az:" ,az )
37
+ print ("current_state: " ,current_state )
38
+ print ("volume_type: " ,volume_type )
37
39
38
40
# Get instance details from volume
39
41
for each_instance in each_item ['Attachments' ]:
@@ -47,37 +49,40 @@ class VolumeModificationRateExceeded(Error):
47
49
print ("Device Name:" , device )
48
50
print ("Current Disk Size:" , current_size )
49
51
50
- if current_size == 12 :
52
+
53
+ if current_size == 10 :
51
54
print ("Disk already same size as target size.. exiting" )
52
55
sys .exit (0 )
53
56
else :
54
57
print ("Attempting to increasing volume size from AWS ..." )
55
58
volumemodify = ec2_client .modify_volume (
56
59
DryRun = False ,
57
60
VolumeId = volume_id ,
58
- Size = 12 ,
61
+ Size = 10 ,
59
62
VolumeType = volume_type
60
63
)
64
+ waiter = ec2_client .get_waiter ('volume_in_use' )
65
+ waiter .wait (VolumeIds = [volume_id ])
61
66
print ("Volume has been modified from AWS System.." )
62
67
63
68
# Logging into the instance to get disk partitions using paramiko
64
69
65
- key = paramiko .RSAKey .from_private_key_file ("/home/samperay/MyLinuxEC2KeyPair.pem" )
66
- client = paramiko .SSHClient ()
67
- client .set_missing_host_key_policy (paramiko .AutoAddPolicy ())
68
- client .connect (hostname = public_ip , username = "ec2-user" , pkey = key )
70
+ # key = paramiko.RSAKey.from_private_key_file("/home/samperay/MyLinuxEC2KeyPair.pem")
71
+ # client = paramiko.SSHClient()
72
+ # client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
73
+ # client.connect(hostname=public_ip, username="ec2-user", pkey=key)
69
74
70
- localfile = "/home/samperay/Documents/gitprojects/aws-python/getDiskInfo.py"
71
- remotefile = "/tmp/getDiskInfo.py"
75
+ # localfile = "/home/samperay/Documents/gitprojects/aws-python/getDiskInfo.py"
76
+ # remotefile = "/tmp/getDiskInfo.py"
72
77
73
- # Copy file locally to remote host
74
- sftp = client .open_sftp ()
75
- sftp .put (localfile , remotefile )
76
- sftp .close ()
78
+ # # Copy file locally to remote host
79
+ # sftp = client.open_sftp()
80
+ # sftp.put(localfile, remotefile)
81
+ # sftp.close()
77
82
78
- #Execute a command(cmd) after connecting/ssh to an instance
79
- stdin ,stdout ,stderr = client .exec_command ("chmod +x /tmp/getDiskInfo.py; python /tmp/getDiskInfo.py" )
80
- print (stdout .read ())
83
+ # # Execute a command(cmd) after connecting/ssh to an instance
84
+ # stdin,stdout,stderr = client.exec_command("chmod +x /tmp/getDiskInfo.py; python /tmp/getDiskInfo.py")
85
+ # print(stdout.read())
81
86
82
- # close the client connection once the job is done
83
- client .close ()
87
+ # # close the client connection once the job is done
88
+ # client.close()
0 commit comments