Skip to content

Commit

Permalink
Merge pull request #561 from inspec/zenspider/refactor/os
Browse files Browse the repository at this point in the history
Refactor OS detection.
  • Loading branch information
Ryan Davis authored Jan 28, 2020
2 parents 0a3979b + 5cf121b commit c22dae9
Show file tree
Hide file tree
Showing 10 changed files with 477 additions and 560 deletions.
3 changes: 1 addition & 2 deletions .expeditor/buildkite/verify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ echo "--- push bundle cache"
push_bundle

echo "+++ bundle exec rake"
bundle exec rake

bundle exec rake ${RAKE_TASK:-}
16 changes: 12 additions & 4 deletions .expeditor/verify.pipeline.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
---
steps:

- label: lint-ruby-2.6
command:
- RAKE_TASK=lint /workdir/.expeditor/buildkite/verify.sh
expeditor:
executor:
docker:
image: ruby:2.6

- label: run-tests-ruby-2.4
command:
- /workdir/.expeditor/buildkite/verify.sh
expeditor:
executor:
docker:
image: ruby:2.4-stretch
image: ruby:2.4

- label: run-tests-ruby-2.5
command:
- /workdir/.expeditor/buildkite/verify.sh
expeditor:
executor:
docker:
image: ruby:2.5-stretch
image: ruby:2.5

- label: run-tests-ruby-2.6
command:
- /workdir/.expeditor/buildkite/verify.sh
expeditor:
executor:
docker:
image: ruby:2.6-stretch
image: ruby:2.6

- label: run-tests-ruby-2.7
command:
- /workdir/.expeditor/buildkite/verify.sh
expeditor:
executor:
docker:
image: ruby:2.7-rc
image: ruby:2.7

- label: run-tests-ruby-2.6-windows
command:
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RuboCop::RakeTask.new(:lint) do |task|
end

# run tests
task default: %i{test lint}
task default: %i{test}

Rake::TestTask.new do |t|
t.libs << "test"
Expand Down
14 changes: 4 additions & 10 deletions lib/train/platforms/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,10 @@ def in_family(family)
end

def detect(&block)
if block_given?
@detect = block
self
elsif @detect.nil?
# we are returning a block that just returns false here
# to skip the family/platform evaluation if detect is not set
->(_) { false }
else
@detect
end
@detect = block if block

# TODO: detect shouldn't be a setter and getter at the same time
@detect ||= ->(_) { false }
end
end
end
24 changes: 20 additions & 4 deletions lib/train/platforms/detect/helpers/os_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,8 @@ def brocade_version
res = command_output("version")

m = res.match(/^Fabric OS:\s+v(\S+)$/)
unless m.nil?
return @cache[:brocade] = { version: m[1], type: "fos" }
end

@cache[:brocade] = nil
@cache[:brocade] = m && { version: m[1], type: "fos" }
end

def cisco_show_version
Expand Down Expand Up @@ -156,5 +153,24 @@ def uuid_from_string(string)
# rubocop:disable Style/FormatString
"%08x-%04x-%04x-%04x-%04x%08x" % ary
end

def json_cmd(cmd)
cmd = @backend.run_command(cmd)
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
rescue JSON::ParserError
nil
end

def set_from_uname
@platform[:name] = unix_uname_s.lines.first.chomp
@platform[:release] = unix_uname_r.lines.first.chomp
true
end
end
end
7 changes: 7 additions & 0 deletions lib/train/platforms/detect/helpers/os_linux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def redhatish_version(conf)
end
end

def redhatish(path)
if (raw = unix_file_contents(path))
@platform[:release] = redhatish_version(raw)
true
end
end

def linux_os_release
data = unix_file_contents("/etc/os-release")
return if data.nil?
Expand Down
5 changes: 5 additions & 0 deletions lib/train/platforms/detect/helpers/os_windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ def check_powershell
true
end

def local_windows?
@backend.class.to_s == "Train::Transports::Local::Connection" &&
ruby_host_os(/mswin|mingw32|windows/)
end

# reads os name and version from wmic
# @see https://msdn.microsoft.com/en-us/library/bb742610.aspx#EEAA
# Thanks to Matt Wrock (https://github.com/mwrock) for this hint
Expand Down
4 changes: 2 additions & 2 deletions lib/train/platforms/detect/scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def scan
top.each do |_name, plat|
# we are doing a instance_eval here to make sure we have the proper
# context with all the detect helper methods
next unless instance_eval(&plat.detect) == true
next unless instance_eval(&plat.detect)

# if we have a match start looking at the children
plat_result = scan_children(plat)
Expand All @@ -43,7 +43,7 @@ def scan

def scan_children(parent)
parent.children.each do |plat, condition|
next unless instance_eval(&plat.detect) == true
next unless instance_eval(&plat.detect)

if plat.class == Train::Platforms::Platform
return plat if condition.empty? || check_condition(condition)
Expand Down
Loading

0 comments on commit c22dae9

Please sign in to comment.