Class: Train::Platforms::Detect::Scanner

Inherits:
Object
  • Object
show all
Includes:
Helpers::OSCommon
Defined in:
lib/train/platforms/detect/scanner.rb

Instance Method Summary collapse

Methods included from Helpers::OSCommon

#brocade_version, #cisco_show_version, #command_output, #ruby_host_os, #unix_file_contents, #unix_file_exist?, #unix_uname_m, #unix_uname_r, #unix_uname_s, #unix_uuid, #unix_uuid_from_chef, #unix_uuid_from_machine_file, #uuid_from_command, #uuid_from_string, #winrm?

Methods included from Helpers::Windows

#detect_windows, #read_wmic, #read_wmic_cpu, #windows_uuid, #windows_uuid_from_chef, #windows_uuid_from_machine_file, #windows_uuid_from_registry, #windows_uuid_from_wmic

Methods included from Helpers::Linux

#linux_os_release, #lsb_config, #lsb_release, #parse_os_release_info, #read_linux_lsb, #redhatish_platform, #redhatish_version

Constructor Details

#initialize(backend) ⇒ Scanner

Returns a new instance of Scanner.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/train/platforms/detect/scanner.rb', line 9

def initialize(backend)
  @backend = backend
  @platform = {}
  @family_hierarchy = []

  # detect cache variables
  @files = {}
  @uname = {}
  @lsb = {}
  @cache = {}
end

Instance Method Details

#check_condition(condition) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/train/platforms/detect/scanner.rb', line 66

def check_condition(condition)
  condition.each do |k, v|
    op, expected = v.strip.split(' ')
    op = '==' if op == '='
    return false if @platform[k].nil? || !instance_eval("'#{@platform[k]}' #{op} '#{expected}'")
  end

  true
end

#get_platform(plat) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/train/platforms/detect/scanner.rb', line 76

def get_platform(plat)
  plat.backend = @backend
  plat.platform = @platform
  plat.add_platform_methods
  plat.family_hierarchy = @family_hierarchy
  plat
end

#scanObject

Main detect method to scan all platforms for a match

Returns:

  • Train::Platform instance or error if none found



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/train/platforms/detect/scanner.rb', line 24

def scan
  # start with the platform/families who have no families (the top levels)
  top = Train::Platforms.top_platforms
  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

    # if we have a match start looking at the children
    plat_result = scan_children(plat)
    next if plat_result.nil?

    # return platform to backend
    @family_hierarchy << plat.name
    return get_platform(plat_result)
  end

  fail Train::PlatformDetectionFailed, 'Sorry, we are unable to detect your platform'
end

#scan_children(parent) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/train/platforms/detect/scanner.rb', line 44

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

    if plat.class == Train::Platforms::Platform
      return plat if condition.empty? || check_condition(condition)
    elsif plat.class == Train::Platforms::Family
      plat = scan_family_children(plat)
      return plat unless plat.nil?
    end
  end

  nil
end

#scan_family_children(plat) ⇒ Object



59
60
61
62
63
64
# File 'lib/train/platforms/detect/scanner.rb', line 59

def scan_family_children(plat)
  child_result = scan_children(plat) unless plat.children.nil?
  return if child_result.nil?
  @family_hierarchy << plat.name
  child_result
end