Skip to content

Commit

Permalink
issue #51 - WIP - add code for spot instance and fleet requests (base…
Browse files Browse the repository at this point in the history
…d solely on API docs) with LOTS of debugging for others to test with
  • Loading branch information
jantman committed Jul 30, 2016
1 parent 3eff5ed commit c79f18e
Show file tree
Hide file tree
Showing 3 changed files with 408 additions and 5 deletions.
62 changes: 58 additions & 4 deletions awslimitchecker/services/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,65 @@ def _find_usage_instances(self):

def _find_usage_spot_instances(self):
"""calculate spot instance request usage and update Limits"""
pass
logger.debug('Getting spot instance request usage')
res = self.conn.describe_spot_instance_requests()
count = 0
for req in res['SpotInstanceRequests']:
if req['State'] in ['open', 'active']:
count += 1
logger.debug('Counting spot instance request %s state=%s',
req['SpotInstanceRequestId'], req['State'])
else:
logger.debug('NOT counting spot instance request %s state=%s',
req['SpotInstanceRequestId'], req['State'])
self.limits['Max spot instance requests per region']._add_current_usage(
count,
aws_type='AWS::EC2::SpotInstanceRequest'
)

def _find_usage_spot_fleets(self):
"""calculate spot fleet request usage and update Limits"""
pass
logger.debug('Getting spot fleet request usage')
res = self.conn.describe_spot_fleet_requests()
if 'NextToken' in res:
logger.error('Error: describe_spot_fleet_requests() response '
'includes pagination token, but pagination not '
'configured in awslimitchecker.')
# @TODO: assumption: 'Max target capacity for all spot fleets in region'
# only counts active fleets (not submitted)
active_fleets = 0
total_target_cap = 0
lim_cap_per_fleet = self.limits['Max target capacity per spot fleet']
lim_launch_specs = self.limits[
'Max launch specifications per spot fleet']
for fleet in res['SpotFleetRequestConfigs']:
_id = fleet['SpotFleetRequestId']
if fleet['SpotFleetRequestState'] != 'active':
logger.debug('Skipping spot fleet request %s in state %s',
_id, fleet['SpotFleetRequestState'])
continue
active_fleets += 1
cap = fleet['SpotFleetRequestConfig']['TargetCapacity']
launch_specs = len(
fleet['SpotFleetRequestConfig']['LaunchSpecifications'])
total_target_cap += cap
logger.debug('Active fleet %s: target capacity=%s, %d launch specs',
_id, cap,
launch_specs)
lim_cap_per_fleet._add_current_usage(
cap, resource_id=_id, aws_type='AWS::EC2::SpotFleetRequest')
lim_launch_specs._add_current_usage(
launch_specs, resource_id=_id,
aws_type='AWS::EC2::SpotFleetRequest')
logger.debug('Total active spot fleets: %d; total target capacity '
'for all spot fleets: %d', active_fleets, total_target_cap)
self.limits['Max active spot fleets per region']._add_current_usage(
active_fleets, aws_type='AWS::EC2::SpotFleetRequest'
)
self.limits['Max target capacity for all spot '
'fleets in region']._add_current_usage(
total_target_cap, aws_type='AWS::EC2::SpotFleetRequest'
)

def _get_reserved_instance_count(self):
"""
Expand Down Expand Up @@ -304,8 +358,8 @@ def _get_limits_spot(self):
limit_type='Spot instance requests'
)

limits['Max spot fleets per region'] = AwsLimit(
'Max spot fleets per region',
limits['Max active spot fleets per region'] = AwsLimit(
'Max active spot fleets per region',
self,
1000,
self.warning_threshold,
Expand Down
261 changes: 261 additions & 0 deletions awslimitchecker/tests/services/result_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,267 @@ def test_find_usage_networking_eni_sg(self):
]
}

test_find_usage_spot_instances = {
'SpotInstanceRequests': [
{
'SpotInstanceRequestId': 'reqID1',
'SpotPrice': 'string',
'Type': 'one-time',
'State': 'closed',
'Fault': {
'Code': 'string',
'Message': 'string'
},
'Status': {
'Code': 'string',
'UpdateTime': datetime(2015, 1, 1),
'Message': 'string'
},
'ValidFrom': datetime(2015, 1, 1),
'ValidUntil': datetime(2015, 1, 1),
'LaunchGroup': 'string',
'AvailabilityZoneGroup': 'string',
'LaunchSpecification': {
'ImageId': 'string',
'KeyName': 'string',
'SecurityGroups': [
{
'GroupName': 'string',
'GroupId': 'string'
},
],
'UserData': 'string',
'AddressingType': 'string',
'InstanceType': 't1.micro',
'Placement': {
'AvailabilityZone': 'string',
'GroupName': 'string'
},
'KernelId': 'string',
'RamdiskId': 'string',
'BlockDeviceMappings': [
{
'VirtualName': 'string',
'DeviceName': 'string',
'Ebs': {
'SnapshotId': 'string',
'VolumeSize': 123,
'DeleteOnTermination': True,
'VolumeType': 'standard',
'Iops': 123,
'Encrypted': True
},
'NoDevice': 'string'
},
],
'SubnetId': 'string',
'NetworkInterfaces': [
{
'NetworkInterfaceId': 'string',
'DeviceIndex': 123,
'SubnetId': 'string',
'Description': 'string',
'PrivateIpAddress': 'string',
'Groups': [
'string',
],
'DeleteOnTermination': True,
'PrivateIpAddresses': [
{
'PrivateIpAddress': 'string',
'Primary': True
},
],
'SecondaryPrivateIpAddressCount': 123,
'AssociatePublicIpAddress': True
},
],
'IamInstanceProfile': {
'Arn': 'string',
'Name': 'string'
},
'EbsOptimized': True,
'Monitoring': {
'Enabled': True
}
},
'InstanceId': 'string',
'CreateTime': datetime(2015, 1, 1),
'ProductDescription': 'Linux/UNIX (Amazon VPC)',
'BlockDurationMinutes': 123,
'ActualBlockHourlyPrice': 'string',
'Tags': [
{
'Key': 'string',
'Value': 'string'
},
],
'LaunchedAvailabilityZone': 'string'
},
{
'SpotInstanceRequestId': 'reqID2',
'Type': 'persistent',
'State': 'active',
},
{
'SpotInstanceRequestId': 'reqID3',
'Type': 'persistent',
'State': 'open',
},
{
'SpotInstanceRequestId': 'reqID4',
'Type': 'persistent',
'State': 'failed',
},
]
}

test_find_usage_spot_fleets = {
'SpotFleetRequestConfigs': [
{
'SpotFleetRequestId': 'req1',
'SpotFleetRequestState': 'failed',
'SpotFleetRequestConfig': {
'ClientToken': 'string',
'SpotPrice': 'string',
'TargetCapacity': 456,
'ValidFrom': datetime(2015, 1, 1),
'ValidUntil': datetime(2015, 1, 1),
'TerminateInstancesWithExpiration': True,
'IamFleetRole': 'string',
'LaunchSpecifications': [
{
'ImageId': 'string',
'KeyName': 'string',
'SecurityGroups': [
{
'GroupName': 'string',
'GroupId': 'string'
},
],
'UserData': 'string',
'AddressingType': 'string',
'InstanceType': 't1.micro',
'Placement': {
'AvailabilityZone': 'string',
'GroupName': 'string'
},
'KernelId': 'string',
'RamdiskId': 'string',
'BlockDeviceMappings': [
{
'VirtualName': 'string',
'DeviceName': 'string',
'Ebs': {
'SnapshotId': 'string',
'VolumeSize': 123,
'DeleteOnTermination': True,
'VolumeType': 'standard',
'Iops': 123,
'Encrypted': True
},
'NoDevice': 'string'
},
],
'Monitoring': {
'Enabled': True
},
'SubnetId': 'string',
'NetworkInterfaces': [
{
'NetworkInterfaceId': 'string',
'DeviceIndex': 123,
'SubnetId': 'string',
'Description': 'string',
'PrivateIpAddress': 'string',
'Groups': [
'string',
],
'DeleteOnTermination': True,
'PrivateIpAddresses': [
{
'PrivateIpAddress': 'string',
'Primary': True
},
],
'SecondaryPrivateIpAddressCount': 123,
'AssociatePublicIpAddress': True
},
],
'IamInstanceProfile': {
'Arn': 'string',
'Name': 'string'
},
'EbsOptimized': True,
'WeightedCapacity': 123.0,
'SpotPrice': 'string'
},
],
'ExcessCapacityTerminationPolicy': 'default',
'AllocationStrategy': 'lowestPrice',
'FulfilledCapacity': 123.0,
'Type': 'request'
},
'CreateTime': datetime(2015, 1, 1)
},
{
'SpotFleetRequestId': 'req2',
'SpotFleetRequestState': 'active',
'SpotFleetRequestConfig': {
'TargetCapacity': 11,
'LaunchSpecifications': [
{
'ImageId': 'string',
},
{
'ImageId': 'string',
},
{
'ImageId': 'string',
},
],
'Type': 'request'
},
},
{
'SpotFleetRequestId': 'req3',
'SpotFleetRequestState': 'modifying',
'SpotFleetRequestConfig': {
'TargetCapacity': 22,
'LaunchSpecifications': [
{
'ImageId': 'string',
},
{
'ImageId': 'string',
},
{
'ImageId': 'string',
},
{
'ImageId': 'string',
},
],
'Type': 'request'
},
},
{
'SpotFleetRequestId': 'req4',
'SpotFleetRequestState': 'active',
'SpotFleetRequestConfig': {
'TargetCapacity': 33,
'LaunchSpecifications': [
{
'ImageId': 'string',
},
],
'Type': 'request'
},
},
],
'NextToken': 'string'
}


class IAM(object):

Expand Down
Loading

0 comments on commit c79f18e

Please sign in to comment.