Permalink
Browse files

[OHAI-412] Split Ohai::System responsibilites into 2 classes

  • Loading branch information...
1 parent d338698 commit 894c847c190c4bdfd64710d3ed09b0d795205d2d @danielsdeleo danielsdeleo committed Oct 24, 2012
Showing with 2,049 additions and 1,967 deletions.
  1. +136 −0 lib/ohai/dsl/plugin.rb
  2. +48 −0 lib/ohai/mixin/seconds_to_human.rb
  3. +2 −2 lib/ohai/plugins/linux/uptime.rb
  4. +0 −23 lib/ohai/plugins/uptime.rb
  5. +27 −115 lib/ohai/system.rb
  6. 0 spec/data/plugins/foo.rb
  7. +76 −0 spec/ohai/dsl/plugin_spec.rb
  8. +15 −12 spec/spec_helper.rb
  9. +0 −53 spec/unit/mixin/from_file_spec.rb
  10. +89 −85 spec/unit/plugins/c_spec.rb
  11. +2 −2 spec/unit/plugins/chef_spec.rb
  12. +6 −5 spec/unit/plugins/darwin/hostname_spec.rb
  13. +16 −16 spec/unit/plugins/darwin/kernel_spec.rb
  14. +361 −361 spec/unit/plugins/darwin/network_spec.rb
  15. +24 −24 spec/unit/plugins/darwin/platform_spec.rb
  16. +7 −6 spec/unit/plugins/dmi_spec.rb
  17. +14 −14 spec/unit/plugins/erlang_spec.rb
  18. +17 −11 spec/unit/plugins/eucalyptus_spec.rb
  19. +5 −4 spec/unit/plugins/freebsd/hostname_spec.rb
  20. +10 −8 spec/unit/plugins/freebsd/kernel_spec.rb
  21. +9 −8 spec/unit/plugins/freebsd/platform_spec.rb
  22. +11 −10 spec/unit/plugins/groovy_spec.rb
  23. +8 −7 spec/unit/plugins/hostname_spec.rb
  24. +38 −36 spec/unit/plugins/java_spec.rb
  25. +11 −9 spec/unit/plugins/kernel_spec.rb
  26. +40 −39 spec/unit/plugins/linux/cpu_spec.rb
  27. +58 −57 spec/unit/plugins/linux/filesystem_spec.rb
  28. +9 −8 spec/unit/plugins/linux/hostname_spec.rb
  29. +6 −4 spec/unit/plugins/linux/kernel_spec.rb
  30. +31 −30 spec/unit/plugins/linux/lsb_spec.rb
  31. +193 −192 spec/unit/plugins/linux/network_spec.rb
  32. +162 −173 spec/unit/plugins/linux/platform_spec.rb
  33. +13 −22 spec/unit/plugins/linux/uptime_spec.rb
  34. +68 −67 spec/unit/plugins/linux/virtualization_spec.rb
  35. +11 −10 spec/unit/plugins/lua_spec.rb
  36. +11 −10 spec/unit/plugins/mono_spec.rb
  37. +5 −4 spec/unit/plugins/netbsd/hostname_spec.rb
  38. +9 −7 spec/unit/plugins/netbsd/kernel_spec.rb
  39. +9 −8 spec/unit/plugins/netbsd/platform_spec.rb
  40. +189 −214 spec/unit/plugins/network_spec.rb
  41. +4 −3 spec/unit/plugins/ohai_spec.rb
  42. +7 −6 spec/unit/plugins/ohai_time_spec.rb
  43. +6 −5 spec/unit/plugins/openbsd/hostname_spec.rb
  44. +10 −8 spec/unit/plugins/openbsd/kernel_spec.rb
  45. +10 −9 spec/unit/plugins/openbsd/platform_spec.rb
  46. +16 −15 spec/unit/plugins/os_spec.rb
  47. +12 −11 spec/unit/plugins/passwd_spec.rb
  48. +17 −16 spec/unit/plugins/perl_spec.rb
  49. +11 −10 spec/unit/plugins/php_spec.rb
  50. +25 −24 spec/unit/plugins/platform_spec.rb
  51. +11 −10 spec/unit/plugins/python_spec.rb
  52. +78 −38 spec/unit/plugins/rackspace_spec.rb
  53. +4 −3 spec/unit/plugins/ruby_spec.rb
  54. +9 −8 spec/unit/plugins/sigar/network_route_spec.rb
  55. +7 −6 spec/unit/plugins/solaris2/hostname_spec.rb
  56. +12 −13 spec/unit/plugins/solaris2/kernel_spec.rb
  57. +16 −14 spec/unit/plugins/solaris2/network_spec.rb
  58. +12 −12 spec/unit/plugins/solaris2/platform_spec.rb
  59. +28 −27 spec/unit/plugins/solaris2/virtualization_spec.rb
  60. +8 −73 spec/unit/system_spec.rb
View
@@ -0,0 +1,136 @@
+require 'ohai/mixin/command'
+require 'ohai/mixin/from_file'
+require 'ohai/mixin/seconds_to_human'
+
+module Ohai
+ module DSL
+ class Plugin
+
+ include Ohai::Mixin::Command
+ include Ohai::Mixin::FromFile
+ include Ohai::Mixin::SecondsToHuman
+
+ attr_reader :file
+ attr_reader :data
+
+ def initialize(controller, file)
+ @controller = controller
+ @data = controller.data
+ @providers = controller.providers
+ @file = file
+ end
+
+ def run
+ from_file(@file)
+ end
+
+ def require_plugin(*args)
+ @controller.require_plugin(*args)
+ end
+
+ def hints
+ @controller.hints
+ end
+
+ def [](key)
+ @data[key]
+ end
+
+ def []=(key, value)
+ @data[key] = value
+ end
+
+ def each(&block)
+ @data.each do |key, value|
+ block.call(key, value)
+ end
+ end
+
+ def attribute?(name)
+ @data.has_key?(name)
+ end
+
+ def set(name, *value)
+ set_attribute(name, *value)
+ end
+
+ def from(cmd)
+ status, stdout, stderr = run_command(:command => cmd)
+ return "" if stdout.nil? || stdout.empty?
+ stdout.strip
+ end
+
+ 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
+
+ # Set the value equal to the stdout of the command, plus run through a regex - the first piece of match data is the value.
+ 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.empty?
+ stdout.chomp!.strip
+ md = stdout.match(regex)
+ return md[1]
+ end
+ end
+
+ def set_attribute(name, *values)
+ @data[name] = Array18(*values)
+ @data[name]
+ end
+
+ def get_attribute(name)
+ @data[name]
+ end
+
+ def hint?(name)
+ @json_parser ||= Yajl::Parser.new
+
+ return hints[name] if hints[name]
+
+ Ohai::Config[:hints_path].each do |path|
+ filename = File.join(path, "#{name}.json")
+ if File.exist?(filename)
+ begin
+ hash = @json_parser.parse(File.read(filename))
+ hints[name] = hash || Hash.new # hint should exist because the file did, even if it didn't contain anything
+ rescue Yajl::ParseError => e
+ Ohai::Log.error("Could not parse hint file at #{filename}: #{e.message}")
+ end
+ end
+ end
+
+ hints[name]
+ end
+
+ def method_missing(name, *args)
+ return get_attribute(name) if args.length == 0
+
+ set_attribute(name, *args)
+ end
+
+ private
+
+ def Array18(*args)
+ return nil if args.empty?
+ return args.first if args.length == 1
+ return *args
+ end
+
+
+ end
+ end
+end
@@ -0,0 +1,48 @@
+#
+# Author:: Adam Jacob (<adam@opscode.com>)
+# Copyright:: Copyright (c) 2008 Opscode, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+module Ohai
+ module Mixin
+ module SecondsToHuman
+ def seconds_to_human(seconds)
+ days = seconds.to_i / 86400
+ seconds -= 86400 * days
+
+ hours = seconds.to_i / 3600
+ seconds -= 3600 * hours
+
+ minutes = seconds.to_i / 60
+ seconds -= 60 * minutes
+
+ if days > 1
+ return sprintf("%d days %02d hours %02d minutes %02d seconds", days, hours, minutes, seconds)
+ elsif days == 1
+ return sprintf("%d day %02d hours %02d minutes %02d seconds", days, hours, minutes, seconds)
+ elsif hours > 0
+ return sprintf("%d hours %02d minutes %02d seconds", hours, minutes, seconds)
+ elsif minutes > 0
+ return sprintf("%d minutes %02d seconds", minutes, seconds)
+ else
+ return sprintf("%02d seconds", seconds)
+ end
+ end
+ end
+ end
+end
+
+
@@ -20,9 +20,9 @@
uptime, idletime = File.open("/proc/uptime").gets.split(" ")
uptime_seconds uptime.to_i
-uptime self._seconds_to_human(uptime.to_i)
+uptime seconds_to_human(uptime.to_i)
idletime_seconds idletime.to_i
-idletime self._seconds_to_human(idletime.to_i)
+idletime seconds_to_human(idletime.to_i)
View
@@ -16,27 +16,4 @@
# limitations under the License.
#
-def _seconds_to_human(seconds)
- days = seconds.to_i / 86400
- seconds -= 86400 * days
-
- hours = seconds.to_i / 3600
- seconds -= 3600 * hours
-
- minutes = seconds.to_i / 60
- seconds -= 60 * minutes
-
- if days > 1
- return sprintf("%d days %02d hours %02d minutes %02d seconds", days, hours, minutes, seconds)
- elsif days == 1
- return sprintf("%d day %02d hours %02d minutes %02d seconds", days, hours, minutes, seconds)
- elsif hours > 0
- return sprintf("%d hours %02d minutes %02d seconds", hours, minutes, seconds)
- elsif minutes > 0
- return sprintf("%d minutes %02d seconds", minutes, seconds)
- else
- return sprintf("%02d seconds", seconds)
- end
-end
-
Oops, something went wrong.

0 comments on commit 894c847

Please sign in to comment.