Class: Rudy::AWS::EC2::Volumes

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

Constant Summary collapse

KNOWN_STATES =
[:available, :creating, :deleting, :attached, :detaching].freeze

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

Creates a Rudy::AWS::EC2::Volume object from:

volumeSet: 
  item: 
  - status: available
    size: "1"
    snapshotId: 
    availabilityZone: us-east-1b
    attachmentSet: 
    createTime: "2009-03-17T20:10:48.000Z"
    volumeId: vol-48826421
    attachmentSet: 
      item: 
      - attachTime: "2009-03-17T21:49:54.000Z"
        status: attached
        device: /dev/sdh
        instanceId: i-956af3fc
        volumeId: vol-48826421

requestId: 8fc30e5b-a9c3-4fe0-a979-0f71e639a7c7


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/rudy/aws/ec2/volume.rb', line 191

def self.from_hash(h)
  vol = Rudy::AWS::EC2::Volume.new
  vol.status = h['status']
  vol.size = h['size']
  vol.snapid = h['snapshotId']
  vol.zone = h['availabilityZone']
  vol.awsid = h['volumeId']
  vol.create_time = h['createTime']
  if h['attachmentSet'].is_a?(Hash)
    item = h['attachmentSet']['item'].first
    vol.status = item['status']   # Overwrite "available status". Possibly a bad idea. 
    vol.device = item['device']
    vol.attach_time = item['attachTime']
    vol.instid = item['instanceId']
  end
  vol
end

.get_vol_id(vol_id) ⇒ Object

  • vol_id is a String or Rudy::AWS::EC2::Volume

Returns the volume ID



211
212
213
# File 'lib/rudy/aws/ec2/volume.rb', line 211

def self.get_vol_id(vol_id)
  (vol_id.is_a?(Rudy::AWS::EC2::Volume)) ? vol_id.awsid : vol_id
end

.known_state?(state) ⇒ Boolean

Is state a known EC2 volume state? See: KNOWN_STATES

Returns:

  • (Boolean)


216
217
218
219
220
# File 'lib/rudy/aws/ec2/volume.rb', line 216

def self.known_state?(state)
  return false unless state
  state &&= state.to_sym
  KNOWN_STATES.member?(state)
end

Instance Method Details

#any?(state = nil, vol_id = []) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/rudy/aws/ec2/volume.rb', line 145

def any?(state=nil,vol_id=[])
  !list(state, vol_id).nil?
end

#attach(vol_id, inst_id, device) ⇒ Object

Raises:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rudy/aws/ec2/volume.rb', line 93

def attach(vol_id, inst_id, device)
  vol_id = Volumes.get_vol_id(vol_id)
  inst_id = inst_id.is_a?(Rudy::AWS::EC2::Instance) ? inst_id.awsid : inst_id
  raise NoVolumeID unless vol_id
  raise VolumeAlreadyAttached, vol_id if attached?(vol_id)
  raise NoInstanceID unless inst_id
  raise NoDevice unless device

  opts = {
    :volume_id => vol_id, 
    :instance_id => inst_id, 
    :device => device.to_s    # Solaris devices are numbers
  }
  ret = execute_request(false) { @ec2.attach_volume(opts) }
  (ret['status'] == 'attaching')
end

#create(size, zone, snapid = nil) ⇒ Object

  • size the number of GB



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

def create(size, zone, snapid=nil)
  opts = {
    :availability_zone => zone.to_s,
    :size => (size || 1).to_s
  }

  opts[:snapshot_id] = snapid if snapid

  # "status"=>"creating", 
  # "size"=>"1", 
  # "snapshotId"=>nil, 
  # "requestId"=>"d42ff744-48b5-4f47-a3f0-7aba57a13eb9", 
  # "availabilityZone"=>"us-east-1b", 
  # "createTime"=>"2009-03-17T20:10:48.000Z", 
  # "volumeId"=>"vol-48826421"
  vol = execute_request({}) { @ec2.create_volume(opts) }

  # TODO: use a waiter?
  #Rudy.waiter(1, 30) do
  #  ret = @@ec2.volumes.available?(volume.awsid)
  #end

  reqid = vol['requestId']
  Volumes.from_hash(vol) || nil
end

#destroy(vol_id) ⇒ Object

Raises:



86
87
88
89
90
91
# File 'lib/rudy/aws/ec2/volume.rb', line 86

def destroy(vol_id)
  vol_id = Volumes.get_vol_id(vol_id)
  raise VolumeNotAvailable, vol_id unless available?(vol_id)
  ret = execute_request({}) { @ec2.delete_volume(:volume_id => vol_id) }
  (ret['return'] == 'true') 
end

#detach(vol_id) ⇒ Object

Raises:



110
111
112
113
114
115
116
# File 'lib/rudy/aws/ec2/volume.rb', line 110

def detach(vol_id)
  vol_id = Volumes.get_vol_id(vol_id)
  raise NoVolumeID unless vol_id
  raise VolumeNotAttached, vol_id unless attached?(vol_id)
  ret = execute_request({}) { @ec2.detach_volume(:volume_id => vol_id) }
  (ret['status'] == 'detaching') 
end

#exists?(vol_id) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
152
153
# File 'lib/rudy/aws/ec2/volume.rb', line 149

def exists?(vol_id)
  vol_id = Volumes.get_vol_id(vol_id)
  vol = get(vol_id)
  !vol.nil?
end

#get(vol_id) ⇒ Object



155
156
157
158
# File 'lib/rudy/aws/ec2/volume.rb', line 155

def get(vol_id)
  vol_id = Volumes.get_vol_id(vol_id)
  list(:any, vol_id).first rescue nil
end

#list(state = nil, vol_id = []) ⇒ Object



119
120
121
# File 'lib/rudy/aws/ec2/volume.rb', line 119

def list(state=nil, vol_id=[])
  list_as_hash(state, vol_id).values
end

#list_as_hash(state = nil, vol_id = []) ⇒ Object

Raises:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rudy/aws/ec2/volume.rb', line 123

def list_as_hash(state=nil, vol_id=[])
  state &&= state.to_sym
  state = nil if state == :any
  # A nil state is fine, but we don't want an unknown one!
  raise UnknownState, state if state && !Volumes.known_state?(state)

  opts = { 
    :volume_id => vol_id ? [vol_id].flatten : [] 
  }

  vlist = execute_request({}) { @ec2.describe_volumes(opts) }

  volumes = {}
  return volumes unless vlist['volumeSet'].is_a?(Hash)
  vlist['volumeSet']['item'].each do |vol|
    v = Volumes.from_hash(vol)
    next if state && v.state != state.to_s
    volumes[v.awsid] = v
  end
  volumes
end