Module Rudy::AWS::EC2::Volumes

  1. lib/rudy/aws/ec2/volume.rb

Included modules

  1. Rudy::AWS::EC2

Constants

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

Public class methods

from_hash (h)

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
[show source]
     # File lib/rudy/aws/ec2/volume.rb, line 200
200:       def self.from_hash(h)
201:         vol = Rudy::AWS::EC2::Volume.new
202:         vol.status = h['status']
203:         vol.size = h['size']
204:         vol.snapid = h['snapshotId']
205:         vol.zone = h['availabilityZone']
206:         vol.awsid = h['volumeId']
207:         vol.created = h['createTime']
208:         if h['attachmentSet'].is_a?(Hash)
209:           item = h['attachmentSet']['item'].first
210:           vol.status = item['status']   # Overwrite "available status". Possibly a bad idea. 
211:           vol.device = item['device']
212:           vol.attached = item['attachTime']
213:           vol.instid = item['instanceId']
214:         end
215:         vol.postprocess
216:         vol
217:       end
get_vol_id (vol_id)

Returns the volume ID

[show source]
     # File lib/rudy/aws/ec2/volume.rb, line 221
221:       def self.get_vol_id(vol_id)
222:         (vol_id.is_a?(Rudy::AWS::EC2::Volume)) ? vol_id.awsid : vol_id
223:       end
known_state? (state)

Is state a known EC2 volume state? See: KNOWN_STATES

[show source]
     # File lib/rudy/aws/ec2/volume.rb, line 226
226:       def self.known_state?(state)
227:         return false unless state
228:         state &&= state.to_sym
229:         KNOWN_STATES.member?(state)
230:       end

Public instance methods

any? (state=nil,vol_id=[])
[show source]
     # File lib/rudy/aws/ec2/volume.rb, line 151
151:       def any?(state=nil,vol_id=[])
152:         vols = list(state, vol_id)
153:         !vols.nil?
154:       end
attach (vol_id, inst_id, device)
[show source]
    # File lib/rudy/aws/ec2/volume.rb, line 84
84:       def attach(vol_id, inst_id, device)
85:         vol_id = Volumes.get_vol_id(vol_id)
86:         inst_id = inst_id.is_a?(Rudy::AWS::EC2::Instance) ? inst_id.awsid : inst_id
87:         raise NoVolumeID unless vol_id
88:         raise VolumeAlreadyAttached, vol_id if attached?(vol_id)
89:         raise NoInstanceID unless inst_id
90:         raise NoDevice unless device
91:       
92:         opts = {
93:           :volume_id => vol_id, 
94:           :instance_id => inst_id, 
95:           :device => device.to_s    # Solaris devices are numbers
96:         }
97:         ret = Rudy::AWS::EC2.execute_request(false) { @@ec2.attach_volume(opts) }
98:         (ret['status'] == 'attaching')
99:       end
create (size, zone, snapid=nil)
  • size the number of GB
[show source]
    # File lib/rudy/aws/ec2/volume.rb, line 51
51:       def create(size, zone, snapid=nil)
52:         opts = {
53:           :availability_zone => zone.to_s,
54:           :size => (size || 1).to_s
55:         }
56:       
57:         opts[:snapshot_id] = snapid if snapid
58:       
59:         # "status"=>"creating", 
60:         # "size"=>"1", 
61:         # "snapshotId"=>nil, 
62:         # "requestId"=>"d42ff744-48b5-4f47-a3f0-7aba57a13eb9", 
63:         # "availabilityZone"=>"us-east-1b", 
64:         # "createTime"=>"2009-03-17T20:10:48.000Z", 
65:         # "volumeId"=>"vol-48826421"
66:         vol = Rudy::AWS::EC2.execute_request({}) { @@ec2.create_volume(opts) }
67:       
68:         # TODO: use a waiter?
69:         #Rudy.waiter(1, 30) do
70:         #  ret = @@@ec2.volumes.available?(volume.awsid)
71:         #end
72:       
73:         reqid = vol['requestId']
74:         Volumes.from_hash(vol) || nil
75:       end
destroy (vol_id)
[show source]
    # File lib/rudy/aws/ec2/volume.rb, line 77
77:       def destroy(vol_id)
78:         vol_id = Volumes.get_vol_id(vol_id)
79:         raise VolumeNotAvailable, vol_id unless available?(vol_id)
80:         ret = Rudy::AWS::EC2.execute_request({}) { @@ec2.delete_volume(:volume_id => vol_id) }
81:         (ret['return'] == 'true') 
82:       end
detach (vol_id)
[show source]
     # File lib/rudy/aws/ec2/volume.rb, line 101
101:       def detach(vol_id)
102:         vol_id = Volumes.get_vol_id(vol_id)
103:         raise NoVolumeID unless vol_id
104:         raise VolumeNotAttached, vol_id unless attached?(vol_id)
105:         ret = Rudy::AWS::EC2.execute_request({}) { 
106:           @@ec2.detach_volume(:volume_id => vol_id) 
107:         }
108:         (ret['status'] == 'detaching') 
109:       end
exists? (vol_id)
[show source]
     # File lib/rudy/aws/ec2/volume.rb, line 156
156:       def exists?(vol_id)
157:         vol_id = Volumes.get_vol_id(vol_id)
158:         vol = get(vol_id)
159:         return false if vol.nil?
160:         return false if vol.deleting?
161:         !vol.nil?
162:       end
get (vol_id)
[show source]
     # File lib/rudy/aws/ec2/volume.rb, line 164
164:       def get(vol_id)
165:         vol_id = Volumes.get_vol_id(vol_id)
166:         list(:any, vol_id).first rescue nil
167:       end
list (state=nil, vol_id=[], &each_vol)
[show source]
     # File lib/rudy/aws/ec2/volume.rb, line 112
112:       def list(state=nil, vol_id=[], &each_vol)
113:         volumes = list_as_hash(state, vol_id, &each_vol)
114:         volumes &&= volumes.values
115:         volumes
116:       end
list_as_hash (state=nil, vol_id=[], &each_vol)
[show source]
     # File lib/rudy/aws/ec2/volume.rb, line 118
118:       def list_as_hash(state=nil, vol_id=[], &each_vol)
119:         state &&= state.to_sym
120:         state = nil if state == :any
121:         # A nil state is fine, but we don't want an unknown one!
122:         raise UnknownState, state if state && !Volumes.known_state?(state)
123:       
124:         opts = { 
125:           :volume_id => vol_id ? [vol_id].flatten : [] 
126:         }
127: 
128:         vlist = Rudy::AWS::EC2.execute_request({}) { 
129:           @@ec2.describe_volumes(opts) 
130:         }
131: 
132:         volumes = {}
133:         return volumes unless vlist['volumeSet'].is_a?(Hash)
134:         vlist['volumeSet']['item'].each do |vol|
135:           v = Volumes.from_hash(vol)
136:           next if state && v.state != state.to_s
137:           volumes[v.awsid] = v
138:         end
139:         volumes.values.each { |v| each_vol.call(v) } if each_vol
140:         volumes = nil if volumes.empty?
141:         volumes
142:       end
list_by_instance (instid, &each_vol)
[show source]
     # File lib/rudy/aws/ec2/volume.rb, line 144
144:       def list_by_instance(instid, &each_vol)
145:         instid = instid.awsid if instid.is_a? Rudy::AWS::EC2::Instance
146:         volumes = list(:attached, &each_vol)
147:         volumes &&= volumes.select { |v| v.instid == instid }
148:         volumes
149:       end