Class: Train::Platforms::Detect::Specifications::OS

Inherits:
Object
  • Object
show all
Defined in:
lib/train/platforms/detect/specifications/os.rb

Class Method Summary collapse

Class Method Details

.loadObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/train/platforms/detect/specifications/os.rb', line 12

def self.load
  plat = Train::Platforms

  # master family
  plat.family("os").detect { true }

  plat.family("windows").in_family("os")
      .detect do
        # Can't return from a `proc` thus the `is_windows` shenanigans
        is_windows = false
        is_windows = true if winrm?

        if @backend.class.to_s == "Train::Transports::Local::Connection"
          is_windows = true if ruby_host_os(/mswin|mingw32|windows/)
        end

        # Try to detect windows even for ssh transport
        if !is_windows && detect_windows == true
          is_windows = true
        end

        is_windows
      end
  # windows platform
  plat.name("windows").in_family("windows")
      .detect do
        true if detect_windows == true
      end

  # unix master family
  plat.family("unix").in_family("os")
      .detect do
        # we want to catch a special case here where cisco commands
        # don't return an exit status and still print to stdout
        if unix_uname_s =~ /./ && !unix_uname_s.start_with?("Line has invalid autocommand ") && !unix_uname_s.start_with?("The command you have entered")
          @platform[:arch] = unix_uname_m
          true
        end
      end

  # linux master family
  plat.family("linux").in_family("unix")
      .detect do
        true if unix_uname_s =~ /linux/i
      end

  # debian family
  plat.family("debian").in_family("linux")
      .detect do
        true unless unix_file_contents("/etc/debian_version").nil?
      end
  plat.name("ubuntu").title("Ubuntu Linux").in_family("debian")
      .detect do
        lsb = read_linux_lsb
        if lsb && lsb[:id] =~ /ubuntu/i
          @platform[:release] = lsb[:release]
          true
        end
      end
  plat.name("linuxmint").title("LinuxMint").in_family("debian")
      .detect do
        lsb = read_linux_lsb
        if lsb && lsb[:id] =~ /linuxmint/i
          @platform[:release] = lsb[:release]
          true
        end
      end
  plat.name("raspbian").title("Raspbian Linux").in_family("debian")
      .detect do
        if (linux_os_release && linux_os_release["NAME"] =~ /raspbian/i) || \
            unix_file_exist?("/usr/bin/raspi-config")
          @platform[:release] = unix_file_contents("/etc/debian_version").chomp
          true
        end
      end
  plat.name("debian").title("Debian Linux").in_family("debian")
      .detect do
        lsb = read_linux_lsb
        if lsb && lsb[:id] =~ /debian/i
          @platform[:release] = lsb[:release]
          true
        end

        # if we get this far we have to be some type of debian
        @platform[:release] = unix_file_contents("/etc/debian_version").chomp
        true
      end

  # fedora family
  plat.family("fedora").in_family("linux")
      .detect do
        true if linux_os_release && linux_os_release["NAME"] =~ /fedora/i
      end
  plat.name("fedora").title("Fedora").in_family("fedora")
      .detect do
        @platform[:release] = linux_os_release["VERSION_ID"]
        true
      end

  # arista_eos family
  # this checks for the arista bash shell
  # must come before redhat as it uses fedora under the hood
  plat.family("arista_eos").title("Arista EOS Family").in_family("linux")
      .detect do
        true
      end
  plat.name("arista_eos_bash").title("Arista EOS Bash Shell").in_family("arista_eos")
      .detect do
        if unix_file_exist?("/usr/bin/FastCli")
          cmd = @backend.run_command('FastCli -p 15 -c "show version | json"')
          if cmd.exit_status == 0 && !cmd.stdout.empty?
            require "json"
            begin
              eos_ver = JSON.parse(cmd.stdout)
              @platform[:release] = eos_ver["version"]
              @platform[:arch] = eos_ver["architecture"]
              true
            rescue JSON::ParserError
              nil
            end
          end
        end
      end

  # redhat family
  plat.family("redhat").in_family("linux")
      .detect do
        # I am not sure this returns true for all redhats in this family
        # for now we are going to just try each platform
        # return true unless unix_file_contents('/etc/redhat-release').nil?

        true
      end
  plat.name("centos").title("Centos Linux").in_family("redhat")
      .detect do
        lsb = read_linux_lsb
        if lsb && lsb[:id] =~ /centos/i
          @platform[:release] = lsb[:release]
          true
        elsif linux_os_release && linux_os_release["NAME"] =~ /centos/i
          @platform[:release] = redhatish_version(unix_file_contents("/etc/redhat-release"))
          true
        end
      end
  plat.name("oracle").title("Oracle Linux").in_family("redhat")
      .detect do
        if !(raw = unix_file_contents("/etc/oracle-release")).nil?
          @platform[:release] = redhatish_version(raw)
          true
        elsif !(raw = unix_file_contents("/etc/enterprise-release")).nil?
          @platform[:release] = redhatish_version(raw)
          true
        end
      end
  plat.name("scientific").title("Scientific Linux").in_family("redhat")
      .detect do
        lsb = read_linux_lsb
        if lsb && lsb[:id] =~ /scientific/i
          @platform[:release] = lsb[:release]
          true
        end
      end
  plat.name("xenserver").title("Xenserer Linux").in_family("redhat")
      .detect do
        lsb = read_linux_lsb
        if lsb && lsb[:id] =~ /xenserver/i
          @platform[:release] = lsb[:release]
          true
        end
      end
  plat.name("parallels-release").title("Parallels Linux").in_family("redhat")
      .detect do
        if !(raw = unix_file_contents("/etc/parallels-release")).nil?
          @platform[:name] = redhatish_platform(raw)
          @platform[:release] = raw[/(\d\.\d\.\d)/, 1]
          true
        end
      end
  plat.name("wrlinux").title("Wind River Linux").in_family("redhat")
      .detect do
        if linux_os_release && linux_os_release["ID_LIKE"] =~ /wrlinux/i
          @platform[:release] = linux_os_release["VERSION"]
          true
        end
      end
  plat.name("amazon").title("Amazon Linux").in_family("redhat")
      .detect do
        lsb = read_linux_lsb
        if lsb && lsb[:id] =~ /amazon/i
          @platform[:release] = lsb[:release]
          true
        elsif (raw = unix_file_contents("/etc/system-release")) =~ /amazon/i
          @platform[:name] = redhatish_platform(raw)
          @platform[:release] = redhatish_version(raw)
          true
        end
      end
  plat.name("cloudlinux").title("CloudLinux").in_family("redhat")
      .detect do
        lsb = read_linux_lsb
        if lsb && lsb[:id] =~ /cloudlinux/i
          @platform[:release] = lsb[:release]
          true
        elsif (raw = unix_file_contents("/etc/redhat-release")) =~ /cloudlinux/i
          @platform[:name] = redhatish_platform(raw)
          @platform[:release] = redhatish_version(raw)
          true
        end
      end
  # keep redhat at the end as a fallback for anything with a redhat-release
  plat.name("redhat").title("Red Hat Linux").in_family("redhat")
      .detect do
        lsb = read_linux_lsb
        if lsb && lsb[:id] =~ /redhat/i
          @platform[:release] = lsb[:release]
          true
        elsif !(raw = unix_file_contents("/etc/redhat-release")).nil?
          # must be some type of redhat
          @platform[:name] = redhatish_platform(raw)
          @platform[:release] = redhatish_version(raw)
          true
        end
      end

  # suse family
  plat.family("suse").in_family("linux")
      .detect do
        if !(suse = unix_file_contents("/etc/SuSE-release")).nil?
          # https://rubular.com/r/UKaYWolCYFMfp1
          version = suse.scan(/VERSION = (\d+)\nPATCHLEVEL = (\d+)/).flatten.join(".")
          # https://rubular.com/r/b5PN3hZDxa5amV
          version = suse[/VERSION\s?=\s?"?([\d\.]{2,})"?/, 1] if version == ""
          @platform[:release] = version
          true
        end
      end
  plat.name("opensuse").title("OpenSUSE Linux").in_family("suse")
      .detect do
        true if unix_file_contents("/etc/SuSE-release") =~ /^opensuse/i
      end
  plat.name("suse").title("Suse Linux").in_family("suse")
      .detect do
        true if unix_file_contents("/etc/SuSE-release") =~ /suse/i
      end

  # arch
  plat.name("arch").title("Arch Linux").in_family("linux")
      .detect do
        if !unix_file_contents("/etc/arch-release").nil?
          # Because this is a rolling release distribution,
          # use the kernel release, ex. 4.1.6-1-ARCH
          @platform[:release] = unix_uname_r
          true
        end
      end

  # slackware
  plat.name("slackware").title("Slackware Linux").in_family("linux")
      .detect do
        if !(raw = unix_file_contents("/etc/slackware-version")).nil?
          @platform[:release] = raw.scan(/(\d+|\.+)/).join
          true
        end
      end

  # gentoo
  plat.name("gentoo").title("Gentoo Linux").in_family("linux")
      .detect do
        if !(raw = unix_file_contents("/etc/gentoo-release")).nil?
          @platform[:release] = raw.scan(/(\d+|\.+)/).join
          true
        end
      end

  # exherbo
  plat.name("exherbo").title("Exherbo Linux").in_family("linux")
      .detect do
        unless unix_file_contents("/etc/exherbo-release").nil?
          # Because this is a rolling release distribution,
          # use the kernel release, ex. 4.1.6
          @platform[:release] = unix_uname_r
          true
        end
      end

  # alpine
  plat.name("alpine").title("Alpine Linux").in_family("linux")
      .detect do
        if !(raw = unix_file_contents("/etc/alpine-release")).nil?
          @platform[:release] = raw.strip
          true
        end
      end

  # coreos
  plat.name("coreos").title("CoreOS Linux").in_family("linux")
      .detect do
        unless unix_file_contents("/etc/coreos/update.conf").nil?
          lsb = read_linux_lsb
          @platform[:release] = lsb[:release]
          true
        end
      end

  # brocade family detected here if device responds to 'uname' command,
  # happens when logging in as root
  plat.family("brocade").title("Brocade Family").in_family("linux")
      .detect do
        !brocade_version.nil?
      end

  # genaric linux
  # this should always be last in the linux family list
  plat.name("linux").title("Genaric Linux").in_family("linux")
      .detect do
        true
      end

  # openvms
  plat.name("openvms").title("OpenVMS").in_family("unix")
      .detect do
        if unix_uname_s =~ /unrecognized command verb/i
          cmd = @backend.run_command("show system/noprocess")
          unless cmd.exit_status != 0 || cmd.stdout.empty?
            @platform[:name] = cmd.stdout.downcase.split(" ")[0]
            cmd = @backend.run_command('write sys$output f$getsyi("VERSION")')
            @platform[:release] = cmd.stdout.downcase.split("\n")[1][1..-1]
            cmd = @backend.run_command('write sys$output f$getsyi("ARCH_NAME")')
            @platform[:arch] = cmd.stdout.downcase.split("\n")[1]
            true
          end
        end
      end

  # aix
  plat.family("aix").in_family("unix")
      .detect do
        true if unix_uname_s =~ /aix/i
      end
  plat.name("aix").title("Aix").in_family("aix")
      .detect do
        out = @backend.run_command("uname -rvp").stdout
        m = out.match(/(\d+)\s+(\d+)\s+(.*)/)
        unless m.nil?
          @platform[:release] = "#{m[2]}.#{m[1]}"
          @platform[:arch] = m[3].to_s
        end
        true
      end

  # solaris family
  plat.family("solaris").in_family("unix")
      .detect do
        if unix_uname_s =~ /sunos/i
          unless (version = /^5\.(?<release>\d+)$/.match(unix_uname_r)).nil?
            @platform[:release] = version["release"]
          end

          arch = @backend.run_command("uname -p")
          @platform[:arch] = arch.stdout.chomp if arch.exit_status == 0
          true
        end
      end
  plat.name("smartos").title("SmartOS").in_family("solaris")
      .detect do
        rel = unix_file_contents("/etc/release")
        if /^.*(SmartOS).*$/ =~ rel
          true
        end
      end
  plat.name("omnios").title("Omnios").in_family("solaris")
      .detect do
        rel = unix_file_contents("/etc/release")
        if !(m = /^\s*(OmniOS).*r(\d+).*$/.match(rel)).nil?
          @platform[:release] = m[2]
          true
        end
      end
  plat.name("openindiana").title("Openindiana").in_family("solaris")
      .detect do
        rel = unix_file_contents("/etc/release")
        if !(m = /^\s*(OpenIndiana).*oi_(\d+).*$/.match(rel)).nil?
          @platform[:release] = m[2]
          true
        end
      end
  plat.name("opensolaris").title("Open Solaris").in_family("solaris")
      .detect do
        rel = unix_file_contents("/etc/release")
        if !(m = /^\s*(OpenSolaris).*snv_(\d+).*$/.match(rel)).nil?
          @platform[:release] = m[2]
          true
        end
      end
  plat.name("nexentacore").title("Nexentacore").in_family("solaris")
      .detect do
        rel = unix_file_contents("/etc/release")
        if /^\s*(NexentaCore)\s.*$/ =~ rel
          true
        end
      end
  plat.name("solaris").title("Solaris").in_family("solaris")
      .detect do
        rel = unix_file_contents("/etc/release")
        if !(m = /Oracle Solaris (\d+)/.match(rel)).nil?
          # TODO: should be string!
          @platform[:release] = m[1]
          true
        elsif /^\s*(Solaris)\s.*$/ =~ rel
          true
        end

        # must be some unknown solaris
        true
      end

  # hpux
  plat.family("hpux").in_family("unix")
      .detect do
        true if unix_uname_s =~ /hp-ux/i
      end
  plat.name("hpux").title("Hpux").in_family("hpux")
      .detect do
        @platform[:release] = unix_uname_r.lines[0].chomp
        true
      end

  # qnx
  plat.family("qnx").in_family("unix")
      .detect do
        true if unix_uname_s =~ /qnx/i
      end
  plat.name("qnx").title("QNX").in_family("qnx")
      .detect do
        @platform[:name] = unix_uname_s.lines[0].chomp.downcase
        @platform[:release] = unix_uname_r.lines[0].chomp
        @platform[:arch] = unix_uname_m
        true
      end

  # bsd family
  plat.family("bsd").in_family("unix")
      .detect do
        # we need a better way to determin this family
        # for now we are going to just try each platform
        true
      end
  plat.family("darwin").in_family("bsd")
      .detect do
        if unix_uname_s =~ /darwin/i
          cmd = unix_file_contents("/usr/bin/sw_vers")
          unless cmd.nil?
            m = cmd.match(/^ProductVersion:\s+(.+)$/)
            @platform[:release] = m.nil? ? nil : m[1]
            m = cmd.match(/^BuildVersion:\s+(.+)$/)
            @platform[:build] = m.nil? ? nil : m[1]
          end
          @platform[:release] = unix_uname_r.lines[0].chomp if @platform[:release].nil?
          @platform[:arch] = unix_uname_m
          true
        end
      end
  plat.name("mac_os_x").title("macOS X").in_family("darwin")
      .detect do
        cmd = unix_file_contents("/System/Library/CoreServices/SystemVersion.plist")
        @platform[:uuid_command] = "system_profiler SPHardwareDataType | awk '/UUID/ { print $3; }'"
        true if cmd =~ /Mac OS X/i
      end
  plat.name("darwin").title("Darwin").in_family("darwin")
      .detect do
        # must be some other type of darwin
        @platform[:name] = unix_uname_s.lines[0].chomp
        true
      end
  plat.name("freebsd").title("Freebsd").in_family("bsd")
      .detect do
        if unix_uname_s =~ /freebsd/i
          @platform[:name] = unix_uname_s.lines[0].chomp
          @platform[:release] = unix_uname_r.lines[0].chomp
          true
        end
      end
  plat.name("openbsd").title("Openbsd").in_family("bsd")
      .detect do
        if unix_uname_s =~ /openbsd/i
          @platform[:name] = unix_uname_s.lines[0].chomp
          @platform[:release] = unix_uname_r.lines[0].chomp
          true
        end
      end
  plat.name("netbsd").title("Netbsd").in_family("bsd")
      .detect do
        if unix_uname_s =~ /netbsd/i
          @platform[:name] = unix_uname_s.lines[0].chomp
          @platform[:release] = unix_uname_r.lines[0].chomp
          true
        end
      end

  # arista_eos family
  plat.family("arista_eos").title("Arista EOS Family").in_family("os")
      .detect do
        true
      end
  plat.name("arista_eos").title("Arista EOS").in_family("arista_eos")
      .detect do
        cmd = @backend.run_command("show version | json")
        if cmd.exit_status == 0 && !cmd.stdout.empty?
          require "json"
          begin
            eos_ver = JSON.parse(cmd.stdout)
            @platform[:release] = eos_ver["version"]
            @platform[:arch] = eos_ver["architecture"]
            true
          rescue JSON::ParserError
            nil
          end
        end
      end

  # esx
  plat.family("esx").title("ESXi Family").in_family("os")
      .detect do
        true if unix_uname_s =~ /vmkernel/i
      end
  plat.name("vmkernel").in_family("esx")
      .detect do
        @platform[:name] = unix_uname_s.lines[0].chomp
        @platform[:release] = unix_uname_r.lines[0].chomp
        true
      end

  # cisco_ios family
  plat.family("cisco").title("Cisco Family").in_family("os")
      .detect do
        !cisco_show_version.nil?
      end
  plat.name("cisco_ios").title("Cisco IOS").in_family("cisco")
      .detect do
        v = cisco_show_version
        next unless v[:type] == "ios"
        @platform[:release] = v[:version]
        @platform[:arch] = nil
        true
      end
  plat.name("cisco_ios_xe").title("Cisco IOS XE").in_family("cisco")
      .detect do
        v = cisco_show_version
        next unless v[:type] == "ios-xe"
        @platform[:release] = v[:version]
        @platform[:arch] = nil
        true
      end
  plat.name("cisco_nexus").title("Cisco Nexus").in_family("cisco")
      .detect do
        v = cisco_show_version
        next unless v[:type] == "nexus"
        @platform[:release] = v[:version]
        @platform[:arch] = nil
        @platform[:uuid_command] = "show version | include Processor"
        true
      end

  # brocade family
  plat.family("brocade").title("Brocade Family").in_family("os")
      .detect do
        !brocade_version.nil?
      end

  plat.name("brocade_fos").title("Brocade FOS").in_family("brocade")
      .detect do
        v = brocade_version
        next unless v[:type] == "fos"
        @platform[:release] = v[:version]
        @platform[:arch] = nil
        true
      end
end