From d0549d6e11e5bc4a5f1dd55872fa1ca2a3fc2a79 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Tue, 11 Oct 2016 07:50:34 -0700 Subject: [PATCH] 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. --- plugins/guests/debian/guest.rb | 19 ++++--------------- plugins/guests/linux/guest.rb | 16 +++++++++++++++- plugins/guests/mint/guest.rb | 9 +++++---- plugins/guests/tinycore/guest.rb | 9 ++++----- plugins/guests/ubuntu/guest.rb | 24 +++++------------------- 5 files changed, 33 insertions(+), 44 deletions(-) diff --git a/plugins/guests/debian/guest.rb b/plugins/guests/debian/guest.rb index 7fac75df0..9fb54c670 100644 --- a/plugins/guests/debian/guest.rb +++ b/plugins/guests/debian/guest.rb @@ -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 diff --git a/plugins/guests/linux/guest.rb b/plugins/guests/linux/guest.rb index 8effae8ad..d86d1daf3 100644 --- a/plugins/guests/linux/guest.rb +++ b/plugins/guests/linux/guest.rb @@ -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 diff --git a/plugins/guests/mint/guest.rb b/plugins/guests/mint/guest.rb index d12e42055..b03d43c83 100644 --- a/plugins/guests/mint/guest.rb +++ b/plugins/guests/mint/guest.rb @@ -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 diff --git a/plugins/guests/tinycore/guest.rb b/plugins/guests/tinycore/guest.rb index ae24f0066..d1fea1aed 100644 --- a/plugins/guests/tinycore/guest.rb +++ b/plugins/guests/tinycore/guest.rb @@ -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 diff --git a/plugins/guests/ubuntu/guest.rb b/plugins/guests/ubuntu/guest.rb index f60108eab..09d02b236 100644 --- a/plugins/guests/ubuntu/guest.rb +++ b/plugins/guests/ubuntu/guest.rb @@ -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