2015-09-20 11:34:06 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
#
|
|
|
|
# This is heavily based on:
|
|
|
|
#
|
|
|
|
# OHAI https://github.com/chef/ohai
|
|
|
|
# by Adam Jacob, Chef Software Inc
|
|
|
|
#
|
|
|
|
|
2015-09-26 11:12:52 +00:00
|
|
|
require 'vulcano/plugins/backend_detect_darwin'
|
|
|
|
require 'vulcano/plugins/backend_detect_linux'
|
|
|
|
require 'vulcano/plugins/backend_detect_unix'
|
|
|
|
require 'vulcano/plugins/backend_detect_windows'
|
|
|
|
|
2015-09-20 11:34:06 +00:00
|
|
|
class Vulcano::Plugins::Backend
|
|
|
|
class OSCommon
|
2015-09-26 11:12:52 +00:00
|
|
|
include DetectDarwin
|
|
|
|
include DetectLinux
|
|
|
|
include DetectUnix
|
|
|
|
include DetectWindows
|
|
|
|
|
2015-09-20 11:34:06 +00:00
|
|
|
def initialize(backend, platform = nil)
|
|
|
|
@backend = backend
|
|
|
|
@platform = platform || {}
|
|
|
|
detect_family
|
|
|
|
end
|
|
|
|
|
|
|
|
def [](key)
|
|
|
|
@platform[key]
|
|
|
|
end
|
|
|
|
|
2015-09-28 09:15:41 +00:00
|
|
|
OS = {
|
|
|
|
'redhat' => %w{
|
2015-09-28 10:46:46 +00:00
|
|
|
redhat oracle centos fedora amazon scientific xenserver
|
2015-09-28 09:15:41 +00:00
|
|
|
},
|
|
|
|
'debian' => %w{
|
|
|
|
debian ubuntu linuxmint raspbian
|
|
|
|
},
|
|
|
|
'suse' => %w{
|
|
|
|
suse opensuse
|
|
|
|
},
|
|
|
|
'bsd' => %w{
|
2015-09-28 10:14:40 +00:00
|
|
|
freebsd netbsd openbsd darwin
|
2015-09-28 09:15:41 +00:00
|
|
|
},
|
|
|
|
'solaris' => %w{
|
|
|
|
solaris smartos openindiana opensolaris solaris2 nexentacore
|
|
|
|
},
|
2015-09-27 21:55:00 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 09:52:50 +00:00
|
|
|
OS['linux'] = %w{alpine arch coreos exherbo gentoo slackware} +
|
2015-09-28 09:15:41 +00:00
|
|
|
OS['redhat'] + OS['debian'] + OS['suse']
|
2015-09-27 21:55:00 +00:00
|
|
|
|
2015-09-28 10:14:40 +00:00
|
|
|
OS['unix'] = %w{unix aix} + OS['linux'] + OS['solaris'] + OS['bsd']
|
2015-09-27 21:55:00 +00:00
|
|
|
|
|
|
|
# Helper methods to check the OS type
|
2015-09-28 09:15:41 +00:00
|
|
|
# Provides methods in the form of: linux?, unix?, solaris? ...
|
|
|
|
OS.keys.each do |os_family|
|
|
|
|
define_method((os_family+'?').to_sym) do
|
|
|
|
OS[os_family].include?(@platform[:family])
|
|
|
|
end
|
2015-09-27 21:55:00 +00:00
|
|
|
end
|
|
|
|
|
2015-09-20 11:34:06 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def detect_family
|
|
|
|
# if some information is already defined, try to verify it
|
|
|
|
# with the remaining detection
|
|
|
|
unless @platform[:family].nil?
|
|
|
|
# return ok if the preconfigured family yielded a good result
|
|
|
|
return true if detect_family_type
|
|
|
|
# if not, reset the platform to presets and run the full detection
|
|
|
|
# TODO: print an error message in this case, as the instantiating
|
|
|
|
# backend is doing something wrong
|
|
|
|
@platform = {}
|
|
|
|
end
|
2015-09-22 17:34:09 +00:00
|
|
|
|
|
|
|
# TODO: extend base implementation for detecting the family type
|
|
|
|
# to Windows and others
|
|
|
|
case uname_s
|
|
|
|
when /linux/i
|
|
|
|
@platform[:family] = 'linux'
|
|
|
|
when /./
|
|
|
|
@platform[:family] = 'unix'
|
|
|
|
else
|
|
|
|
# Don't know what this is
|
|
|
|
@platform[:family] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# try to detect the platform
|
|
|
|
return nil unless @platform[:family].nil?
|
|
|
|
detect_family_type
|
2015-09-20 11:34:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def detect_family_type
|
2015-09-22 17:34:09 +00:00
|
|
|
pf = @platform[:family]
|
|
|
|
|
|
|
|
return detect_windos if pf == 'windows'
|
|
|
|
return detect_darwin if pf == 'darwin'
|
|
|
|
|
|
|
|
if %w{freebsd netbsd openbsd aix solaris2}.include?(pf)
|
|
|
|
return detect_via_uname
|
2015-09-20 11:34:06 +00:00
|
|
|
end
|
2015-09-22 17:34:09 +00:00
|
|
|
|
|
|
|
# unix based systems combine the above
|
2015-09-27 21:56:25 +00:00
|
|
|
return true if pf == 'unix' and detect_darwin
|
|
|
|
return true if pf == 'unix' and detect_via_uname
|
2015-09-22 17:34:09 +00:00
|
|
|
|
2015-09-26 11:12:52 +00:00
|
|
|
# if we arrive here, we most likey have a regular linux
|
2015-09-22 17:34:09 +00:00
|
|
|
detect_linux
|
2015-09-20 11:34:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_config(path)
|
|
|
|
res = @backend.run_command("test -f #{path} && cat #{path}")
|
|
|
|
# ignore files that can't be read
|
|
|
|
return nil if res.exit_status != 0
|
|
|
|
res.stdout
|
|
|
|
end
|
|
|
|
|
|
|
|
def unix_file?(path)
|
|
|
|
@backend.run_command("test -f #{path}").exit_status == 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|