Permalink
Browse files

Merge branch 'OHAI-412-plus'

- Fixes OHAI-412
- Fixes rspec deprecations in rspec 2.14
  • Loading branch information...
2 parents 17aaf51 + b1169f4 commit 26de224f733ae7593e5e41435b188d718e831f57 @danielsdeleo danielsdeleo committed Jul 15, 2013
Showing with 2,581 additions and 2,494 deletions.
  1. +1 −1 Gemfile
  2. +136 −0 lib/ohai/dsl/plugin.rb
  3. +48 −0 lib/ohai/mixin/seconds_to_human.rb
  4. +1 −1 lib/ohai/plugins/darwin/uptime.rb
  5. +1 −1 lib/ohai/plugins/freebsd/uptime.rb
  6. +7 −3 lib/ohai/plugins/linode.rb
  7. +2 −2 lib/ohai/plugins/linux/uptime.rb
  8. +1 −1 lib/ohai/plugins/netbsd/uptime.rb
  9. +1 −1 lib/ohai/plugins/openbsd/uptime.rb
  10. +1 −1 lib/ohai/plugins/sigar/uptime.rb
  11. +1 −1 lib/ohai/plugins/solaris2/uptime.rb
  12. +0 −23 lib/ohai/plugins/uptime.rb
  13. +1 −1 lib/ohai/plugins/windows/uptime.rb
  14. +27 −112 lib/ohai/system.rb
  15. +2 −1 ohai.gemspec
  16. 0 spec/data/plugins/foo.rb
  17. +76 −0 spec/ohai/dsl/plugin_spec.rb
  18. +15 −12 spec/spec_helper.rb
  19. +9 −9 spec/unit/mixin/ec2_metadata_spec.rb
  20. +0 −53 spec/unit/mixin/from_file_spec.rb
  21. +20 −20 spec/unit/plugins/azure_spec.rb
  22. +89 −85 spec/unit/plugins/c_spec.rb
  23. +4 −4 spec/unit/plugins/chef_spec.rb
  24. +81 −80 spec/unit/plugins/cloud_spec.rb
  25. +7 −8 spec/unit/plugins/darwin/cpu_spec.rb
  26. +6 −5 spec/unit/plugins/darwin/hostname_spec.rb
  27. +18 −16 spec/unit/plugins/darwin/kernel_spec.rb
  28. +361 −361 spec/unit/plugins/darwin/network_spec.rb
  29. +32 −32 spec/unit/plugins/darwin/platform_spec.rb
  30. +5 −6 spec/unit/plugins/darwin/system_profiler_spec.rb
  31. +9 −8 spec/unit/plugins/dmi_spec.rb
  32. +71 −58 spec/unit/plugins/ec2_spec.rb
  33. +14 −14 spec/unit/plugins/erlang_spec.rb
  34. +38 −32 spec/unit/plugins/eucalyptus_spec.rb
  35. +5 −4 spec/unit/plugins/freebsd/hostname_spec.rb
  36. +10 −8 spec/unit/plugins/freebsd/kernel_spec.rb
  37. +9 −8 spec/unit/plugins/freebsd/platform_spec.rb
  38. +25 −24 spec/unit/plugins/freebsd/virtualization_spec.rb
  39. +42 −34 spec/unit/plugins/gce_spec.rb
  40. +11 −10 spec/unit/plugins/groovy_spec.rb
  41. +8 −7 spec/unit/plugins/hostname_spec.rb
  42. +38 −36 spec/unit/plugins/java_spec.rb
  43. +11 −9 spec/unit/plugins/kernel_spec.rb
  44. +32 −28 spec/unit/plugins/linode_spec.rb
  45. +43 −42 spec/unit/plugins/linux/cpu_spec.rb
  46. +86 −85 spec/unit/plugins/linux/filesystem_spec.rb
  47. +9 −8 spec/unit/plugins/linux/hostname_spec.rb
  48. +7 −4 spec/unit/plugins/linux/kernel_spec.rb
  49. +44 −43 spec/unit/plugins/linux/lsb_spec.rb
  50. +198 −197 spec/unit/plugins/linux/network_spec.rb
  51. +193 −204 spec/unit/plugins/linux/platform_spec.rb
  52. +15 −24 spec/unit/plugins/linux/uptime_spec.rb
  53. +91 −90 spec/unit/plugins/linux/virtualization_spec.rb
  54. +11 −10 spec/unit/plugins/lua_spec.rb
  55. +11 −10 spec/unit/plugins/mono_spec.rb
  56. +5 −4 spec/unit/plugins/netbsd/hostname_spec.rb
  57. +9 −7 spec/unit/plugins/netbsd/kernel_spec.rb
  58. +9 −8 spec/unit/plugins/netbsd/platform_spec.rb
  59. +205 −231 spec/unit/plugins/network_spec.rb
  60. +11 −10 spec/unit/plugins/nodejs_spec.rb
  61. +4 −3 spec/unit/plugins/ohai_spec.rb
  62. +9 −8 spec/unit/plugins/ohai_time_spec.rb
  63. +6 −5 spec/unit/plugins/openbsd/hostname_spec.rb
  64. +10 −8 spec/unit/plugins/openbsd/kernel_spec.rb
  65. +10 −9 spec/unit/plugins/openbsd/platform_spec.rb
  66. +16 −15 spec/unit/plugins/os_spec.rb
  67. +13 −12 spec/unit/plugins/passwd_spec.rb
  68. +17 −16 spec/unit/plugins/perl_spec.rb
  69. +11 −10 spec/unit/plugins/php_spec.rb
  70. +25 −24 spec/unit/plugins/platform_spec.rb
  71. +11 −10 spec/unit/plugins/python_spec.rb
  72. +58 −52 spec/unit/plugins/rackspace_spec.rb
  73. +25 −23 spec/unit/plugins/root_group_spec.rb
  74. +15 −14 spec/unit/plugins/ruby_spec.rb
  75. +11 −10 spec/unit/plugins/sigar/network_route_spec.rb
  76. +8 −7 spec/unit/plugins/solaris2/hostname_spec.rb
  77. +12 −13 spec/unit/plugins/solaris2/kernel_spec.rb
  78. +18 −16 spec/unit/plugins/solaris2/network_spec.rb
  79. +16 −16 spec/unit/plugins/solaris2/platform_spec.rb
  80. +40 −39 spec/unit/plugins/solaris2/virtualization_spec.rb
  81. +13 −12 spec/unit/plugins/ssh_host_keys_spec.rb
  82. +10 −75 spec/unit/system_spec.rb
View
@@ -1,4 +1,4 @@
-source :rubygems
+source "https://rubygems.org"
gemspec
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] << @file
+ 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
+
+
@@ -25,7 +25,7 @@
stdout.each do |line|
if line =~ /kern.boottime:\D+(\d+)/
uptime_seconds Time.new.to_i - $1.to_i
- uptime self._seconds_to_human(uptime_seconds)
+ uptime seconds_to_human(uptime_seconds)
end
end
end
@@ -25,7 +25,7 @@
stdout.each do |line|
if line =~ /kern.boottime:\D+(\d+)/
uptime_seconds Time.new.to_i - $1.to_i
- uptime self._seconds_to_human(uptime_seconds)
+ uptime seconds_to_human(uptime_seconds)
end
end
end
View
@@ -23,7 +23,9 @@
#
# Returns true or false
def has_linode_kernel?
- kernel[:release].split('-').last =~ /linode/
+ if kernel_data = kernel
+ kernel_data[:release].split('-').last =~ /linode/
+ end
end
# Identifies the linode cloud by preferring the hint, then
@@ -40,8 +42,10 @@ def looks_like_linode?
#
# Alters linode mash with new interface based on name parameter
def get_ip_address(name, eth)
- network[:interfaces][eth][:addresses].each do |key, info|
- linode[name] = key if info['family'] == 'inet'
+ if eth_iface = network[:interfaces][eth]
+ eth_iface[:addresses].each do |key, info|
+ linode[name] = key if info['family'] == 'inet'
+ 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)
@@ -25,7 +25,7 @@
stdout.each do |line|
if line =~ /kern.boottime:\D+(\d+)/
uptime_seconds Time.new.to_i - $1.to_i
- uptime self._seconds_to_human(uptime_seconds)
+ uptime seconds_to_human(uptime_seconds)
end
end
end
@@ -25,7 +25,7 @@
stdout.each do |line|
if line =~ /kern.boottime=(.+)/
uptime_seconds Time.new.to_i - Time.parse($1).to_i
- uptime self._seconds_to_human(uptime_seconds)
+ uptime seconds_to_human(uptime_seconds)
end
end
end
@@ -24,4 +24,4 @@
uptime = sigar.uptime.uptime
uptime_seconds uptime.to_i * 1000
-uptime self._seconds_to_human(uptime.to_i)
+uptime seconds_to_human(uptime.to_i)
@@ -29,7 +29,7 @@
stdout.each do |line|
if line =~ /.* boot (.+)/
uptime_seconds Time.now.to_i - DateTime.parse($1).strftime('%s').to_i
- uptime self._seconds_to_human(uptime_seconds)
+ uptime seconds_to_human(uptime_seconds)
break
end
end
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
-
@@ -20,4 +20,4 @@
provides "uptime", "uptime_seconds"
uptime_seconds ::WMI::Win32_PerfFormattedData_PerfOS_System.find(:first).SystemUpTime.to_i
-uptime self._seconds_to_human(uptime_seconds)
+uptime seconds_to_human(uptime_seconds)
Oops, something went wrong.

0 comments on commit 26de224

Please sign in to comment.