def deleteuser(user_choices): """ This function is used to delete user by giving/selecting parameters """ progressbar("Deleting user") username = user_choices['user'][0] try: iam.delete_user(UserName=str(username)) print("\n \n User " + username + " has been deleted \n \n") except botocore.exceptions.ClientError as e: coloredtext("There was an error while deleting user: \n\n\n") print(e)
def deletevpc(vpc_choices): """ This function is used to delete vpc by giving/selecting parameters """ progressbar("Deleting VPC") vpcname = vpc_choices['vpc'][0] try: ec2.delete_vpc(VpcId=str(vpcname)) print("\n \n vpc " + vpcname + " has been deleted \n \n") except botocore.exceptions.ClientError as e: coloredtext("There was an error while deleting vpc: \n\n\n") print(e)
def deletegroup(group_choices): """ This function is used to delete group by giving/selecting parameters """ progressbar("Deleting Group") groupname = group_choices['group'][0] try: iam.delete_group(GroupName=str(groupname)) print("\n \n Group " + groupname + " has been deleted \n \n") except botocore.exceptions.ClientError as e: coloredtext("There was an error while deleting group: \n\n\n") print(e)
def deleterole(role_choices): """ This function is used to delete IAM role by giving/selecting parameters """ progressbar("Deleting IAM role") rolenames = role_choices['role'] try: for rolename in rolenames: iam.delete_role(RoleName=str(rolename)) print("\n \n IAM role " + rolename + " has been deleted \n \n") except botocore.exceptions.ClientError as e: coloredtext("There was an error while deleting access key: \n\n\n") print(e)
def deletekeypair(keypair_choices): """ This function is used to delete keypair by giving/selecting parameters """ #print("deleting keypair") progressbar("Deleting Keypair") keypairname = keypair_choices['keypair'][0] try: ec2.delete_key_pair(KeyName=str(keypairname)) print("\n \n Keypair " + keypairname + " has been deleted \n \n") except botocore.exceptions.ClientError as e: coloredtext("There was an error while deleting keypair: \n\n\n") print(e)
def deleteaccesskey(accesskey_choices): """ This function is used to delete accesskey by giving/selecting parameters """ progressbar("Deleting Access Key") accesskeyname = accesskey_choices['accesskey'][0] try: iam.delete_access_key(AccessKeyId=str(accesskeyname)) print("\n \n Accesskey " + accesskeyname + " has been deleted \n \n") except botocore.exceptions.ClientError as e: coloredtext("There was an error while deleting access key: \n\n\n") print(e)
def deletesecuritygroup(securitygroup_choices): """ This function is used to delete security group by giving/selecting parameters """ progressbar("Deleting Security Group") securitygroupname = securitygroup_choices['securitygroup'][0] try: print("\n \n securitygroup " + securitygroupname + " has been deleted \n \n") ec2.delete_security_group(GroupId=str(securitygroupname)) except botocore.exceptions.ClientError as e: coloredtext("There was an error while deleting security group: \n\n\n") print(e)
def deleteobject(bucket_choices, object_choices): """ This function is used to delete the objects/s in S3 """ progressbar("Deleting Object") bucketname = bucket_choices['bucket'][0] objectnames = object_choices['object'] try: for objectname in objectnames: s3.delete_object(Bucket=str(bucketname), Key=str(objectname)) print("\n \n Object " + objectname + " has been deleted \n \n") except botocore.exceptions.ClientError as e: coloredtext("There was an error while deleting object: \n\n\n") print(e)
def deletebucket(bucket_choices): """ This function is used to delete the bucket/s in S3 """ progressbar("Deleting Bucket") bucketnames = bucket_choices['bucket'] try: for bucketname in bucketnames: s3.delete_bucket(Bucket=str(bucketname)) print("\n \n Bucket " + bucketname + " has been deleted \n \n") except botocore.exceptions.ClientError as e: coloredtext("There was an error while deleting Bucket: \n\n\n") print(e)
def terminateinstance(instance_choices): """ This function is used to terminate instances by giving/selecting parameters """ #print("Terminating Instance") progressbar("Terminating Instance") instancename = instance_choices['instance'][0] try: ec2.terminate_instances(InstanceIds=[ str(instancename), ]) print("\n \n Instance " + instancename + " has been terminated \n \n") except botocore.exceptions.ClientError as e: coloredtext("There was an error while terminating instance: \n\n\n") print(e)
def getbuckets(show): """ This function is used to get the list of all buckets in S3 """ bucketlist = [] try: buckets = s3.list_buckets() except botocore.exceptions.ClientError as e: coloredtext("There was an error while getting bucket data: \n\n\n") print(e) for i in buckets['Buckets']: bucket = i['Name'] if show: print("> " + bucket) bucketlist.append({'name': bucket}) #print(bucketlist) return bucketlist
def download_object(bucket_choices, object_choices): ### ERROR : Getting file not found error progressbar("Downloading Object") bucketname = bucket_choices['bucket'][0] objectnames = object_choices['object'] try: for objectname in objectnames: print(objectname) s3r.Bucket(str(bucketname)).download_file(str(objectname), objectname) #s3r.meta.client.download_file(str(bucketname), str(objectname), '~/') print("\n \n Object " + objectname + " has been downloaded \n \n") except botocore.exceptions.ClientError as e: coloredtext("There was an error while deleting object: \n\n\n") print(e)
def getroles(show): """ This function is used to get the list of all roles in IAM """ try: roles = iam.list_roles() except botocore.exceptions.ClientError as e: coloredtext("There was an error while getting user data: \n\n\n") print(e) roleslist = [] for role in roles['Roles']: name = role['RoleName'] if show: print("> " + name) roleslist.append({"name": name}) return roleslist
def getusers(show): """ This function is used to get the list of all users in IAM """ try: users = iam.list_users() except botocore.exceptions.ClientError as e: coloredtext("There was an error while getting user data: \n\n\n") print(e) userlist = [] for user in users['Users']: name = user['UserName'] if show: print("> " + name) userlist.append({"name": name}) return userlist
def getgroups(show): """ This function is used to get the list of all Groups in IAM """ try: groups = iam.list_groups() except botocore.exceptions.ClientError as e: coloredtext("There was an error while getting group data: \n\n\n") print(e) grouplist = [] for group in groups['Groups']: name = group['GroupName'] if show: print("> " + name) grouplist.append({"name": name}) return grouplist
def getkeypairs(show): """ This function is used to get the list of all the keypairs in EC2 in that region """ keypairlist = [] try: keypairs = ec2.describe_key_pairs() except botocore.exceptions.ClientError as e: coloredtext("There was an error while getting keypair data: \n\n\n") print(e) for keypair in keypairs['KeyPairs']: name = keypair['KeyName'] if show: print("name: " + name) keypairlist.append({"name": name}) return keypairlist
def getvpcs(show): """ This function is used to get the list of all the VPC's in that region """ vpclist = [] try: vpcs = ec2.describe_vpcs() except botocore.exceptions.ClientError as e: coloredtext("There was an error while getting vpc data: \n\n\n") print(e) for vpc in vpcs['Vpcs']: name = vpc['VpcId'] cidr = vpc['CidrBlock'] if show: print("VPC Id: " + name + " CIDR: " + cidr) vpclist.append({"name": name}) return vpclist
def getaccesskeys(show): """ This function is used to get the list of all accesskeys """ try: accesskeys = iam.list_access_keys() except botocore.exceptions.ClientError as e: coloredtext("There was an error while getting access key data: \n\n\n") print(e) accesskeylist = [] for accesskey in accesskeys['AccessKeyMetadata']: name = accesskey['UserName'] accesskeyid = accesskey['AccessKeyId'] if show: print("> " + name) accesskeylist.append({"name": accesskeyid}) return accesskeylist
def listobjects(bucket_choices): """ This function is used to list the objects in a bucket """ objectlist = [] bucketname = bucket_choices['bucket'][0] try: objects = s3.list_objects(Bucket=str(bucketname)) except botocore.exceptions.ClientError as e: coloredtext("There was an error while deleting Bucket: \n\n\n") print(e) print("\n\n These are the objects in the bucket: " + bucketname + "\n\n") for i in objects['Contents']: object = i['Key'] print("> " + object) objectlist.append({'name': object}) return objectlist
def vpcactions(action): # TODO : resolve issue here if action == 'Create VPC': cidrblock = input( "Insert the CIDR block for the vpc example, 10.0.0.0/16 : ") #path=input("Whre do you want to save the keypair? ") if getconfirmation(): try: ec2.create_vpc(CidrBlock=str(cidrblock)) except botocore.exceptions.ClientError as e: coloredtext("There was an error while creating VPC: \n\n\n") print(e) #key.save(str(path)) confirm_or_exit('VPC') if action == 'Delete VPC': vpc_choices = prompt(vpc_choice, style=custom_style_2) pprint(vpc_choices) if getconfirmation(): ec2class.deletevpc(vpc_choices) confirm_or_exit('VPC')
def getsecuritygroups(show): """ This function is used to get the list of all the Security Groups in that region """ securitygrouplist = [] try: securitygroups = ec2.describe_security_groups() except botocore.exceptions.ClientError as e: coloredtext( "There was an error while getting security group data: \n\n\n") print(e) for securitygroup in securitygroups['SecurityGroups']: name = securitygroup['GroupName'] gid = securitygroup['GroupId'] description = securitygroup['Description'] if show: print("name: " + name + " Descripton: " + description) securitygrouplist.append({"name": gid}) return securitygrouplist
def getinstances(show): """ This function is used to get the list of all the instances in EC2 in that region """ serverlist = [] count = 0 try: servers = ec2.describe_instances() except botocore.exceptions.ClientError as e: coloredtext( "There was an error while getting ec2 instance data: \n\n\n") print(e) for reservation in servers['Reservations']: for inst in reservation['Instances']: count += 1 name = inst['InstanceId'] state = inst['State']['Name'] serverid = "server" + str(count) if show: print("Id: " + name + " State: " + state) serverlist.append({"name": name}) return serverlist
def iamactions(action): if action == 'Create User': username = input("What is the name of the user you want to create: ") if getconfirmation(): try: print("Creating user") iam.create_user(UserName=str(username)) print("\n \n User " + username + " has been created \n \n") except botocore.exceptions.ClientError as e: coloredtext("There was an error while creating user: \n\n\n") print(e) confirm_or_exit('IAM') if action == 'Create Group': groupname = input("What is the name of the group you want to create: ") if getconfirmation(): try: print("Creating group") iam.create_group(GroupName=str(groupname)) print("\n \n GRoup " + groupname + " has been created \n \n") except botocore.exceptions.ClientError as e: coloredtext("There was an error while creating group: \n\n\n") print(e) confirm_or_exit('IAM') if action == 'Delete User': user_choices = prompt(user_choice, style=custom_style_2) pprint(user_choices) if getconfirmation(): iamclass.deleteuser(user_choices) confirm_or_exit('IAM') if action == 'Delete Group': print("Make sure that the Group is empty before you delete it") group_choices = prompt(group_choice, style=custom_style_2) pprint(group_choices) if getconfirmation(): iamclass.deletegroup(group_choices) confirm_or_exit('IAM') if action == 'Add User to Group': print("Select the user you want to add") user_choices = prompt(user_choice, style=custom_style_2) pprint(user_choices) userid = user_choices['user'][0] group_choices = prompt(group_choice, style=custom_style_2) pprint(group_choices) groupid = group_choices['group'][0] if getconfirmation(): try: iam.add_user_to_group(GroupName=str(groupid), UserName=str(userid)) except botocore.exceptions.ClientError as e: coloredtext( "There was an error while adding user to group: \n\n\n") print(e) confirm_or_exit('IAM') if action == 'List Access Keys': iamclass.getaccesskeys(True) confirm_or_exit('IAM') if action == 'Create Access Key': print("Select the user you want to create accesskey for") user_choices = prompt(user_choice, style=custom_style_2) pprint(user_choices) userid = user_choices['user'][0] if getconfirmation(): try: iam.create_access_key(UserName=str(userid)) except botocore.exceptions.ClientError as e: coloredtext( "There was an error while creating access key: \n\n\n") print(e) confirm_or_exit('IAM') if action == 'Delete Access Key': accesskey_choices = prompt(accesskey_choice, style=custom_style_2) pprint(accesskey_choices) if getconfirmation(): iamclass.deleteaccesskey(accesskey_choices) confirm_or_exit('IAM') if action == 'List Roles': iamclass.getroles(True) confirm_or_exit('IAM') if action == 'Delete Roles': role_choices = prompt(role_choice, style=custom_style_2) pprint(role_choices) if getconfirmation(): iamclass.deleterole(role_choices)
def ec2actions(action): #####################INSTANCES################ if action == 'Run Instances': os = input("What is the OS? ") count = input("How many servers you want to run? ") instance_type = input("Which Instance type you want to run") keyname = input("Which key pair you want to use") if getconfirmation(): try: ec2.run_instances(ImageId=str(os), InstanceType=str(instance_type), MaxCount=int(count), MinCount=int(count), KeyName=str(keyname)) print("Running instances now") except botocore.exceptions.ClientError as e: coloredtext("There was an error while run instance: \n\n\n") print(e) confirm_or_exit('EC2') if action == 'Start Instances': instance_choices = prompt(instance_choice, style=custom_style_2) pprint(instance_choices) if getconfirmation(): ec2class.startinstance(instance_choices) confirm_or_exit('EC2') if action == 'Stop Instances': instance_choices = prompt(instance_choice, style=custom_style_2) pprint(instance_choices) if getconfirmation(): ec2class.stopinstance(instance_choices) confirm_or_exit('EC2') if action == 'Terminate Instances': instance_choices = prompt(instance_choice, style=custom_style_2) pprint(instance_choices) if getconfirmation(): ec2class.terminateinstance(instance_choices) confirm_or_exit('EC2') #########################SECURITY GROUP ################################ if action == 'Create Security Groups': groupname = input("What is the name you want to give to the group? ") #vpcid=input("Select the vpc for the Security group") ##currenty manually entering will add vpc selection later description = input("Give a short description for the group: ") vpc_choices = prompt(vpc_choice, style=custom_style_2) pprint(vpc_choices) vpcid = vpc_choices['vpc'][0] if getconfirmation(): try: ec2.create_security_group(Description=str(description), GroupName=str(groupname), VpcId=str(vpcid)) except botocore.exceptions.ClientError as e: coloredtext( "There was an error while creating security group: \n\n\n") print(e) confirm_or_exit('EC2') if action == 'List Security Groups': ec2class.getsecuritygroups(True) confirm_or_exit('EC2') if action == 'Delete Security Groups': securitygroup_choices = prompt(securitygroup_choice, style=custom_style_2) pprint(securitygroup_choices) if getconfirmation(): ec2class.deletesecuritygroup(securitygroup_choices) confirm_or_exit('EC2') #########################KEYPAIRS ################################ if action == 'Create Keypair': keyname = input("What is the name of the keypair you want to create? ") #path=input("Whre do you want to save the keypair? ") if getconfirmation(): try: key = ec2.create_key_pair(KeyName=str(keyname)) print("\n \n Keypair " + keyname + " has been created \n \n") except botocore.exceptions.ClientError as e: coloredtext( "There was an error while creating keypair: \n\n\n") print(e) confirm_or_exit('EC2') if action == 'List Keypairs': ec2class.getkeypairs(True) confirm_or_exit('EC2') #options.extend(['Create Keypairs','Delete Keypairs','Exit']) if action == 'Delete Keypair': keypair_choices = prompt(keypair_choice, style=custom_style_2) pprint(keypair_choices) if getconfirmation(): ec2class.deletekeypair(keypair_choices) confirm_or_exit('EC2')
def s3actions(action): # TODO : resolve Location constraint error (ap-south working) if action == 'Create Bucket': bucket_name = input( "What is the name of the bucket you want to create ( Use comma if you want to create multiple buckets): " ) ###Need to add this functionality later (from mobile app script) region_choices = prompt(region_choice, style=custom_style_2) pprint(region_choices) region = region_choices['region'] if getconfirmation(): progressbar("Creating Bucket") try: s3.create_bucket(Bucket=str(bucket_name), CreateBucketConfiguration={ 'LocationConstraint': str(region) }) print("\n \n Bucket " + bucket_name + " has been created \n \n") except botocore.exceptions.ClientError as e: coloredtext("There was an error while creating Bucket: \n\n\n") print(e) confirm_or_exit('S3') if action == 'Delete Bucket': bucket_choices = prompt(bucket_choice, style=custom_style_2) pprint(bucket_choices) if getconfirmation(): s3class.deletebucket(bucket_choices) confirm_or_exit('S3') if action == 'List Bucket Objects': bucket_choices = prompt(bucket_choice, style=custom_style_2) pprint(bucket_choices) s3class.listobjects(bucket_choices) s3objectquestions = [{ 'type': 'list', 'name': 'objectaction', 'message': 'Which action do you want to make ?', 'choices': [ 'Upload object to bucket', 'Delete Object from Bucket', 'Download object from Bucket', 'Go Back' ] }] def delete_object( bucket_choices): # inner function Hidden from outer code print(bucket_choices['bucket'][0]) object_list = s3class.listobjects(bucket_choices) object_choice = [{ 'type': 'checkbox', 'qmark': '😃', 'message': 'Select objects', 'name': 'object', #'choices': ['test1','test2'], 'choices': object_list }] object_choices = prompt(object_choice, style=custom_style_2) pprint(object_choices) s3class.deleteobject(bucket_choices, object_choices) confirm_or_exit('S3') def download_object( bucket_choices): # inner function Hidden from outer code print(bucket_choices['bucket'][0]) object_list = s3class.listobjects(bucket_choices) object_choice = [{ 'type': 'checkbox', 'qmark': '😃', 'message': 'Select objects', 'name': 'object', #'choices': ['test1','test2'], 'choices': object_list }] object_choices = prompt(object_choice, style=custom_style_2) pprint(object_choices) s3class.download_object(bucket_choices, object_choices) confirm_or_exit('S3') s3objectprompt = prompt(s3objectquestions, style=custom_style_2) s3objectaction = s3objectprompt['objectaction'] if s3objectaction == 'Go Back': main() elif s3objectaction == 'Delete Object from Bucket': delete_object(bucket_choices) elif s3objectaction == 'Download object from Bucket': download_object(bucket_choices) if action == 'Delete Object from Bucket': print("test")