lib/rudy/metadata/disk.rb in rudy-0.3.2 vs lib/rudy/metadata/disk.rb in rudy-0.4.0
- old
+ new
@@ -24,13 +24,10 @@
field :device
#field :backups => Array
field :size
def initialize
- @device = "/dev/sdh"
- @zone = DEFAULT_ZONE
- @region = DEFAULT_REGION
@backups = []
@rtype = @@rtype.to_s
@raw_volume = false
end
@@ -44,11 +41,11 @@
def name
Disk.generate_name(@zone, @environment, @role, @position, @path)
end
def valid?
- @zone && @environment && @role && @position && @path
+ @zone && @environment && @role && @position && @path && @size && @device
end
def to_query(more=[], remove=[])
criteria = [:rtype, :zone, :environment, :role, :position, :path, *more]
criteria -= [*remove].flatten
@@ -74,18 +71,18 @@
["disk", zon, env, rol, pos, *dirs].join(RUDY_DELIM)
end
def Disk.get(sdb, name)
disk = sdb.get_attributes(RUDY_DOMAIN, name)
-
- raise "Disk #{name} does not exist!" unless disk && disk.has_key?(:attributes)
+ return nil unless disk && disk.has_key?(:attributes) && !disk[:attributes].empty?
+# raise "Disk #{name} does not exist!" unless
Rudy::MetaData::Disk.from_hash(disk[:attributes])
end
- def Disk.destroy(sdb, name)
- disk = Disk.get(sdb, name) # get raises an exception if the disk doesn't exist
- sdb.destroy(RUDY_DOMAIN, name)
+ def Disk.destroy(sdb, disk)
+ disk = Disk.get(sdb, disk) if disk.is_a?(String) # get raises an exception if the disk doesn't exist
+ sdb.destroy(RUDY_DOMAIN, disk.name)
true # wtf: RightAws::SimpleDB doesn't tell us whether it succeeds. We'll assume!
end
def Disk.save(sdb, disk)
sdb.store(RUDY_DOMAIN, disk.name, disk.to_hash, :replace)
@@ -96,60 +93,28 @@
# which is not part of the disk's name.
query = disk.to_query(:device, :path)
!sdb.query_with_attributes(RUDY_DOMAIN, query).empty?
end
- def Disk.from_volume(sdb, vol_id)
+ def Disk.find_from_volume(sdb, vol_id)
query = "['awsid' = '#{vol_id}']"
res = sdb.query_with_attributes(RUDY_DOMAIN, query)
if res.empty?
nil
else
disk = Rudy::MetaData::Disk.from_hash(res.values.first)
end
end
- def Disk.update_volume(sdb, ec2, disk, machine)
-
- disk = Disk.get(sdb, disk) if disk.is_a?(String)
- raise "You must provide a disk name or obect" unless disk.is_a?(Rudy::MetaData::Disk)
-
-
- # Make sure the volume is still running
- disk.awsid = nil if disk.awsid && !ec2.volumes.exists?(disk.awsid)
-
-
- # Otherwise we need to start one
- unless disk.awsid
- puts "No active EBS volume found for #{disk.name}"
-
- # TODO: pull actual backups
- backups = Rudy::MetaData::Backup.for_disk(sdb, disk, 2)
-
- if backups.is_a?(Array) && !backups.empty?
- backup = backups.first
- if ec2.snapshots.exists?(backup.awsid)
- puts "We'll use the most recent backup (#{backup.awsid})..."
- volume = ec2.volumes.create(disk.zone, disk.size, backup.awsid)
- else
- puts "The backup refers to a snapshot that doesn't exist."
- puts backup.name, backup.awsid
- puts "You need to delete this backup metadata before continuing."
- exit 1
- end
- else
- puts "We'll create one from scratch..."
- volume = ec2.volumes.create(disk.zone, disk.size, nil)
- disk.raw_volume = true
- end
-
- puts "Saving disk metadata"
- disk.awsid = volume[:aws_id]
- Disk.save(sdb, disk)
- puts ""
+
+ def Disk.find_from_path(sdb, path)
+ query = "['path' = '#{path}']"
+ res = sdb.query_with_attributes(RUDY_DOMAIN, query)
+ if res.empty?
+ nil
+ else
+ disk = Rudy::MetaData::Disk.from_hash(res.values.first)
end
-
- disk
end
def Disk.list(sdb, zon, env=nil, rol=nil, pos=nil)
query = ''
query << "['rtype' = '#{@@rtype}']" if zon