Example #1
0
def main():
    argument_spec = ec2_argument_spec()
    argument_spec.update(
        dict(
            name=dict(required=True),
            key_material=dict(required=False),
            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')
    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'''
            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, e:
                module.fail_json(msg="Unable to delete key pair '%s' - %s" %
                                 (key, e))
            else:
                key = None
                changed = True
        else:
            '''no match found, no changes required'''
Example #2
0
def main():
    argument_spec = ec2_argument_spec()
    argument_spec.update(dict(
            name=dict(required=True),
            key_material=dict(required=False),
            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')
    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'''
            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, e:
                module.fail_json(msg="Unable to delete key pair '%s' - %s" % (key, e))
            else:
                key = None
                changed = True
        else:
            '''no match found, no changes required'''
Example #3
0
def create_keypair(econfig_file=None, region=None, keyname="bcbio"):
    """Create a bcbio keypair and import to ec2. Gives us access to keypair locally and at AWS.
    """
    import boto
    import boto.ec2
    if econfig_file:
        keypair_dir = os.path.dirname(econfig_file).replace("elasticluster", "aws_keypairs")
    else:
        keypair_dir = os.path.join(os.getcwd(), "aws_keypairs")
    if not os.path.exists(keypair_dir):
        os.makedirs(keypair_dir)
    private_key = os.path.join(os.path.join(keypair_dir, keyname))
    new_key = not os.path.exists(private_key)
    if new_key:
        cmd = ["ssh-keygen", "-t", "rsa", "-N", "", "-f", private_key, "-C", "bcbio_aws_keypair"]
        subprocess.check_call(cmd)
    public_key = private_key + ".pub"
    if region:
        ec2 = boto.ec2.connect_to_region(region)
    else:
        ec2 = boto.connect_ec2()
    key = ec2.get_key_pair(keyname)
    if key and new_key:
        print("Non matching key %s found in AWS, removing." % keyname)
        ec2.delete_key_pair(keyname)
        key = None
    if not key:
        print("Key %s not found in AWS, importing created key" % keyname)
        with open(public_key) as in_handle:
            ec2.import_key_pair(keyname, in_handle.read())
    return {"user_key_name": keyname, "user_key_private": private_key,
            "user_key_public": public_key}
Example #4
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)
 def _check_keypair(self, keyname):
     try:
         region = self._pnda_env['aws_parameters']['AWS_REGION']
         ec2 = boto.ec2.connect_to_region(region)
         stored_key = ec2.get_key_pair(keyname)
         if stored_key is None:
             raise Exception("Key not found %s" % keyname)
         CONSOLE.info('Keyfile.......... OK')
     except:
         CONSOLE.info('Keyfile.......... ERROR')
         CONSOLE.error('Failed to find key %s in ec2.', keyname)
         CONSOLE.error(traceback.format_exc())
         sys.exit(1)
Example #6
0
def check_keypair(keyname, keyfile):
    if not os.path.isfile(keyfile):
        CONSOLE.info('Keyfile.......... ERROR')
        CONSOLE.error('Did not find local file named %s' % keyfile)
        sys.exit(1)

    try:
        region = pnda_env['ec2_access']['AWS_REGION']
        ec2 = boto.ec2.connect_to_region(region)
        stored_key = ec2.get_key_pair(keyname)
        if stored_key is None:
            raise Exception("Key not found %s" % keyname)
        CONSOLE.info('Keyfile.......... OK')
    except:
        CONSOLE.info('Keyfile.......... ERROR')
        CONSOLE.error('Failed to find key %s in ec2.' % keyname)
        CONSOLE.error(traceback.format_exc())
        sys.exit(1)
Example #7
0
def check_keypair(keyname, keyfile, existing_machines_def_file):
    if not os.path.isfile(keyfile):
        CONSOLE.info('Keyfile.......... ERROR')
        CONSOLE.error('Did not find local file named %s', keyfile)
        sys.exit(1)

    if existing_machines_def_file is not None:
        # TODO: Check ssh access to each machine here
        pass
    else:
        try:
            region = PNDA_ENV['ec2_access']['AWS_REGION']
            ec2 = boto.ec2.connect_to_region(region)
            stored_key = ec2.get_key_pair(keyname)
            if stored_key is None:
                raise Exception("Key not found %s" % keyname)
            CONSOLE.info('Keyfile.......... OK')
        except:
            CONSOLE.info('Keyfile.......... ERROR')
            CONSOLE.error('Failed to find key %s in ec2.', keyname)
            CONSOLE.error(traceback.format_exc())
            sys.exit(1)
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
Example #9
0
    elif state == 'present':
        if key:
            # existing key found
            if key_material:
                # 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()
Example #10
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)
Example #11
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
Example #12
0
    'science-left-db'  :  {'az': AZ_LEFT,  'type': INSTANCE_8GB, 'role': 'db' },
    'science-right-app' : {'az': AZ_RIGHT, 'type': INSTANCE_4GB, 'role': 'app'},
    'science-right-adm' : {'az': AZ_RIGHT, 'type': INSTANCE_4GB, 'role': 'adm'},
    'science-right-db ' : {'az': AZ_RIGHT, 'type': INSTANCE_8GB, 'role': 'db' },
    }

logging.basicConfig(level=logging.INFO)

ec2 = boto.ec2.connect_to_region(REGION)

zones = ec2.get_all_zones()
logging.info('Zones: %s' % zones)

# should look for a science pair and if none, create and save it.
# boto.ec2.securitygroup...
key_pair = ec2.get_key_pair(KEY_NAME)
logging.info('Key pair: %s' % key_pair)

#key_pair = ec2.create_key_pair(KEY_NAME)
#key_pair.save(os.path.expanduser(os.path.join('~', '.ssh')))

for name, settings in INSTANCES.items():
    reservation = ec2.run_instances(instance_type=settings['type'],
                                    placement=settings['az'],
                                    user_data='NAME=%s' % name, # How best to use?
                                    image_id=IMAGE_ID,
                                    key_name=KEY_NAME,
                                    security_groups=SECURITY_GROUPS,
                                    )

    instance = reservation.instances[0] # We MUST only create one instance at a time, above