Skip to content

Commit

Permalink
move around platform files into more logical spots
Browse files Browse the repository at this point in the history
Signed-off-by: Jared Quick <jquick@chef.io>
  • Loading branch information
jquick committed Nov 1, 2017
1 parent 7cc0267 commit 83e3599
Show file tree
Hide file tree
Showing 19 changed files with 34 additions and 72 deletions.
6 changes: 4 additions & 2 deletions lib/train/platforms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions lib/train/platforms/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 0 additions & 3 deletions lib/train/platforms/detect.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
8 changes: 3 additions & 5 deletions lib/train/platforms/detect/scanner.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion lib/train/platform.rb → lib/train/platforms/platform.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 5 additions & 12 deletions lib/train/plugins/base_connection.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# encoding: utf-8
#
# Author:: Salim Afiune (<salim@afiunemaya.com.mx>)
# Author:: Fletcher Nichol (<fnichol@nichol.ca>)
# Author:: Dominik Richter (<dominik.richter@gmail.com>)

require 'train/errors'
require 'train/extras'
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
#
Expand Down
8 changes: 0 additions & 8 deletions lib/train/transports/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand All @@ -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?
Expand Down
8 changes: 0 additions & 8 deletions lib/train/transports/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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?
Expand Down
1 change: 0 additions & 1 deletion lib/train/transports/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
8 changes: 0 additions & 8 deletions lib/train/transports/ssh_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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?
Expand Down
8 changes: 0 additions & 8 deletions lib/train/transports/winrm_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/unit/platforms/detect/os_common_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class OsDetectLinuxTester
attr_reader :platform
include Train::Platforms::Detect::OSCommon
include Train::Platforms::Detect::Helpers::OSCommon

def initialize
@platform = {}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/platforms/detect/os_linux_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/unit/platforms/detect/os_windows_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class OsDetectWindowsTester
attr_reader :platform, :backend
include Train::Platforms::Detect::Windows
include Train::Platforms::Detect::Helpers::Windows

def initialize
@platform = {}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/platforms/os_detect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 83e3599

Please sign in to comment.