Ejemplo n.º 1
0
def max_instances(user, profile, params):
    client = get_ec2_client(profile)
    response = client.describe_account_attributes(
        AttributeNames=['max-instances'])
    jsonpath = 'AccountAttributes[0].AttributeValues[0].AttributeValue'
    max_instances = parse(jsonpath).find(response)

    if max_instances:
        max_instances = max_instances[0].value
    else:
        raise RestException('Unable to extract "max-instances" attribute.')

    return {'maxinstances': max_instances}
Ejemplo n.º 2
0
    def validate(self, doc):
        name = doc['name']

        if type(doc['publicIPs']) != bool:
            raise ValidationException('Value must be of type boolean',
                                      'publicIPs')

        if not name:
            raise ValidationException('A name must be provided', 'name')

        # Check for duplicate names
        query = {
            'name': name,
            'userId': doc['userId']
        }
        if '_id' in doc:
            query['_id'] = {'$ne': doc['_id']}

        if self.findOne(query):
            raise ValidationException('A profile with that name already exists',
                                      'name')

        client = None
        # First validate the credentials
        try:
            client = get_ec2_client(doc)
            client.describe_account_attributes()
        except ClientError as ce:
            code = parse('Error.Code').find(ce.response)
            if code:
                code = code[0].value
            else:
                raise

            if code == ClientErrorCode.AuthFailure:
                raise ValidationException('Invalid AWS credentials')
        except EndpointConnectionError as ece:
            raise ValidationException(ece.message)

        # Now validate the region
        self._validate_region(client, doc)

        # Only do the rest of the validation if this is a new profile (not
        # a key update )
        if '_id' not in doc:
            # Now validate the zone
            self._validate_zone(client, doc)

        return doc
Ejemplo n.º 3
0
    def _validate_zone(self, client, doc):
        try:
            client = get_ec2_client(doc)
            client.describe_availability_zones(
                ZoneNames=[doc['availabilityZone']])

        except ClientError as ce:
            code = parse('Error.Code').find(ce.response)
            if code:
                code = code[0].value
            else:
                raise

            if code == ClientErrorCode.InvalidParameterValue:
                raise ValidationException(
                    'Invalid zone', 'availabilityZone')
        except EndpointConnectionError as ece:
            raise ValidationException(ece.message)
Ejemplo n.º 4
0
def delete_profile(user, profile, params):

    query = {'profileId': profile['_id']}

    if ModelImporter.model('volume', 'cumulus').findOne(query):
        raise RestException(
            'Unable to delete profile as it is associated with'
            ' a volume', 400)

    if ModelImporter.model('cluster', 'cumulus').findOne(query):
        raise RestException(
            'Unable to delete profile as it is associated with'
            ' a cluster', 400)

    # Clean up key associate with profile
    cumulus.aws.ec2.tasks.key.delete_key_pair.delay(_filter(profile),
                                                    get_task_token()['_id'])

    client = get_ec2_client(profile)
    client.delete_key_pair(KeyName=str(profile['_id']))

    ModelImporter.model('aws', 'cumulus').remove(profile)
Ejemplo n.º 5
0
def generate_key_pair(aws_profile, girder_token):
    try:
        client = get_ec2_client(aws_profile)
        key_path = _key_path(aws_profile)
        key_pair = client.create_key_pair(KeyName=aws_profile['_id'])

        with open(key_path, 'wb') as fp:
            fp.write(key_pair['KeyMaterial'].encode('utf8'))
        os.chmod(key_path, stat.S_IRUSR)

        aws_profile['status'] = 'available'

    except Exception as ex:
        aws_profile['status'] = 'error'
        aws_profile['errorMessage'] = '%s: %s' % (type(ex).__name__, ex)
        traceback.print_exc()

    update_url = '%s/user/%s/aws/profiles/%s' % (cumulus.config.girder.baseUrl,
                                                 aws_profile['userId'],
                                                 aws_profile['_id'])

    headers = {'Girder-Token':  girder_token}
    r = requests.patch(update_url, json=aws_profile, headers=headers)
    check_status(r)
Ejemplo n.º 6
0
def running_instances(user, profile, params):

    return {'runninginstances': get_ec2_client(profile).running_instances()}