Module Rudy::Metadata

  1. lib/rudy/metadata.rb

Included modules

  1. Rudy::Huxtable

Constants

COMMON_FIELDS = [:region, :zone, :environment, :role].freeze

Public class methods

build_criteria (rtype, fields={}, less=[])

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.

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

Creates instances of the following and stores to class variables:

[show source]
    # File lib/rudy/metadata.rb, line 29
29:     def self.connect(accesskey, secretkey, region, reconnect=false)
30:       return @@rsdb unless reconnect || @@rsdb.nil?
31:       @@rsdb  = Rudy::AWS::SDB.new accesskey, secretkey, region
32:       true
33:     end
create_domain (n)

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

[show source]
    # File lib/rudy/metadata.rb, line 44
44:     def self.create_domain(n)
45:       @@domain = n if @@rsdb.create_domain n
46:     end
destroy (n)
[show source]
    # File lib/rudy/metadata.rb, line 71
71:     def self.destroy(n)
72:       Rudy::Huxtable.ld "DESTROY: #{n}" if Rudy.debug?
73:       @@rsdb.destroy @@domain, n
74:     end
destroy_domain (n)

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

[show source]
    # File lib/rudy/metadata.rb, line 49
49:     def self.destroy_domain(n)
50:       Rudy::Huxtable.ld "DESTROY: #{n}" if Rudy.debug?
51:       @@rsdb.destroy_domain n
52:       @@domain = Rudy::DOMAIN
53:       true
54:     end
domain (name=nil)
[show source]
    # File lib/rudy/metadata.rb, line 34
34:     def self.domain(name=nil)
35:       return @@domain if name.nil?
36:       @@domain = name
37:     end
domain= (*args)

An alias for Rudy::Metadata.domain

[show source]
    # File lib/rudy/metadata.rb, line 39
39:     def self.domain=(*args)
40:       domain *args
41:     end
exists? (n)
[show source]
    # File lib/rudy/metadata.rb, line 62
62:     def self.exists?(n)
63:       !get(n).nil?
64:     end
get (n)

Get a record from SimpleDB with the key n

[show source]
    # File lib/rudy/metadata.rb, line 57
57:     def self.get(n) 
58:       Rudy::Huxtable.ld "GET: #{n}" if Rudy.debug?
59:       @@rsdb.get @@domain, n
60:     end
get_rclass (rtype)
[show source]
    # File lib/rudy/metadata.rb, line 13
13:     def self.get_rclass(rtype)
14:       case rtype
15:       when Rudy::Machines::RTYPE
16:         Rudy::Machines
17:       when Rudy::Disks::RTYPE
18:         Rudy::Disks
19:       when Rudy::Backups::RTYPE
20:         Rudy::Backups
21:       else
22:         raise UnknownRecordType, rtype
23:       end
24:     end
included (obj)
[show source]
     # File lib/rudy/metadata.rb, line 156
156:     def self.included(obj)
157:       obj.send :include, Rudy::Metadata::InstanceMethods
158:       
159:       # Add common storable fields. 
160:       [COMMON_FIELDS, :position].flatten.each do |n|
161:         obj.field n
162:       end
163:       
164:     end
new (rtype, opts={})
[show source]
     # File lib/rudy/metadata.rb, line 166
166:     def initialize(rtype, opts={})
167:       @rtype = rtype
168:       @position = position || @@global.position || '01'
169:       
170:       COMMON_FIELDS.each { |n|
171:         ld "SETTING: #{n}: #{@@global.send(n)}" if @@global.verbose > 3
172:         instance_variable_set("@#{n}", @@global.send(n))
173:       }
174:       
175:       opts.each_pair do |n,v|
176:         raise "Unknown attribute for #{self.class}: #{n}" if !self.has_field? n
177:         next if v.nil?
178:         ld "RESETTING: #{n}: #{v}" if @@global.verbose > 3
179:         self.send("#{n}=", v)
180:       end
181:       
182:     end
put (n, o, replace=false)
[show source]
    # File lib/rudy/metadata.rb, line 66
66:     def self.put(n, o, replace=false)
67:       Rudy::Huxtable.ld "PUT: #{n}" if Rudy.debug?
68:       @@rsdb.put @@domain, n, o, replace
69:     end
select (fields={})

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.

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

Public instance methods

== (other)

Compares the names between two Rudy::Metadata objects.

[show source]
     # File lib/rudy/metadata.rb, line 225
225:     def ==(other)
226:       return false unless other === self.class
227:       self.name == other.name
228:     end
descriptors (*additional)
[show source]
     # File lib/rudy/metadata.rb, line 201
201:     def descriptors(*additional)
202:       criteria = {
203:         :region => @region,  :zone => @zone,
204:         :environment => @environment, :role => @role
205:       }
206:       additional.each do |att|
207:         criteria[att] = self.send(att)
208:       end
209:       ld "DESCRIPTORS: #{criteria.inspect} (#{additional})"
210:       criteria
211:     end
destroy (force=false)
[show source]
     # File lib/rudy/metadata.rb, line 195
195:     def destroy(force=false)
196:       raise UnknownObject, self.name unless self.exists?
197:       Rudy::Metadata.destroy self.name
198:       true
199:     end
exists? ()

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

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

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

[show source]
     # File lib/rudy/metadata.rb, line 215
215:     def refresh!
216:       raise UnknownObject, self.name unless self.exists?
217:       h = Rudy::Metadata.get self.name
218:       return false if h.nil? || h.empty?
219:       obj = self.from_hash(h)
220:       obj.postprocess
221:       obj
222:     end
save (replace=false)
[show source]
     # File lib/rudy/metadata.rb, line 189
189:     def save(replace=false)
190:       raise DuplicateRecord, self.name unless replace || !self.exists?
191:       Rudy::Metadata.put self.name, self.to_hash, replace
192:       true
193:     end