Skip to content

Commit

Permalink
Reverting accidental push to master re: #204
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Leff <adam@leff.co>
  • Loading branch information
adamleff committed Nov 13, 2017
1 parent 639b1e7 commit cd5e3b2
Show file tree
Hide file tree
Showing 42 changed files with 1,043 additions and 1,557 deletions.
1 change: 0 additions & 1 deletion lib/train.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require 'train/options'
require 'train/plugins'
require 'train/errors'
require 'train/platforms'
require 'uri'

module Train
Expand Down
3 changes: 0 additions & 3 deletions lib/train/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,4 @@ class ClientError < ::StandardError; end
# Base exception class for all exceptions that are caused by other failures
# in the transport layer.
class TransportError < ::StandardError; end

# Exception for when no platform can be detected
class PlatformDetectionFailed < ::StandardError; end
end
1 change: 1 addition & 0 deletions lib/train/extras.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

module Train::Extras
require 'train/extras/command_wrapper'
require 'train/extras/os_common'
require 'train/extras/stat'

CommandResult = Struct.new(:stdout, :stderr, :exit_status)
Expand Down
60 changes: 60 additions & 0 deletions lib/train/extras/linux_lsb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# encoding: utf-8
# author: Dominik Richter
# author: Christoph Hartmann
#
# This is heavily based on:
#
# OHAI https://github.com/chef/ohai
# by Adam Jacob, Chef Software Inc
#

module Train::Extras
module LinuxLSB
def lsb_config(content)
{
id: content[/^DISTRIB_ID=["']?(.+?)["']?$/, 1],
release: content[/^DISTRIB_RELEASE=["']?(.+?)["']?$/, 1],
codename: content[/^DISTRIB_CODENAME=["']?(.+?)["']?$/, 1],
}
end

def lsb_release
raw = @backend.run_command('lsb_release -a').stdout
{
id: raw[/^Distributor ID:\s+(.+)$/, 1],
release: raw[/^Release:\s+(.+)$/, 1],
codename: raw[/^Codename:\s+(.+)$/, 1],
}
end

def lsb
return @lsb if defined?(@lsb)
@lsb = {}
if !(raw = get_config('/etc/lsb-release')).nil?
@lsb = lsb_config(raw)
elsif unix_file?('/usr/bin/lsb_release')
@lsb = lsb_release
end
@lsb
end

def detect_linux_via_lsb
return false if lsb[:id].nil?
id = lsb[:id].downcase
case id
when /redhat/
@platform[:family] = 'redhat'
when /amazon/
@platform[:family] = 'amazon'
when /scientificsl/
@platform[:family] = 'scientific'
when /xenserver/
@platform[:family] = 'xenserver'
else
@platform[:family] = id
end
@platform[:release] = lsb[:release]
true
end
end
end
151 changes: 151 additions & 0 deletions lib/train/extras/os_common.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# encoding: utf-8
# author: Dominik Richter
# author: Christoph Hartmann
#
# This is heavily based on:
#
# OHAI https://github.com/chef/ohai
# by Adam Jacob, Chef Software Inc
#

require 'train/extras/os_detect_darwin'
require 'train/extras/os_detect_linux'
require 'train/extras/os_detect_unix'
require 'train/extras/os_detect_windows'
require 'train/extras/os_detect_esx'
require 'train/extras/os_detect_arista_eos'
require 'train/extras/os_detect_openvms'

module Train::Extras
class OSCommon
include Train::Extras::DetectDarwin
include Train::Extras::DetectLinux
include Train::Extras::DetectUnix
include Train::Extras::DetectWindows
include Train::Extras::DetectEsx
include Train::Extras::DetectAristaEos
include Train::Extras::DetectOpenVMS

attr_accessor :backend
def initialize(backend, platform = nil)
@backend = backend
@platform = platform || {}
detect_family
end

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

def to_hash
@platform
end

OS = { # rubocop:disable Style/MutableConstant
'redhat' => REDHAT_FAMILY,
'debian' => DEBIAN_FAMILY,
'suse' => SUSE_FAMILY,
'fedora' => %w{fedora},
'bsd' => %w{
freebsd netbsd openbsd darwin
},
'solaris' => %w{
solaris smartos omnios openindiana opensolaris nexentacore
},
'windows' => %w{
windows
},
'aix' => %w{
aix
},
'hpux' => %w{
hpux
},
'esx' => %w{
esx
},
'darwin' => %w{
darwin
},
}

OS['linux'] = %w{linux alpine arch coreos exherbo gentoo slackware fedora amazon} + OS['redhat'] + OS['debian'] + OS['suse']

OS['unix'] = %w{unix aix hpux qnx} + OS['linux'] + OS['solaris'] + OS['bsd']

# Helper methods to check the OS type
# Provides methods in the form of: linux?, unix?, solaris? ...
OS.keys.each do |os_family|
define_method((os_family + '?').to_sym) do
OS[os_family].include?(@platform[:family])
end
end

private

def detect_family
# if some information is already defined, try to verify it
# with the remaining detection
unless @platform[:family].nil?
# return ok if the preconfigured family yielded a good result
return true if detect_family_type
# if not, reset the platform to presets and run the full detection
# TODO: print an error message in this case, as the instantiating
# backend is doing something wrong
@platform = {}
end

# TODO: extend base implementation for detecting the family type
# to Windows and others
case uname_s
when /unrecognized command verb/
@platform[:family] = 'openvms'
when /linux/i
@platform[:family] = 'linux'
when /./
@platform[:family] = 'unix'
else
# Don't know what this is
@platform[:family] = nil
end

# try to detect the platform if the platform is set to nil, otherwise this code will never work
return nil if @platform[:family].nil?
detect_family_type
end

def detect_family_type # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
pf = @platform[:family]

return detect_windows if pf == 'windows'
return detect_darwin if pf == 'darwin'
return detect_esx if pf == 'esx'
return detect_openvms if pf =='openvms'

if %w{freebsd netbsd openbsd aix solaris2 hpux}.include?(pf)
return detect_via_uname
end

# unix based systems combine the above
return true if pf == 'unix' and detect_darwin
return true if pf == 'unix' and detect_esx
# This is assuming that pf is set to unix, this should be if pf == 'linux'
return true if pf == 'unix' and detect_arista_eos
return true if pf == 'unix' and detect_via_uname

# if we arrive here, we most likey have a regular linux
detect_linux
end

def get_config(path)
res = @backend.run_command("test -f #{path} && cat #{path}")
# ignore files that can't be read
return nil if res.exit_status != 0
res.stdout
end

def unix_file?(path)
@backend.run_command("test -f #{path}").exit_status == 0
end
end
end
34 changes: 34 additions & 0 deletions lib/train/extras/os_detect_arista_eos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# encoding: utf-8
# author: Jere Julian
#
# Arista EOS has 2 modes. Most compliance tests will use the network CLI
# but when working with vagrant, its common to encounter the raw bash shell.
require 'json'

module Train::Extras
module DetectAristaEos
def detect_arista_eos
if unix_file?('/usr/bin/FastCli')
cmd = @backend.run_command('FastCli -p 15 -c "show version | json"')
@platform[:name] = 'arista_eos_bash'
family = 'fedora'
else
cmd = @backend.run_command('show version | json')
end

# in PTY mode, stderr is matched with stdout, therefore it may not be empty
output = cmd.stdout
if cmd.exit_status == 0 && !output.empty?
eos_ver = JSON.parse(output)
@platform[:name] = @platform[:name] || 'arista_eos'
family ||= 'arista_eos'
@platform[:family] = family
@platform[:release] = eos_ver['version']
@platform[:arch] = eos_ver['architecture']
true
else
false
end
end
end
end
40 changes: 40 additions & 0 deletions lib/train/extras/os_detect_darwin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# encoding: utf-8
# author: Dominik Richter
# author: Christoph Hartmann
#
# This is heavily based on:
#
# OHAI https://github.com/chef/ohai
# by Adam Jacob, Chef Software Inc
#
require 'train/extras/uname'

module Train::Extras
module DetectDarwin
include Train::Extras::Uname

def detect_darwin
cmd = @backend.run_command('/usr/bin/sw_vers')
# TODO: print an error in this step of the detection,
# as it shouldnt happen
return false if cmd.exit_status != 0
# TODO: ditto on error
return false if cmd.stdout.empty?

name = cmd.stdout[/^ProductName:\s+(.+)$/, 1]
# TODO: ditto on error
return false if name.nil?
@platform[:name] = name.downcase.chomp.tr(' ', '_')
@platform[:release] = cmd.stdout[/^ProductVersion:\s+(.+)$/, 1]
@platform[:build] = cmd.stdout[/^BuildVersion:\s+(.+)$/, 1]
# TODO: keep for now due to backwards compatibility with serverspec
@platform[:family] = 'darwin'
detect_darwin_arch
true
end

def detect_darwin_arch
@platform[:arch] = uname_m
end
end
end
22 changes: 22 additions & 0 deletions lib/train/extras/os_detect_esx.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# encoding: utf-8
# author: Dominik Richter
# author: Christoph Hartmann
#
# This is heavily based on:
#
# OHAI https://github.com/chef/ohai
# by Adam Jacob, Chef Software Inc
#

module Train::Extras
module DetectEsx
def detect_esx
if uname_s.downcase.chomp == 'vmkernel'
@platform[:family] = 'esx'
@platform[:name] = uname_s.lines[0].chomp
@platform[:release] = uname_r.lines[0].chomp
true
end
end
end
end
Loading

0 comments on commit cd5e3b2

Please sign in to comment.