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.



33
34
35
36
# File 'lib/ohai/system.rb', line 33

def initialize
  @data = Mash.new
  @seen_plugins = Hash.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



152
153
154
155
156
# File 'lib/ohai/system.rb', line 152

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.



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

def data
  @data
end

#seen_pluginsObject

Returns the value of attribute seen_plugins.



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

def seen_plugins
  @seen_plugins
end

Class Method Details

.json_create(o) ⇒ Object

Create an Ohai::System from JSON



144
145
146
147
148
149
150
# File 'lib/ohai/system.rb', line 144

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



38
39
40
# File 'lib/ohai/system.rb', line 38

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

#[]=(key, value) ⇒ Object



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

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

#all_pluginsObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ohai/system.rb', line 87

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)


52
53
54
55
# File 'lib/ohai/system.rb', line 52

def attribute?(name)
  Ohai::Log.debug("Attribute #{name} is #{@data.has_key?(name)}")
  @data.has_key?(name) 
end

#each(&block) ⇒ Object



46
47
48
49
50
# File 'lib/ohai/system.rb', line 46

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

#from(cmd) ⇒ Object



61
62
63
64
# File 'lib/ohai/system.rb', line 61

def from(cmd)
  status, stdout, stderr = run_command(:command => cmd)
  stdout.chomp!
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.



67
68
69
70
71
72
73
74
# File 'lib/ohai/system.rb', line 67

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

#get_attribute(name) ⇒ Object



82
83
84
85
# File 'lib/ohai/system.rb', line 82

def get_attribute(name)
  Ohai::Log.debug("Getting attribute #{name}, value #{@data[name].inspect}")
  @data[name]
end

#json_pretty_printObject

Pretty Print this object as JSON



139
140
141
# File 'lib/ohai/system.rb', line 139

def json_pretty_print
  JSON.pretty_generate(@data)
end

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



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

def require_plugin(plugin_name, force=false)
  unless force
    return true if @seen_plugins[plugin_name]
  end
  
  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.info("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



57
58
59
# File 'lib/ohai/system.rb', line 57

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

#set_attribute(name, *value) ⇒ Object



76
77
78
79
80
# File 'lib/ohai/system.rb', line 76

def set_attribute(name, *value)
  Ohai::Log.debug("Setting attribute #{name} to #{value.inspect}")
  @data[name] = *value
  @data[name]
end

#to_json(*a) ⇒ Object

Serialize this object as a hash



132
133
134
135
136
# File 'lib/ohai/system.rb', line 132

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