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:
parent
4336aff67d
commit
d82d0fb657
@ -3,21 +3,24 @@ module VagrantPlugins
|
|||||||
module Cap
|
module Cap
|
||||||
class Flavor
|
class Flavor
|
||||||
def self.flavor(machine)
|
def self.flavor(machine)
|
||||||
# Read the version file
|
# Pick up version info from `/etc/os-release`. This file started to exist
|
||||||
output = ""
|
# in CentOS 7. For versions before that (i.e. CentOS 6) just plain `:centos`
|
||||||
machine.communicate.sudo("cat /etc/centos-release") do |_, data|
|
# should do.
|
||||||
output = data
|
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
|
||||||
|
end
|
||||||
|
rescue
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
if version.nil? || version < 1
|
||||||
# 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
|
|
||||||
return :centos
|
return :centos
|
||||||
|
else
|
||||||
|
return "centos_#{version}".to_sym
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -3,21 +3,24 @@ module VagrantPlugins
|
|||||||
module Cap
|
module Cap
|
||||||
class Flavor
|
class Flavor
|
||||||
def self.flavor(machine)
|
def self.flavor(machine)
|
||||||
# Read the version file
|
# Pick up version info from `/etc/os-release`. This file started to exist
|
||||||
output = ""
|
# in RHEL 7. For versions before that (i.e. RHEL 6) just plain `:rhel`
|
||||||
machine.communicate.sudo("cat /etc/redhat-release") do |_, data|
|
# should do.
|
||||||
output = data
|
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
|
||||||
|
end
|
||||||
|
rescue
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
if version.nil? || version < 1
|
||||||
# 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
|
|
||||||
return :rhel
|
return :rhel
|
||||||
|
else
|
||||||
|
return "rhel_#{version}".to_sym
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -21,16 +21,31 @@ describe "VagrantPlugins::GuestCentos::Cap::Flavor" do
|
|||||||
describe ".flavor" do
|
describe ".flavor" do
|
||||||
let(:cap) { caps.get(:flavor) }
|
let(:cap) { caps.get(:flavor) }
|
||||||
|
|
||||||
{
|
# /etc/os-release was added in EL7+
|
||||||
"CentOS Linux 2.4 release 7" => :centos_7,
|
context "without /etc/os-release file" do
|
||||||
"CentOS Linux release 8.1.1911 (Core)" => :centos_8,
|
{
|
||||||
|
"" => :centos
|
||||||
"CentOS" => :centos,
|
}.each do |str, expected|
|
||||||
"banana" => :centos,
|
it "returns #{expected} for #{str}" do
|
||||||
}.each do |str, expected|
|
comm.stub_command("test -f /etc/os-release", exit_code: 1)
|
||||||
it "returns #{expected} for #{str}" do
|
expect(cap.flavor(machine)).to be(expected)
|
||||||
comm.stub_command("cat /etc/centos-release", stdout: str)
|
end
|
||||||
expect(cap.flavor(machine)).to be(expected)
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@ -21,22 +21,31 @@ describe "VagrantPlugins::GuestRedHat::Cap::Flavor" do
|
|||||||
describe ".flavor" do
|
describe ".flavor" do
|
||||||
let(:cap) { caps.get(:flavor) }
|
let(:cap) { caps.get(:flavor) }
|
||||||
|
|
||||||
{
|
# /etc/os-release was added in EL7+
|
||||||
"Red Hat Enterprise Linux 2.4 release 7" => :rhel_7,
|
context "without /etc/os-release file" do
|
||||||
"Red Hat Enterprise Linux release 7" => :rhel_7,
|
{
|
||||||
"Scientific Linux release 7" => :rhel_7,
|
"" => :rhel
|
||||||
"CloudLinux release 7.2 (Valeri Kubasov)" => :rhel_7,
|
}.each do |str, expected|
|
||||||
|
it "returns #{expected} for #{str}" do
|
||||||
"CloudLinux release 8.1.1911 (Valeri Kubasov)" => :rhel_8,
|
comm.stub_command("test -f /etc/os-release", exit_code: 1)
|
||||||
"Red Hat Enterprise Linux release 8" => :rhel_8,
|
expect(cap.flavor(machine)).to be(expected)
|
||||||
|
end
|
||||||
"Red Hat Enterprise Linux" => :rhel,
|
end
|
||||||
"RHEL 6" => :rhel,
|
end
|
||||||
"banana" => :rhel,
|
context "with /etc/os-release file" do
|
||||||
}.each do |str, expected|
|
{
|
||||||
it "returns #{expected} for #{str}" do
|
"7" => :rhel_7,
|
||||||
comm.stub_command("cat /etc/redhat-release", stdout: str)
|
"8" => :rhel_8,
|
||||||
expect(cap.flavor(machine)).to be(expected)
|
"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
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user