Skip to content

Commit

Permalink
Volume attaching / detaching tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
delano committed Apr 8, 2009
1 parent bc528b2 commit 67c2c79
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/rudy.rb
Expand Up @@ -159,7 +159,7 @@ def Rudy.waiter(duration=2, max=120, logger=STDOUT, dot='.', &check)
def Rudy.bell(chimes=1, logger=STDERR)
return if @@quiet
chimed = chimes.to_i
logger.print "\a"*chimes
logger.print "\a"*chimes if logger
true # be like Rudy.bug()
end

Expand Down
41 changes: 30 additions & 11 deletions lib/rudy/aws/ec2/volume.rb
Expand Up @@ -54,13 +54,21 @@ class EC2::Volumes

def attach(inst_id, vol_id, device)
vol_id = (vol_id.is_a?(Rudy::AWS::EC2::Volume)) ? vol_id.awsid : vol_id
inst_id = inst_id.is_a?(Rudy::AWS::EC2::Instace) ? inst_id.awsid : inst_id
@aws.attach_volume(:volume_id => vol_id, :instance_id => inst_id, :device => device)
inst_id = inst_id.is_a?(Rudy::AWS::EC2::Instance) ? inst_id.awsid : inst_id

ret = execute_request(false) {
@aws.attach_volume(:volume_id => vol_id, :instance_id => inst_id, :device => device)
}
(ret['status'] == 'attaching')
end

def detach(vol_id)
vol_id = (vol_id.is_a?(Rudy::AWS::EC2::Volume)) ? vol_id.awsid : vol_id
@aws.detach_volume(:volume_id => vol_id)

ret = execute_request({}) {
@aws.detach_volume(:volume_id => vol_id)
}
(ret['status'] == 'detaching')
end


Expand All @@ -77,7 +85,11 @@ def list_as_hash(state=nil, vol_id=[])
:volume_id => vol_id ? [vol_id].flatten : []
}
begin
vlist = @aws.describe_volumes(opts) || {}

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

# NOTE: The InternalError is returned for non-existent volume IDs.
# It's probably a bug so we're ignoring it -- Dave.
rescue ::EC2::InternalError => ex
Expand Down Expand Up @@ -110,11 +122,23 @@ def create(size, zone, snapid=nil)
# "availabilityZone"=>"us-east-1b",
# "createTime"=>"2009-03-17T20:10:48.000Z",
# "volumeId"=>"vol-48826421"
vol = @aws.create_volume(opts) || {}
vol = execute_request({}) {
@aws.create_volume(opts)
}

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


def destroy(vol_id)
vol_id = (vol_id.is_a?(Rudy::AWS::EC2::Volume)) ? vol_id.awsid : vol_id
ret = execute_request({}) {
@aws.delete_volume(:volume_id => vol_id)
}
(ret['return'] == 'true')
end

def self.from_hash(h)
# ---
# volumeSet:
Expand Down Expand Up @@ -151,12 +175,7 @@ def self.from_hash(h)
end
vol
end

def destroy(vol_id)
vol_id = (vol_id.is_a?(Rudy::AWS::EC2::Volume)) ? vol_id.awsid : vol_id
ret = @aws.delete_volume(:volume_id => vol_id)
(ret && ret['return'] == 'true')
end


def any?(state=nil,vol_id=[])
!(list(state, vol_id) || []).empty?
Expand Down
26 changes: 13 additions & 13 deletions lib/rudy/volumes.rb
Expand Up @@ -18,30 +18,29 @@ def create(size, zone=nil, snapshot=nil)
vol
end

def attach(volume, instance)
def attach(volume, instance, device="/dev/sdh")
volume = get(volume)
raise "No instance supplied" unless instance.is_a?(Rudy::AWS::EC2::Instance)
raise "No instance id" unless instance.awsid
raise "No device supplied" unless device
raise "Volume #{volume.awsid} already attached" if attached?(volume)

switch_user(:root)

ret = false
begin
@logger.print "Attaching Volume "
volume = @@ec2.volumes.attach(instance.awsid, volume.awsid, volume.device)
# {"attachTime"=>"2009-03-19T13:45:59.000Z", "status"=>"attaching", "device"=>"/dev/sdm",
# "requestId"=>"1c494a5d-a727-4fbc-a422-fa70898ca28a", "instanceId"=>"i-f17ae298",
# "volumeId"=>"vol-69f71100", "xmlns"=>"http://ec2.amazonaws.com/doc/2008-12-01/"}
ret = @@ec2.volumes.attach(instance.awsid, volume.awsid, device)
raise "Unknown error" unless ret

Rudy.waiter(1, 30, @logger) do
attached?(volume.awsid)
ret = attached?(volume.awsid)
end

rescue => ex
@logger.puts ex.backtrace if debug?
raise "Error attaching #{volume.device} to #{volume.awsid}: #{ex.message}"
raise "Error attaching #{volume.awsid} to #{instance.awsid}: #{ex.message}"
end

attached?(volume.awsid)
ret
end

def destroy(volume)
Expand All @@ -67,20 +66,21 @@ def destroy(volume)
def detach(volume)
volume = get(volume)
raise "#{volume.awsid} is not attached" unless @@ec2.volumes.attached?(volume.awsid)


ret = false
begin
@logger.print "Dettaching #{volume.awsid} "
@@ec2.volumes.detach(volume.awsid)
Rudy.waiter(1, 30) do
@@ec2.volumes.available?(volume.awsid)
ret = @@ec2.volumes.available?(volume.awsid)
end

rescue => ex
puts ex.backtrace if debug?
raise "Error detaching volume #{volume.awsid}: #{ex.message}"
end

available?(volume.awsid)
ret
end


Expand Down
13 changes: 9 additions & 4 deletions test/50_commands/50_machines_test.rb
Expand Up @@ -62,11 +62,16 @@ class Case_50_Commands
assert @rmach.console.is_a?(String), "No console output"
end

should "(45) attach disk to instance" do
volume = @rvol.create(volume_size)
should "(45) attach volume to instance and then detach it" do
volume = @rvol.create(1)
#volume = @rvol.get('vol-9934d4f0')
instances = @rmach.list(:running)
assert !instances.empty?, "No instances running"
instance = instances.first
assert !volume.attached?, "Volume is attached"
assert instance.running?, "Instance not running"
assert volume.available?, "Volume not available"
assert @rvol.attach(volume, instances.first), "Volume #{volume.awsid} not attached to #{instance.awsid}"
assert @rvol.attach(volume, instance), "Volume #{volume.awsid} not attached to #{instance.awsid}"
assert @rvol.detach(volume), "Volume not detached (#{volume.awsid})"
assert @rvol.destroy(volume), "Volume not destroyed (#{volume.awsid})"
end
Expand All @@ -76,7 +81,7 @@ class Case_50_Commands
assert @rmach.destroy, "Group not destroyed"
end

should "(95) destroy machine group" do
should "(95) destroy security group" do
# We can't delete the machine group until all instances are terminated
Rudy.waiter(2, 60, @@logger) { !@@ec2.instances.any?(:running) }
@rgroup.list do |group|
Expand Down

0 comments on commit 67c2c79

Please sign in to comment.