Skip to content

Commit bb56a25

Browse files
Yocto Project family and Yocto Linux and balenaOS platform detec… (#558)
Yocto Project family and Yocto Linux and balenaOS platform detection
2 parents 19ceb02 + ad9a706 commit bb56a25

File tree

2 files changed

+69
-15
lines changed

2 files changed

+69
-15
lines changed

lib/train/platforms/detect/specifications/os.rb

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ def self.load
1717

1818
plat.family("windows").in_family("os")
1919
.detect do
20-
# Can't return from a `proc` thus the `is_windows` shenanigans
20+
# Can't return from a `proc` thus the `is_windows` shenanigans
2121
is_windows = false
2222
is_windows = true if winrm?
2323

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

28-
# Try to detect windows even for ssh transport
28+
# Try to detect windows even for ssh transport
2929
if !is_windows && detect_windows == true
3030
is_windows = true
3131
end
@@ -41,8 +41,8 @@ def self.load
4141
# unix master family
4242
plat.family("unix").in_family("os")
4343
.detect do
44-
# we want to catch a special case here where cisco commands
45-
# don't return an exit status and still print to stdout
44+
# we want to catch a special case here where cisco commands
45+
# don't return an exit status and still print to stdout
4646
if unix_uname_s =~ /./ && !unix_uname_s.start_with?("Line has invalid autocommand ") && !unix_uname_s.start_with?("The command you have entered")
4747
@platform[:arch] = unix_uname_m
4848
true
@@ -92,7 +92,7 @@ def self.load
9292
true
9393
end
9494

95-
# if we get this far we have to be some type of debian
95+
# if we get this far we have to be some type of debian
9696
@platform[:release] = unix_file_contents("/etc/debian_version").chomp
9797
true
9898
end
@@ -136,9 +136,9 @@ def self.load
136136
# redhat family
137137
plat.family("redhat").in_family("linux")
138138
.detect do
139-
# I am not sure this returns true for all redhats in this family
140-
# for now we are going to just try each platform
141-
# return true unless unix_file_contents('/etc/redhat-release').nil?
139+
# I am not sure this returns true for all redhats in this family
140+
# for now we are going to just try each platform
141+
# return true unless unix_file_contents('/etc/redhat-release').nil?
142142

143143
true
144144
end
@@ -318,16 +318,45 @@ def self.load
318318
end
319319
end
320320

321+
# yocto family
322+
plat.family("yocto").in_family("linux")
323+
.detect do
324+
# /etc/issue isn't specific to yocto, but it's the only way to detect
325+
# the platform because there are no other identifying files
326+
if unix_file_contents("/etc/issue") &&
327+
(unix_file_contents("/etc/issue").match?("Poky") ||
328+
unix_file_contents("/etc/issue").match?("balenaOS"))
329+
true
330+
end
331+
end
332+
plat.name("yocto").title("Yocto Project Linux").in_family("yocto")
333+
.detect do
334+
if unix_file_contents("/etc/issue").match?("Poky")
335+
# assuming the Poky version is preferred over the /etc/version build
336+
@platform[:release] = unix_file_contents("/etc/issue").match('\d+(\.\d+)+')[0]
337+
true
338+
end
339+
end
340+
plat.name("balenaos").title("balenaOS Linux").in_family("yocto")
341+
.detect do
342+
# balenaOS does have the /etc/os-release file
343+
if unix_file_contents("/etc/issue").match?("balenaOS") &&
344+
linux_os_release["NAME"] =~ /balenaos/i
345+
@platform[:release] = linux_os_release["META_BALENA_VERSION"]
346+
true
347+
end
348+
end
349+
321350
# brocade family detected here if device responds to 'uname' command,
322351
# happens when logging in as root
323352
plat.family("brocade").title("Brocade Family").in_family("linux")
324353
.detect do
325354
!brocade_version.nil?
326355
end
327356

328-
# genaric linux
357+
# generic linux
329358
# this should always be last in the linux family list
330-
plat.name("linux").title("Genaric Linux").in_family("linux")
359+
plat.name("linux").title("Generic Linux").in_family("linux")
331360
.detect do
332361
true
333362
end
@@ -426,7 +455,7 @@ def self.load
426455
true
427456
end
428457

429-
# must be some unknown solaris
458+
# must be some unknown solaris
430459
true
431460
end
432461

@@ -457,8 +486,8 @@ def self.load
457486
# bsd family
458487
plat.family("bsd").in_family("unix")
459488
.detect do
460-
# we need a better way to determin this family
461-
# for now we are going to just try each platform
489+
# we need a better way to determine this family
490+
# for now we are going to just try each platform
462491
true
463492
end
464493
plat.family("darwin").in_family("bsd")
@@ -484,7 +513,7 @@ def self.load
484513
end
485514
plat.name("darwin").title("Darwin").in_family("darwin")
486515
.detect do
487-
# must be some other type of darwin
516+
# must be some other type of darwin
488517
@platform[:name] = unix_uname_s.lines[0].chomp
489518
true
490519
end

test/unit/platforms/os_detect_test.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def debian_scan(id, version)
202202

203203
describe "/etc/os-release" do
204204
describe "when not on a wrlinux build" do
205-
it "fail back to genaric linux" do
205+
it "fail back to generic linux" do
206206
os_release = "ID_LIKE=cisco-unkwown\nVERSION=unknown"
207207
files = {
208208
"/etc/os-release" => os_release,
@@ -278,6 +278,31 @@ def debian_scan(id, version)
278278
end
279279
end
280280

281+
describe "yocto" do
282+
it "sets the correct family, name, and release on yocto" do
283+
files = {
284+
"/etc/issue" => "Poky (Yocto Project Reference Distro) 2.7 \\n \\l",
285+
}
286+
platform = scan_with_files("linux", files)
287+
_(platform[:name]).must_equal("yocto")
288+
_(platform[:family]).must_equal("yocto")
289+
_(platform[:release]).must_equal("2.7")
290+
end
291+
end
292+
293+
describe "balenaos" do
294+
it "sets the correct family, name, and release on balenaos" do
295+
files = {
296+
"/etc/issue" => "balenaOS 2.46.1 \n \l",
297+
"/etc/os-release" => "ID=\"balena-os\"\nNAME=\"balenaOS\"\nVERSION=\"2.46.1+rev1\"\nVERSION_ID=\"2.46.1+rev1\"\nPRETTY_NAME=\"balenaOS 2.46.1+rev1\"\nMACHINE=\"raspberrypi3\"\nVARIANT=\"Development\"\nVARIANT_ID=\"dev\"\nMETA_BALENA_VERSION=\"2.46.1\"\nRESIN_BOARD_REV=\"e194600\"\nMETA_RESIN_REV=\"f2295d2\"\nSLUG=\"raspberrypi3\"\n",
298+
}
299+
platform = scan_with_files("linux", files)
300+
_(platform[:name]).must_equal("balenaos")
301+
_(platform[:family]).must_equal("yocto")
302+
_(platform[:release]).must_equal("2.46.1")
303+
end
304+
end
305+
281306
describe "qnx" do
282307
it "sets the correct info for qnx platform" do
283308
platform = scan_with_files("qnx", {})

0 commit comments

Comments
 (0)