Class: Rubber::Configuration::Instance

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rubber/instance.rb

Overview

Contains the ec2 instance configuration defined in instance.yml

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Instance

Returns a new instance of Instance.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rubber/instance.rb', line 12

def initialize(file)
  Rubber.logger.debug{"Reading rubber instances from #{file}"}
  @file = file
  @items = {}
  @artifacts = {'volumes' => {}, 'static_ips' => {}}
  if ENV['FILTER']
    @filters = ENV['FILTER'].split(/\s*,\s*/)
  end

  if File.exist?(@file)
    item_list = File.open(@file) { |f| YAML.load(f) }
    if item_list
      item_list.each do |i|
        if i.is_a? InstanceItem
          @items[i.name] = i
        elsif i.is_a? Hash
          @artifacts.merge!(i)
        end
      end
    end
  end
end

Instance Attribute Details

#artifactsObject (readonly)

Returns the value of attribute artifacts.



9
10
11
# File 'lib/rubber/instance.rb', line 9

def artifacts
  @artifacts
end

#fileObject (readonly)

Returns the value of attribute file.



9
10
11
# File 'lib/rubber/instance.rb', line 9

def file
  @file
end

Instance Method Details

#[](name) ⇒ Object



42
43
44
# File 'lib/rubber/instance.rb', line 42

def [](name)
  @items[name] || @items[name.gsub(/\..*/, '')]
end

#add(instance_item) ⇒ Object



60
61
62
# File 'lib/rubber/instance.rb', line 60

def add(instance_item)
  @items[instance_item.name] = instance_item
end

#all_rolesObject



56
57
58
# File 'lib/rubber/instance.rb', line 56

def all_roles()
  @items.collect {|n, i| i.role_names}.flatten.uniq
end

#each(&block) ⇒ Object



68
69
70
# File 'lib/rubber/instance.rb', line 68

def each(&block)
  @items.values.each &block
end

#filteredObject



52
53
54
# File 'lib/rubber/instance.rb', line 52

def filtered()
  @items.values.find_all {|ic| ! @filters || @filters.include?(ic.name)}
end

#for_role(role_name, options = nil) ⇒ Object

gets the instances for the given role. If options is nil, all roles match, otherwise the role has to have options that match exactly



48
49
50
# File 'lib/rubber/instance.rb', line 48

def for_role(role_name, options=nil)
  @items.values.find_all {|ic| ic.roles.any? {|r| r.name == role_name && (! options || r.options == options)}}
end

#remove(name) ⇒ Object



64
65
66
# File 'lib/rubber/instance.rb', line 64

def remove(name)
  @items.delete(name)
end

#saveObject



35
36
37
38
39
40
# File 'lib/rubber/instance.rb', line 35

def save()
  data = []
  data.push(*@items.values)
  data.push(@artifacts)
  File.open(@file, "w") { |f| f.write(YAML.dump(data)) }
end

#sizeObject



72
73
74
# File 'lib/rubber/instance.rb', line 72

def size
  @items.size
end