lib/rudy/metadata/disk.rb in rudy-0.2.4 vs lib/rudy/metadata/disk.rb in rudy-0.3.0
- old
+ new
@@ -1,54 +1,62 @@
module Rudy
- class MetaData
- attr_accessor :sdb
-
- def initalize(sdb)
- @sdb = sdb
- end
-
- end
- class MetaData
+
+ module MetaData
class Disk < Storable
+ @@rtype = 'disk'
+
+ # This is a flag used internally to specify that a volume has been
+ # created for this disk, but not formated.
+ attr_accessor :raw_volume
+
+ field :rtype
field :awsid
field :environment
field :role
field :path
field :position
field :zone
field :region
field :device
- field :backups => Array
+ #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
+ def rtype
+ @@rtype.to_s
+ end
+
+ def rtype=(val)
+ end
+
def name
Disk.generate_name(@zone, @environment, @role, @position, @path)
end
def valid?
@zone && @environment && @role && @position && @path
end
def to_query(more=[], remove=[])
- criteria = [:zone, :environment, :role, :position, :path, *more]
+ criteria = [:rtype, :zone, :environment, :role, :position, :path, *more]
criteria -= [*remove].flatten
query = []
criteria.each do |n|
- query << "['#{n}' = '#{self.send(n.to_sym)}']"
+ query << "['#{n}' = '#{self.send(n.to_sym)}'] "
end
query.join(" intersection ")
end
def to_s
@@ -66,10 +74,11 @@
["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)
Rudy::MetaData::Disk.from_hash(disk[:attributes])
end
def Disk.destroy(sdb, name)
@@ -87,10 +96,20 @@
# 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)
+ 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)
@@ -101,37 +120,49 @@
# Otherwise we need to start one
unless disk.awsid
puts "No active EBS volume found for #{disk.name}"
- backup = disk.backups.last
- if backup
- puts "We'll use the most recent backup..."
- volume = ec2.volumes.create(disk.zone, disk.size, backup)
- puts "Attaching #{disk.awsid} to #{id} (#{disk.device})"
- ec2.volumes.attach(machine[:aws_instance_id], disk.awsid, disk.device)
+ # 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.awsid = volume[:aws_id]
- puts "Saving disk metadata"
- Disk.save(sdb, disk)
+ disk.raw_volume = true
end
+
+ puts "Saving disk metadata"
+ disk.awsid = volume[:aws_id]
+ Disk.save(sdb, disk)
puts ""
end
disk
end
def Disk.list(sdb, zon, env=nil, rol=nil, pos=nil)
query = ''
- query << "['zone' = '#{zon}']" if zon
+ query << "['rtype' = '#{@@rtype}']" if zon
+ query << " intersection ['zone' = '#{zon}']" if zon
query << " intersection ['environment' = '#{env}']" if env
query << " intersection ['role' = '#{rol}']" if rol
query << " intersection ['position' = '#{pos}']" if pos
list = []
sdb.query_with_attributes(RUDY_DOMAIN, query).each_pair do |name, hash|
+ #puts "DISK: #{hash.to_yaml}"
list << Rudy::MetaData::Disk.from_hash(hash)
end
list
end
end