Class: SlashPort::Component

Inherits:
Object
  • Object
show all
Defined in:
app/models/base/component.rb,
app/models/components/mysql.rb,
app/models/components/puppet.rb,
app/models/components/linuxhost.rb

Direct Known Subclasses

LinuxHost, LinuxProcess, Mysql, Puppet

Defined Under Namespace

Classes: LinuxHost, LinuxProcess, Mysql, Puppet

Constant Summary collapse

@@subclasses =
Array.new
@@components =
Array.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute(options = {}) ⇒ Object

class-level to easily map a attribute name to a method arguments:

:name => attribute name
:handler => method handler name
:doc => attribute documentation
:sort => [optional] array of keys for sort (used with var.text output)


110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/base/component.rb', line 110

def self.attribute(options = {})
  if options[:doc] == nil
    raise "Attribute #{self.name}/#{name} has no description"
  end
  name = options[:name]
  puts "#{self.name}: new attribute #{name}"
  options[:sort] ||= []

  # remember: this is a class-level instance attribute
  @attributes[options[:name]] = Attribute.new(options[:handler], options[:doc], options[:sort])
end

.attributes(filter = nil) ⇒ Object



137
138
139
# File 'app/models/base/component.rb', line 137

def self.attributes(filter=nil)
  return @attributes
end

.class_initializeObject

class-level initialization. This is called when ruby first creates this class object, a hack made possible by overriding Class#inherited (see ‘def inherited’ above).



144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/models/base/component.rb', line 144

def self.class_initialize
  #puts "#{self}::class_initialize"
  # remember, this is a class-level instance attribute
  @attributes = Registry.new
  @configs = Registry.new
  @label = self.name.split("::")[-1].downcase

  # disable this component by default
  #puts "Disabling component '#{@label}' (default action, you must enable it if you want to use it)"
  #disable
  enable
end

.componentsObject

Show me all subclasses of SlashPort::Component



158
159
160
161
162
163
164
165
166
167
# File 'app/models/base/component.rb', line 158

def self.components
  if @@components.length == 0
    @@subclasses.each do |klass|
      component = klass.new
      @@components << component
    end
  end

  return @@components
end

.config(name, handler, description = nil) ⇒ Object

class-level to easily map a variable name to a handler



123
124
125
126
127
128
129
130
131
# File 'app/models/base/component.rb', line 123

def self.config(name, handler, description=nil)
  if description == nil
    raise "Config #{self.name}/#{name} has no description"
  end
  #puts "#{self.name}: new config #{name}"

  # remember: this is a class-level instance variable
  @configs[name] = Variable.new(handler, description)
end

.configs(filter = nil) ⇒ Object

def self.config



133
134
135
# File 'app/models/base/component.rb', line 133

def self.configs(filter=nil)
  return @configs
end

.disableObject



193
194
195
# File 'app/models/base/component.rb', line 193

def self.disable
  @active = false
end

.enableObject



197
198
199
# File 'app/models/base/component.rb', line 197

def self.enable
  @active = true
end

.get_attributes(filter = nil) ⇒ Object



181
182
183
# File 'app/models/base/component.rb', line 181

def self.get_attributes(filter=nil)
  return self.get_things("attributes", filter)
end

.get_configs(filter = nil) ⇒ Object



185
186
187
# File 'app/models/base/component.rb', line 185

def self.get_configs(filter=nil)
  return self.get_things("configs", filter)
end

.get_things(thing, filter = nil) ⇒ Object

def self.components



169
170
171
172
173
174
175
176
177
178
179
# File 'app/models/base/component.rb', line 169

def self.get_things(thing, filter=nil)
  data = []
  self.components.each do |component|
    next unless component.class.is_enabled?
    result = component.send("get_#{thing}", filter)
    if result
      data += result
    end
  end
  return data
end

.inherited(subclass) ⇒ Object

See Class#inherited for what this method



95
96
97
98
99
100
101
102
# File 'app/models/base/component.rb', line 95

def self.inherited(subclass)
  puts "#{subclass.name} inherits #{self.name}"
  @@subclasses << subclass

  if subclass.respond_to?(:class_initialize)
    subclass.class_initialize
  end
end

.is_enabled?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'app/models/base/component.rb', line 201

def self.is_enabled?
  return @active
end

.labelObject



189
190
191
# File 'app/models/base/component.rb', line 189

def self.label
  return @label
end

Instance Method Details

#_want(pattern, *values) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/base/component.rb', line 24

def _want(pattern, *values)
  if pattern == nil
    return true
  end

  want = false
  values.each do |value|
    #puts "Checking #{pattern.class}/#{pattern} against #{value.class}/#{value.inspect}"
    if (pattern.is_a?(Regexp) and !!(value.to_s =~ pattern))
      #puts "Match! =~"
      want = true
      break
    elsif value == pattern
      #puts "Match! =="
      want = true
      break
    end
  end
  return want
end

#attributesObject



8
9
10
# File 'app/models/base/component.rb', line 8

def attributes
  return self.class.attributes
end

#configsObject



12
13
14
# File 'app/models/base/component.rb', line 12

def configs
  return self.class.configs
end

#get_attributes(filter = nil) ⇒ Object



16
17
18
# File 'app/models/base/component.rb', line 16

def get_attributes(filter=nil)
  get_things(attributes, filter)
end

#get_configs(filter = nil) ⇒ Object



20
21
22
# File 'app/models/base/component.rb', line 20

def get_configs(filter=nil)
  get_things(configs, filter)
end

#get_things(thing, filter = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/base/component.rb', line 45

def get_things(thing, filter=nil)
  return unless _want(filter["component"], self.class.label)

  data = []

  thing.each do |section, var|
    next unless _want(filter["section"], section)
    results = self.send(var.handler)
    next if results == nil
    results = [results] if !results.is_a?(Array)

    results.each do |result|
      result.labels["component"] = self.class.label
      result.labels["section"] = section
      result.labels["host"] = Socket.gethostname

      keep = true
      filter.each do |filterkey,filtervalue|
        want = _want(filtervalue, result.labels[filterkey],
                     result.data[filterkey])
        #puts "Filter: #{want} => #{filterkey} #{filtervalue}"

        if (!want)
          keep = false
          break
        end
      end

      if keep
        # if our filter includes a 'data' name, then we
        # should strip all nonmatching data entries.
        if (result.data.keys & filter.keys).length > 0
          result.data.each_key do |key|
            next if filter.has_key?(key)
            #puts "Removing #{key} data"
            result.data.delete(key)
          end
        end
        data << result
      end
    end
  end
  return data
end

#path(*names) ⇒ Object



90
91
92
# File 'app/models/base/component.rb', line 90

def path(*names)
  return [self.class.label, *names].join("/")
end