Class: Rudy::AWS::EC2::Images

Inherits:
Object
  • Object
show all
Includes:
Base, ObjectBase
Defined in:
lib/rudy/aws/ec2/image.rb

Instance Attribute Summary

Attributes included from Base

#ec2

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#initialize

Methods included from Huxtable

change_environment, change_position, change_region, change_role, change_zone, #check_keys, #config_dirname, create_domain, #current_group_name, #current_machine_address, #current_machine_count, #current_machine_group, #current_machine_hostname, #current_machine_image, #current_machine_name, #current_machine_size, #current_user, #current_user_keypairpath, debug?, #debug?, domain, domain_exists?, #group_metadata, #has_keypair?, #has_keys?, #has_pem_keys?, #has_root_keypair?, keypair_path_to_name, #known_machine_group?, #root_keypairname, #root_keypairpath, #switch_user, update_config, update_global, update_logger, #user_keypairname, #user_keypairpath

Class Method Details

.from_hash(h) ⇒ Object

imageOwnerId: “203338247012”

kernelId: aki-a71cf9ce
ramdiskId: ari-a51cf9cc
imageState: available
imageId: ami-dd34d3b4
architecture: i386
isPublic: "false"
imageLocation: solutious-rudy-us/debian-squeeze-m1.small-v5.manifest.xml
imageType: machine


133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rudy/aws/ec2/image.rb', line 133

def Images.from_hash(h)
  img = Rudy::AWS::EC2::Image.new
  img.owner = h['imageOwnerId']
  img.aki = h['kernelId']
  img.ari = h['ramdiskId']
  img.state = h['imageState']
  img.awsid = h['imageId']
  img.arch = h['architecture']
  img.visibility = h['isPublic'] == 'true' ? 'public' : 'private'
  img.location = h['imageLocation']
  img.kind = h['imageType']
  img
end

Instance Method Details

#deregister(id) ⇒ Object

id AMI ID to deregister (ami-XXXXXXX) Returns true when successful. Otherwise throws an exception.



104
105
106
107
108
109
110
111
# File 'lib/rudy/aws/ec2/image.rb', line 104

def deregister(id)
  opts = {
    :image_id => id
  }
  ret = @ec2.deregister_image(opts)
  return false unless ret && ret.is_a?(Hash)
  true
end

#list(owner = [], image_ids = [], executable_by = [], &each_image) ⇒ Object



61
62
63
64
65
# File 'lib/rudy/aws/ec2/image.rb', line 61

def list(owner=[], image_ids=[], executable_by=[], &each_image)
  images = list_as_hash(owner, image_ids, executable_by)
  images &&= images.values
  images
end

#list_as_hash(owner = [], image_ids = [], executable_by = [], &each_image) ⇒ Object



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
# File 'lib/rudy/aws/ec2/image.rb', line 67

def list_as_hash(owner=[], image_ids=[], executable_by=[], &each_image)
  owner &&= [owner].flatten.compact
  image_ids &&= [image_ids].flatten.compact
  executable_by &&= [executable_by].flatten.compact
  
  # Remove dashes from aws account numbers
  owner &&= owner.collect { |o| o.tr('-', '') }
  # If we got Image objects, we want just the IDs.
  # This method always returns an Array.
  image_ids = objects_to_image_ids(image_ids)
  
  opts = {
    :owner_id => owner || [],
    :image_id => image_ids || [],
    :executable_by => executable_by || []
  }
  
  response = execute_request({}) { @ec2.describe_images(opts) }
  
  return nil unless response['imagesSet'].is_a?(Hash)  # No instances 

  resids = []
  images = {}
  response['imagesSet']['item'].each do |res|      
    resids << res['reservationId']
    img = Images.from_hash(res)
    images[img.awsid] = img
  end
  
  images.each_value { |image| each_image.call(image) } if each_image
  
  images = nil if images.empty? # Don't return an empty hash
  images
end

#register(path) ⇒ Object

path the S3 path to the manifest (bucket/file.manifest.xml) Returns the AMI ID when successful, otherwise throws an exception.



115
116
117
118
119
120
121
122
# File 'lib/rudy/aws/ec2/image.rb', line 115

def register(path)
  opts = {
    :image_location => path
  }
  ret = @ec2.register_image(opts)
  return nil unless ret && ret.is_a?(Hash)
  ret['imageId']
end