module Rudy::Metadata

Constants

COMMON_FIELDS

Public Class Methods

build_criteria(rtype, fields={}, less=[]) click to toggle source

Generates a default criteria for all metadata based on region, zone, environment, and role. If a position has been specified in the globals it will also be included.

  • rtype is the record type. One of: m, disk, or back.

  • fields replaces and adds values to this criteria

  • less removes keys from the default criteria.

Returns a Hash.

# File lib/rudy/metadata.rb, line 96
def self.build_criteria(rtype, fields={}, less=[])
  fields ||= {}
  fields[:rtype] = rtype
  fields[:position] = @@global.position unless @@global.position.nil?
  names = Rudy::Metadata::COMMON_FIELDS
  values = names.collect { |n| @@global.send(n.to_sym) }
  mixer = names.zip(values).flatten
  criteria = Hash[*mixer].merge(fields)
  criteria.reject! { |n,v| less.member?(n) }
  Rudy::Huxtable.ld "CRITERIA: #{criteria.inspect}"
  criteria
end
connect(accesskey, secretkey, region, reconnect=false) click to toggle source

Creates instances of the following and stores to class variables:

# File lib/rudy/metadata.rb, line 29
def self.connect(accesskey, secretkey, region, reconnect=false)
  return @@rsdb unless reconnect || @@rsdb.nil?
  @@rsdb  = Rudy::AWS::SDB.new accesskey, secretkey, region
  true
end
create_domain(n) click to toggle source

Creates a SimpleDB domain named n and updates +@@domain+ if successful

# File lib/rudy/metadata.rb, line 44
def self.create_domain(n)
  @@domain = n if @@rsdb.create_domain n
end
destroy(n) click to toggle source
# File lib/rudy/metadata.rb, line 71
def self.destroy(n)
  Rudy::Huxtable.ld "DESTROY: #{n}" if Rudy.debug?
  @@rsdb.destroy @@domain, n
end
destroy_domain(n) click to toggle source

Destroys a SimpleDB domain named n and sets +@@domain+ to Rudy::DOMAIN

# File lib/rudy/metadata.rb, line 49
def self.destroy_domain(n)
  Rudy::Huxtable.ld "DESTROY: #{n}" if Rudy.debug?
  @@rsdb.destroy_domain n
  @@domain = Rudy::DOMAIN
  true
end
domain(name=nil) click to toggle source
# File lib/rudy/metadata.rb, line 34
def self.domain(name=nil)
  return @@domain if name.nil?
  @@domain = name
end
domain=(*args) click to toggle source

An alias for ::domain

# File lib/rudy/metadata.rb, line 39
def self.domain=(*args)
  domain *args
end
exists?(n) click to toggle source
# File lib/rudy/metadata.rb, line 62
def self.exists?(n)
  !get(n).nil?
end
get(n) click to toggle source

Get a record from SimpleDB with the key n

# File lib/rudy/metadata.rb, line 57
def self.get(n) 
  Rudy::Huxtable.ld "GET: #{n}" if Rudy.debug?
  @@rsdb.get @@domain, n
end
get_rclass(rtype) click to toggle source
# File lib/rudy/metadata.rb, line 13
def self.get_rclass(rtype)
  case rtype
  when Rudy::Machines::RTYPE
    Rudy::Machines
  when Rudy::Disks::RTYPE
    Rudy::Disks
  when Rudy::Backups::RTYPE
    Rudy::Backups
  else
    raise UnknownRecordType, rtype
  end
end
included(obj) click to toggle source
# File lib/rudy/metadata.rb, line 156
def self.included(obj)
  obj.send :include, Rudy::Metadata::InstanceMethods
  
  # Add common storable fields. 
  [COMMON_FIELDS, :position].flatten.each do |n|
    obj.field n
  end
  
end
new(rtype, opts={}) click to toggle source
# File lib/rudy/metadata.rb, line 166
def initialize(rtype, opts={})
  @rtype = rtype
  @position = position || @@global.position || '01'
  
  COMMON_FIELDS.each { |n|
    ld "SETTING: #{n}: #{@@global.send(n)}" if @@global.verbose > 3
    instance_variable_set("@#{n}", @@global.send(n))
  }
  
  opts.each_pair do |n,v|
    raise "Unknown attribute for #{self.class}: #{n}" if !self.has_field? n
    next if v.nil?
    ld "RESETTING: #{n}: #{v}" if @@global.verbose > 3
    self.send("#{n}=", v)
  end
  
end
put(n, o, replace=false) click to toggle source
# File lib/rudy/metadata.rb, line 66
def self.put(n, o, replace=false)
  Rudy::Huxtable.ld "PUT: #{n}" if Rudy.debug?
  @@rsdb.put @@domain, n, o, replace
end
select(fields={}) click to toggle source

Generates and executes a SimpleDB select query based on the specified fields Hash. See self.build_criteria.

Returns a Hash. keys are SimpleDB object IDs and values are the object attributes.

# File lib/rudy/metadata.rb, line 81
def self.select(fields={})
  squery = Rudy::AWS::SDB.generate_select @@domain, fields
  Rudy::Huxtable.ld "SELECT: #{squery}" if Rudy.debug?
  @@rsdb.select squery
end

Public Instance Methods

==(other) click to toggle source

Compares the names between two Rudy::Metadata objects.

# File lib/rudy/metadata.rb, line 225
def ==(other)
  return false unless other === self.class
  self.name == other.name
end
descriptors(*additional) click to toggle source
# File lib/rudy/metadata.rb, line 201
def descriptors(*additional)
  criteria = {
    :region => @region,  :zone => @zone,
    :environment => @environment, :role => @role
  }
  additional.each do |att|
    criteria[att] = self.send(att)
  end
  ld "DESCRIPTORS: #{criteria.inspect} (#{additional})"
  criteria
end
destroy(force=false) click to toggle source
# File lib/rudy/metadata.rb, line 195
def destroy(force=false)
  raise UnknownObject, self.name unless self.exists?
  Rudy::Metadata.destroy self.name
  true
end
exists?() click to toggle source

Is there an object in SimpleDB where the key == self.name

# File lib/rudy/metadata.rb, line 231
def exists?
  !Rudy::Metadata.get(self.name).nil?
end
name(*other) click to toggle source
# File lib/rudy/metadata.rb, line 184
def name(*other)
  parts = [@rtype, @zone, @environment, @role, @position, *other].flatten
  parts.join Rudy::DELIM
end
refresh!() click to toggle source

Refresh the metadata object from SimpleDB. If the record doesn't exist it will raise an UnknownObject error

# File lib/rudy/metadata.rb, line 215
def refresh!
  raise UnknownObject, self.name unless self.exists?
  h = Rudy::Metadata.get self.name
  return false if h.nil? || h.empty?
  obj = self.from_hash(h)
  obj.postprocess
  obj
end
save(replace=false) click to toggle source
# File lib/rudy/metadata.rb, line 189
def save(replace=false)
  raise DuplicateRecord, self.name unless replace || !self.exists?
  Rudy::Metadata.put self.name, self.to_hash, replace
  true
end