|
| 1 | +import os |
| 2 | +from subprocess import PIPE, run, check_output,call |
| 3 | + |
| 4 | +profile = "" |
| 5 | +keyfile = "" |
| 6 | + |
| 7 | +# wrapper to get ouput of system command |
| 8 | +def out(command): |
| 9 | + result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True) |
| 10 | + return result.stdout |
| 11 | + |
| 12 | + |
| 13 | +# function to launch AWS instances |
| 14 | +def launchInstance(): |
| 15 | + keyname, securityGroup, rc = keyAndSecurity() |
| 16 | + |
| 17 | + if rc != 0: |
| 18 | + return |
| 19 | + |
| 20 | + print("\n1. Amazon Linux 2 AMI 64-bit (x86)\t2. RHEL 8 64-bit (x86)\t 3. Custom Image ID\t 4. Exit Instance Creation") |
| 21 | + for i in range(5): |
| 22 | + amiChoice = input("Enter choice (1-4) : ") |
| 23 | + if amiChoice == '1': |
| 24 | + imageID = "ami-0e306788ff2473ccb" |
| 25 | + break |
| 26 | + elif amiChoice == '2': |
| 27 | + imageID = "ami-052c08d70def0ac62" |
| 28 | + break |
| 29 | + elif amiChoice == '3': |
| 30 | + imageID = input("Enter Image ID : ") |
| 31 | + break |
| 32 | + elif amiChoice == '4': |
| 33 | + print("Exiting...") |
| 34 | + return |
| 35 | + else: |
| 36 | + if i == 4: |
| 37 | + print("Entered wrong option 5 times. Exiting...") |
| 38 | + return |
| 39 | + print("Invalid choice!!! Choose from 1-4\n") |
| 40 | + |
| 41 | + |
| 42 | + print("Create Instance with instanceID : {}, type : t2.micro, key-name : {}, security-group : {} ?".format(imageID, keyname, securityGroup)) |
| 43 | + create = input("Enter Y to confirm : ") |
| 44 | + if create == 'Y' or create == 'y': |
| 45 | + errorInstance = call( |
| 46 | + "aws ec2 run-instances --image-id {} --count 1 --instance-type t2.micro --key-name {} --security-groups {} --profile {}".format(imageID, keyname, securityGroup, profile)) |
| 47 | + if errorInstance == 0: |
| 48 | + instanceID = input("Enter the instance ID : ") |
| 49 | + nametag = input("Enter name tag for instance (Eg. MyInstance): ") |
| 50 | + call( |
| 51 | + "aws ec2 create-tags --resources {} --tags Key=Name,Value={} --profile {}".format(instanceID, nametag, profile)) |
| 52 | + print("Instance is created and running.\n") |
| 53 | + return |
| 54 | + |
| 55 | + |
| 56 | +# function to create key-pair and security group for instance |
| 57 | +def keyAndSecurity(): |
| 58 | + while True: |
| 59 | + print("\nDefault values for each field taken if left blank => key-pair : MyKeyPair | security-group : my-sg | --description : My Security Group ") |
| 60 | + keyPair = input("1. Enter name for new key-pair : ") |
| 61 | + secGroup = input("2. Enter name for security group : ") |
| 62 | + descSecurity = input( |
| 63 | + "Enter description for security group {} : ".format(secGroup)) |
| 64 | + if keyPair == '': |
| 65 | + keyPair = "MyKeyPair" |
| 66 | + if secGroup == '': |
| 67 | + secGroup = "my-sg" |
| 68 | + if descSecurity == '': |
| 69 | + descSecurity = "My Security Group" |
| 70 | + print("Entered fields : \nkey-pair : {}\nsecurity-group : {}\ndescription : {}\n".format( |
| 71 | + keyPair, secGroup, descSecurity)) |
| 72 | + confirm = input("Press Y to confirm : ") |
| 73 | + if(confirm == 'Y' or confirm == 'y'): |
| 74 | + # Creating Key-Pair |
| 75 | + os.system(r"mkdir %USERPROFILE%\KeyPairs") |
| 76 | + print(r"{}.pem created in folder C:\Users\%USERNAME%\KeyPairs ".format(keyPair)) |
| 77 | + errorKeyPair = os.system( |
| 78 | + r"aws ec2 create-key-pair --key-name {0} --profile {1} --output text > %USERPROFILE%\KeyPairs\{0}.pem".format(keyPair, profile)) |
| 79 | + # Creating Security-Group |
| 80 | + errorSecurityGroup = call( |
| 81 | + 'aws ec2 create-security-group --group-name {} --description "{}" --profile {}'.format(secGroup, descSecurity, profile)) |
| 82 | + if errorKeyPair != 0 or errorSecurityGroup != 0: |
| 83 | + recreate = input( |
| 84 | + "\nWould you like to delete created key-pair or security-group and try again ? (Press Y for Yes): ") |
| 85 | + if recreate == 'Y' or recreate == 'y': |
| 86 | + if errorKeyPair != 0 and errorSecurityGroup == 0: |
| 87 | + rc = 11 |
| 88 | + call( |
| 89 | + "aws ec2 delete-security-group --group-name {} --profile {}".format(secGroup, profile)) |
| 90 | + elif errorSecurityGroup != 0 and errorKeyPair == 0: |
| 91 | + rc = 12 |
| 92 | + call( |
| 93 | + "aws ec2 delete-key-pair --key-name {} --profile {}".format(keyPair, profile)) |
| 94 | + continue |
| 95 | + else: |
| 96 | + rc = 1 |
| 97 | + break |
| 98 | + else: |
| 99 | + rc = 0 |
| 100 | + print("rc : {}".format(rc)) |
| 101 | + break |
| 102 | + return (keyPair, secGroup, rc) |
| 103 | + |
| 104 | + |
| 105 | +# function to add inbound rules to security group |
| 106 | +def inboundRules(): |
| 107 | + sgname = input("\nEnter security group name : ") |
| 108 | + print("1. All Traffic (Default) 2. TCP 3. UDP 4. SSH 5. Custom Protocol : ") |
| 109 | + for i in range(5): |
| 110 | + plChoice = input("Choose between 1-5 : ") |
| 111 | + if plChoice == '1' or plChoice == '': |
| 112 | + protocol = 'all' |
| 113 | + port = 'all' |
| 114 | + break |
| 115 | + elif plChoice == '2': |
| 116 | + protocol = 'tcp' |
| 117 | + port = '0-65535' |
| 118 | + break |
| 119 | + elif plChoice == '3': |
| 120 | + protocol = 'udp' |
| 121 | + port = '0-65535' |
| 122 | + break |
| 123 | + elif plChoice == '4': |
| 124 | + protocol = 'tcp' |
| 125 | + port = '22' |
| 126 | + break |
| 127 | + elif plChoice == '5': |
| 128 | + protocol = input("Enter protocol number (Eg. 1 for ICMP, 6 for TCP etc.) : ") |
| 129 | + port = input("Enter port range (leave blank for all by default): ") |
| 130 | + if port == '': |
| 131 | + port = 'all' |
| 132 | + break |
| 133 | + else: |
| 134 | + if i == 4: |
| 135 | + print("Entered wrong option 5 times. Exiting...") |
| 136 | + return |
| 137 | + print("Invalid choice!!! Choose from 1-5\n") |
| 138 | + cidr = input("Enter custom cidr (leave blank for 0.0.0.0/0 by default) : ") |
| 139 | + if cidr == '': |
| 140 | + cidr = '0.0.0.0/0' |
| 141 | + confirm = input("Confirm creation of inbound rule for Security Group : {} => for Protocol : {} \ |
| 142 | + Port : {} and CIDR : {} ? (Press Y to confirm) : ".format(sgname, protocol, port, cidr)) |
| 143 | + if confirm == 'y' or confirm == 'Y': |
| 144 | + errorcheck = call("aws ec2 authorize-security-group-ingress --group-name {} --protocol {} \ |
| 145 | + --port {} --cidr {} --profile {}".format(sgname, protocol, port, cidr, profile)) |
| 146 | + print("rc : {}".format(errorcheck)) |
| 147 | + return |
| 148 | + |
| 149 | + |
| 150 | +def ec2menu(prof): |
| 151 | + |
| 152 | + global profile, keyfile |
| 153 | + profile = prof |
| 154 | + |
| 155 | + print('\n') |
| 156 | + while True: |
| 157 | + print("\nEC2 MENU\ |
| 158 | + \n--------") |
| 159 | + print("\n1. Launch instance \t2. Show Key-Pairs \t3. Delete Key-Pair\ |
| 160 | + \n4. Show Security-Groups \t5. Delete Security-Group \t6. Add Inbound Rules\ |
| 161 | + \n7. Show Instances \t8. Start Instance \t9. Stop Instance\ |
| 162 | + \n10.Terminate Instance \t11.Create EBS volume \t12.Attach EBS Volume\ |
| 163 | + \n13.Show available storage \t14.Mount New Partition \t15.Show the filesystem\ |
| 164 | + \n\nPress Q to exit") |
| 165 | + |
| 166 | + choice = input("> ") |
| 167 | + if choice == '1': |
| 168 | + launchInstance() |
| 169 | + elif choice == '2': |
| 170 | + os.system( |
| 171 | + "aws ec2 describe-key-pairs --profile {}".format(profile)) |
| 172 | + elif choice == '3': |
| 173 | + keypair = input("\nEnter name of key-pair to delete : ") |
| 174 | + error = os.system( |
| 175 | + "aws ec2 delete-key-pair --key-name {} --profile {}".format(keypair, profile)) |
| 176 | + if error == 0: |
| 177 | + print("Key-Pair : {} deleted.".format(keypair)) |
| 178 | + elif choice == '4': |
| 179 | + os.system( |
| 180 | + "aws ec2 describe-security-groups --profile {}".format(profile)) |
| 181 | + elif choice == '5': |
| 182 | + sg = input("\nEnter name of security-group to delete : ") |
| 183 | + error = os.system( |
| 184 | + "aws ec2 delete-security-group --group-name {} --profile {}".format(sg, profile)) |
| 185 | + if error == 0: |
| 186 | + print("Security-Group : {} deleted.".format(sg)) |
| 187 | + elif choice == '6': |
| 188 | + inboundRules() |
| 189 | + elif choice == '7': |
| 190 | + os.system("aws ec2 describe-instances --profile {}".format(profile)) |
| 191 | + elif choice == '8': |
| 192 | + instance_id = input("\nEnter instance-id : ") |
| 193 | + error = call("aws ec2 start-instances --instance-ids {} \ |
| 194 | + --profile {}".format(instance_id, profile)) |
| 195 | + if error == 0: |
| 196 | + print("Instance started successfully.") |
| 197 | + print("rc : {}".format(error)) |
| 198 | + elif choice == '9': |
| 199 | + instance_id = input("\nEnter instance-id : ") |
| 200 | + error = call("aws ec2 stop-instances --instance-ids {} \ |
| 201 | + --profile {}".format(instance_id, profile)) |
| 202 | + if error == 0: |
| 203 | + print("Instance stopped successfully.") |
| 204 | + print("rc : {}".format(error)) |
| 205 | + elif choice == '10': |
| 206 | + instance_id = input("\nEnter instance-id : ") |
| 207 | + error = call("aws ec2 terminate-instances --instance-ids {} \ |
| 208 | + --profile {}".format(instance_id, profile)) |
| 209 | + if error == 0: |
| 210 | + print("Instance termination processed. Instance will be deleted in some time.") |
| 211 | + print("rc : {}".format(error)) |
| 212 | + elif choice == '11': |
| 213 | + size = input("Enter volume size : ") |
| 214 | + az = input("Enter availability zone (Default : ap-south-1a): ") |
| 215 | + if az == '': |
| 216 | + az = 'ap-south-1a' |
| 217 | + error = call("aws ec2 create-volume --availability-zone {} --size {} \ |
| 218 | + --profile {}".format(az, size, profile)) |
| 219 | + if error == 0: |
| 220 | + print("EBS Volume created successfully") |
| 221 | + print("rc : {}".format(error)) |
| 222 | + elif choice == '12': |
| 223 | + volume = input("Enter your volume-id : ") |
| 224 | + instance = input("Enter instance-id : ") |
| 225 | + error = call("aws ec2 attach-volume --volume-id {} --instance-id {} \ |
| 226 | + --device /dev/sdh --profile {}".format(volume, instance, profile)) |
| 227 | + if error == 0: |
| 228 | + print("EBS Volume attached successfully") |
| 229 | + print("rc : {}".format(error)) |
| 230 | + elif choice == '13': |
| 231 | + key = input("Enter key name with .pem extension: ") |
| 232 | + address = out("echo %USERPROFILE%").rstrip("\n") |
| 233 | + key = r"{}\KeyPairs\{}".format(address, key) |
| 234 | + instance = input("Enter instance-id : ") |
| 235 | + publicDns = out("aws ec2 describe-instances --instance-ids {}\ |
| 236 | + --query Reservations[*].Instances[*].[PublicDnsName] --output text".format(instance)).rstrip("\n") |
| 237 | + error = os.system('ssh -i {} ec2-user@{} sudo fdisk -l'.format(key, publicDns)) |
| 238 | + print("rc : {}".format(error)) |
| 239 | + elif choice == '14': |
| 240 | + key = input("Enter key name with .pem extension: ") |
| 241 | + address = out("echo %USERPROFILE%").rstrip("\n") |
| 242 | + key = r"{}\KeyPairs\{}".format(address, key) |
| 243 | + instance = input("\nEnter instance-id : ") |
| 244 | + publicDns = out("aws ec2 describe-instances --instance-ids {}\ |
| 245 | + --query Reservations[*].Instances[*].[PublicDnsName] --output text".format(instance)).rstrip("\n") |
| 246 | + name = input("Enter name of volume you wish to create partition in : ") |
| 247 | + error = os.system("ssh -i {} ec2-user@{} sudo fdisk {}".format(key, publicDns, name)) |
| 248 | + error2, error3 = 1,1 |
| 249 | + if error == 0: |
| 250 | + print("Partition {} created.".format(name)) |
| 251 | + error2 = os.system("ssh -i {} ec2-user@{} sudo mkfs.ext4 {}".format(key, publicDns, name)) |
| 252 | + if error2 == 0: |
| 253 | + print("Partition {} formatted.".format(name)) |
| 254 | + mount = input("Enter name for new mount point : ") |
| 255 | + error3 = os.system("ssh -i {} ec2-user@{} sudo mkdir {}".format(key, publicDns, mount)) |
| 256 | + error4 = os.system("ssh -i {} ec2-user@{} sudo mount {} {}".format(key, publicDns, name, mount)) |
| 257 | + if error3 == 0 and error4 == 0: |
| 258 | + print("Partition {} mounted on directory {}.".format(name, mount)) |
| 259 | + print("rc : {}".format(error+error2+error3+error4)) |
| 260 | + |
| 261 | + elif choice == '15': |
| 262 | + key = input("Enter key name with .pem extension: ") |
| 263 | + address = out("echo %USERPROFILE%").rstrip("\n") |
| 264 | + key = r"{}\KeyPairs\{}".format(address, key) |
| 265 | + instance = input("\nEnter instance-id : ") |
| 266 | + publicDns = out("aws ec2 describe-instances --instance-ids {}\ |
| 267 | + --query Reservations[*].Instances[*].[PublicDnsName] --output text".format(instance)).rstrip("\n") |
| 268 | + error = os.system("ssh -i {} ec2-user@{} sudo df -h".format(key, publicDns)) |
| 269 | + print("rc : {}".format(error)) |
| 270 | + |
| 271 | + elif choice == 'Q' or choice == 'q': |
| 272 | + print("Exiting...\n") |
| 273 | + break |
| 274 | + else: |
| 275 | + print("Invalid choice!!! Choose from 1-15 or Q to exit.\n") |
| 276 | + return |
| 277 | + |
| 278 | + |
| 279 | + |
0 commit comments