Skip to content

Commit

Permalink
Merge branch 'OHAI-412-plus'
Browse files Browse the repository at this point in the history
- Fixes OHAI-412
- Fixes rspec deprecations in rspec 2.14
  • Loading branch information
danielsdeleo committed Jul 15, 2013
2 parents 17aaf51 + b1169f4 commit 26de224
Show file tree
Hide file tree
Showing 82 changed files with 2,581 additions and 2,494 deletions.
2 changes: 1 addition & 1 deletion Gemfile
@@ -1,4 +1,4 @@
source :rubygems
source "https://rubygems.org"

gemspec

Expand Down
136 changes: 136 additions & 0 deletions lib/ohai/dsl/plugin.rb
@@ -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
48 changes: 48 additions & 0 deletions lib/ohai/mixin/seconds_to_human.rb
@@ -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


2 changes: 1 addition & 1 deletion lib/ohai/plugins/darwin/uptime.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/ohai/plugins/freebsd/uptime.rb
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions lib/ohai/plugins/linode.rb
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/ohai/plugins/linux/uptime.rb
Expand Up @@ -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)



2 changes: 1 addition & 1 deletion lib/ohai/plugins/netbsd/uptime.rb
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/ohai/plugins/openbsd/uptime.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/ohai/plugins/sigar/uptime.rb
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion lib/ohai/plugins/solaris2/uptime.rb
Expand Up @@ -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
Expand Down
23 changes: 0 additions & 23 deletions lib/ohai/plugins/uptime.rb
Expand Up @@ -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


2 changes: 1 addition & 1 deletion lib/ohai/plugins/windows/uptime.rb
Expand Up @@ -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)

0 comments on commit 26de224

Please sign in to comment.