diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2014-11-03 12:16:38 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2014-11-08 11:24:36 -0800 |
commit | 73302b061284f485c9ec67cc17badc48269dc094 (patch) | |
tree | 0f4436c6eb27b67ffa29c6d2a4dc76b2fbc07acb | |
parent | 161df7e0fc6fc2f5fa4377209d5013c7b5457d6e (diff) | |
download | chef-73302b061284f485c9ec67cc17badc48269dc094.tar.gz |
extract which to a mixin, use for systemctl
-rw-r--r-- | lib/chef/mixin/which.rb | 37 | ||||
-rw-r--r-- | lib/chef/platform/service_helpers.rb | 15 | ||||
-rw-r--r-- | lib/chef/util/selinux.rb | 12 |
3 files changed, 51 insertions, 13 deletions
diff --git a/lib/chef/mixin/which.rb b/lib/chef/mixin/which.rb new file mode 100644 index 0000000000..4179c97b62 --- /dev/null +++ b/lib/chef/mixin/which.rb @@ -0,0 +1,37 @@ +#-- +# Author:: Lamont Granquist <lamont@getchef.io> +# Copyright:: Copyright (c) 2010 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +class Chef + module Mixin + module Which + def which(cmd, opts = {}) + extra_path = + if opts[:extra_path].nil? + [ '/bin', '/usr/bin', '/sbin', '/usr/sbin' ] + else + [ opts[:extra_path] ].flatten + end + paths = ENV['PATH'].split(File::PATH_SEPARATOR) + extra_path + paths.each do |path| + filename = File.join(path, cmd) + return filename if File.executable?(filename) + end + false + end + end + end +end diff --git a/lib/chef/platform/service_helpers.rb b/lib/chef/platform/service_helpers.rb index e603591bce..4b7c27b8de 100644 --- a/lib/chef/platform/service_helpers.rb +++ b/lib/chef/platform/service_helpers.rb @@ -18,6 +18,7 @@ # XXX: mixing shellout into a mixin into classes has to be code smell require 'chef/mixin/shell_out' +require 'chef/mixin/which' class Chef class Platform @@ -25,6 +26,7 @@ class Chef class << self include Chef::Mixin::ShellOut + include Chef::Mixin::Which # This helper is mostly used to sort out the mess of different # linux mechanisms that can be used to start services. It does @@ -102,8 +104,15 @@ class Chef private + def systemctl_path + if @systemctl_path.nil? + @systemctl_path = which("systemctl") + end + @systemctl_path + end + def systemd_sanity_check? - ::File.exist?("/bin/systemctl") && File.exist?("/proc/1/comm") && File.open("/proc/1/comm").gets.chomp == "systemd" + systemctl_path && File.exist?("/proc/1/comm") && File.open("/proc/1/comm").gets.chomp == "systemd" end def extract_systemd_services(command) @@ -117,8 +126,8 @@ class Chef end def platform_has_systemd_unit?(service_name) - services = extract_systemd_services("systemctl --all") + - extract_systemd_services("systemctl list-unit-files") + services = extract_systemd_services("#{systemctl_path} --all") + + extract_systemd_services("#{systemctl_path} list-unit-files") services.include?(service_name) rescue Mixlib::ShellOut::ShellCommandFailed false diff --git a/lib/chef/util/selinux.rb b/lib/chef/util/selinux.rb index 92d5756552..778da042e3 100644 --- a/lib/chef/util/selinux.rb +++ b/lib/chef/util/selinux.rb @@ -21,6 +21,7 @@ # limitations under the License. require 'chef/mixin/shell_out' +require 'chef/mixin/which' class Chef class Util @@ -32,6 +33,7 @@ class Chef module Selinux include Chef::Mixin::ShellOut + include Chef::Mixin::Which # We want to initialize below variables once during a # chef-client run therefore they are class variables. @@ -67,15 +69,6 @@ class Chef @@selinuxenabled_path end - def which(cmd) - paths = ENV['PATH'].split(File::PATH_SEPARATOR) + [ '/bin', '/usr/bin', '/sbin', '/usr/sbin' ] - paths.each do |path| - filename = File.join(path, cmd) - return filename if File.executable?(filename) - end - false - end - def check_selinux_enabled? if selinuxenabled_path cmd = shell_out!(selinuxenabled_path, :returns => [0,1]) @@ -97,4 +90,3 @@ class Chef end end end - |