Skip to content

Commit

Permalink
style fixes and wrapped os specifications in a class
Browse files Browse the repository at this point in the history
Signed-off-by: Jared Quick <jquick@chef.io>
  • Loading branch information
jquick committed Oct 31, 2017
1 parent 1c50340 commit 7cc0267
Show file tree
Hide file tree
Showing 13 changed files with 471 additions and 446 deletions.
3 changes: 0 additions & 3 deletions lib/train.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/train/platforms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'train/platforms/common'
require 'train/platforms/family'
require 'train/platform'
require 'train/platforms/detect'

module Train::Platforms
class << self
Expand Down
2 changes: 1 addition & 1 deletion lib/train/platforms/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/train/platforms/detect.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding: utf-8

require 'train/platforms/specifications/os'
require 'train/platforms/detect/scanner'

module Train::Platforms
Expand Down
10 changes: 5 additions & 5 deletions lib/train/platforms/detect/os_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -32,25 +32,25 @@ 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
end

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
31 changes: 21 additions & 10 deletions lib/train/platforms/detect/os_linux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion lib/train/platforms/detect/scanner.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# encoding: utf-8

require 'train/platforms/specifications/os'
require 'train/platforms/detect/os_common'

module Train::Platforms::Detect
Expand Down
Loading

0 comments on commit 7cc0267

Please sign in to comment.