Class: Ohai::System

Inherits:
Object
  • Object
show all
Includes:
Mixin::Command, Mixin::FromFile
Defined in:
lib/ohai/system.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::Command

popen4, run_command

Methods included from Mixin::FromFile

#from_file

Constructor Details

#initializeSystem

Returns a new instance of System.



34
35
36
37
38
39
# File 'lib/ohai/system.rb', line 34

def initialize
  @data = Mash.new
  @seen_plugins = Hash.new
  @providers = Mash.new
  @plugin_path = ""
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



215
216
217
218
219
# File 'lib/ohai/system.rb', line 215

def method_missing(name, *args)
  return get_attribute(name) if args.length == 0 
  
  set_attribute(name, *args)
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



29
30
31
# File 'lib/ohai/system.rb', line 29

def data
  @data
end

#seen_pluginsObject

Returns the value of attribute seen_plugins.



29
30
31
# File 'lib/ohai/system.rb', line 29

def seen_plugins
  @seen_plugins
end

Class Method Details

.json_create(o) ⇒ Object

Create an Ohai::System from JSON



207
208
209
210
211
212
213
# File 'lib/ohai/system.rb', line 207

def self.json_create(o)
  ohai = new
  o.each do |key, value|
    ohai.data[key] = value unless key == "json_class"
  end
  ohai
end

Instance Method Details

#[](key) ⇒ Object



41
42
43
# File 'lib/ohai/system.rb', line 41

def [](key)
  @data[key]
end

#[]=(key, value) ⇒ Object



45
46
47
# File 'lib/ohai/system.rb', line 45

def []=(key, value)
  @data[key] = value
end

#all_pluginsObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ohai/system.rb', line 105

def all_plugins
  require_plugin('os')
  
  Ohai::Config[:plugin_path].each do |path|
    [ 
      Dir[File.join(path, '*')], 
      Dir[File.join(path, @data[:os], '**', '*')] 
    ].flatten.each do |file|
      file_regex = Regexp.new("#{path}#{File::SEPARATOR}(.+).rb$")
      md = file_regex.match(file)
      if md
        plugin_name = md[1].gsub(File::SEPARATOR, "::")
        require_plugin(plugin_name) unless @seen_plugins.has_key?(plugin_name)
      end
    end
  end
end

#attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/ohai/system.rb', line 55

def attribute?(name)
  @data.has_key?(name) 
end

#attributes_print(a) ⇒ Object



203
204
205
# File 'lib/ohai/system.rb', line 203

def attributes_print(a)
  JSON.pretty_generate(@data[a])
end

#collect_providers(providers) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ohai/system.rb', line 123

def collect_providers(providers)
  refreshments = []
  if providers.is_a?(Mash)
    providers.keys.each do |provider|
      if provider.eql?("_providers")
        refreshments << providers[provider]
      else
        refreshments << collect_providers(providers[provider])
      end
    end
  else
    refreshments << providers
  end
  refreshments.flatten.uniq
end

#each(&block) ⇒ Object



49
50
51
52
53
# File 'lib/ohai/system.rb', line 49

def each(&block)
  @data.each do |key, value|
    block.call(key, value)
  end
end

#from(cmd) ⇒ Object



63
64
65
66
67
# File 'lib/ohai/system.rb', line 63

def from(cmd)
  status, stdout, stderr = run_command(:command => cmd)
  return "" if stdout.nil?
  stdout.chomp!.strip
end

#from_with_regex(cmd, *regex_list) ⇒ Object

Set the value equal to the stdout of the command, plus run through a regex - the first piece of match data is the value.



86
87
88
89
90
91
92
93
94
# File 'lib/ohai/system.rb', line 86

def from_with_regex(cmd, *regex_list)
  regex_list.flatten.each do |regex|
    status, stdout, stderr = run_command(:command => cmd)
    return "" if stdout.nil?
    stdout.chomp!.strip
    md = stdout.match(regex)
    return md[1]
  end
end

#get_attribute(name) ⇒ Object



101
102
103
# File 'lib/ohai/system.rb', line 101

def get_attribute(name)
  @data[name]
end

#json_pretty_printObject

Pretty Print this object as JSON



199
200
201
# File 'lib/ohai/system.rb', line 199

def json_pretty_print
  JSON.pretty_generate(@data)
end

#provides(*paths) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ohai/system.rb', line 69

def provides(*paths)
  paths.each do |path|
    parts = path.split('/')
    h = @providers
    unless parts.length == 0
      parts.shift if parts[0].length == 0
      parts.each do |part|
        h[part] ||= Mash.new
        h = h[part]
      end
    end
    h[:_providers] ||= []
    h[:_providers] << @plugin_path
  end
end

#refresh_plugins(path = '/') ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/ohai/system.rb', line 139

def refresh_plugins(path = '/')
  parts = path.split('/')
  if parts.length == 0
    h = @providers
  else
    parts.shift if parts[0].length == 0
    h = @providers
    parts.each do |part|
      break unless h.has_key?(part)
      h = h[part]
    end
  end

  refreshments = collect_providers(h)
  Ohai::Log.debug("Refreshing plugins: #{refreshments.join(", ")}")
  
  refreshments.each do |r|
    @seen_plugins.delete(r) if @seen_plugins.has_key?(r)
  end
  refreshments.each do |r|
    require_plugin(r) unless @seen_plugins.has_key?(r)        
  end
end

#require_plugin(plugin_name, force = false) ⇒ Object Also known as: _require_plugin



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/ohai/system.rb', line 163

def require_plugin(plugin_name, force=false)
  unless force
    return true if @seen_plugins[plugin_name]
  end
  
  @plugin_path = plugin_name
  
  filename = "#{plugin_name.gsub("::", File::SEPARATOR)}.rb"
        
  Ohai::Config[:plugin_path].each do |path|
    check_path = File.expand_path(File.join(path, filename))
    begin
      @seen_plugins[plugin_name] = true
      Ohai::Log.debug("Loading plugin #{plugin_name}")
      from_file(check_path)
      return true
    rescue IOError => e
      Ohai::Log.debug("No #{plugin_name} at #{check_path}")
    rescue Exception,Errno::ENOENT => e
      Ohai::Log.debug("Plugin #{plugin_name} threw exception #{e.inspect}")
    end
  end
end

#set(name, *value) ⇒ Object



59
60
61
# File 'lib/ohai/system.rb', line 59

def set(name, *value)
  set_attribute(name, *value)
end

#set_attribute(name, *value) ⇒ Object



96
97
98
99
# File 'lib/ohai/system.rb', line 96

def set_attribute(name, *value)
  @data[name] = *value
  @data[name]
end

#to_json(*a) ⇒ Object

Serialize this object as a hash



192
193
194
195
196
# File 'lib/ohai/system.rb', line 192

def to_json(*a)
  output = @data.clone
  output["json_class"] = self.class.name
  output.to_json(*a)
end