2010-03-16 18:46:21 -04:00
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
module EC2
|
|
|
|
class Real
|
2009-08-22 01:04:12 -04:00
|
|
|
|
|
|
|
# Launch specified instances
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# * image_id<~String> - Id of machine image to load on instances
|
|
|
|
# * min_count<~Integer> - Minimum number of instances to launch. If this
|
|
|
|
# exceeds the count of available instances, no instances will be
|
|
|
|
# launched. Must be between 1 and maximum allowed for your account
|
|
|
|
# (by default the maximum for an account is 20)
|
|
|
|
# * max_count<~Integer> - Maximum number of instances to launch. If this
|
|
|
|
# exceeds the number of available instances, the largest possible
|
|
|
|
# number of instances above min_count will be launched instead. Must
|
|
|
|
# be between 1 and maximum allowed for you account
|
|
|
|
# (by default the maximum for an account is 20)
|
|
|
|
# * options<~Hash>:
|
|
|
|
# * 'Placement.AvailabilityZone'<~String> - Placement constraint for instances
|
2010-05-18 00:02:03 -04:00
|
|
|
# * 'BlockDeviceMapping'<~Array>: array of hashes
|
|
|
|
# * 'DeviceName'<~String> - where the volume will be exposed to instance
|
|
|
|
# * 'VirtualName'<~String> - volume virtual device name
|
|
|
|
# * 'Ebs.SnapshotId'<~String> - id of snapshot to boot volume from
|
|
|
|
# * 'Ebs.VolumeSize'<~String> - size of volume in GiBs required unless snapshot is specified
|
|
|
|
# * 'Ebs.DeleteOnTermination'<~String> - specifies whether or not to delete the volume on instance termination
|
|
|
|
# * 'SecurityGroup'<~Array> or <~String> - Name of security group(s) for instances
|
2010-02-20 16:40:46 -05:00
|
|
|
# * 'InstanceInitiatedShutdownBehaviour'<~String> - specifies whether volumes are stopped or terminated when instance is shutdown
|
2009-08-22 01:04:12 -04:00
|
|
|
# * 'InstanceType'<~String> - Type of instance to boot. Valid options
|
2010-01-10 16:22:18 -05:00
|
|
|
# in ['m1.small', 'm1.large', 'm1.xlarge', 'c1.medium', 'c1.xlarge', 'm2.2xlarge', 'm2.4xlarge']
|
2009-08-22 01:04:12 -04:00
|
|
|
# default is 'm1.small'
|
|
|
|
# * 'KernelId'<~String> - Id of kernel with which to launch
|
|
|
|
# * 'KeyName'<~String> - Name of a keypair to add to booting instances
|
|
|
|
# * 'Monitoring.Enabled'<~Boolean> - Enables monitoring, defaults to
|
|
|
|
# disabled
|
|
|
|
# * 'RamdiskId'<~String> - Id of ramdisk with which to launch
|
|
|
|
# * 'UserData'<~String> - Additional data to provide to booting instances
|
|
|
|
#
|
|
|
|
# ==== Returns
|
2009-11-02 21:48:49 -05:00
|
|
|
# * response<~Excon::Response>:
|
2009-08-22 01:04:12 -04:00
|
|
|
# * body<~Hash>:
|
|
|
|
# * 'groupSet'<~Array>: groups the instances are members in
|
|
|
|
# * 'groupName'<~String> - Name of group
|
|
|
|
# * 'instancesSet'<~Array>: returned instances
|
|
|
|
# * instance<~Hash>:
|
|
|
|
# * 'amiLaunchIndex'<~Integer> - reference to instance in launch group
|
2010-02-20 16:40:46 -05:00
|
|
|
# * 'architecture'<~String> - architecture of image in [i386, x86_64]
|
|
|
|
# * 'blockDeviceMapping'<~Array>
|
|
|
|
# * 'attachTime'<~Time> - time of volume attachment
|
|
|
|
# * 'deleteOnTermination'<~Boolean> - whether or not to delete volume on termination
|
|
|
|
# * 'deviceName'<~String> - specifies how volume is exposed to instance
|
|
|
|
# * 'status'<~String> - status of attached volume
|
|
|
|
# * 'volumeId'<~String> - Id of attached volume
|
2009-08-22 01:04:12 -04:00
|
|
|
# * 'dnsName'<~String> - public dns name, blank until instance is running
|
|
|
|
# * 'imageId'<~String> - image id of ami used to launch instance
|
|
|
|
# * 'instanceId'<~String> - id of the instance
|
|
|
|
# * 'instanceState'<~Hash>:
|
|
|
|
# * 'code'<~Integer> - current status code
|
|
|
|
# * 'name'<~String> - current status name
|
|
|
|
# * 'instanceType'<~String> - type of instance
|
2010-02-20 16:40:46 -05:00
|
|
|
# * 'ipAddress'<~String> - public ip address assigned to instance
|
2009-08-22 01:04:12 -04:00
|
|
|
# * 'kernelId'<~String> - Id of kernel used to launch instance
|
|
|
|
# * 'keyName'<~String> - name of key used launch instances or blank
|
|
|
|
# * 'launchTime'<~Time> - time instance was launched
|
|
|
|
# * 'monitoring'<~Hash>:
|
|
|
|
# * 'state'<~Boolean - state of monitoring
|
|
|
|
# * 'placement'<~Hash>:
|
|
|
|
# * 'availabilityZone'<~String> - Availability zone of the instance
|
|
|
|
# * 'privateDnsName'<~String> - private dns name, blank until instance is running
|
2010-02-20 16:40:46 -05:00
|
|
|
# * 'privateIpAddress'<~String> - private ip address assigned to instance
|
2009-08-22 01:04:12 -04:00
|
|
|
# * 'productCodes'<~Array> - Product codes for the instance
|
|
|
|
# * 'ramdiskId'<~String> - Id of ramdisk used to launch instance
|
|
|
|
# * 'reason'<~String> - reason for most recent state transition, or blank
|
2010-02-20 16:40:46 -05:00
|
|
|
# * 'rootDeviceName'<~String> - specifies how the root device is exposed to the instance
|
|
|
|
# * 'rootDeviceType'<~String> - root device type used by AMI in [ebs, instance-store]
|
2009-08-22 01:04:12 -04:00
|
|
|
# * 'ownerId'<~String> - Id of owner
|
|
|
|
# * 'requestId'<~String> - Id of request
|
|
|
|
# * 'reservationId'<~String> - Id of reservation
|
|
|
|
def run_instances(image_id, min_count, max_count, options = {})
|
2010-05-18 00:02:03 -04:00
|
|
|
if block_device_mapping = options.delete('BlockDeviceMapping')
|
|
|
|
block_device_mapping.each_with_index do |mapping, index|
|
|
|
|
for key, value in mapping
|
|
|
|
options.merge!({ format("BlockDeviceMapping.%d.#{key}", index) => value })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if security_groups = [*options.delete('SecurityGroup')]
|
2010-05-18 13:29:38 -04:00
|
|
|
options.merge!(AWS.indexed_param('SecurityGroup', security_groups))
|
2010-05-18 00:02:03 -04:00
|
|
|
end
|
2009-10-21 17:50:27 -04:00
|
|
|
if options['UserData']
|
|
|
|
options['UserData'] = Base64.encode64(options['UserData'])
|
|
|
|
end
|
2009-08-22 01:04:12 -04:00
|
|
|
request({
|
2010-03-16 01:15:33 -04:00
|
|
|
'Action' => 'RunInstances',
|
|
|
|
'ImageId' => image_id,
|
|
|
|
'MinCount' => min_count,
|
|
|
|
'MaxCount' => max_count,
|
|
|
|
:parser => Fog::Parsers::AWS::EC2::RunInstances.new
|
|
|
|
}.merge!(options))
|
2009-08-22 01:04:12 -04:00
|
|
|
end
|
2009-07-13 22:14:59 -04:00
|
|
|
|
|
|
|
end
|
2009-08-22 01:04:12 -04:00
|
|
|
|
2010-03-16 18:46:21 -04:00
|
|
|
class Mock
|
2009-08-22 01:04:12 -04:00
|
|
|
|
|
|
|
def run_instances(image_id, min_count, max_count, options = {})
|
2009-11-20 14:08:08 -05:00
|
|
|
response = Excon::Response.new
|
2009-08-22 01:04:12 -04:00
|
|
|
response.status = 200
|
2009-08-23 14:01:37 -04:00
|
|
|
|
|
|
|
group_set = [ (options['GroupId'] || 'default') ]
|
|
|
|
instances_set = []
|
|
|
|
reservation_id = Fog::AWS::Mock.reservation_id
|
|
|
|
|
2009-08-22 01:04:12 -04:00
|
|
|
min_count.times do |i|
|
|
|
|
instance_id = Fog::AWS::Mock.instance_id
|
2010-04-13 17:42:16 -04:00
|
|
|
instance = {
|
2010-03-16 18:46:21 -04:00
|
|
|
'amiLaunchIndex' => i,
|
|
|
|
'blockDeviceMapping' => [],
|
2010-05-05 17:22:22 -04:00
|
|
|
'dnsName' => nil,
|
2010-03-16 18:46:21 -04:00
|
|
|
'imageId' => image_id,
|
|
|
|
'instanceId' => instance_id,
|
|
|
|
'instanceState' => { 'code' => 0, 'name' => 'pending' },
|
|
|
|
'instanceType' => options['InstanceType'] || 'm1.small',
|
|
|
|
'kernelId' => options['KernelId'] || Fog::AWS::Mock.kernel_id,
|
2010-05-27 14:27:08 -04:00
|
|
|
# 'keyName' => options['KeyName'],
|
2010-03-16 18:46:21 -04:00
|
|
|
'launchTime' => Time.now,
|
|
|
|
'monitoring' => { 'state' => options['Monitoring.Enabled'] || false },
|
|
|
|
'placement' => { 'availabilityZone' => options['Placement.AvailabilityZone'] || Fog::AWS::Mock.availability_zone },
|
2010-05-05 17:22:22 -04:00
|
|
|
'privateDnsName' => nil,
|
2010-03-16 18:46:21 -04:00
|
|
|
'productCodes' => [],
|
|
|
|
'ramdiskId' => options['RamdiskId'] || Fog::AWS::Mock.ramdisk_id,
|
2010-05-05 17:22:22 -04:00
|
|
|
'reason' => nil,
|
2010-03-16 18:46:21 -04:00
|
|
|
'rootDeviceType' => 'instance-store'
|
2009-08-22 01:04:12 -04:00
|
|
|
}
|
2010-04-13 17:42:16 -04:00
|
|
|
instances_set << instance
|
|
|
|
@data[:instances][instance_id] = instance.merge({
|
2010-05-27 14:27:08 -04:00
|
|
|
'architecture' => 'i386',
|
2010-04-13 17:42:16 -04:00
|
|
|
'groupSet' => group_set,
|
|
|
|
'ownerId' => @owner_id,
|
2010-05-05 17:22:22 -04:00
|
|
|
'privateIpAddress' => nil,
|
2010-04-13 17:42:16 -04:00
|
|
|
'reservationId' => reservation_id,
|
|
|
|
})
|
2009-08-22 01:04:12 -04:00
|
|
|
end
|
|
|
|
response.body = {
|
2009-08-23 14:01:37 -04:00
|
|
|
'groupSet' => group_set,
|
|
|
|
'instancesSet' => instances_set,
|
2010-04-07 23:53:59 -04:00
|
|
|
'ownerId' => @owner_id,
|
2009-08-23 14:01:37 -04:00
|
|
|
'requestId' => Fog::AWS::Mock.request_id,
|
|
|
|
'reservationId' => reservation_id
|
|
|
|
}
|
2009-08-22 01:04:12 -04:00
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-07-13 22:14:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|