1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
module AWS
class EC2
# Represents an Amazon Machine Image (AMI).
#
# @attr_reader [String] name The name of the AMI that was provided
# during image creation.
#
# @attr [String] description A description of the image.
#
# @attr_reader [String] location The location of the AMI.
#
# @attr_reader [Symbol] state Current state of the AMI. If the
# state is `:available`, the image is successfully registered
# and available for launching. Valid values:
#
# * `:available`
# * `:pending`
# * `:failed`
#
# @attr_reader [String] owner_id The AWS account ID of the image owner.
#
# @attr_reader [String] owner_alias The AWS account alias (e.g.,
# `"amazon"`) or AWS account ID that owns the AMI.
#
# @attr_reader [Symbol] architecture The architecture of the
# image (e.g. `:i386`).
#
# @attr_reader [Symbol] type The type of image.
# Valid values are:
#
# * `:machine`
# * `:kernel`
# * `:ramdisk`
#
# @attr_reader [String] kernel_id The kernel ID associated with
# the image, if any. Only applicable for machine images.
#
# @attr_reader [String] ramdisk_id The RAM disk ID associated
# with the image, if any. Only applicable for machine images.
#
# @attr_reader [String] platform Value is `windows` for Windows
# AMIs; otherwise blank.
#
# @attr_reader [Object] state_reason The reason for the image's
# most recent state change. The return value is an object with
# the following methods:
#
# * `code` - Reason code for the state change.
# * `message` - A textual description of the state change.
#
# @attr_reader [Symbol] root_device_type The root device type
# used by the AMI. Possible values:
#
# * `:ebs`
# * `:instance_store`
#
# @attr_reader [String] root_device_name The root device name
# (e.g., `"/dev/sda1"`, or `"xvda"`).
#
# @attr_reader [Symbol] virtualization_type The type of
# virtualization of the AMI. Possible values:
#
# * `:paravirtual`
# * `:hvm`
#
# @attr_reader [Symbol] hypervisor The image's hypervisor type.
# Possible values are:
#
# * `:ovm`
# * `:xen`
#
# @attr_reader [Array<String>] product_codes Returns an array of
# product codes attached to this instance.
#
# @attr_reader [String] creation_date The date and time the image was created in ISO-8601 format
#
class Image < Resource
include TaggedItem
include HasPermissions
# @param [String] image_id
def initialize image_id, options = {}
@image_id = image_id
super
end
# @return [String] The ID of the AMI.
attr_reader :image_id
alias_method :id, :image_id
attribute :name, :static => true
mutable_attribute :description
attribute :location, :from => :image_location, :static => true
attribute :state, :from => :image_state, :to_sym => true
attribute :owner_id, :from => :image_owner_id, :static => true
attribute :owner_alias, :from => :image_owner_alias, :static => true
attribute :architecture, :to_sym => true, :static => true
attribute :type, :from => :image_type, :to_sym => true, :static => true
attribute :kernel_id, :static => true
attribute :ramdisk_id, :static => true
attribute :platform, :static => true
attribute :state_reason
attribute :root_device_type, :to_sym => true, :static => true
attribute :root_device_name, :static => true
attribute :virtualization_type, :to_sym => true, :static => true
attribute :hypervisor, :to_sym => true, :static => true
attribute :block_device_mapping
protected :block_device_mapping
attribute :product_codes, :static => true do
translates_output do |list|
(list || []).collect{|item| item.product_code }
end
end
attribute :creation_date, :static => true
populates_from(:describe_images) do |resp|
resp.image_index[id]
end
alias_method :launch_permissions, :permissions
# @note This method will not return data for ephemeral volumes.
# @return [Hash] Returns a hash of block
# device mappings for the image. In each entry, the key is
# the device name (e.g. `"/dev/sda1"`) and the value is an
# hash with the following keys that return information
# about the block device:
#
# * `:snapshot_id` - The ID of the snapshot that will be used to
# create this device (may be `nil`).
# * `:volume_size` - The size of the volume, in GiBs.
# * `:delete_on_termination` - True if the Amazon EBS volume is
# deleted on instance termination.
#
# @see {#block_devices}
def block_device_mappings
(block_device_mapping || []).inject({}) do |h, mapping|
if ebs = mapping[:ebs]
h[mapping[:device_name]] = ebs
end
h
end
end
# @return [Array<Hash>] Returns a list of all block device mappings.
# This list may contain ephemeral volumes.
def block_devices
block_device_mapping.to_a
end
# Deregisters this AMI. Once deregistered, the AMI cannot be
# used to launch new instances.
# @return [nil]
def deregister
client.deregister_image(:image_id => id)
nil
end
alias_method :delete, :deregister
# Runs a single instance of this image.
#
# @param [Hash] options
# @option (see InstanceCollection#create)
# @return (see InstanceCollection#create)
def run_instance options = {}
instances = InstanceCollection.new(:config => config)
instances.create(options.merge(:image => self))
end
# Runs multiple instances of this image.
#
# @param [Integer] count How many instances to request. You can specify
# this either as an integer or as a Range, to indicate the
# minimum and maximum number of instances to run. Note that
# for a new account you can request at most 20 instances at
# a time.
# @param [Hash] options
# @option (see InstanceCollection#create)
# @return [Array<Instance>] An array containing an {Instance} object for
# each instance that was run.
def run_instances count, options = {}
run_instance(options.merge(:count => count))
end
# @return [Boolean] Returns `true` if the AMI exists (is returned by
# the DescribeImages action).
def exists?
resp = client.describe_images(:filters => [
{ :name => "image-id", :values => [id] }
])
!resp.images_set.empty?
end
# @return [Image] The kernel associated with the image, if
# any. Only applicable for machine images.
def kernel
if id = kernel_id
Image.new(id, :config => config)
end
end
# @return [Image] The RAM disk associated with the image, if
# any. Only applicable for machine images.
def ramdisk
if id = ramdisk_id
Image.new(id, :config => config)
end
end
# Adds one or more product codes:
#
# image.add_product_codes 'ABCXYZ', 'MNOPQR'
#
# You can also pass an array of product codes:
#
# image.add_product_codes ['ABCXYZ', 'MNOPQR']
#
# @param [Array<String>] product_codes
#
# @return [nil]
#
def add_product_codes *product_codes
client_opts = {}
client_opts[:image_id] = self.id
client_opts[:product_codes] = product_codes.flatten
client.modify_image_attribute(client_opts)
nil
end
# @api private
def __permissions_attribute__
"launchPermission"
end
end
end
end
|