Skip to content

Commit 05d96d5

Browse files
committed
Disk modification changes completed
1 parent 69bdeeb commit 05d96d5

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

diskExtensionEC2.py

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@
55
import paramiko
66
import sys
77
from pprint import pprint
8-
9-
class Error(Exception):
10-
pass
11-
12-
class VolumeModificationRateExceeded(Error):
13-
pass
14-
8+
from time import sleep
159

1610
aws_mgmt_console = boto3.session.Session(profile_name="default",region_name="ap-south-1")
1711

@@ -21,8 +15,11 @@ class VolumeModificationRateExceeded(Error):
2115
# Get Required Info from Instance
2216
for each_item in ec2_client.describe_instances()['Reservations']:
2317
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
2623

2724
# Get list of Volume ID and its associated Instance ID
2825
response = ec2_client.describe_volumes()['Volumes']
@@ -34,6 +31,11 @@ class VolumeModificationRateExceeded(Error):
3431
az = each_item['AvailabilityZone']
3532
current_state = each_item['State']
3633
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)
3739

3840
# Get instance details from volume
3941
for each_instance in each_item['Attachments']:
@@ -47,37 +49,40 @@ class VolumeModificationRateExceeded(Error):
4749
print("Device Name:", device)
4850
print("Current Disk Size:", current_size)
4951

50-
if current_size == 12:
52+
53+
if current_size == 10:
5154
print("Disk already same size as target size.. exiting")
5255
sys.exit(0)
5356
else:
5457
print("Attempting to increasing volume size from AWS ...")
5558
volumemodify = ec2_client.modify_volume(
5659
DryRun = False,
5760
VolumeId = volume_id,
58-
Size = 12,
61+
Size = 10,
5962
VolumeType = volume_type
6063
)
64+
waiter = ec2_client.get_waiter('volume_in_use')
65+
waiter.wait(VolumeIds=[volume_id])
6166
print("Volume has been modified from AWS System..")
6267

6368
# Logging into the instance to get disk partitions using paramiko
6469

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)
6974

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"
7277

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()
7782

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())
8186

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()

getDiskInfo.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ def partitions(disk):
2727
device, Type, size, used, available, percent, mountpoint = output.split("\n")[1].split()
2828

2929
if Type == 'xfs':
30-
os.system('sudo xfs_growfs -d '+mountpoint)
30+
os.system('sudo xfs_growfs -d'+mountpoint)
3131
print('file system has been extended')
3232
elif Type == 'ext3' or 'ext4':
3333
print('Need to work on ext file systems')
3434

35-
sys.exit(0)
36-
35+
sys.exit(0)

0 commit comments

Comments
 (0)