guests/linux: Provide common linux detection style

Defines a common and generic linux detection strategy which can be
subclassed and easily reused by providing a custom detection constant.
This commit is contained in:
Chris Roberts 2016-10-11 07:50:34 -07:00
parent 6050b13f43
commit d0549d6e11
5 changed files with 33 additions and 44 deletions

View File

@ -1,21 +1,10 @@
require "vagrant"
require_relative '../linux/guest'
module VagrantPlugins
module GuestDebian
class Guest < Vagrant.plugin("2", :guest)
def detect?(machine)
machine.communicate.test <<-EOH.gsub(/^ {10}/, "")
if test -r /etc/os-release; then
source /etc/os-release && test xdebian = x$ID
elif test -x /usr/bin/lsb_release; then
/usr/bin/lsb_release -i 2>/dev/null | grep -q Debian
elif test -r /etc/issue; then
cat /etc/issue | grep 'Debian'
else
exit 1
fi
EOH
end
class Guest < VagrantPlugins::GuestLinux::Guest
# Name used for guest detection
GUEST_DETECTION_NAME = "debian".freeze
end
end
end

View File

@ -1,8 +1,22 @@
module VagrantPlugins
module GuestLinux
class Guest < Vagrant.plugin("2", :guest)
# Name used for guest detection
GUEST_DETECTION_NAME = "linux".freeze
def detect?(machine)
machine.communicate.test("uname -s | grep 'Linux'")
machine.communicate.test <<-EOH.gsub(/^ */, '')
if test -r /etc/os-release; then
source /etc/os-release && test x#{self.class.const_get(:GUEST_DETECTION_NAME)} = x$ID && exit
fi
if test -x /usr/bin/lsb_release; then
/usr/bin/lsb_release -i 2>/dev/null | grep -qi #{self.class.const_get(:GUEST_DETECTION_NAME)} && exit
fi
if test -r /etc/issue; then
cat /etc/issue | grep -qi #{self.class.const_get(:GUEST_DETECTION_NAME)} && exit
fi
exit 1
EOH
end
end
end

View File

@ -1,9 +1,10 @@
require_relative '../linux/guest'
module VagrantPlugins
module GuestMint
class Guest < Vagrant.plugin("2", :guest)
def detect?(machine)
machine.communicate.test("cat /etc/issue | grep 'Linux Mint'")
end
class Guest < VagrantPlugins::GuestLinux::Guest
# Name used for guest detection
GUEST_DETECTION_NAME = "Linux Mint".freeze
end
end
end

View File

@ -1,11 +1,10 @@
require "vagrant"
require_relative '../linux/guest'
module VagrantPlugins
module GuestTinyCore
class Guest < Vagrant.plugin("2", :guest)
def detect?(machine)
machine.communicate.test("cat /etc/issue | grep 'Core Linux'")
end
class Guest < VagrantPlugins::GuestLinux::Guest
# Name used for guest detection
GUEST_DETECTION_NAME = "Core Linux".freeze
end
end
end

View File

@ -1,24 +1,10 @@
require_relative '../linux/guest'
module VagrantPlugins
module GuestUbuntu
class Guest < Vagrant.plugin("2", :guest)
def detect?(machine)
# This command detects if we are running on Ubuntu. /etc/os-release is
# available on modern Ubuntu versions, but does not exist on 14.04 and
# previous versions, so we fall back to lsb_release.
#
# GH-7524
# GH-7625
#
machine.communicate.test <<-EOH.gsub(/^ {10}/, "")
if test -r /etc/os-release; then
source /etc/os-release && test xubuntu = x$ID
elif test -x /usr/bin/lsb_release; then
/usr/bin/lsb_release -i 2>/dev/null | grep -q Ubuntu
else
exit 1
fi
EOH
end
class Guest < VagrantPlugins::GuestLinux::Guest
# Name used for guest detection
GUEST_DETECTION_NAME = "ubuntu".freeze
end
end
end