Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Language plugins: Improve failure logging, update specs, general cleanup #805

Merged
merged 4 commits into from
Apr 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/ohai/plugins/c.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,26 @@

def collect(cmd, &block)
so = shell_out(cmd)
yield(so) if so.exitstatus == 0
if so.exitstatus == 0
yield(so)
else
Ohai::Log.debug("Plugin C '#{cmd}' failed. Skipping data.")
end
rescue Ohai::Exceptions::Exec
# ignore
Ohai::Log.debug("Plugin C '#{cmd}' binary could not be found. Skipping data.")
end

collect_data do
c = Mash.new

#gcc
collect("gcc -v") do |so|
# Sample output:
# Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
# Apple LLVM version 7.3.0 (clang-703.0.29)
# Target: x86_64-apple-darwin15.4.0
# Thread model: posix
# InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
description = so.stderr.split($/).last
output = description.split
if output.length >= 3
Expand Down Expand Up @@ -112,6 +122,6 @@ def collect(cmd, &block)
end
end

languages[:c] = c if c.keys.length > 0
languages[:c] = c unless c.empty?
end
end
8 changes: 3 additions & 5 deletions lib/ohai/plugins/elixir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
# Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
#
# Elixir 1.2.4
if so.exitstatus == 0
if so.exitstatus == 0 && so.stdout =~ /^Elixir (\S*)/
elixir = Mash.new
if so.stdout =~ /^Elixir (\S*)/
elixir[:version] = $1
languages[:elixir] = elixir
end
elixir[:version] = $1
languages[:elixir] = elixir
end
rescue Ohai::Exceptions::Exec
Ohai::Log.debug('Elixir plugin: Could not shell_out "elixir -v". Skipping plugin')
Expand Down
18 changes: 11 additions & 7 deletions lib/ohai/plugins/go.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
depends "languages"

collect_data do
output = nil
go = Mash.new
so = shell_out("go version")
if so.exitstatus == 0
output = so.stdout.split
go[:version] = output[2].slice!(2..16)
languages[:go] = go if go[:version]
begin
so = shell_out("go version")
# Sample output:
# go version go1.6.1 darwin/amd64
if so.exitstatus == 0 && so.stdout =~ /go(\S+)/
go = Mash.new
go[:version] = $1
languages[:go] = go
end
rescue Ohai::Exceptions::Exec
Ohai::Log.debug('Go plugin: Could not shell_out "go version". Skipping plugin')
end
end
end
22 changes: 11 additions & 11 deletions lib/ohai/plugins/groovy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@

Ohai.plugin(:Groovy) do
provides "languages/groovy"

depends "languages"

collect_data do
output = nil

groovy = Mash.new

so = shell_out("groovy -v")
if so.exitstatus == 0
output = so.stdout.split
if output.length >= 2
groovy[:version] = output[2]
begin
so = shell_out("groovy -v")
# Sample output:
# Groovy Version: 2.4.6 JVM: 1.8.0_60 Vendor: Oracle Corporation OS: Mac OS X
if so.exitstatus == 0 && so.stdout =~ /Groovy Version: (\S+).*JVM: (\S+)/
groovy = Mash.new
groovy[:version] = $1
groovy[:jvm] = $2
languages[:groovy] = groovy
end
languages[:groovy] = groovy if groovy[:version]
rescue Ohai::Exceptions::Exec
Ohai::Log.debug('Groovy plugin: Could not shell_out "groovy -v". Skipping plugin')
end
end
end
34 changes: 21 additions & 13 deletions lib/ohai/plugins/java.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,29 @@
depends "languages"

def get_java_info
java = Mash.new
so = shell_out("java -mx64m -version")
if so.exitstatus == 0
so.stderr.split(/\r?\n/).each do |line|
case line
when /(?:java|openjdk) version \"([0-9\.\_]+)\"/
java[:version] = $1
when /^(.+Runtime Environment.*) \((build)\s*(.+)\)$/
java[:runtime] = { "name" => $1, "build" => $3 }
when /^(.+ (Client|Server) VM) \(build\s*(.+)\)$/
java[:hotspot] = { "name" => $1, "build" => $3 }
begin
so = shell_out("java -mx64m -version")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this pattern

so = shell_out(...)
if so.exitstatus == 0
  do stuff
end
languages ...

seems ripe for extracting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed

# Sample output:
# java version "1.8.0_60"
# Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
# Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
if so.exitstatus == 0
java = Mash.new
so.stderr.split(/\r?\n/).each do |line|
case line
when /(?:java|openjdk) version \"([0-9\.\_]+)\"/
java[:version] = $1
when /^(.+Runtime Environment.*) \((build)\s*(.+)\)$/
java[:runtime] = { "name" => $1, "build" => $3 }
when /^(.+ (Client|Server) VM) \(build\s*(.+)\)$/
java[:hotspot] = { "name" => $1, "build" => $3 }
end
end
end

languages[:java] = java if java[:version]
languages[:java] = java unless java.empty?
end
rescue Ohai::Exceptions::Exec
Ohai::Log.debug('Java plugin: Could not shell_out "java -mx64m -version". Skipping plugin')
end
end

Expand Down
21 changes: 10 additions & 11 deletions lib/ohai/plugins/lua.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@

Ohai.plugin(:Lua) do
provides "languages/lua"

depends "languages"

collect_data do
output = nil

lua = Mash.new

so = shell_out("lua -v")
if so.exitstatus == 0
output = so.stderr.split
if output.length >= 1
lua[:version] = output[1]
begin
so = shell_out("lua -v")
# Sample output:
# Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
if so.exitstatus == 0
lua = Mash.new
lua[:version] = so.stderr.split[1]
languages[:lua] = lua if lua[:version]
end
languages[:lua] = lua if lua[:version]
rescue Ohai::Exceptions::Exec
Ohai::Log.debug('Lua plugin: Could not shell_out "lua -v". Skipping plugin')
end
end
end
4 changes: 2 additions & 2 deletions lib/ohai/plugins/mono.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
depends "languages"

collect_data do

begin
so = shell_out("mono -V")
# Sample output:
# Mono JIT compiler version 4.2.3 (Stable 4.2.3.4/832de4b Wed Mar 30 13:57:48 PDT 2016)
# Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
# TLS: normal
Expand All @@ -33,7 +34,6 @@
# Misc: softdebug
# LLVM: supported, not enabled.
# GC: sgen
so = shell_out("mono -V")
if so.exitstatus == 0
mono = Mash.new
output = so.stdout.split
Expand Down
24 changes: 13 additions & 11 deletions lib/ohai/plugins/nodejs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,23 @@

Ohai.plugin(:Nodejs) do
provides "languages/nodejs"

depends "languages"

collect_data do
output = nil

nodejs = Mash.new

so = shell_out("node -v")
if so.exitstatus == 0
output = so.stdout.split
if output.length >= 1
nodejs[:version] = output[0][1..output[0].length]
begin
so = shell_out("node -v")
# Sample output:
# v5.10.1
if so.exitstatus == 0
nodejs = Mash.new
output = so.stdout.split
if output.length >= 1
nodejs[:version] = output[0][1..output[0].length]
end
languages[:nodejs] = nodejs if nodejs[:version]
end
languages[:nodejs] = nodejs if nodejs[:version]
rescue Ohai::Exceptions::Exec
Ohai::Log.debug('Nodejs plugin: Could not shell_out "node -v". Skipping plugin')
end
end
end
31 changes: 17 additions & 14 deletions lib/ohai/plugins/perl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,28 @@

Ohai.plugin(:Perl) do
provides "languages/perl"

depends "languages"

collect_data do
output = nil

perl = Mash.new
so = shell_out("perl -V:version -V:archname")
if so.exitstatus == 0
so.stdout.split(/\r?\n/).each do |line|
case line
when /^version=\'(.+)\';$/
perl[:version] = $1
when /^archname=\'(.+)\';$/
perl[:archname] = $1
begin
so = shell_out("perl -V:version -V:archname")
# Sample output:
# version='5.18.2';
# archname='darwin-thread-multi-2level';
if so.exitstatus == 0
perl = Mash.new
so.stdout.split(/\r?\n/).each do |line|
case line
when /^version=\'(.+)\';$/
perl[:version] = $1
when /^archname=\'(.+)\';$/
perl[:archname] = $1
end
end
languages[:perl] = perl unless perl.empty?
end
languages[:perl] = perl
rescue Ohai::Exceptions::Exec
Ohai::Log.debug('Perl plugin: Could not shell_out "perl -V:version -V:archname". Skipping plugin')
end

end
end
38 changes: 22 additions & 16 deletions lib/ohai/plugins/php.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,33 @@

Ohai.plugin(:PHP) do
provides "languages/php"

depends "languages"

collect_data do
php = Mash.new

so = shell_out("php -v")
if so.exitstatus == 0
so.stdout.each_line do |line|
case line
when /PHP (\S+).+built: ([^)]+)/
php[:version] = $1
php[:builddate] = $2
when /Zend Engine v([^\s]+),/
php[:zend_engine_version] = $1
when /Zend OPcache v([^\s]+),/
php[:zend_opcache_version] = $1
begin
so = shell_out("php -v")
# Sample output:
# PHP 5.5.31 (cli) (built: Feb 20 2016 20:33:10)
# Copyright (c) 1997-2015 The PHP Group
# Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
if so.exitstatus == 0
php = Mash.new
so.stdout.each_line do |line|
case line
when /PHP (\S+).+built: ([^)]+)/
php[:version] = $1
php[:builddate] = $2
when /Zend Engine v([^\s]+),/
php[:zend_engine_version] = $1
when /Zend OPcache v([^\s]+),/
php[:zend_opcache_version] = $1
end
end
end

languages[:php] = php if php[:version]
languages[:php] = php unless php.empty?
end
rescue Ohai::Exceptions::Exec
Ohai::Log.debug('Php plugin: Could not shell_out "php -v". Skipping plugin')
end
end
end
56 changes: 30 additions & 26 deletions lib/ohai/plugins/powershell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,38 @@
depends "languages"

collect_data(:windows) do
powershell = Mash.new
so = shell_out("powershell.exe -NoLogo -NonInteractive -NoProfile -command $PSVersionTable")
# Sample output:
#
# Name Value
# ---- -----
# PSVersion 4.0
# WSManStackVersion 3.0
# SerializationVersion 1.1.0.1
# CLRVersion 4.0.30319.34014
# BuildVersion 6.3.9600.16394
# PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
# PSRemotingProtocolVersion 2.2
begin
so = shell_out("powershell.exe -NoLogo -NonInteractive -NoProfile -command $PSVersionTable")
# Sample output:
#
# Name Value
# ---- -----
# PSVersion 4.0
# WSManStackVersion 3.0
# SerializationVersion 1.1.0.1
# CLRVersion 4.0.30319.34014
# BuildVersion 6.3.9600.16394
# PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
# PSRemotingProtocolVersion 2.2

if so.exitstatus == 0
version_info = {}
so.stdout.strip.each_line do |line|
kv = line.strip.split(/\s+/, 2)
version_info[kv[0]] = kv[1] if kv.length == 2
if so.exitstatus == 0
powershell = Mash.new
version_info = {}
so.stdout.strip.each_line do |line|
kv = line.strip.split(/\s+/, 2)
version_info[kv[0]] = kv[1] if kv.length == 2
end
powershell[:version] = version_info["PSVersion"]
powershell[:ws_man_stack_version] = version_info["WSManStackVersion"]
powershell[:serialization_version] = version_info["SerializationVersion"]
powershell[:clr_version] = version_info["CLRVersion"]
powershell[:build_version] = version_info["BuildVersion"]
powershell[:compatible_versions] = parse_compatible_versions(version_info["PSCompatibleVersions"])
powershell[:remoting_protocol_version] = version_info["PSRemotingProtocolVersion"]
languages[:powershell] = powershell unless powershell.empty?
end
powershell[:version] = version_info["PSVersion"]
powershell[:ws_man_stack_version] = version_info["WSManStackVersion"]
powershell[:serialization_version] = version_info["SerializationVersion"]
powershell[:clr_version] = version_info["CLRVersion"]
powershell[:build_version] = version_info["BuildVersion"]
powershell[:compatible_versions] = parse_compatible_versions(version_info["PSCompatibleVersions"])
powershell[:remoting_protocol_version] = version_info["PSRemotingProtocolVersion"]
languages[:powershell] = powershell if powershell[:version]
rescue Ohai::Exceptions::Exec
Ohai::Log.debug('Powershell plugin: Could not shell_out "powershell.exe -NoLogo -NonInteractive -NoProfile -command $PSVersionTable". Skipping plugin')
end
end

Expand Down
Loading