Beispiel #1
0
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
Beispiel #2
0
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
Beispiel #3
0
def create_keypair(source=KEYNAME):

    try:
        kp = ec2.delete_key_pair(source)
    except (boto.exception.EC2ResponseError):
        pass

    kp = ec2.create_key_pair(source)
    filename = os.environ.get(
        'EC2_KEY_PATH', './keys/ec2-{}.pem'.format(
            datetime.datetime.now().strftime('%Y-%m-%d_%H:%M')))
    latest_filename = os.environ.get('EC2_KEY_PATH', './latest.pem')
    kfile = open(filename, 'wb')
    latest_kfile = open(latest_filename, 'wb')

    def file_mode(user, group, other):
        return user * (8**2) + group * (8**1) + other * (8**0)

    kfile.write(kp.material)
    latest_kfile.write(kp.material)
    kfile.close()
    latest_kfile.close()
    os.chmod(filename, file_mode(7, 0, 0))
    os.chmod(latest_filename, file_mode(7, 0, 0))
    return filename
Beispiel #4
0
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
Beispiel #5
0
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
Beispiel #6
0
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
Beispiel #7
0
def create_KeyPair(name):
    temp = "./Key_Pairs/"
    temp += name
    temp += ".pem"
    outfile = open(temp, 'w')
    key_pair = ec2.create_key_pair(KeyName=name)
    KeyPairOut = str(key_pair.key_material)
    outfile.write(KeyPairOut)
    return name
Beispiel #8
0
def create_credentials():
    """
    Create credentials to remotely loging into EC2 instances (key pair)
    """
    ec2 = boto.connect_ec2()
    key_path = fab.env.get('key_filename')
    if not os.path.exists(key_path):
        # only needs to be done once
        key_pair = ec2.create_key_pair(key_name)
        key_pair.save(os.path.dirname(key_path))
Beispiel #9
0
def create_keypair(key_name="mqlibtest"):
    """
    Create the public-key crypto pair so we can log in to our new instances.
    AWS stores the public key under a name we provide, we need to save the
    private key ourselves.
    """
    if os.path.isfile(SSH_FOLDER + key_name + ".pem"):
        return  # Key already created
    ec2 = boto.ec2.connect_to_region(AWS_REGION)
    key = ec2.create_key_pair(key_name)
    key.save(SSH_FOLDER)
Beispiel #10
0
def create_keypair(key_name="mqlibtest"):
    """
    Create the public-key crypto pair so we can log in to our new instances.
    AWS stores the public key under a name we provide, we need to save the
    private key ourselves.
    """
    if os.path.isfile(SSH_FOLDER + key_name + ".pem"):
        return  # Key already created
    ec2 = boto.ec2.connect_to_region(AWS_REGION)
    key = ec2.create_key_pair(key_name)
    key.save(SSH_FOLDER)
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 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
Beispiel #14
0
 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)
Beispiel #15
0
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
Beispiel #16
0
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
Beispiel #17
0
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
Beispiel #18
0
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
Beispiel #19
0
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
Beispiel #20
0
    def _gen_key(self, aws_region='us-east-1', key_location= '/home/sgeadmin' ):
        ec2 = boto.ec2.connect_to_region( aws_region )
        key_name = 'sc-key-%s-%s' % (self._model.master_name, aws_region )
        k_file = '.'.join( [ key_name, 'pem' ])

        if os.path.isfile(os.path.join(key_location,k_file )) and \
                ec2.get_key_pair( key_name ):
            #we have a key and key_pair
            return key_name
        elif os.path.isfile(os.path.join(key_location, k_file )):
            #we have a key and no key pair
            os.remove( os.path.join(key_location, k_file) )
        elif ec2.get_key_pair( key_name ):
            #we have a key_pair, but no key
            ec2.delete_key_pair( key_name )
        key = ec2.create_key_pair( key_name )
        key.save( key_location )
        os.chmod( os.path.join( key_location, k_file ), 0600 )
        return (key_location , key_name)
Beispiel #21
0
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)
Beispiel #22
0
def create_key_pair():

    # if(os.path.exists('ec2-keypairv2.pem')):
    #     print("Key file already exists!")
    #     os.remove("ec2-keypairv2.pem")

    # create a file to store the key locally
    outfile = open('ec2-keypairv2.pem', 'w')

    if (check_key_pair_exists("ec2-keypairv2")):
        print("Key pair exists attached to ec2. deleting!")
        ec2.delete_key_pair("ec2-keypairv2")

    # call the boto ec2 function to create a key pair
    key_pair = ec2.create_key_pair(key_name='ec2-keypairv2')

    # capture the key and store it in a file
    key_pair_out = str(key_pair.material)
    outfile.write(key_pair_out)
    outfile.close()
Beispiel #23
0
 def initialize_key_pair(self):
     """
     try to create key pairs by region
     :return: available regions
     """
     available_regions = {}
     for region, ami in amis_per_regions.iteritems():
         try:
             ec2 = boto.ec2.connect_to_region(region, aws_access_key_id=self.key, aws_secret_access_key=self.secret)
             try:
                 key_pair = ec2.create_key_pair(self.ec2_key_name)  # only needs to be done once
                 key_pair.save(SSH_PATH)
             except boto.exception.EC2ResponseError as e:  # already exist
                 msg = "The keypair '{0}' already exists.".format(self.ec2_key_name)
                 if msg != e.message:
                     raise e
             available_regions[region] = ami
         except Exception as e:
             print " !!!! not supported. \n{0}".format(e)
     return available_regions
Beispiel #24
0
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 create_keypair(source = KEYNAME):
    try:
        kp = ec2.delete_key_pair(source)
    except (boto.exception.EC2ResponseError):
        pass

    kp = ec2.create_key_pair(source)
    filename = os.environ.get('EC2_KEY_PATH', './keys/ec2-{}.pem'.format(datetime.datetime.now().strftime('%Y-%m-%d_%H:%M')))
    latest_filename = os.environ.get('EC2_KEY_PATH', './keys/latest.pem')
    kfile = open(filename, 'wb')
    latest_kfile = open(latest_filename, 'wb')
    def file_mode(user, group, other):
        return user*(8**2) + group*(8**1) + other*(8**0)
    kfile.write(kp.material)
    latest_kfile.write(kp.material)
    kfile.close()
    latest_kfile.close()
    os.chmod(filename, file_mode(7,0,0))
    os.chmod(latest_filename, file_mode(7,0,0))
    return filename
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
Beispiel #27
0
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
Beispiel #28
0
    def real_launch(self, region, item_to_vote, instances=1):
        startup = script % {'ITEM': item_to_vote}
        ips = []
        print "launching in {0}: instances: {1}".format(region, instances)
        # print startup
        try:
            ec2 = boto.ec2.connect_to_region(region, aws_access_key_id=self.key, aws_secret_access_key=self.secret)
            try:
                key_pair = ec2.create_key_pair(self.ec2_key_name)  # only needs to be done once
                key_pair.save(SSH_PATH)
            except boto.exception.EC2ResponseError as e:
                #print e
                pass
            reservation = ec2.run_instances(image_id=amis_per_regions[region],
                                            min_count=instances, max_count=instances,
                                            key_name=self.ec2_key_name,
                                            user_data=startup)
            ips.extend(self._insert_instances(reservation, ec2))

        except Exception as e:
            print " !!!! not supported. \n{0}".format(e)
        return ips
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 launch_instance(ami='ami-d114f295',
                    instance_type='t2.micro',
                    key_name='spapanaidu',
                    key_extension='.pub',
                    key_dir='C:\\Workspace\\ssh\\non_prod\\',
                    group_name='sg',
                    ssh_port=22,
                    cidr='0.0.0.0/0',
                    tag='sg',
                    user_data=None,
                    cmd_shell=True,
                    login_user='******',
                    ssh_passwd=None):
    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")
    # ec2 = boto.connect_ec2(aws_access_key_id='',
    #                        aws_secret_access_key='')
    instances = ec2.get_all_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.
    try:
        key = ec2.get_key_pair('spapanaidu')
    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(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
Beispiel #33
0
def main():
    argument_spec = ec2_argument_spec()
    argument_spec.update(
        dict(
            name=dict(required=True),
            key_material=dict(required=False),
            force=dict(required=False, type='bool', default=True),
            state=dict(default='present', choices=['present', 'absent']),
            wait=dict(type='bool', default=False),
            wait_timeout=dict(default=300),
        ))
    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )

    if not HAS_BOTO:
        module.fail_json(msg='boto required for this module')

    name = module.params['name']
    state = module.params.get('state')
    key_material = module.params.get('key_material')
    force = module.params.get('force')
    wait = module.params.get('wait')
    wait_timeout = int(module.params.get('wait_timeout'))

    changed = False

    ec2 = ec2_connect(module)

    # find the key if present
    key = ec2.get_key_pair(name)

    # Ensure requested key is absent
    if state == 'absent':
        if key:
            '''found a match, delete it'''
            if not module.check_mode:
                try:
                    key.delete()
                    if wait:
                        start = time.time()
                        action_complete = False
                        while (time.time() - start) < wait_timeout:
                            if not ec2.get_key_pair(name):
                                action_complete = True
                                break
                            time.sleep(1)
                        if not action_complete:
                            module.fail_json(
                                msg=
                                "timed out while waiting for the key to be removed"
                            )
                except Exception as e:
                    module.fail_json(
                        msg="Unable to delete key pair '%s' - %s" % (key, e))
            key = None
            changed = True

    # Ensure requested key is present
    elif state == 'present':
        if key:
            # existing key found
            if key_material and force:
                # EC2's fingerprints are non-trivial to generate, so push this key
                # to a temporary name and make ec2 calculate the fingerprint for us.
                #
                # http://blog.jbrowne.com/?p=23
                # https://forums.aws.amazon.com/thread.jspa?messageID=352828

                # find an unused name
                test = 'empty'
                while test:
                    randomchars = [
                        random.choice(string.ascii_letters + string.digits)
                        for x in range(0, 10)
                    ]
                    tmpkeyname = "ansible-" + ''.join(randomchars)
                    test = ec2.get_key_pair(tmpkeyname)

                # create tmp key
                tmpkey = ec2.import_key_pair(tmpkeyname, key_material)
                # get tmp key fingerprint
                tmpfingerprint = tmpkey.fingerprint
                # delete tmp key
                tmpkey.delete()

                if key.fingerprint != tmpfingerprint:
                    if not module.check_mode:
                        key.delete()
                        key = ec2.import_key_pair(name, key_material)

                        if wait:
                            start = time.time()
                            action_complete = False
                            while (time.time() - start) < wait_timeout:
                                if ec2.get_key_pair(name):
                                    action_complete = True
                                    break
                                time.sleep(1)
                            if not action_complete:
                                module.fail_json(
                                    msg=
                                    "timed out while waiting for the key to be re-created"
                                )

                    changed = True
            pass

        # if the key doesn't exist, create it now
        else:
            '''no match found, create it'''
            if not module.check_mode:
                if key_material:
                    '''We are providing the key, need to import'''
                    key = ec2.import_key_pair(name, key_material)
                else:
                    '''
                    No material provided, let AWS handle the key creation and
                    retrieve the private key
                    '''
                    key = ec2.create_key_pair(name)

                if wait:
                    start = time.time()
                    action_complete = False
                    while (time.time() - start) < wait_timeout:
                        if ec2.get_key_pair(name):
                            action_complete = True
                            break
                        time.sleep(1)
                    if not action_complete:
                        module.fail_json(
                            msg=
                            "timed out while waiting for the key to be created"
                        )

            changed = True

    if key:
        data = {'name': key.name, 'fingerprint': key.fingerprint}
        if key.material:
            data.update({'private_key': key.material})

        module.exit_json(changed=changed, key=data)
    else:
        module.exit_json(changed=changed, key=None)
Beispiel #34
0
def deploy():
    ec2 = boto.ec2.connect_to_region('us-east-1',
                                     aws_access_key_id='',
                                     aws_secret_access_key='')
    ec2_key_pair = ec2.get_key_pair("key_pair")
    print ec2_key_pair
    if ec2_key_pair == None:
        ec2_key_pair = ec2.create_key_pair("key_pair")
        ec2_key_pair.save(".")

    group = ec2.get_all_security_groups(filters={'group-name': 'group'})
    if not group:
        group = ec2.create_security_group("csc326-groupg326-2-006",
                                          "a group for ec2")
        group.authorize("icmp", -1, -1, "0.0.0.0/0")
        group.authorize("tcp", 22, 22, "0.0.0.0/0")
        group.authorize("tcp", 80, 80, "0.0.0.0/0")
        group.authorize('tcp', 8080, 8080, '0.0.0.0/0')
    else:
        group = group[0]

    instances = ec2.get_only_instances()
    runninginstances = [i for i in instances if i.state == 'running']
    instance = None
    if not instances or (instances and not runninginstances):
        instances = ec2.run_instances(
            image_id='ami-8caa1ce4',
            key_name='key_pair',
            security_groups=['csc326-groupg326-2-006'],
            instance_type='t1.micro')
        instances = instances.instances[0]
    else:
        instances = instances[0]
    instance = instances

    print "Waiting for instance to be ready to run.."
    while instance.update() != 'running':
        time.sleep(10)

    print "Instance is ready and running"

    os.system("ssh -i key_pair.pem ubuntu@%s sudo apt-get update" %
              (instance.ip_address))
    os.system(
        "ssh -i key_pair.pem ubuntu@%s sudo apt-get install -y python-pip" %
        (instance.ip_address))
    os.system("ssh -i key_pair.pem ubuntu@%s sudo pip install bottle" %
              (instance.ip_address))
    os.system("ssh -i key_pair.pem ubuntu@%s sudo pip install beaker" %
              (instance.ip_address))
    os.system(
        "ssh -i key_pair.pem ubuntu@%s sudo pip install google-api-python-client"
        % (instance.ip_address))
    os.system("ssh -i key_pair.pem ubuntu@%s sudo pip install oauth2client" %
              (instance.ip_address))
    os.system("ssh -i key_pair.pem ubuntu@%s sudo pip install BeautifulSoup4" %
              (instance.ip_address))
    os.system("ssh -i key_pair.pem ubuntu@%s sudo pip install BeautifulSoup4" %
              (instance.ip_address))
    os.system("ssh -i key_pair.pem ubuntu@%s mkdir app" %
              (instance.ip_address))
    os.system("scp -i key_pair.pem ~/RandomSearch/Lab2/* ubuntu@%s:~/app" %
              (instance.ip_address))
    os.system(
        "ssh -i key_pair.pem ubuntu@%s sudo nohup python app/frontEnd.py" %
        (instance.ip_address))
    #elasticIPaddr = ec2.allocate_address()
    #elasticIPaddr.associate(instance.id)
    return instance.ip_address
conn = boto.ec2.connect_to_region("us-east-2",
                                  aws_access_key_id='#########',
                                  aws_secret_access_key='##########')
'''
Launch your ec2 instance. This requires ami image id and instance type.
Refer to the AWS documentation for details. You need to setup your
key-pair and security group before launching.

'''
''' Creating key pair '''

ec2 = boto3.resource('ec2')

try:
    outfile = open('keypair.pem', 'w')
    key_pair = ec2.create_key_pair(KeyName='keypair')
    KeyPairOut = str(key_pair.key_material)
    outfile.write(KeyPairOut)
except ClientError as e:
    print("Key Pair Already Exist")
''' Creating Security Group '''

ec2 = boto3.client('ec2')

response = ec2.describe_vpcs()
vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')

try:
    response = ec2.create_security_group(
        GroupName='SECURITYGROUP',
        Description='security group for ec2 instance',
Beispiel #36
0
def launch_spot_instance(id,
                         profile,
                         spot_wait_sleep=5,
                         instance_wait_sleep=3):
    ec2 = boto.ec2.connect_to_region(profile['region'])

    if not 'key_pair' in profile:
        print('key pair {0} does not exist'.format(profile['key_pair'][0]))
        profile['key_pair'] = ('KP-' + id, 'KP-' + id + '.pem')
        try:
            print >> sys.stderr, 'Creating key pair...',
            keypair = ec2.create_key_pair('KP-' + id)
            keypair.save('.')
            print >> sys.stderr, 'created'
        except boto.exception.EC2ResponseError as e:
            if e.code == 'InvalidKeyPair.Duplicate':
                print >> sys.stderr, 'already exists'
            else:
                raise e

    if not 'security_group' in profile:
        try:
            print >> sys.stderr, 'Creating security group...',
            sc = ec2.create_security_group('SG-' + id,
                                           'Security Group for ' + id)
            for proto, fromport, toport, ip in profile['firewall']:
                sc.authorize(proto, fromport, toport, ip)
            profile['security_group'] = (sc.id, sc.name)
            print >> sys.stderr, 'created'
        except boto.exception.EC2ResponseError as e:
            if e.code == 'InvalidGroup.Duplicate':
                print >> sys.stderr, 'already exists'
                sc = ec2.get_all_security_groups(groupnames=['SG-' + id])[0]
                profile['security_group'] = (sc.id, sc.name)
            else:
                raise e

    existing_requests = ec2.get_all_spot_instance_requests(
        filters={
            'launch.group-id': profile['security_group'][0],
            'state': ['open', 'active']
        })
    if existing_requests:
        if len(existing_requests) > 1:
            raise Exception('Too many existing spot requests')
        print >> sys.stderr, 'Reusing existing spot request'
        spot_req_id = existing_requests[0].id
    else:
        bdm = boto.ec2.blockdevicemapping.BlockDeviceMapping()
        bdm['/dev/sda1'] = boto.ec2.blockdevicemapping.BlockDeviceType(
            volume_type='gp2',
            size=profile['disk_size'],
            delete_on_termination=profile['disk_delete_on_termination'])
        bdm['/dev/sdb'] = boto.ec2.blockdevicemapping.BlockDeviceType(
            ephemeral_name='ephemeral0')
        print >> sys.stderr, 'Requesting spot instance'
        spot_reqs = ec2.request_spot_instances(
            price=profile['price'],
            image_id=profile['image_id'],
            instance_type=profile['type'],
            placement=profile['region'] + profile['availability_zone'],
            security_groups=[profile['security_group'][1]],
            key_name=profile['key_pair'][0],
            block_device_map=bdm,
            instance_profile_arn=
            'arn:aws:iam::720533437540:instance-profile/ec2_ml')
        spot_req_id = spot_reqs[0].id

    print >> sys.stderr, 'Waiting for launch',
    instance_id = None
    spot_tag_added = False
    while not instance_id:
        spot_req = ec2.get_all_spot_instance_requests(
            request_ids=[spot_req_id])[0]
        if not spot_tag_added:
            spot_req.add_tag('Name', id)
            spot_tag_added = True
        if spot_req.state == 'failed':
            # print(dir(spot_req))
            raise Exception('spto request failed')
            print('Spot request failed - {0}'.format(spot_req.status))
            sys.exit(0)
        instance_id = spot_req.instance_id
        if not instance_id:
            print >> sys.stderr, '.',
            time.sleep(spot_wait_sleep)
    print >> sys.stderr

    print >> sys.stderr, 'Retrieving instance by id'
    reservations = ec2.get_all_instances(instance_ids=[instance_id])
    instance = reservations[0].instances[0]
    instance.add_tag('Name', id)
    print >> sys.stderr, 'Got instance: ' + str(
        instance.id) + ' [' + instance.state + ']'
    print >> sys.stderr, 'Waiting for instance to boot',
    while not instance.state in ['running', 'terminated', 'shutting-down']:
        print >> sys.stderr, '.',
        time.sleep(instance_wait_sleep)
        instance.update()
    print >> sys.stderr
    if instance.state != 'running':
        raise Exception('Instance was terminated')
    return instance
Beispiel #37
0
def def_keypair(keyname):
    key = ec2.create_key_pair(keyname)
    key.save(path)
    return key
Beispiel #38
0
                    changed = True
            pass

        # if the key doesn't exist, create it now
        else:
            '''no match found, create it'''
            if not module.check_mode:
                if key_material:
                    '''We are providing the key, need to import'''
                    key = ec2.import_key_pair(name, key_material)
                else:
                    '''
                    No material provided, let AWS handle the key creation and 
                    retrieve the private key
                    '''
                    key = ec2.create_key_pair(name)

                if wait:
                    start = time.time()
                    action_complete = False
                    while (time.time() - start) < wait_timeout:
                        if ec2.get_key_pair(name):
                            action_complete = True
                            break
                        time.sleep(1)
                    if not action_complete:
                        module.fail_json(msg="timed out while waiting for the key to be created")

            changed = True

    if key:
Beispiel #39
0
    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
Beispiel #40
0
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