guests: make CentOS/RHEL flavor detection more dynamic

Instead of having to update these flavor.rb files every time a
new version of CentOS/RHEL come out let's dynamically pick up
the version (7,8,9) from the machine.

This was inspired slightly by the Rocky Linux guest flavor.rb.
This commit is contained in:
Dusty Mabe 2022-06-03 23:16:31 -04:00 committed by sophia
parent 4336aff67d
commit d82d0fb657
4 changed files with 82 additions and 52 deletions

View File

@ -3,21 +3,24 @@ module VagrantPlugins
module Cap
class Flavor
def self.flavor(machine)
# Read the version file
output = ""
machine.communicate.sudo("cat /etc/centos-release") do |_, data|
output = data
# Pick up version info from `/etc/os-release`. This file started to exist
# in CentOS 7. For versions before that (i.e. CentOS 6) just plain `:centos`
# should do.
version = nil
if machine.communicate.test("test -f /etc/os-release")
begin
machine.communicate.execute("source /etc/os-release && printf $VERSION_ID") do |type, data|
if type == :stdout
version = data.split(".").first.to_i
end
# Detect various flavors we care about
if output =~ /(CentOS)( .+)? 7/i
return :centos_7
elsif output =~ /(CentOS)( .+)? 8/i
return :centos_8
elsif output =~ /(CentOS)( .+)? 9/i
return :centos_9
else
end
rescue
end
end
if version.nil? || version < 1
return :centos
else
return "centos_#{version}".to_sym
end
end
end

View File

@ -3,21 +3,24 @@ module VagrantPlugins
module Cap
class Flavor
def self.flavor(machine)
# Read the version file
output = ""
machine.communicate.sudo("cat /etc/redhat-release") do |_, data|
output = data
# Pick up version info from `/etc/os-release`. This file started to exist
# in RHEL 7. For versions before that (i.e. RHEL 6) just plain `:rhel`
# should do.
version = nil
if machine.communicate.test("test -f /etc/os-release")
begin
machine.communicate.execute("source /etc/os-release && printf $VERSION_ID") do |type, data|
if type == :stdout
version = data.split(".").first.to_i
end
# Detect various flavors we care about
if output =~ /(Red Hat Enterprise|Scientific|Cloud|Virtuozzo)\s*Linux( .+)? release 7/i
return :rhel_7
elsif output =~ /(Red Hat Enterprise|Scientific|Cloud|Virtuozzo)\s*Linux( .+)? release 8/i
return :rhel_8
elsif output =~ /(Red Hat Enterprise|Scientific|Cloud|Virtuozzo)\s*Linux( .+)? release 9/i
return :rhel_9
else
end
rescue
end
end
if version.nil? || version < 1
return :rhel
else
return "rhel_#{version}".to_sym
end
end
end

View File

@ -21,17 +21,32 @@ describe "VagrantPlugins::GuestCentos::Cap::Flavor" do
describe ".flavor" do
let(:cap) { caps.get(:flavor) }
# /etc/os-release was added in EL7+
context "without /etc/os-release file" do
{
"CentOS Linux 2.4 release 7" => :centos_7,
"CentOS Linux release 8.1.1911 (Core)" => :centos_8,
"CentOS" => :centos,
"banana" => :centos,
"" => :centos
}.each do |str, expected|
it "returns #{expected} for #{str}" do
comm.stub_command("cat /etc/centos-release", stdout: str)
comm.stub_command("test -f /etc/os-release", exit_code: 1)
expect(cap.flavor(machine)).to be(expected)
end
end
end
context "with /etc/os-release file" do
{
"7" => :centos_7,
"8" => :centos_8,
"9.0" => :centos_9,
"9.1" => :centos_9,
"" => :centos,
"banana" => :centos,
}.each do |str, expected|
it "returns #{expected} for #{str}" do
comm.stub_command("test -f /etc/os-release", exit_code: 0)
comm.stub_command("source /etc/os-release && printf $VERSION_ID", stdout: str)
expect(cap.flavor(machine)).to be(expected)
end
end
end
end
end

View File

@ -21,23 +21,32 @@ describe "VagrantPlugins::GuestRedHat::Cap::Flavor" do
describe ".flavor" do
let(:cap) { caps.get(:flavor) }
# /etc/os-release was added in EL7+
context "without /etc/os-release file" do
{
"Red Hat Enterprise Linux 2.4 release 7" => :rhel_7,
"Red Hat Enterprise Linux release 7" => :rhel_7,
"Scientific Linux release 7" => :rhel_7,
"CloudLinux release 7.2 (Valeri Kubasov)" => :rhel_7,
"CloudLinux release 8.1.1911 (Valeri Kubasov)" => :rhel_8,
"Red Hat Enterprise Linux release 8" => :rhel_8,
"Red Hat Enterprise Linux" => :rhel,
"RHEL 6" => :rhel,
"banana" => :rhel,
"" => :rhel
}.each do |str, expected|
it "returns #{expected} for #{str}" do
comm.stub_command("cat /etc/redhat-release", stdout: str)
comm.stub_command("test -f /etc/os-release", exit_code: 1)
expect(cap.flavor(machine)).to be(expected)
end
end
end
context "with /etc/os-release file" do
{
"7" => :rhel_7,
"8" => :rhel_8,
"9.0" => :rhel_9,
"9.1" => :rhel_9,
"" => :rhel,
"banana" => :rhel,
}.each do |str, expected|
it "returns #{expected} for #{str}" do
comm.stub_command("test -f /etc/os-release", exit_code: 0)
comm.stub_command("source /etc/os-release && printf $VERSION_ID", stdout: str)
expect(cap.flavor(machine)).to be(expected)
end
end
end
end
end