Class: Rubber::Configuration::Instance

Inherits:
Object
  • Object
show all
Includes:
Enumerable, MonitorMixin
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(instance_storage, opts = {}) ⇒ Instance

Returns a new instance of Instance.



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

def initialize(instance_storage, opts={})
  super()
  
  @instance_storage = instance_storage
  @opts = opts

  @items = {}
  @artifacts = {'volumes' => {}, 'static_ips' => {}, 'vpc' => {}}

  @filters = Rubber::Util::parse_aliases(ENV['FILTER'])
  @filters, @filters_negated = @filters.partition {|f| f !~ /^-/ }
  @filters_negated = @filters_negated.collect {|f| f[1..-1] }

  @filter_roles = Rubber::Util::parse_aliases(ENV['FILTER_ROLES'])
  @filter_roles, @filter_roles_negated = @filter_roles.partition {|f| f !~ /^-/ }
  @filter_roles_negated = @filter_roles_negated.collect {|f| f[1..-1] }

  load()
end

Instance Attribute Details

#artifactsObject (readonly)

Returns the value of attribute artifacts.



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

def artifacts
  @artifacts
end

#instance_storageObject (readonly)

Returns the value of attribute instance_storage.



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

def instance_storage
  @instance_storage
end

Instance Method Details

#[](name) ⇒ Object



133
134
135
# File 'lib/rubber/instance.rb', line 133

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

#add(instance_item) ⇒ Object



179
180
181
# File 'lib/rubber/instance.rb', line 179

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

#all_rolesObject



175
176
177
# File 'lib/rubber/instance.rb', line 175

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

#each(&block) ⇒ Object



187
188
189
# File 'lib/rubber/instance.rb', line 187

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

#filteredObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rubber/instance.rb', line 143

def filtered
  filtered_results = []

  validate_filters()

  if @filters.size == 0 && @filter_roles.size == 0
    filtered_results.concat(@items.values)
  else
    @items.values.each do |ic|
        filtered_results << ic if @filters.include?(ic.name)
        filtered_results << ic if ic.roles.any? {|r| @filter_roles.include?(r.name)}
    end
  end

  filtered_results.delete_if {|ic| @filters_negated.include?(ic.name) }
  filtered_results.delete_if {|ic| ic.roles.any? {|r| @filter_roles_negated.include?(r.name)} }

  return filtered_results
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



139
140
141
# File 'lib/rubber/instance.rb', line 139

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

#load(instance_storage = @instance_storage) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rubber/instance.rb', line 34

def load(instance_storage=@instance_storage)
  case instance_storage
    when /file:(.*)/
      location = $1
      File.open(location, 'r') {|f| load_from_file(f) } if File.exist?(location)
    when /storage:(.*)/
      location = $1
      bucket = location.split("/")[0]
      key = location.split("/")[1..-1].join("/")
      data = Rubber.cloud.storage(bucket).fetch(key)
      StringIO.open(data, 'r') {|f| load_from_file(f) } if data
    when /table:(.*)/
      location = $1
      load_from_table(location)
    else
      raise "Invalid instance_storage: #{instance_storage}\n" +
          "Must be one of file:, table:, storage:"
  end
end

#load_from_file(io) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubber/instance.rb', line 54

def load_from_file(io)
  item_list =  YAML.load(io.read)
  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

#load_from_table(table_key) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rubber/instance.rb', line 67

def load_from_table(table_key)
  Rubber.logger.debug{"Reading rubber instances from cloud table #{table_key}"}
  store = Rubber.cloud.table_store(table_key)
  items = store.find()
  items.each do |name, data|
    case name
      when '_artifacts_'
        @artifacts = data
      else
        ic = InstanceItem.from_hash(data.merge({'name' => name}))
        @items[ic.name] = ic 
    end
  end
end

#remove(name) ⇒ Object



183
184
185
# File 'lib/rubber/instance.rb', line 183

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

#save(instance_storage = @instance_storage, backup = ) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rubber/instance.rb', line 82

def save(instance_storage=@instance_storage, backup=@opts[:backup])
  synchronize do
    case instance_storage
      when /file:(.*)/
        location = $1
        File.open(location, 'w') {|f| save_to_file(f) }
      when /storage:(.*)/
        location = $1
        bucket = location.split("/")[0]
        key = location.split("/")[1..-1].join("/")
        data = StringIO.open {|f| save_to_file(f); f.string }
        Rubber.cloud.storage(bucket).store(key, data)
      when /table:(.*)/
        location = $1
        save_to_table(location)
      else
        raise "Invalid instance_storage: #{instance_storage}\n" +
            "Must be one of file:, table:, storage:"
    end
  end
  
  save(backup, false) if backup
end

#save_to_file(io) ⇒ Object



106
107
108
109
110
111
# File 'lib/rubber/instance.rb', line 106

def save_to_file(io)
  data = []
  data.push(*@items.values)
  data.push(@artifacts)
  io.write(YAML.dump(data))
end

#save_to_table(table_key) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rubber/instance.rb', line 113

def save_to_table(table_key)
  store = Rubber.cloud.table_store(table_key)
  
  # delete all before writing to handle removals
  store.find().each do |k, v|
    store.delete(k)
  end
  
  # only write out non-empty artifacts
  artifacts = @artifacts.select {|k, v| v.size > 0}
  if artifacts.size > 0
    store.put('_artifacts_', artifacts)
  end
  
  # write out all the instance data
  @items.values.each do |item|
    store.put(item.name, item.to_hash)
  end
end

#sizeObject



191
192
193
# File 'lib/rubber/instance.rb', line 191

def size
  @items.size
end

#validate_filtersObject



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rubber/instance.rb', line 163

def validate_filters
  aliases = @items.values.collect{|ic| ic.name}
  [@filters, @filters_negated].flatten.each do |f|
    raise "Filter doesn't match any hosts: #{f}" if ! aliases.include?(f)
  end

  roles = all_roles
  [@filter_roles, @filter_roles_negated].flatten.each do |f|
    raise "Filter doesn't match any roles: #{f}" if ! roles.include?(f)
  end
end