def setup_aws_account(): prep_paths(env.ssh_directory, env.deploy_directory) ec2 = connect_to_ec2() # Check to see if specified keypair already exists. # If we get an InvalidKeyPair.NotFound error back from EC2, # it means that it doesn't exist and we need to create it. try: key_name = env.aws_ssh_key_name key = ec2.get_all_key_pairs(keynames=[key_name])[0] print "key name {} already exists".format(key_name) except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print 'Creating keypair: %s' % env.aws_ssh_key_name # Create an SSH key to use when logging into instances. key = ec2.create_key_pair(env.aws_ssh_key_name) # AWS will store the public key but the private key is # generated and returned and needs to be stored locally. # The save method will also chmod the file to protect # your private key. key.save(directory_path=env.ssh_directory) else: raise
def setup_aws_account(): ec2 = connect_to_ec2() # Check to see if specified keypair already exists. # If we get an InvalidKeyPair.NotFound error back from EC2, # it means that it doesn't exist and we need to create it. try: key_name = aws_cfg["key_name"] key = ec2.get_all_key_pairs(keynames=[key_name])[0] print "key name {} already exists".format(key_name) except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print 'Creating keypair: %s' % aws_cfg["key_name"] # Create an SSH key to use when logging into instances. key = ec2.create_key_pair(aws_cfg["key_name"]) # Make sure the specified key_dir actually exists. # If not, create it. key_dir = aws_cfg["key_dir"] key_dir = os.path.expanduser(key_dir) key_dir = os.path.expandvars(key_dir) if not os.path.isdir(key_dir): os.mkdir(key_dir, 0700) # AWS will store the public key but the private key is # generated and returned and needs to be stored locally. # The save method will also chmod the file to protect # your private key. key.save(key_dir) else: raise
def sync_keypairs(keypair_name, public_key_file): """ Synchronize SSH keypairs across all EC2 regions. keypair_name The name of the keypair. public_key_file The path to the file containing the public key portion of the keypair. """ fp = open(public_key_file) material = fp.read() fp.close() for region in boto.ec2.regions(): ec2 = region.connect() # Try to list the keypair. If it doesn't exist # in this region, then import it. try: key = ec2.get_all_key_pairs(keynames=[keypair_name])[0] print 'Keypair(%s) already exists in %s' % (keypair_name, region.name) except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print 'Importing keypair(%s) to %s' % (keypair_name, region.name) ec2.import_key_pair(keypair_name, material)
def setup_aws_account(): ec2 = connect_to_ec2() print ec2 # Check to see if specified keypair already exists. # If we get an InvalidKeyPair.NotFound error back from EC2, # it means that it doesn't exist and we need to create it. try: key_name = aws_cfg["key_name"] key = ec2.get_all_key_pairs(keynames=[key_name])[0] print "key name {} already exists".format(key_name) except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print 'Creating keypair: %s' % aws_cfg["key_name"] # Create an SSH key to use when logging into instances. key = ec2.create_key_pair(aws_cfg["key_name"]) # Make sure the specified key_dir actually exists. # If not, create it. key_dir = aws_cfg["key_dir"] key_dir = os.path.expanduser(key_dir) key_dir = os.path.expandvars(key_dir) if not os.path.isdir(key_dir): os.mkdir(key_dir, 0700) # AWS will store the public key but the private key is # generated and returned and needs to be stored locally. # The save method will also chmod the file to protect # your private key. key.save(key_dir) else: raise
def launch_instance(connect=True): """Launch an instance and wait for it to start running. Returns a tuple consisting of the Instance object and the CmdShell object, if request, or None. -connect tells to perform the SSH connection test to the newly created instance (up to 1 min of time) """ config = Config() # Create a connection to EC2 service (assuming credentials are in boto config) ec2 = boto.ec2.connect_to_region(config.get("region")) # Check to see if specified key pair already exists. # If we get an InvalidKeyPair.NotFound error back from EC2, # it means that it doesn't exist and we need to create it. key_name = config.get("key_name") logger = logging.getLogger(__name__) try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] except ec2.ResponseError, e: if e.code == "InvalidKeyPair.NotFound": output = "Creating key pair: %s" % key_name print output logger.info(output) # Create an SSH key to use when logging into instances. key = ec2.create_key_pair(key_name) # AWS will store the public key but the private key is # generated and returned and needs to be stored locally. # The save method will also chmod the file to protect # your private key. key.save(static.KEY_DIR) else: raise
def ec2_delete_keys(ec2): key = ec2.get_all_key_pairs() for k in key: try: print "---Deleting key: " + k.name ec2.delete_key_pair(k.name) except Exception, e: print(e)
def launch_instance(ami='ami-c6402cf6', instance_type='t1.micro', key_name='miguel', key_extension='.pem', key_dir='/home/skalas/.ssh/', group_name='Postgresql', ssh_port=22, cidr='0.0.0.0/0', tag='cruz', user_data='', cmd_shell=True, login_user='******', ssh_passwd=None): # Launch an instance and wait for it to start running. # Returns a tuple consisting of the Instance object and the CmdShell object, if request, or None. # ami The ID of the Amazon Machine Image that this instance will be based on. Default is a 64-bit Amazon Linux EBS image. # instance_type The type of the instance. # key_name The name of the SSH Key used for logging into the instance. It will be created if it does not exist. # key_extension The file extension for SSH private key files. # key_dir group_name # ssh_port cidr # tag # user_data # cmd_shell # login_user # ssh_passwd The password for your SSH key if it is encrypted with a passphrase. cmd = None # Create a connection to EC2 service. # You can pass credentials in to the connect_ec2 method explicitly # or you can use the default credentials in your ~/.boto config file # as we are doing here. # hotmail #gmail ec2 = boto.ec2.connect_to_region("us-west-2", aws_access_key_id='', aws_secret_access_key='') # Check to see if specified keypair already exists. # If we get an InvalidKeyPair.NotFound error back from EC2, # it means that it doesn't exist and we need to create it. try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print 'Creating keypair: %s' % key_name # Create an SSH key to use when logging into instances. key = ec2.create_key_pair(key_name) # AWS will store the public key but the private key is # generated and returned and needs to be stored locally. # The save method will also chmod the file to protect # your private key. key.save(key_dir) else: raise
def launch_instance(ami='ami-c6402cf6', instance_type='t1.micro', key_name='miguel', key_extension='.pem', key_dir='/home/skalas/.ssh/', group_name='Postgresql', ssh_port=22, cidr='0.0.0.0/0', tag='cruz', user_data='', cmd_shell=True, login_user='******', ssh_passwd=None): # Launch an instance and wait for it to start running. # Returns a tuple consisting of the Instance object and the CmdShell object, if request, or None. # ami The ID of the Amazon Machine Image that this instance will be based on. Default is a 64-bit Amazon Linux EBS image. # instance_type The type of the instance. # key_name The name of the SSH Key used for logging into the instance. It will be created if it does not exist. # key_extension The file extension for SSH private key files. # key_dir group_name # ssh_port cidr # tag # user_data # cmd_shell # login_user # ssh_passwd The password for your SSH key if it is encrypted with a passphrase. cmd = None # Create a connection to EC2 service. # You can pass credentials in to the connect_ec2 method explicitly # or you can use the default credentials in your ~/.boto config file # as we are doing here. # hotmail #gmail ec2 =boto.ec2.connect_to_region("us-west-2", aws_access_key_id='', aws_secret_access_key='') # Check to see if specified keypair already exists. # If we get an InvalidKeyPair.NotFound error back from EC2, # it means that it doesn't exist and we need to create it. try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print 'Creating keypair: %s' % key_name # Create an SSH key to use when logging into instances. key = ec2.create_key_pair(key_name) # AWS will store the public key but the private key is # generated and returned and needs to be stored locally. # The save method will also chmod the file to protect # your private key. key.save(key_dir) else: raise
def start_instance(): #Establish connection to aws ec2 = boto.connect_ec2(aws_access_key_id,aws_secret_access_key) ec2 = boto.ec2.connect_to_region(region) try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print e.code else: raise
def get_or_create_key_pair(key_name=None, key_dir='~/.ssh', region='us-west-2'): try: ec2 = boto.ec2.connect_to_region(region) key = ec2.get_all_key_pairs(keynames=[key_name]) print key except ec2.ResponseError, e: print e if e.code == 'InvalidKeyPair.NotFound': # Create an SSH key to use when logging into instances. key = ec2.create_key_pair(key_name) if not key.save(key_dir): print('Key could not be created\n') raise else: raise
def startEC2Instance(self, ami="ami-2727f84e", key_name="automation-key3", instance_type="t1.micro", group_name = "open5", key_extension=".pem", key_dir="~/.ssh", ssh_port=22, solr_port=8983, http_port=80, tag="instance test", cidr = "0.0.0.0/0", user_data="None", login_usr="******"): ec2 = boto.connect_ec2() try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print 'Creating keypair: %s' % key_name key = ec2.create_key_pair(key_name) key.save(key_dir)
def checkKeyPair(self, keypair_name, public_key_file): fp = open(public_key_file) material = fp.read() fp.close() for region in boto.ec2.regions(): ec2 = region.connect() try: key = ec2.get_all_key_pairs(keynames=[keypair_name])[0] print 'Keypair(%s) already exists in %s' % \ (keypair_name, region.name) except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print 'Importing keypair(%s) to %s' % \ (keypair_name, region.name) ec2.import_key_pair(keypair_name, material)
def generate_key(key_name=keyname, key_dir=key_dir, ssh_passwd=None): ec2 = conn_region try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': # Create an SSH key key = ec2.create_key_pair(key_name) #Save key key.save(key_dir) print "Success" else: raise
def generate_key(key_name=keyname, key_extension=".pem", key_dir=key_dir, ssh_passwd=None): ec2 = conn_region try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] except ec2.ResponseError, e: if e.code == "InvalidKeyPair.NotFound": # Create an SSH key key = ec2.create_key_pair(key_name) # Save key key.save(key_dir) print "Success" else: raise
def ec2run_completers(self, event): cmd_param = event.line.split() if event.line.endswith(' '): cmd_param.append('') arg = cmd_param.pop() arg = cmd_param.pop() if arg in ('-t', '--instance-type'): return [ 'm1.small', 'm1.large', 'm1.xlarge', 'c1.medium', 'c1.xlarge', 'm2.xlarge', 'm2.2xlarge', 'm2.4xlarge', 'cc1.4xlarge', 't1.micro' ] elif arg in ('-k', '--keys'): return [k.name for k in ec2.get_all_key_pairs()] elif arg in ('-n', '--instance-count'): return ['1', '1-'] # just examples really elif arg in ('-g', '--group'): return [g.name for g in ec2.get_all_security_groups()] elif arg in ('-d', '--user-data'): return [] elif arg in ('-f', '--user-data-file'): return [] # TODO hook normal file complete elif arg in ('-z', '--availability-zone'): return [z.name for z in ec2.get_all_zones()] elif arg in ('--instance-initiated-shutdown-behavior'): return ['stop', 'terminate'] elif arg in ('--placement-group'): return [g.name for g in ec2.get_all_placement_groups()] elif arg in ('--private-ip-address'): return [] elif arg in ('--kernel'): return [] # TODO elif arg in ('--ramdisk'): return [] # TODO elif arg in ('--subnet'): return [] # TODO else: params = ec2run_parameters[:] # drop from params any already used for c in cmd_param: o = ec2run_parser.get_option(c) if o: for v in o._short_opts + o._long_opts: if v in params: params.remove(v) return params + ami.keys()
def launch_instance(ami='ami-7341831a', instance_type='t1.micro', count=1, spot=False, key_name='paws', key_extension='.pem', key_dir='~/.ssh', group_name='paws', ssh_port=22, cidr='0.0.0.0/0', tags={"owner":"Mike Grundy"}, user_data=None, cmd_shell=True, login_user='******', ssh_passwd=None, getPass=False, bdm=None): cmd = None create_group = False # Create a connection to EC2 service. #ec2 = boto.connect_ec2() ec2 = boto.vpc.connect_to_region("us-east-1") # Check to see if specified keypair already exists. # If we get an InvalidKeyPair.NotFound error back from EC2, # it means that it doesn't exist and we need to create it. try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] print key except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print 'Creating keypair: %s' % key_name # Create an SSH key to use when logging into instances. key = ec2.create_key_pair(key_name) # AWS will store the public key but the private key is # generated and returned and needs to be stored locally. # The save method will also chmod the file to protect # your private key. key.save(key_dir) else: raise
def generate_key(key_name=keyname, key_dir=key_dir, ssh_passwd=None): ec2 = conn_region try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] except ec2.ResponseError as e: if e.code == 'InvalidKeyPair.NotFound': # Create an SSH key key = ec2.create_key_pair(key_name) # Save key save_key_path = "%s/%s.pem" % (key_dir, key_name) with open(save_key_path, "w") as file: file.write(key.material) print("Success") else: raise
def ec2run_completers(self, event): cmd_param = event.line.split() if event.line.endswith(' '): cmd_param.append('') arg = cmd_param.pop() arg = cmd_param.pop() if arg in ('-t', '--instance-type'): return ['m1.small', 'm1.large', 'm1.xlarge', 'c1.medium', 'c1.xlarge', 'm2.xlarge', 'm2.2xlarge', 'm2.4xlarge', 'cc1.4xlarge', 't1.micro'] elif arg in ('-k', '--keys'): return [k.name for k in ec2.get_all_key_pairs()] elif arg in ('-n', '--instance-count'): return ['1', '1-'] # just examples really elif arg in ('-g', '--group'): return [g.name for g in ec2.get_all_security_groups()] elif arg in ('-d', '--user-data'): return [] elif arg in ('-f', '--user-data-file'): return [] # TODO hook normal file complete elif arg in ('-z', '--availability-zone'): return [z.name for z in ec2.get_all_zones()] elif arg in ('--instance-initiated-shutdown-behavior'): return ['stop', 'terminate'] elif arg in ('--placement-group'): return [g.name for g in ec2.get_all_placement_groups()] elif arg in ('--private-ip-address'): return [] elif arg in ('--kernel'): return [] # TODO elif arg in ('--ramdisk'): return [] # TODO elif arg in ('--subnet'): return [] # TODO else: params = ec2run_parameters[:] # drop from params any already used for c in cmd_param: o = ec2run_parser.get_option(c) if o: for v in o._short_opts + o._long_opts: if v in params: params.remove(v) return params + ami.keys()
def createAllKP(): """ Create all key pairs in all regions """ if not os.path.exists(keysDir): os.makedirs(keysDir) for info in conf_HVM: keyName = 'Key-' + info['region'] + '-' + info['zone'] try: os.remove(keysDir + '/' + keyName + '.pem') except OSError: pass print "Key creation :", keyName ec2 = boto.ec2.connect_to_region(info['region'] + '-' + info['zone']) # check if the key pair exists kps = [kp for kp in ec2.get_all_key_pairs() if kp.name == keyName] if kps: ec2.delete_key_pair(keyName) key = ec2.create_key_pair(keyName) key.save(keysDir)
def createAllKP(): """ Create all key pairs in all regions """ if not os.path.exists(keysDir): os.makedirs(keysDir) for info in conf_HVM: keyName = 'Key-'+info['region']+'-'+info['zone'] try: os.remove(keysDir+'/'+keyName+'.pem') except OSError: pass print "Key creation :",keyName ec2 = boto.ec2.connect_to_region(info['region']+'-'+info['zone']) # check if the key pair exists kps = [kp for kp in ec2.get_all_key_pairs() if kp.name == keyName] if kps: ec2.delete_key_pair(keyName) key = ec2.create_key_pair(keyName) key.save(keysDir)
def list(region=None): if region == None: regions = map(lambda r: r.name, boto.ec2.regions()) for region in regions: Instance.list(region) return try: ec2 = boto.ec2.connect_to_region(region) cf = boto.cloudformation.connect_to_region(region) keys = map(lambda key: key.name, ec2.get_all_key_pairs()) stacks = map(lambda stack: stack.stack_name, cf.describe_stacks()) instances = set() instances.update(keys) instances.update(stacks) print "Region: %s" % region for instance in instances: print "\t%s" % instance except: print "Could not connect to %s" % region
def launch_instance(ami='ami-a8e87591', instance_type='t2.micro', key_name='pro', key_extension='.pem', key_dir='~/.ssh', group_name='bicher', ssh_port=22, cidr='0.0.0.0/0', tag='pow', user_data=None): ec2 = boto.ec2.connect_to_region('cn-north-1') try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print 'Creating keypair: %s' % key_name key = ec2.create_key_pair(key_name) key.save(key_dir) else: raise
def launch_instance(ami='ami-7341831a', instance_type='t1.micro', count=1, spot=False, key_name='paws', key_extension='.pem', key_dir='~/.ssh', group_name='paws', ssh_port=22, cidr='0.0.0.0/0', tags={"requestor": "Mike Grundy"}, user_data=None, cmd_shell=True, login_user='******', ssh_passwd=None, bdm=None): cmd = None # Create a connection to EC2 service. ec2 = boto.connect_ec2() # Check to see if specified keypair already exists. # If we get an InvalidKeyPair.NotFound error back from EC2, # it means that it doesn't exist and we need to create it. try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print 'Creating keypair: %s' % key_name # Create an SSH key to use when logging into instances. key = ec2.create_key_pair(key_name) # AWS will store the public key but the private key is # generated and returned and needs to be stored locally. # The save method will also chmod the file to protect # your private key. key.save(key_dir) else: raise
def create_key_pair(ec2, key_name, key_dir, key_extension='.pem'): # Create an SSH key to use when logging into instances. # Check to see if specified keypair already exists. # If we get an InvalidKeyPair.NotFound error back from EC2, # it means that it doesn't exist and we need to create it. print 'Create key_pair %s at %s'%(key_name, key_dir) try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] print 'Keypair found. Not creating.' except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print 'Creating keypair: %s' % key_name key = ec2.create_key_pair(key_name) # AWS will store the public key but the private key is # generated and returned and needs to be stored locally. # The save method will also chmod the file to protect # your private key. key.save(key_dir) else: raise
def launch_instance( ami="ami-a8e87591", instance_type="t2.micro", key_name="pro", key_extension=".pem", key_dir="~/.ssh", group_name="bicher", ssh_port=22, cidr="0.0.0.0/0", tag="pow", user_data=None, ): ec2 = boto.ec2.connect_to_region("cn-north-1") try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] except ec2.ResponseError, e: if e.code == "InvalidKeyPair.NotFound": print "Creating keypair: %s" % key_name key = ec2.create_key_pair(key_name) key.save(key_dir) else: raise
def validate_provider_data(self, serializer_attrs, all_data): attrs = super(AWSCloudProvider, self).validate_provider_data(serializer_attrs, all_data) region = attrs[self.REGION].slug access_key = all_data[self.ACCESS_KEY] secret_key = all_data[self.SECRET_KEY] keypair = all_data[self.KEYPAIR] errors = {} from stackdio.api.cloud.models import CloudAccount # Check for duplicates accounts = CloudAccount.objects.filter(provider__name=self.SHORT_NAME) for account in accounts: account_yaml = yaml.safe_load(account.yaml) if account.region.slug == region and account_yaml[account.slug]['id'] == access_key: err_msg = ('You may not have multiple cloud accounts with the same access key ' 'in the same region. Please generate a new access key if you would ' 'like to have 2 cloud accounts in the same AWS account.') errors.setdefault(self.REGION, []).append(err_msg) if errors: raise ValidationError(errors) # check authentication credentials ec2 = None try: ec2 = boto.ec2.connect_to_region( region, aws_access_key_id=access_key, aws_secret_access_key=secret_key, ) ec2.get_all_zones() except boto.exception.EC2ResponseError: err_msg = 'Unable to authenticate to AWS with the provided keys.' errors.setdefault(self.ACCESS_KEY, []).append(err_msg) errors.setdefault(self.SECRET_KEY, []).append(err_msg) if errors: raise ValidationError(errors) # check keypair try: ec2.get_all_key_pairs(keypair) except boto.exception.EC2ResponseError: errors.setdefault(self.KEYPAIR, []).append( 'The keypair \'{0}\' does not exist in this account.'.format(keypair) ) # check route 53 domain domain = all_data[self.ROUTE53_DOMAIN] if domain: try: # connect to route53 and check that the domain is available r53 = boto.connect_route53(access_key, secret_key) found_domain = False hosted_zones = r53.get_all_hosted_zones() hosted_zones = hosted_zones['ListHostedZonesResponse']['HostedZones'] for hosted_zone in hosted_zones: if hosted_zone['Name'].startswith(domain): found_domain = True break if not found_domain: err = 'The Route53 domain \'{0}\' does not exist in ' \ 'this account.'.format(domain) errors.setdefault(self.ROUTE53_DOMAIN, []).append(err) # except boto.exception.DNSServerError as e: except Exception as e: logger.exception('Route53 issue?') errors.setdefault(self.ROUTE53_DOMAIN, []).append(str(e)) # check VPC required fields vpc_id = attrs[self.VPC_ID] if vpc_id: vpc = None try: vpc = boto.vpc.connect_to_region( region, aws_access_key_id=access_key, aws_secret_access_key=secret_key, ) except boto.exception.EC2ResponseError: err_msg = ('Unable to authenticate to AWS VPC with the ' 'provided keys.') errors.setdefault(self.ACCESS_KEY, []).append(err_msg) errors.setdefault(self.SECRET_KEY, []).append(err_msg) if not errors: try: vpc.get_all_vpcs([vpc_id]) except boto.exception.EC2ResponseError: errors.setdefault(self.VPC_ID, []).append( 'The VPC \'{0}\' does not exist in this account.' .format(vpc_id) ) if errors: raise ValidationError(errors) return attrs
def launch_instance( ec2_region=defaults.EC2_REGION, ami=defaults.AMI_BITNAMI_DJANGOSTACK_UBUNTU32, instance_type=defaults.MICRO_INSTANCE, key_name=defaults.KEY_NAME, key_extension=defaults.KEY_EXTENSION, key_dir=defaults.KEY_DIR, group_name="myucsc", ssh_port=22, cidr="0.0.0.0/0", tag=defaults.INSTANCE_TAG, user_data=None, cmd_shell=False, login_user=defaults.EC2_USER_BITNAMI, # default user on BitNami AMIs, ec2-user is the default on Ubuntu AMIs ssh_passwd=None, ): """ Launch an instance and wait for it to start running. Returns a tuple consisting of the Instance object and the CmdShell object, if request, or None. ami The ID of the Amazon Machine Image that this instance will be based on. Default is a 64-bit Amazon Linux EBS image. instance_type The type of the instance. key_name The name of the SSH Key used for logging into the instance. It will be created if it does not exist. key_extension The file extension for SSH private key files. key_dir The path to the directory containing SSH private keys. This is usually ~/.ssh. group_name The name of the security group used to control access to the instance. It will be created if it does not exist. ssh_port The port number you want to use for SSH access (default 22). cidr The CIDR block used to limit access to your instance. tag A name that will be used to tag the instance so we can easily find it later. user_data Data that will be passed to the newly started instance at launch and will be accessible via the metadata service running at http://169.254.169.254. cmd_shell If true, a boto CmdShell object will be created and returned. This allows programmatic SSH access to the new instance. login_user The user name used when SSH'ing into new instance. The default is 'ec2-user' ssh_passwd The password for your SSH key if it is encrypted with a passphrase. """ cmd = None # Create a connection to EC2 service. # You can pass credentials in to the connect_ec2 method explicitly # or you can use the default credentials in your ~/.boto config file # as we are doing here. # ec2 = boto.connect_ec2() ec2 = boto.ec2.connect_to_region(ec2_region) # Check to see if specified keypair already exists. # If we get an InvalidKeyPair.NotFound error back from EC2, # it means that it doesn't exist and we need to create it. try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] except ec2.ResponseError, e: if e.code == "InvalidKeyPair.NotFound": print "Creating keypair: %s" % key_name # Create an SSH key to use when logging into instances. key = ec2.create_key_pair(key_name) # AWS will store the public key but the private key is # generated and returned and needs to be stored locally. # The save method will also chmod the file to protect # your private key. key.save(key_dir) else: raise
print args.keypair_name print args.public_key_file """ Synchronize SSH keypairs across all EC2 regions. keypair_name The name of the keypair. public_key_file The path to the file containing the public key portion of the keypair. """ fp = open(args.public_key_file) material = fp.read() fp.close() for region in boto.ec2.regions(): print 'Importing key to %s region' %region.name ec2 = region.connect() # Try to list the keypair. If it doesn't exist # in this region, then import it. try: key = ec2.get_all_key_pairs(keynames=[args.keypair_name])[0] print 'Keypair(%s) already exists in %s' % (args.keypair_name, region.name) except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print 'Importing keypair(%s) to %s' % (args.keypair_name, region.name) ec2.import_key_pair(args.keypair_name, material)
""" cmd = None # Create a connection to EC2 service. # You can pass credentials in to the connect_ec2 method explicitly # or you can use the default credentials in your ~/.boto config file # as we are doing here. ec2 = boto.ec2.connect_to_region('us-west-1') print ec2 # Check to see if specified keypair already exists. # If we get an InvalidKeyPair.NotFound error back from EC2, # it means that it doesn't exist and we need to create it. try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print 'Invalid Key Pair Specified' % key_name else: raise # Check to see if specified security group already exists. # If we get an InvalidGroup.NotFound error back from EC2, # it means that it doesn't exist and we need to create it. try: group = ec2.get_all_security_groups(groupnames=[group_name])[0] except ec2.ResponseError, e: if e.code == 'InvalidGroup.NotFound': print 'Invalid Security Group specified' % group_name else:
region="eu-west-1" key=raw_input("Enter your aws_access_key_id:") secret=raw_input("Enter your aws_secret_access_key:") ec2=boto.ec2.connect_to_region(region_name=region,aws_access_key_id=key,aws_secret_access_key=secret) live_keys=set() ## To avoid duplicates as many instance may have a same have same key pair unused_keys=list() reservations=ec2.get_all_instances() for res in reservations: for s in res.instances: live_keys.add(s.key_name) key_pairs=ec2.get_all_key_pairs() key_pairs_present=[ str(key_pairs[n]).replace("KeyPair:","") for n in range(len(key_pairs))] unused_keys=[key for key in key_pairs_present if key not in live_keys] print unused_keys # Below will remove all the unused key pairs. """ for k in unused_keys: ec2.delete_key_pair(k) """
def check_key_pair_exists(name): key_pairs = ec2.get_all_key_pairs() for key in key_pairs: if (key.name == name): return True return False
cmd = None # Create a connection to EC2 service. # You can pass credentials in to the connect_ec2 method explicitly # or you can use the default credentials in your ~/.boto config file # as we are doing here. ec2 = boto.ec2.connect_to_region('us-west-1') print ec2 # Check to see if specified keypair already exists. # If we get an InvalidKeyPair.NotFound error back from EC2, # it means that it doesn't exist and we need to create it. try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print 'Invalid Key Pair Specified' % key_name else: raise # Check to see if specified security group already exists. # If we get an InvalidGroup.NotFound error back from EC2, # it means that it doesn't exist and we need to create it. try: group = ec2.get_all_security_groups(groupnames=[group_name])[0] except ec2.ResponseError, e: if e.code == 'InvalidGroup.NotFound': print 'Invalid Security Group specified' % group_name else:
def create_ec2_instance(self): # connect to ec2 self.log.info("Connecting to ec2 ...") ec2 = boto.ec2.connect_to_region( self.config.get(self.section, 'region'), aws_access_key_id=self.config.get(self.default, 'aws_access_key_id'), aws_secret_access_key=self.config.get(self.default, 'aws_secret_access_key') ) vpc_conn = boto.vpc.connect_to_region( self.config.get(self.section, 'region'), aws_access_key_id=self.config.get(self.default, 'aws_access_key_id'), aws_secret_access_key=self.config.get(self.default, 'aws_secret_access_key') ) self.log.info("Ec2 connection success!") compu_key = str(uuid.uuid4()) key = ec2.create_key_pair(compu_key) key.save(self.temp) os.rename(self.temp + '/' + compu_key + '.pem', self.config.get(self.general, 'ssh_pubkey_path')) keys = ec2.get_all_key_pairs() for key in keys: self.log.info("Key found: " + key.name) self.log.info("Starting instance ...") # Create a VPC vpc = vpc_conn.create_vpc('10.0.0.0/16') # Configure the VPC to support DNS resolution and hostname assignment vpc_conn.modify_vpc_attribute(vpc.id, enable_dns_support=True) vpc_conn.modify_vpc_attribute(vpc.id, enable_dns_hostnames=True) # Create an Internet Gateway gateway = vpc_conn.create_internet_gateway() # Attach the Internet Gateway to our VPC vpc_conn.attach_internet_gateway(gateway.id, vpc.id) # Create a Route Table route_table = vpc_conn.create_route_table(vpc.id) # Create a size /16 subnet subnet = vpc_conn.create_subnet(vpc.id, '10.0.0.0/24') # Associate Route Table with our subnet vpc_conn.associate_route_table(route_table.id, subnet.id) # Create a Route from our Internet Gateway to the internet vpc_conn.create_route(route_table.id, '0.0.0.0/0', gateway.id) # Create a new VPC security group sg = vpc_conn.create_security_group('compu_group', 'A group for compucorp', vpc.id) # Authorize access to port 22 from anywhere sg.authorize(ip_protocol='tcp', from_port=22, to_port=22, cidr_ip='0.0.0.0/0') sg.authorize(ip_protocol='tcp', from_port=443, to_port=443, cidr_ip='0.0.0.0/0') # Run an instance in our new VPC reservation = vpc_conn.run_instances(self.config.get(self.section, 'ami_id'), key_name=compu_key, security_group_ids=[sg.id], instance_type=self.config.get(self.section, 'instance_type'), subnet_id=subnet.id) instance = reservation.instances[0] # Wait for the instance to be running and have an public DNS name while instance.state != 'running': self.log.info("Instance state: %s" % instance.state) time.sleep(10) instance.update() # Now create an Elastic IP address for the instance # And associate the EIP with our instance eip = vpc_conn.allocate_address(domain='vpc') eip.associate(instance_id=instance.id) # tag machine ec2.create_tags([instance.id, vpc.id], {"Name": "deployment_"+eip.public_ip}) # Copy key as new name shutil.copy(self.config.get(self.general, 'ssh_pubkey_path'), self.temp + '/' + eip.public_ip + '.pem') self.log.info("Instance state: %s" % instance.state) self.log.info("Public IP: %s" % eip.public_ip) self.log.info("Waiting for SSH service to be available") self.wait_for_ssh(self.config.get(self.general, 'ssh_pubkey_path'), self.config.get(self.box, 'username'), eip.public_ip) return eip.public_ip
def validate_provider_data(self, serializer_attrs, all_data): attrs = super(AWSCloudProvider, self).validate_provider_data(serializer_attrs, all_data) region = attrs[self.REGION].slug access_key = all_data[self.ACCESS_KEY] secret_key = all_data[self.SECRET_KEY] keypair = all_data[self.KEYPAIR] errors = {} from stackdio.api.cloud.models import CloudAccount # Check for duplicates accounts = CloudAccount.objects.filter(provider__name=self.SHORT_NAME) for account in accounts: account_yaml = yaml.safe_load(account.yaml) if account.region.slug == region and account_yaml[ account.slug]['id'] == access_key: err_msg = ( 'You may not have multiple cloud accounts with the same access key ' 'in the same region. Please generate a new access key if you would ' 'like to have 2 cloud accounts in the same AWS account.') errors.setdefault(self.REGION, []).append(err_msg) if errors: raise ValidationError(errors) # check authentication credentials ec2 = None try: ec2 = boto.ec2.connect_to_region( region, aws_access_key_id=access_key, aws_secret_access_key=secret_key, ) ec2.get_all_zones() except boto.exception.EC2ResponseError: err_msg = 'Unable to authenticate to AWS with the provided keys.' errors.setdefault(self.ACCESS_KEY, []).append(err_msg) errors.setdefault(self.SECRET_KEY, []).append(err_msg) if errors: raise ValidationError(errors) # check keypair try: ec2.get_all_key_pairs(keypair) except boto.exception.EC2ResponseError: errors.setdefault(self.KEYPAIR, []).append( 'The keypair \'{0}\' does not exist in this account.'.format( keypair)) # check route 53 domain domain = all_data[self.ROUTE53_DOMAIN] if domain: try: # connect to route53 and check that the domain is available r53 = boto.connect_route53(access_key, secret_key) found_domain = False hosted_zones = r53.get_all_hosted_zones() hosted_zones = hosted_zones['ListHostedZonesResponse'][ 'HostedZones'] for hosted_zone in hosted_zones: if hosted_zone['Name'].startswith(domain): found_domain = True break if not found_domain: err = 'The Route53 domain \'{0}\' does not exist in ' \ 'this account.'.format(domain) errors.setdefault(self.ROUTE53_DOMAIN, []).append(err) # except boto.exception.DNSServerError as e: except Exception as e: logger.exception('Route53 issue?') errors.setdefault(self.ROUTE53_DOMAIN, []).append(str(e)) # check VPC required fields vpc_id = attrs[self.VPC_ID] if vpc_id: vpc = None try: vpc = boto.vpc.connect_to_region( region, aws_access_key_id=access_key, aws_secret_access_key=secret_key, ) except boto.exception.EC2ResponseError: err_msg = ('Unable to authenticate to AWS VPC with the ' 'provided keys.') errors.setdefault(self.ACCESS_KEY, []).append(err_msg) errors.setdefault(self.SECRET_KEY, []).append(err_msg) if not errors: try: vpc.get_all_vpcs([vpc_id]) except boto.exception.EC2ResponseError: errors.setdefault(self.VPC_ID, []).append( 'The VPC \'{0}\' does not exist in this account.'. format(vpc_id)) if errors: raise ValidationError(errors) return attrs
def launch_instance(self,region='us-west-2',ami='ami-16fd7026', instance_type='t1.micro', key_name='paws', key_extension='.pem', key_dir='~/.ssh', group_name='sec-group', ssh_port=22, cidr='0.0.0.0/0', tag='paws', user_data=None, login_user='******', ssh_passwd=None, elastic_address=False): """ Launch an instance and wait for it to start running. Returns a tuple consisting of the Instance object and the CmdShell object, if request, or None. ami The ID of the Amazon Machine Image that this instance will be based on. Default is a 64-bit Amazon Linux EBS image. instance_type The type of the instance. key_name The name of the SSH Key used for logging into the instance. It will be created if it does not exist. key_extension The file extension for SSH private key files. key_dir The path to the directory containing SSH private keys. This is usually ~/.ssh. group_name The name of the security group used to control access to the instance. It will be created if it does not exist. ssh_port The port number you want to use for SSH access (default 22). cidr The CIDR block used to limit access to your instance. tag A name that will be used to tag the instance so we can easily find it later. user_data Data that will be passed to the newly started instance at launch and will be accessible via the metadata service running at http://169.254.169.254. cmd_shell If true, a boto CmdShell object will be created and returned. This allows programmatic SSH access to the new instance. login_user The user name used when SSH'ing into new instance. The default is 'ec2-user' ssh_passwd The password for your SSH key if it is encrypted with a passphrase. """ # Create a connection to EC2 service. # You can pass credentials in to the connect_ec2 method explicitly # or you can use the default credentials in your ~/.boto config file # as we are doing here. ec2 = boto.ec2.connect_to_region(region) # Check to see if specified keypair already exists. # If we get an InvalidKeyPair.NotFound error back from EC2, # it means that it doesn't exist and we need to create it. try: key = ec2.get_all_key_pairs(keynames=[key_name])[0] except ec2.ResponseError, e: if e.code == 'InvalidKeyPair.NotFound': print 'Creating keypair: %s' % key_name # Create an SSH key to use when logging into instances. key = ec2.create_key_pair(key_name) # AWS will store the public key but the private key is # generated and returned and needs to be stored locally. # The save method will also chmod the file to protect # your private key. key.save(key_dir) else: raise
def delete_keys(ec2, keyname, dry_run): keys = ec2.get_all_key_pairs(filters={'key-name': keyname}) for key in keys: print key.name if not dry_run: ec2.delete_key_pair(key.name)