Skip to content

Commit b4f635f

Browse files
committed
xfs file system disk extension completed
1 parent 05d96d5 commit b4f635f

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

diskExtensionEC2.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
# Get Required Info from Instance
1616
for each_item in ec2_client.describe_instances()['Reservations']:
1717
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)
2322

2423
# Get list of Volume ID and its associated Instance ID
2524
response = ec2_client.describe_volumes()['Volumes']
@@ -50,19 +49,19 @@
5049
print("Current Disk Size:", current_size)
5150

5251

53-
if current_size == 10:
52+
if current_size == 12:
5453
print("Disk already same size as target size.. exiting")
5554
sys.exit(0)
5655
else:
5756
print("Attempting to increasing volume size from AWS ...")
5857
volumemodify = ec2_client.modify_volume(
5958
DryRun = False,
6059
VolumeId = volume_id,
61-
Size = 10,
60+
Size = 12,
6261
VolumeType = volume_type
6362
)
64-
waiter = ec2_client.get_waiter('volume_in_use')
65-
waiter.wait(VolumeIds=[volume_id])
63+
print('Extending volume from AWS ......')
64+
sleep(300)
6665
print("Volume has been modified from AWS System..")
6766

6867
# Logging into the instance to get disk partitions using paramiko
@@ -85,4 +84,26 @@
8584
# print(stdout.read())
8685

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

getDiskInfo.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,21 @@ def partitions(disk):
1717
disk = physical_drives()
1818
disk_part = partitions(disk[0])
1919

20-
conv_str = ' '.join(map(str, disk_part))
21-
main_device = "/dev/" + conv_str
20+
conv_str = ' '.join(map(str, disk))
21+
root_device = "/dev/"+conv_str
22+
23+
conv_str1 = ' '.join(map(str, disk_part))
24+
root_partition = "/dev/"+conv_str1
2225

2326
# Fetching mount point from main_device
2427

25-
df = subprocess.Popen(["df", "-hT", main_device], stdout=subprocess.PIPE)
28+
df = subprocess.Popen(["df", "-hT", root_partition], stdout=subprocess.PIPE)
2629
output = df.communicate()[0]
2730
device, Type, size, used, available, percent, mountpoint = output.split("\n")[1].split()
2831

2932
if Type == 'xfs':
30-
os.system('sudo xfs_growfs -d'+mountpoint)
33+
os.system('sudo growpart '+root_device+' 1')
34+
os.system('sudo xfs_growfs -d '+mountpoint)
3135
print('file system has been extended')
3236
elif Type == 'ext3' or 'ext4':
3337
print('Need to work on ext file systems')

0 commit comments

Comments
 (0)