diff --git a/lib/train/platforms.rb b/lib/train/platforms.rb index 513179b5..403181e3 100644 --- a/lib/train/platforms.rb +++ b/lib/train/platforms.rb @@ -2,8 +2,10 @@ require 'train/platforms/common' require 'train/platforms/family' -require 'train/platform' +require 'train/platforms/platform' require 'train/platforms/detect' +require 'train/platforms/detect/scanner' +require 'train/platforms/detect/specifications/os' module Train::Platforms class << self @@ -34,7 +36,7 @@ def self.name(name, condition = {}) return plat end - Train::Platform.new(name, condition) + Train::Platforms::Platform.new(name, condition) end # Create or update a family diff --git a/lib/train/platforms/common.rb b/lib/train/platforms/common.rb index 2cf2ba53..0ce71357 100644 --- a/lib/train/platforms/common.rb +++ b/lib/train/platforms/common.rb @@ -19,9 +19,14 @@ def in_family(family) end def detect(&block) - return @detect unless block_given? - @detect = block - self + if block_given? + @detect = block + self + elsif @detect.nil? + ->(_) { false } + else + @detect + end end end end diff --git a/lib/train/platforms/detect.rb b/lib/train/platforms/detect.rb index f7cdd31a..5d0031e4 100644 --- a/lib/train/platforms/detect.rb +++ b/lib/train/platforms/detect.rb @@ -1,8 +1,5 @@ # encoding: utf-8 -require 'train/platforms/specifications/os' -require 'train/platforms/detect/scanner' - module Train::Platforms module Detect # Main detect method to scan all platforms for a match diff --git a/lib/train/platforms/detect/os_common.rb b/lib/train/platforms/detect/helpers/os_common.rb similarity index 83% rename from lib/train/platforms/detect/os_common.rb rename to lib/train/platforms/detect/helpers/os_common.rb index c9d38667..997fb9c7 100644 --- a/lib/train/platforms/detect/os_common.rb +++ b/lib/train/platforms/detect/helpers/os_common.rb @@ -1,13 +1,13 @@ # encoding: utf-8 -require 'train/platforms/detect/os_linux' -require 'train/platforms/detect/os_windows' +require 'train/platforms/detect/helpers/os_linux' +require 'train/platforms/detect/helpers/os_windows' require 'rbconfig' -module Train::Platforms::Detect +module Train::Platforms::Detect::Helpers module OSCommon - include Train::Platforms::Detect::Linux - include Train::Platforms::Detect::Windows + include Train::Platforms::Detect::Helpers::Linux + include Train::Platforms::Detect::Helpers::Windows def ruby_host_os(regex) ::RbConfig::CONFIG['host_os'] =~ regex diff --git a/lib/train/platforms/detect/os_linux.rb b/lib/train/platforms/detect/helpers/os_linux.rb similarity index 98% rename from lib/train/platforms/detect/os_linux.rb rename to lib/train/platforms/detect/helpers/os_linux.rb index 89246dac..4bfe3f5b 100644 --- a/lib/train/platforms/detect/os_linux.rb +++ b/lib/train/platforms/detect/helpers/os_linux.rb @@ -1,6 +1,6 @@ # encoding: utf-8 -module Train::Platforms::Detect +module Train::Platforms::Detect::Helpers module Linux def redhatish_platform(conf) conf =~ /^red hat/i ? 'redhat' : /(\w+)/i.match(conf)[1].downcase diff --git a/lib/train/platforms/detect/os_windows.rb b/lib/train/platforms/detect/helpers/os_windows.rb similarity index 98% rename from lib/train/platforms/detect/os_windows.rb rename to lib/train/platforms/detect/helpers/os_windows.rb index 153d3014..4b21ce26 100644 --- a/lib/train/platforms/detect/os_windows.rb +++ b/lib/train/platforms/detect/helpers/os_windows.rb @@ -1,6 +1,6 @@ # encoding: utf-8 -module Train::Platforms::Detect +module Train::Platforms::Detect::Helpers module Windows def detect_windows res = @backend.run_command('cmd /c ver') diff --git a/lib/train/platforms/detect/scanner.rb b/lib/train/platforms/detect/scanner.rb index 081ad33a..9427d0f0 100644 --- a/lib/train/platforms/detect/scanner.rb +++ b/lib/train/platforms/detect/scanner.rb @@ -1,10 +1,10 @@ # encoding: utf-8 -require 'train/platforms/detect/os_common' +require 'train/platforms/detect/helpers/os_common' module Train::Platforms::Detect class Scanner - include Train::Platforms::Detect::OSCommon + include Train::Platforms::Detect::Helpers::OSCommon def initialize(backend) @backend = backend @@ -24,7 +24,6 @@ def scan # start with the platform/families who have no families (the top levels) top = Train::Platforms.top_platforms top.each do |_name, plat| - next unless plat.detect next unless instance_eval(&plat.detect) == true # if we have a match start looking at the children @@ -41,10 +40,9 @@ def scan def scan_children(parent) parent.children.each do |plat, condition| - next if plat.detect.nil? next unless instance_eval(&plat.detect) == true - if plat.class == Train::Platform + if plat.class == Train::Platforms::Platform @platform[:family] = parent.name return plat if condition.empty? || check_condition(condition) elsif plat.class == Train::Platforms::Family diff --git a/lib/train/platforms/specifications/os.rb b/lib/train/platforms/detect/specifications/os.rb similarity index 99% rename from lib/train/platforms/specifications/os.rb rename to lib/train/platforms/detect/specifications/os.rb index 220855b8..90709cbf 100644 --- a/lib/train/platforms/specifications/os.rb +++ b/lib/train/platforms/detect/specifications/os.rb @@ -7,9 +7,9 @@ # rubocop:disable Metrics/MethodLength # rubocop:disable Metrics/PerceivedComplexity -module Train::Platforms::Specifications +module Train::Platforms::Detect::Specifications class OS - def self.load_specifications + def self.load plat = Train::Platforms plat.family('windows') diff --git a/lib/train/platform.rb b/lib/train/platforms/platform.rb similarity index 98% rename from lib/train/platform.rb rename to lib/train/platforms/platform.rb index 62425908..a86edf32 100644 --- a/lib/train/platform.rb +++ b/lib/train/platforms/platform.rb @@ -1,6 +1,6 @@ # encoding: utf-8 -module Train +module Train::Platforms class Platform include Train::Platforms::Common attr_accessor :backend, :condition, :families, :family_hierarchy, :platform diff --git a/lib/train/plugins/base_connection.rb b/lib/train/plugins/base_connection.rb index 8a079252..c2620071 100644 --- a/lib/train/plugins/base_connection.rb +++ b/lib/train/plugins/base_connection.rb @@ -1,8 +1,4 @@ # encoding: utf-8 -# -# Author:: Salim Afiune () -# Author:: Fletcher Nichol () -# Author:: Dominik Richter () require 'train/errors' require 'train/extras' @@ -30,6 +26,7 @@ def initialize(options = nil) @options = options || {} @logger = @options.delete(:logger) || Logger.new(STDOUT) @files = {} + Train::Platforms::Detect::Specifications::OS.load end # Closes the session connection, if it is still active. @@ -63,20 +60,16 @@ def run_command(_command) fail Train::ClientError, "#{self.class} does not implement #run_command()" end - # Get information on the operating system which this transport connects to. - # - # @return [OSCommon] operating system information - def os - fail Train::ClientError, "#{self.class} does not implement #os()" - end - # Get information on the operating system which this transport connects to. # # @return [Platform] system information def platform - fail Train::ClientError, "#{self.class} does not implement #platform()" + @platform ||= Train::Platforms::Detect.scan(self) end + # we need to keep os as a method for backwards compatibility with inspec + alias os platform + # Interact with files on the target. Read, write, and get metadata # from files via the transport. # diff --git a/lib/train/transports/docker.rb b/lib/train/transports/docker.rb index c39d34cf..75ce59b8 100644 --- a/lib/train/transports/docker.rb +++ b/lib/train/transports/docker.rb @@ -56,7 +56,6 @@ 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) || @@ -70,13 +69,6 @@ def close # nothing to do at the moment end - def platform - @platform ||= Train::Platforms::Detect.scan(self) - end - - # we need to keep os as a method for backwards compatibility with inspec - alias os platform - def file(path) @files[path] ||=\ if os.aix? diff --git a/lib/train/transports/local.rb b/lib/train/transports/local.rb index 84a5b029..1eeac343 100644 --- a/lib/train/transports/local.rb +++ b/lib/train/transports/local.rb @@ -18,7 +18,6 @@ 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) @@ -37,13 +36,6 @@ def local? true end - def platform - @platform ||= Train::Platforms::Detect.scan(self) - end - - # we need to keep os as a method for backwards compatibility with inspec - alias os platform - def file(path) @files[path] ||= \ if os.windows? diff --git a/lib/train/transports/mock.rb b/lib/train/transports/mock.rb index 9767f4f7..6d05fae6 100644 --- a/lib/train/transports/mock.rb +++ b/lib/train/transports/mock.rb @@ -61,7 +61,6 @@ 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 19ae62dc..5e996796 100644 --- a/lib/train/transports/ssh_connection.rb +++ b/lib/train/transports/ssh_connection.rb @@ -32,7 +32,6 @@ 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) @@ -56,13 +55,6 @@ def close @session = nil end - def platform - @platform ||= Train::Platforms::Detect.scan(self) - end - - # we need to keep os as a method for backwards compatibility with inspec - alias os platform - def file(path) @files[path] ||= \ if os.aix? diff --git a/lib/train/transports/winrm_connection.rb b/lib/train/transports/winrm_connection.rb index 196699d9..b856d743 100644 --- a/lib/train/transports/winrm_connection.rb +++ b/lib/train/transports/winrm_connection.rb @@ -30,7 +30,6 @@ 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) @@ -47,13 +46,6 @@ def close @session = nil end - def platform - @platform ||= Train::Platforms::Detect.scan(self) - end - - # we need to keep os as a method for backwards compatibility with inspec - alias os platform - def file(path) @files[path] ||= Train::File::Remote::Windows.new(self, path) end diff --git a/test/unit/platforms/detect/os_common_test.rb b/test/unit/platforms/detect/os_common_test.rb index 5566e18f..b48f9861 100644 --- a/test/unit/platforms/detect/os_common_test.rb +++ b/test/unit/platforms/detect/os_common_test.rb @@ -4,7 +4,7 @@ class OsDetectLinuxTester attr_reader :platform - include Train::Platforms::Detect::OSCommon + include Train::Platforms::Detect::Helpers::OSCommon def initialize @platform = {} diff --git a/test/unit/platforms/detect/os_linux_test.rb b/test/unit/platforms/detect/os_linux_test.rb index 5f41cc63..bbe42ba1 100644 --- a/test/unit/platforms/detect/os_linux_test.rb +++ b/test/unit/platforms/detect/os_linux_test.rb @@ -4,7 +4,7 @@ require 'train/transports/mock' class OsDetectLinuxTester - include Train::Platforms::Detect::OSCommon + include Train::Platforms::Detect::Helpers::OSCommon end describe 'os_linux' do diff --git a/test/unit/platforms/detect/os_windows_test.rb b/test/unit/platforms/detect/os_windows_test.rb index 4052e318..1cd1a3ab 100644 --- a/test/unit/platforms/detect/os_windows_test.rb +++ b/test/unit/platforms/detect/os_windows_test.rb @@ -5,7 +5,7 @@ class OsDetectWindowsTester attr_reader :platform, :backend - include Train::Platforms::Detect::Windows + include Train::Platforms::Detect::Helpers::Windows def initialize @platform = {} diff --git a/test/unit/platforms/os_detect_test.rb b/test/unit/platforms/os_detect_test.rb index edb4401e..5f043617 100644 --- a/test/unit/platforms/os_detect_test.rb +++ b/test/unit/platforms/os_detect_test.rb @@ -3,7 +3,7 @@ require 'train/transports/mock' class OsDetectLinuxTester - include Train::Platforms::Detect::OSCommon + include Train::Platforms::Detect::Helpers::OSCommon end describe 'os_detect_linux' do