Skip to content

Yocto Project family and Yocto Linux and balenaOS platform detection #558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 43 additions & 14 deletions lib/train/platforms/detect/specifications/os.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ def self.load

plat.family("windows").in_family("os")
.detect do
# Can't return from a `proc` thus the `is_windows` shenanigans
# 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
# Try to detect windows even for ssh transport
if !is_windows && detect_windows == true
is_windows = true
end
Expand All @@ -41,8 +41,8 @@ def self.load
# 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
# 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
Expand Down Expand Up @@ -92,7 +92,7 @@ def self.load
true
end

# if we get this far we have to be some type of debian
# if we get this far we have to be some type of debian
@platform[:release] = unix_file_contents("/etc/debian_version").chomp
true
end
Expand Down Expand Up @@ -136,9 +136,9 @@ def self.load
# 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?
# 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
Expand Down Expand Up @@ -318,16 +318,45 @@ def self.load
end
end

# yocto family
plat.family("yocto").in_family("linux")
.detect do
# /etc/issue isn't specific to yocto, but it's the only way to detect
# the platform because there are no other identifying files
if unix_file_contents("/etc/issue") &&
(unix_file_contents("/etc/issue").match?("Poky") ||
unix_file_contents("/etc/issue").match?("balenaOS"))
true
end
end
plat.name("yocto").title("Yocto Project Linux").in_family("yocto")
.detect do
if unix_file_contents("/etc/issue").match?("Poky")
# assuming the Poky version is preferred over the /etc/version build
@platform[:release] = unix_file_contents("/etc/issue").match('\d+(\.\d+)+')[0]
true
end
end
plat.name("balenaos").title("balenaOS Linux").in_family("yocto")
.detect do
# balenaOS does have the /etc/os-release file
if unix_file_contents("/etc/issue").match?("balenaOS") &&
linux_os_release["NAME"] =~ /balenaos/i
@platform[:release] = linux_os_release["META_BALENA_VERSION"]
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
# generic linux
# this should always be last in the linux family list
plat.name("linux").title("Genaric Linux").in_family("linux")
plat.name("linux").title("Generic Linux").in_family("linux")
.detect do
true
end
Expand Down Expand Up @@ -426,7 +455,7 @@ def self.load
true
end

# must be some unknown solaris
# must be some unknown solaris
true
end

Expand Down Expand Up @@ -457,8 +486,8 @@ def self.load
# 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
# we need a better way to determine this family
# for now we are going to just try each platform
true
end
plat.family("darwin").in_family("bsd")
Expand All @@ -484,7 +513,7 @@ def self.load
end
plat.name("darwin").title("Darwin").in_family("darwin")
.detect do
# must be some other type of darwin
# must be some other type of darwin
@platform[:name] = unix_uname_s.lines[0].chomp
true
end
Expand Down
27 changes: 26 additions & 1 deletion test/unit/platforms/os_detect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def debian_scan(id, version)

describe "/etc/os-release" do
describe "when not on a wrlinux build" do
it "fail back to genaric linux" do
it "fail back to generic linux" do
os_release = "ID_LIKE=cisco-unkwown\nVERSION=unknown"
files = {
"/etc/os-release" => os_release,
Expand Down Expand Up @@ -278,6 +278,31 @@ def debian_scan(id, version)
end
end

describe "yocto" do
it "sets the correct family, name, and release on yocto" do
files = {
"/etc/issue" => "Poky (Yocto Project Reference Distro) 2.7 \\n \\l",
}
platform = scan_with_files("linux", files)
_(platform[:name]).must_equal("yocto")
_(platform[:family]).must_equal("yocto")
_(platform[:release]).must_equal("2.7")
end
end

describe "balenaos" do
it "sets the correct family, name, and release on balenaos" do
files = {
"/etc/issue" => "balenaOS 2.46.1 \n \l",
"/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",
}
platform = scan_with_files("linux", files)
_(platform[:name]).must_equal("balenaos")
_(platform[:family]).must_equal("yocto")
_(platform[:release]).must_equal("2.46.1")
end
end

describe "qnx" do
it "sets the correct info for qnx platform" do
platform = scan_with_files("qnx", {})
Expand Down