From 7cc026725351165531e388c978a0ba55e5e8f477 Mon Sep 17 00:00:00 2001 From: Jared Quick Date: Tue, 31 Oct 2017 17:31:11 -0400 Subject: [PATCH] style fixes and wrapped os specifications in a class Signed-off-by: Jared Quick --- lib/train.rb | 3 - lib/train/platforms.rb | 1 + lib/train/platforms/common.rb | 2 +- lib/train/platforms/detect.rb | 1 + lib/train/platforms/detect/os_common.rb | 10 +- lib/train/platforms/detect/os_linux.rb | 31 +- lib/train/platforms/detect/scanner.rb | 1 - lib/train/platforms/specifications/os.rb | 863 ++++++++++++----------- lib/train/transports/docker.rb | 1 + lib/train/transports/local.rb | 1 + lib/train/transports/mock.rb | 1 + lib/train/transports/ssh_connection.rb | 1 + lib/train/transports/winrm_connection.rb | 1 + 13 files changed, 471 insertions(+), 446 deletions(-) diff --git a/lib/train.rb b/lib/train.rb index 529138ae..c85e90cf 100644 --- a/lib/train.rb +++ b/lib/train.rb @@ -17,9 +17,6 @@ module Train # @param [Array] *args list of arguments for the plugin # @return [Transport] instance of the new transport or nil def self.create(name, *args) - # require built in detect platforms at this level so any manual ones will be at the top - require 'train/platforms/detect' - cls = load_transport(name) cls.new(*args) unless cls.nil? end diff --git a/lib/train/platforms.rb b/lib/train/platforms.rb index 4fc7f797..513179b5 100644 --- a/lib/train/platforms.rb +++ b/lib/train/platforms.rb @@ -3,6 +3,7 @@ require 'train/platforms/common' require 'train/platforms/family' require 'train/platform' +require 'train/platforms/detect' module Train::Platforms class << self diff --git a/lib/train/platforms/common.rb b/lib/train/platforms/common.rb index 15e18a53..2cf2ba53 100644 --- a/lib/train/platforms/common.rb +++ b/lib/train/platforms/common.rb @@ -6,7 +6,7 @@ module Common # if it does not exist and add a child relationship. def in_family(family) if self.class == Train::Platforms::Family && @name == family - fail "Sorry you can not add a family inside itself '#{@name}.in_family(#{family})'" + fail "Unable to add family #{@name} to itself: '#{@name}.in_family(#{family})'" end # add family to the family list diff --git a/lib/train/platforms/detect.rb b/lib/train/platforms/detect.rb index 02aada92..f7cdd31a 100644 --- a/lib/train/platforms/detect.rb +++ b/lib/train/platforms/detect.rb @@ -1,5 +1,6 @@ # encoding: utf-8 +require 'train/platforms/specifications/os' require 'train/platforms/detect/scanner' module Train::Platforms diff --git a/lib/train/platforms/detect/os_common.rb b/lib/train/platforms/detect/os_common.rb index 78936aec..c9d38667 100644 --- a/lib/train/platforms/detect/os_common.rb +++ b/lib/train/platforms/detect/os_common.rb @@ -9,7 +9,7 @@ module OSCommon include Train::Platforms::Detect::Linux include Train::Platforms::Detect::Windows - def rbconfig(regex) + def ruby_host_os(regex) ::RbConfig::CONFIG['host_os'] =~ regex end @@ -32,7 +32,7 @@ def unix_file_exist?(path) @backend.run_command("test -f #{path}").exit_status.zero? end - def uname_call(cmd) + def command_output(cmd) res = @backend.run_command(cmd).stdout res.strip! unless res.nil? res @@ -40,17 +40,17 @@ def uname_call(cmd) def unix_uname_s return @uname[:s] if @uname.key?(:s) - @uname[:s] = uname_call('uname -s') + @uname[:s] = command_output('uname -s') end def unix_uname_r return @uname[:r] if @uname.key?(:r) - @uname[:r] = uname_call('uname -r') + @uname[:r] = command_output('uname -r') end def unix_uname_m return @uname[:m] if @uname.key?(:m) - @uname[:m] = uname_call('uname -m') + @uname[:m] = command_output('uname -m') end end end diff --git a/lib/train/platforms/detect/os_linux.rb b/lib/train/platforms/detect/os_linux.rb index dec83188..89246dac 100644 --- a/lib/train/platforms/detect/os_linux.rb +++ b/lib/train/platforms/detect/os_linux.rb @@ -3,13 +3,18 @@ module Train::Platforms::Detect module Linux def redhatish_platform(conf) - conf[/^red hat/i] ? 'redhat' : conf[/(\w+)/i, 1].downcase + conf =~ /^red hat/i ? 'redhat' : /(\w+)/i.match(conf)[1].downcase end def redhatish_version(conf) - return conf[/((\d+) \(Rawhide\))/i, 1].downcase if conf[/rawhide/i] - return conf[/Linux ((\d+|\.)+)/i, 1] if conf[/derived from .*linux/i] - conf[/release ([\d\.]+)/, 1] + case conf + when /rawhide/i + /((\d+) \(Rawhide\))/i.match(conf)[1].downcase + when /derived from .*linux/i + /Linux ((\d+|\.)+)/i.match(conf)[1] + else + /release ([\d\.]+)/.match(conf)[1] + end end def linux_os_release @@ -37,18 +42,24 @@ def parse_os_release_info(raw) end def lsb_config(content) + id = /^DISTRIB_ID=["']?(.+?)["']?$/.match(content) + release = /^DISTRIB_RELEASE=["']?(.+?)["']?$/.match(content) + codename = /^DISTRIB_CODENAME=["']?(.+?)["']?$/.match(content) { - id: content[/^DISTRIB_ID=["']?(.+?)["']?$/, 1], - release: content[/^DISTRIB_RELEASE=["']?(.+?)["']?$/, 1], - codename: content[/^DISTRIB_CODENAME=["']?(.+?)["']?$/, 1], + id: id.nil? ? nil : id[1], + release: release.nil? ? nil : release[1], + codename: codename.nil? ? nil : codename[1], } end def lsb_release(content) + id = /^Distributor ID:\s+(.+)$/.match(content) + release = /^Release:\s+(.+)$/.match(content) + codename = /^Codename:\s+(.+)$/.match(content) { - id: content[/^Distributor ID:\s+(.+)$/, 1], - release: content[/^Release:\s+(.+)$/, 1], - codename: content[/^Codename:\s+(.+)$/, 1], + id: id.nil? ? nil : id[1], + release: release.nil? ? nil : release[1], + codename: codename.nil? ? nil : codename[1], } end diff --git a/lib/train/platforms/detect/scanner.rb b/lib/train/platforms/detect/scanner.rb index 92f68a76..081ad33a 100644 --- a/lib/train/platforms/detect/scanner.rb +++ b/lib/train/platforms/detect/scanner.rb @@ -1,6 +1,5 @@ # encoding: utf-8 -require 'train/platforms/specifications/os' require 'train/platforms/detect/os_common' module Train::Platforms::Detect diff --git a/lib/train/platforms/specifications/os.rb b/lib/train/platforms/specifications/os.rb index 9e231aeb..220855b8 100644 --- a/lib/train/platforms/specifications/os.rb +++ b/lib/train/platforms/specifications/os.rb @@ -1,456 +1,467 @@ # encoding: utf-8 # rubocop:disable Style/Next +# rubocop:disable Metrics/AbcSize +# rubocop:disable Metrics/CyclomaticComplexity +# rubocop:disable Metrics/ClassLength +# rubocop:disable Metrics/MethodLength +# rubocop:disable Metrics/PerceivedComplexity -plat = Train::Platforms +module Train::Platforms::Specifications + class OS + def self.load_specifications + plat = Train::Platforms -plat.family('windows') - .detect { - if winrm? || (@backend.local? && rbconfig(/mswin|mingw32|windows/)) - true - end - } -# windows platform -plat.name('windows').in_family('windows') - .detect { - true if detect_windows == true - } + plat.family('windows') + .detect { + if winrm? || (@backend.local? && ruby_host_os(/mswin|mingw32|windows/)) + true + end + } + # windows platform + plat.name('windows').in_family('windows') + .detect { + true if detect_windows == true + } -# unix master family -plat.family('unix') - .detect { - if unix_uname_s =~ /./ - @platform[:arch] = unix_uname_m - true - end - } + # unix master family + plat.family('unix') + .detect { + if unix_uname_s =~ /./ + @platform[:arch] = unix_uname_m + true + end + } -# linux master family -plat.family('linux').in_family('unix') - .detect { - true if unix_uname_s =~ /linux/i - } + # linux master family + plat.family('linux').in_family('unix') + .detect { + true if unix_uname_s =~ /linux/i + } -# debian family -plat.family('debian').in_family('linux') - .detect { - true unless unix_file_contents('/etc/debian_version').nil? - } -plat.name('ubuntu').title('Ubuntu Linux').in_family('debian') - .detect { - lsb = read_linux_lsb - if lsb && lsb[:id] =~ /ubuntu/i - @platform[:release] = lsb[:release] - true - end - } -plat.name('linuxmint').title('LinuxMint').in_family('debian') - .detect { - lsb = read_linux_lsb - if lsb && lsb[:id] =~ /linuxmint/i - @platform[:release] = lsb[:release] - true - end - } -plat.name('raspbian').title('Raspbian Linux').in_family('debian') - .detect { - if unix_file_exist?('/usr/bin/raspi-config') - @platform[:release] = unix_file_contents('/etc/debian_version').chomp - true - end - } -plat.name('debian').title('Debian Linux').in_family('debian') - .detect { - lsb = read_linux_lsb - if lsb && lsb[:id] =~ /debian/i - @platform[:release] = lsb[:release] - true - end + # debian family + plat.family('debian').in_family('linux') + .detect { + true unless unix_file_contents('/etc/debian_version').nil? + } + plat.name('ubuntu').title('Ubuntu Linux').in_family('debian') + .detect { + lsb = read_linux_lsb + if lsb && lsb[:id] =~ /ubuntu/i + @platform[:release] = lsb[:release] + true + end + } + plat.name('linuxmint').title('LinuxMint').in_family('debian') + .detect { + lsb = read_linux_lsb + if lsb && lsb[:id] =~ /linuxmint/i + @platform[:release] = lsb[:release] + true + end + } + plat.name('raspbian').title('Raspbian Linux').in_family('debian') + .detect { + if unix_file_exist?('/usr/bin/raspi-config') + @platform[:release] = unix_file_contents('/etc/debian_version').chomp + true + end + } + plat.name('debian').title('Debian Linux').in_family('debian') + .detect { + lsb = read_linux_lsb + if lsb && lsb[:id] =~ /debian/i + @platform[:release] = lsb[:release] + true + end - # if we get this far we have to be some type of debian - true - } + # if we get this far we have to be some type of debian + true + } -# fedora family -plat.family('fedora').in_family('linux') - .detect { - true if linux_os_release && linux_os_release['NAME'] =~ /fedora/i - } -plat.name('fedora').title('Fedora').in_family('fedora') - .detect { - @platform[:release] = linux_os_release['VERSION_ID'] - true - } + # fedora family + plat.family('fedora').in_family('linux') + .detect { + true if linux_os_release && linux_os_release['NAME'] =~ /fedora/i + } + plat.name('fedora').title('Fedora').in_family('fedora') + .detect { + @platform[:release] = linux_os_release['VERSION_ID'] + true + } -# redhat family -plat.family('redhat').in_family('linux') - .detect { - # I am not sure this returns true for all redhats in this family - # for now we are going to just try each platform - # return true unless unix_file_contents('/etc/redhat-release').nil? + # redhat family + plat.family('redhat').in_family('linux') + .detect { + # I am not sure this returns true for all redhats in this family + # for now we are going to just try each platform + # return true unless unix_file_contents('/etc/redhat-release').nil? - true - } -plat.name('centos').title('Centos Linux').in_family('redhat') - .detect { - lsb = read_linux_lsb - if lsb && lsb[:id] =~ /centos/i - @platform[:release] = lsb[:release] - true - elsif unix_file_contents('/etc/os-release') =~ /centos/i - @platform[:release] = redhatish_version(unix_file_contents('/etc/redhat-release')) - true - end - } -# keep redhat after centos as a fallback for redhat base -plat.name('redhat').title('Red Hat Enterplat.ise Linux').in_family('redhat') - .detect { - lsb = read_linux_lsb - if lsb && lsb[:id] =~ /redhat/i - @platform[:release] = lsb[:release] - true - elsif !(raw = unix_file_contents('/etc/redhat-release')).nil? - # must be some type of redhat - @platform[:name] = redhatish_platform(raw) - @platform[:release] = redhatish_version(raw) - true - end - } -plat.name('oracle').title('Oracle Linux').in_family('redhat') - .detect { - if !(raw = unix_file_contents('/etc/oracle-release')).nil? - @platform[:release] = redhatish_version(raw) - true - elsif !(raw = unix_file_contents('/etc/enterprise-release')).nil? - @platform[:release] = redhatish_version(raw) - true - end - } -plat.name('scientific').title('Scientific Linux').in_family('redhat') - .detect { - lsb = read_linux_lsb - if lsb && lsb[:id] =~ /scientificsl/i - @platform[:release] = lsb[:release] - true - end - } -plat.name('xenserver').title('Xenserer Linux').in_family('redhat') - .detect { - lsb = read_linux_lsb - if lsb && lsb[:id] =~ /xenserver/i - @platform[:release] = lsb[:release] - true - end - } -plat.name('parallels-release').title('Parallels Linux').in_family('redhat') - .detect { - if !(raw = unix_file_contents('/etc/parallels-release')).nil? - @platform[:name] = redhatish_platform(raw) - @platform[:release] = raw[/(\d\.\d\.\d)/, 1] - true - end - } -plat.name('wrlinux').title('Wind River Linux').in_family('redhat') - .detect { - if linux_os_release && linux_os_release['ID_LIKE'] =~ /wrlinux/i - @platform[:release] = linux_os_release['VERSION'] - true - end - } -plat.name('amazon').title('Amazon Linux').in_family('redhat') - .detect { - lsb = read_linux_lsb - if lsb && lsb[:id] =~ /amazon/i - @platform[:release] = lsb[:release] - true - elsif !(raw = unix_file_contents('/etc/system-release')).nil? - @platform[:name] = redhatish_platform(raw) - @platform[:release] = redhatish_version(raw) - true - end - } + true + } + plat.name('centos').title('Centos Linux').in_family('redhat') + .detect { + lsb = read_linux_lsb + if lsb && lsb[:id] =~ /centos/i + @platform[:release] = lsb[:release] + true + elsif unix_file_contents('/etc/os-release') =~ /centos/i + @platform[:release] = redhatish_version(unix_file_contents('/etc/redhat-release')) + true + end + } + # keep redhat after centos as a fallback for redhat base + plat.name('redhat').title('Red Hat Enterplat.ise Linux').in_family('redhat') + .detect { + lsb = read_linux_lsb + if lsb && lsb[:id] =~ /redhat/i + @platform[:release] = lsb[:release] + true + elsif !(raw = unix_file_contents('/etc/redhat-release')).nil? + # must be some type of redhat + @platform[:name] = redhatish_platform(raw) + @platform[:release] = redhatish_version(raw) + true + end + } + plat.name('oracle').title('Oracle Linux').in_family('redhat') + .detect { + if !(raw = unix_file_contents('/etc/oracle-release')).nil? + @platform[:release] = redhatish_version(raw) + true + elsif !(raw = unix_file_contents('/etc/enterprise-release')).nil? + @platform[:release] = redhatish_version(raw) + true + end + } + plat.name('scientific').title('Scientific Linux').in_family('redhat') + .detect { + lsb = read_linux_lsb + if lsb && lsb[:id] =~ /scientificsl/i + @platform[:release] = lsb[:release] + true + end + } + plat.name('xenserver').title('Xenserer Linux').in_family('redhat') + .detect { + lsb = read_linux_lsb + if lsb && lsb[:id] =~ /xenserver/i + @platform[:release] = lsb[:release] + true + end + } + plat.name('parallels-release').title('Parallels Linux').in_family('redhat') + .detect { + if !(raw = unix_file_contents('/etc/parallels-release')).nil? + @platform[:name] = redhatish_platform(raw) + @platform[:release] = raw[/(\d\.\d\.\d)/, 1] + true + end + } + plat.name('wrlinux').title('Wind River Linux').in_family('redhat') + .detect { + if linux_os_release && linux_os_release['ID_LIKE'] =~ /wrlinux/i + @platform[:release] = linux_os_release['VERSION'] + true + end + } + plat.name('amazon').title('Amazon Linux').in_family('redhat') + .detect { + lsb = read_linux_lsb + if lsb && lsb[:id] =~ /amazon/i + @platform[:release] = lsb[:release] + true + elsif !(raw = unix_file_contents('/etc/system-release')).nil? + @platform[:name] = redhatish_platform(raw) + @platform[:release] = redhatish_version(raw) + true + end + } -# suse family -plat.family('suse').in_family('linux') - .detect { - if !(suse = unix_file_contents('/etc/SuSE-release')).nil? - version = suse.scan(/VERSION = (\d+)\nPATCHLEVEL = (\d+)/).flatten.join('.') - version = suse[/VERSION = ([\d\.]{2,})/, 1] if version == '' - @platform[:release] = version - true - end - } -plat.name('opensuse').title('OpenSUSE Linux').in_family('suse') - .detect { - true if unix_file_contents('/etc/SuSE-release') =~ /^openSUSE/ - } -plat.name('suse').title('Suse Linux').in_family('suse') - .detect { - true if unix_file_contents('/etc/SuSE-release') =~ /suse/ - } + # suse family + plat.family('suse').in_family('linux') + .detect { + if !(suse = unix_file_contents('/etc/SuSE-release')).nil? + version = suse.scan(/VERSION = (\d+)\nPATCHLEVEL = (\d+)/).flatten.join('.') + version = suse[/VERSION = ([\d\.]{2,})/, 1] if version == '' + @platform[:release] = version + true + end + } + plat.name('opensuse').title('OpenSUSE Linux').in_family('suse') + .detect { + true if unix_file_contents('/etc/SuSE-release') =~ /^openSUSE/ + } + plat.name('suse').title('Suse Linux').in_family('suse') + .detect { + true if unix_file_contents('/etc/SuSE-release') =~ /suse/ + } -# arch -plat.name('arch').title('Arch Linux').in_family('linux') - .detect { - if !unix_file_contents('/etc/arch-release').nil? - # Because this is a rolling release distribution, - # use the kernel release, ex. 4.1.6-1-ARCH - @platform[:release] = unix_uname_r - true - end - } + # arch + plat.name('arch').title('Arch Linux').in_family('linux') + .detect { + if !unix_file_contents('/etc/arch-release').nil? + # Because this is a rolling release distribution, + # use the kernel release, ex. 4.1.6-1-ARCH + @platform[:release] = unix_uname_r + true + end + } -# slackware -plat.name('slackware').title('Slackware Linux').in_family('linux') - .detect { - if !(raw = unix_file_contents('/etc/slackware-version')).nil? - @platform[:release] = raw.scan(/(\d+|\.+)/).join - true - end - } + # slackware + plat.name('slackware').title('Slackware Linux').in_family('linux') + .detect { + if !(raw = unix_file_contents('/etc/slackware-version')).nil? + @platform[:release] = raw.scan(/(\d+|\.+)/).join + true + end + } -# gentoo -plat.name('gentoo').title('Gentoo Linux').in_family('linux') - .detect { - if !(raw = unix_file_contents('/etc/gentoo-release')).nil? - @platform[:release] = raw.scan(/(\d+|\.+)/).join - true - end - } + # gentoo + plat.name('gentoo').title('Gentoo Linux').in_family('linux') + .detect { + if !(raw = unix_file_contents('/etc/gentoo-release')).nil? + @platform[:release] = raw.scan(/(\d+|\.+)/).join + true + end + } -# exherbo -plat.name('exherbo').title('Exherbo Linux').in_family('linux') - .detect { - unless unix_file_contents('/etc/exherbo-release').nil? - # Because this is a rolling release distribution, - # use the kernel release, ex. 4.1.6 - @platform[:release] = unix_uname_r - true - end - } + # exherbo + plat.name('exherbo').title('Exherbo Linux').in_family('linux') + .detect { + unless unix_file_contents('/etc/exherbo-release').nil? + # Because this is a rolling release distribution, + # use the kernel release, ex. 4.1.6 + @platform[:release] = unix_uname_r + true + end + } -# alpine -plat.name('alpine').title('Alpine Linux').in_family('linux') - .detect { - if !(raw = unix_file_contents('/etc/alpine-release')).nil? - @platform[:release] = raw.strip - true - end - } + # alpine + plat.name('alpine').title('Alpine Linux').in_family('linux') + .detect { + if !(raw = unix_file_contents('/etc/alpine-release')).nil? + @platform[:release] = raw.strip + true + end + } -# coreos -plat.name('coreos').title('CoreOS Linux').in_family('linux') - .detect { - unless unix_file_contents('/etc/coreos/update.conf').nil? - lsb = read_linux_lsb - @platform[:release] = lsb[:release] - true - end - } + # coreos + plat.name('coreos').title('CoreOS Linux').in_family('linux') + .detect { + unless unix_file_contents('/etc/coreos/update.conf').nil? + lsb = read_linux_lsb + @platform[:release] = lsb[:release] + true + end + } -# genaric linux -# this should always be last in the linux family list -plat.name('linux').title('Genaric Linux').in_family('linux') - .detect { - true - } + # genaric linux + # this should always be last in the linux family list + plat.name('linux').title('Genaric Linux').in_family('linux') + .detect { + true + } -# openvms -plat.name('openvms').title('OpenVMS').in_family('unix') - .detect { - if unix_uname_s =~ /unrecognized command verb/i - cmd = @backend.run_command('show system/noprocess') - unless cmd.exit_status != 0 || cmd.stdout.empty? - @platform[:name] = cmd.stdout.downcase.split(' ')[0] - cmd = @backend.run_command('write sys$output f$getsyi("VERSION")') - @platform[:release] = cmd.stdout.downcase.split("\n")[1][1..-1] - cmd = @backend.run_command('write sys$output f$getsyi("ARCH_NAME")') - @platform[:arch] = cmd.stdout.downcase.split("\n")[1] - true - end - end - } + # openvms + plat.name('openvms').title('OpenVMS').in_family('unix') + .detect { + if unix_uname_s =~ /unrecognized command verb/i + cmd = @backend.run_command('show system/noprocess') + unless cmd.exit_status != 0 || cmd.stdout.empty? + @platform[:name] = cmd.stdout.downcase.split(' ')[0] + cmd = @backend.run_command('write sys$output f$getsyi("VERSION")') + @platform[:release] = cmd.stdout.downcase.split("\n")[1][1..-1] + cmd = @backend.run_command('write sys$output f$getsyi("ARCH_NAME")') + @platform[:arch] = cmd.stdout.downcase.split("\n")[1] + true + end + end + } -# arista_eos family -plat.family('arista_eos').title('Arista EOS Family').in_family('unix') - .detect { - # we need a better way to determin this family - # for now we are going to just try each platform - true - } -plat.name('arista_eos').title('Arista EOS').in_family('arista_eos') - .detect { - cmd = @backend.run_command('show version | json') - if cmd.exit_status == 0 && !cmd.stdout.empty? - require 'json' - eos_ver = JSON.parse(cmd.stdout) - @platform[:release] = eos_ver['version'] - @platform[:arch] = eos_ver['architecture'] - true - end - } -plat.name('arista_eos_bash').title('Arista EOS Bash Shell').in_family('arista_eos') - .detect { - if unix_file_exist?('/usr/bin/FastCli') - cmd = @backend.run_command('FastCli -p 15 -c "show version | json"') - if cmd.exit_status == 0 && !cmd.stdout.empty? - require 'json' - eos_ver = JSON.parse(cmd.stdout) - @platform[:release] = eos_ver['version'] - @platform[:arch] = eos_ver['architecture'] - true - end - end - } + # arista_eos family + plat.family('arista_eos').title('Arista EOS Family').in_family('unix') + .detect { + # we need a better way to determin this family + # for now we are going to just try each platform + true + } + plat.name('arista_eos').title('Arista EOS').in_family('arista_eos') + .detect { + cmd = @backend.run_command('show version | json') + if cmd.exit_status == 0 && !cmd.stdout.empty? + require 'json' + eos_ver = JSON.parse(cmd.stdout) + @platform[:release] = eos_ver['version'] + @platform[:arch] = eos_ver['architecture'] + true + end + } + plat.name('arista_eos_bash').title('Arista EOS Bash Shell').in_family('arista_eos') + .detect { + if unix_file_exist?('/usr/bin/FastCli') + cmd = @backend.run_command('FastCli -p 15 -c "show version | json"') + if cmd.exit_status == 0 && !cmd.stdout.empty? + require 'json' + eos_ver = JSON.parse(cmd.stdout) + @platform[:release] = eos_ver['version'] + @platform[:arch] = eos_ver['architecture'] + true + end + end + } -# esx -plat.family('esx').title('ESXi Family') - .detect { - true if unix_uname_s =~ /vmkernel/i - } -plat.name('vmkernel').in_family('esx') - .detect { - @platform[:name] = unix_uname_s.lines[0].chomp - @platform[:release] = unix_uname_r.lines[0].chomp - true - } + # esx + plat.family('esx').title('ESXi Family') + .detect { + true if unix_uname_s =~ /vmkernel/i + } + plat.name('vmkernel').in_family('esx') + .detect { + @platform[:name] = unix_uname_s.lines[0].chomp + @platform[:release] = unix_uname_r.lines[0].chomp + true + } -# aix -plat.family('aix').in_family('unix') - .detect { - true if unix_uname_s =~ /aix/ - } -plat.name('aix').title('Aix').in_family('aix') - .detect { - out = @backend.run_command('uname -rvp').stdout - m = out.match(/(\d+)\s+(\d+)\s+(.*)/) - unless m.nil? - @platform[:release] = "#{m[2]}.#{m[1]}" - @platform[:arch] = m[3].to_s - end - true - } + # aix + plat.family('aix').in_family('unix') + .detect { + true if unix_uname_s =~ /aix/ + } + plat.name('aix').title('Aix').in_family('aix') + .detect { + out = @backend.run_command('uname -rvp').stdout + m = out.match(/(\d+)\s+(\d+)\s+(.*)/) + unless m.nil? + @platform[:release] = "#{m[2]}.#{m[1]}" + @platform[:arch] = m[3].to_s + end + true + } -# solaris family -plat.family('solaris').in_family('unix') - .detect { - if unix_uname_s =~ /sunos/i - unless (version = /^5\.(?\d+)$/.match(uname_r)).nil? - @platform[:release] = version['release'] - end + # solaris family + plat.family('solaris').in_family('unix') + .detect { + if unix_uname_s =~ /sunos/i + unless (version = /^5\.(?\d+)$/.match(uname_r)).nil? + @platform[:release] = version['release'] + end - arch = @backend.run_command('uname -p') - @platform[:arch] = arch.stdout.chomp if arch.exit_status == 0 - true - end - } -plat.name('smartos').title('SmartOS').in_family('solaris') - .detect { - rel = unix_file_contents('/etc/release') - if /^.*(SmartOS).*$/ =~ rel - true - end - } -plat.name('omnios').title('Omnios').in_family('solaris') - .detect { - rel = unix_file_contents('/etc/release') - if !(m = /^\s*(OmniOS).*r(\d+).*$/.match(rel)).nil? - @platform[:release] = m[2] - true - end - } -plat.name('openindiana').title('Openindiana').in_family('solaris') - .detect { - rel = unix_file_contents('/etc/release') - if !(m = /^\s*(OpenIndiana).*oi_(\d+).*$/.match(rel)).nil? - @platform[:release] = m[2] - true - end - } -plat.name('opensolaris').title('Open Solaris').in_family('solaris') - .detect { - rel = unix_file_contents('/etc/release') - if /^\s*(OpenSolaris).*snv_(\d+).*$/ =~ rel - @platform[:release] = m[2] - true - end - } -plat.name('nexentacore').title('Nexentacore').in_family('solaris') - .detect { - rel = unix_file_contents('/etc/release') - if /^\s*(NexentaCore)\s.*$/ =~ rel - true - end - } -plat.name('solaris').title('Solaris').in_family('solaris') - .detect { - rel = unix_file_contents('/etc/release') - if !(m = /Oracle Solaris (\d+)/.match(rel)).nil? - # TODO: should be string! - @platform[:release] = m[1] - true - elsif /^\s*(Solaris)\s.*$/ =~ rel - true - end + arch = @backend.run_command('uname -p') + @platform[:arch] = arch.stdout.chomp if arch.exit_status == 0 + true + end + } + plat.name('smartos').title('SmartOS').in_family('solaris') + .detect { + rel = unix_file_contents('/etc/release') + if /^.*(SmartOS).*$/ =~ rel + true + end + } + plat.name('omnios').title('Omnios').in_family('solaris') + .detect { + rel = unix_file_contents('/etc/release') + if !(m = /^\s*(OmniOS).*r(\d+).*$/.match(rel)).nil? + @platform[:release] = m[2] + true + end + } + plat.name('openindiana').title('Openindiana').in_family('solaris') + .detect { + rel = unix_file_contents('/etc/release') + if !(m = /^\s*(OpenIndiana).*oi_(\d+).*$/.match(rel)).nil? + @platform[:release] = m[2] + true + end + } + plat.name('opensolaris').title('Open Solaris').in_family('solaris') + .detect { + rel = unix_file_contents('/etc/release') + if /^\s*(OpenSolaris).*snv_(\d+).*$/ =~ rel + @platform[:release] = m[2] + true + end + } + plat.name('nexentacore').title('Nexentacore').in_family('solaris') + .detect { + rel = unix_file_contents('/etc/release') + if /^\s*(NexentaCore)\s.*$/ =~ rel + true + end + } + plat.name('solaris').title('Solaris').in_family('solaris') + .detect { + rel = unix_file_contents('/etc/release') + if !(m = /Oracle Solaris (\d+)/.match(rel)).nil? + # TODO: should be string! + @platform[:release] = m[1] + true + elsif /^\s*(Solaris)\s.*$/ =~ rel + true + end - # must be some unknown solaris - true - } + # must be some unknown solaris + true + } -# hpux -plat.family('hpux').in_family('unix') - .detect { - true if unix_uname_s =~ /hp-ux/ - } -plat.name('hpux').title('Hpux').in_family('hpux') - .detect { - @platform[:release] = unix_uname_r.lines[0].chomp - true - } + # hpux + plat.family('hpux').in_family('unix') + .detect { + true if unix_uname_s =~ /hp-ux/ + } + plat.name('hpux').title('Hpux').in_family('hpux') + .detect { + @platform[:release] = unix_uname_r.lines[0].chomp + true + } -# bsd family -plat.family('bsd').in_family('unix') - .detect { - # we need a better way to determin this family - # for now we are going to just try each platform - true - } -plat.name('darwin').title('Darwin').in_family('bsd') - .detect { - if unix_uname_s =~ /darwin/ - @platform[:name] = unix_uname_s.lines[0].chomp - @platform[:release] = unix_uname_r.lines[0].chomp - true - end - cmd = @backend.run_command('/usr/bin/sw_vers') - return nil if cmd.exit_status != 0 || cmd.stdout.empty? + # bsd family + plat.family('bsd').in_family('unix') + .detect { + # we need a better way to determin this family + # for now we are going to just try each platform + true + } + plat.name('darwin').title('Darwin').in_family('bsd') + .detect { + if unix_uname_s =~ /darwin/ + @platform[:name] = unix_uname_s.lines[0].chomp + @platform[:release] = unix_uname_r.lines[0].chomp + true + end + cmd = @backend.run_command('/usr/bin/sw_vers') + return nil if cmd.exit_status != 0 || cmd.stdout.empty? - @platform[:release] = cmd.stdout[/^ProductVersion:\s+(.+)$/, 1] - @platform[:build] = cmd.stdout[/^BuildVersion:\s+(.+)$/, 1] - @platform[:arch] = unix_uname_m - true - } -plat.name('freebsd').title('Freebsd').in_family('bsd') - .detect { - if unix_uname_s =~ /freebsd/ - @platform[:name] = unix_uname_s.lines[0].chomp - @platform[:release] = unix_uname_r.lines[0].chomp - true - end - } -plat.name('openbsd').title('Openbsd').in_family('bsd') - .detect { - if unix_uname_s =~ /openbsd/ - @platform[:name] = unix_uname_s.lines[0].chomp - @platform[:release] = unix_uname_r.lines[0].chomp - true - end - } -plat.name('netbsd').title('Netbsd').in_family('bsd') - .detect { - if unix_uname_s =~ /netbsd/ - @platform[:name] = unix_uname_s.lines[0].chomp - @platform[:release] = unix_uname_r.lines[0].chomp - true - end - } + @platform[:release] = cmd.stdout[/^ProductVersion:\s+(.+)$/, 1] + @platform[:build] = cmd.stdout[/^BuildVersion:\s+(.+)$/, 1] + @platform[:arch] = unix_uname_m + true + } + plat.name('freebsd').title('Freebsd').in_family('bsd') + .detect { + if unix_uname_s =~ /freebsd/ + @platform[:name] = unix_uname_s.lines[0].chomp + @platform[:release] = unix_uname_r.lines[0].chomp + true + end + } + plat.name('openbsd').title('Openbsd').in_family('bsd') + .detect { + if unix_uname_s =~ /openbsd/ + @platform[:name] = unix_uname_s.lines[0].chomp + @platform[:release] = unix_uname_r.lines[0].chomp + true + end + } + plat.name('netbsd').title('Netbsd').in_family('bsd') + .detect { + if unix_uname_s =~ /netbsd/ + @platform[:name] = unix_uname_s.lines[0].chomp + @platform[:release] = unix_uname_r.lines[0].chomp + true + end + } + end + end +end diff --git a/lib/train/transports/docker.rb b/lib/train/transports/docker.rb index c63dfd7f..c39d34cf 100644 --- a/lib/train/transports/docker.rb +++ b/lib/train/transports/docker.rb @@ -56,6 +56,7 @@ def reuse_connection class Train::Transports::Docker class Connection < BaseConnection def initialize(conf) + Train::Platforms::Specifications::OS.load_specifications super(conf) @id = options[:host] @container = ::Docker::Container.get(@id) || diff --git a/lib/train/transports/local.rb b/lib/train/transports/local.rb index 9d550322..84a5b029 100644 --- a/lib/train/transports/local.rb +++ b/lib/train/transports/local.rb @@ -18,6 +18,7 @@ def connection(_ = nil) class Connection < BaseConnection def initialize(options) + Train::Platforms::Specifications::OS.load_specifications super(options) @cmd_wrapper = nil @cmd_wrapper = CommandWrapper.load(self, options) diff --git a/lib/train/transports/mock.rb b/lib/train/transports/mock.rb index 6d05fae6..9767f4f7 100644 --- a/lib/train/transports/mock.rb +++ b/lib/train/transports/mock.rb @@ -61,6 +61,7 @@ class Connection < BaseConnection attr_reader :os def initialize(conf = nil) + Train::Platforms::Specifications::OS.load_specifications super(conf) @os = mock_os({}) @commands = {} diff --git a/lib/train/transports/ssh_connection.rb b/lib/train/transports/ssh_connection.rb index c0f070c0..19ae62dc 100644 --- a/lib/train/transports/ssh_connection.rb +++ b/lib/train/transports/ssh_connection.rb @@ -32,6 +32,7 @@ class Train::Transports::SSH class Connection < BaseConnection # rubocop:disable Metrics/ClassLength attr_reader :hostname def initialize(options) + Train::Platforms::Specifications::OS.load_specifications super(options) @username = @options.delete(:username) @hostname = @options.delete(:hostname) diff --git a/lib/train/transports/winrm_connection.rb b/lib/train/transports/winrm_connection.rb index 4f1f704e..196699d9 100644 --- a/lib/train/transports/winrm_connection.rb +++ b/lib/train/transports/winrm_connection.rb @@ -30,6 +30,7 @@ class Train::Transports::WinRM class Connection < BaseConnection # rubocop:disable Metrics/ClassLength attr_reader :hostname def initialize(options) + Train::Platforms::Specifications::OS.load_specifications super(options) @hostname = @options.delete(:hostname) @rdp_port = @options.delete(:rdp_port)