summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThom May <thom@may.lt>2015-11-05 18:16:03 +0000
committerThom May <thom@may.lt>2015-11-05 18:16:03 +0000
commite9e70d503a92ddeed6881e02463d39036648edd9 (patch)
tree177ed9f67a506389585060005e9259e0da859953
parent5020cc6df7cffe45eddddbedcc2a1e18c55cf1e5 (diff)
parent71f1f3b75dfa7c4aea6bd23a468d1a6662a71cef (diff)
downloadchef-e9e70d503a92ddeed6881e02463d39036648edd9.tar.gz
Merge pull request #3836 from nathwill/svc-helpers-alt
simplify service helpers
-rw-r--r--lib/chef/platform/service_helpers.rb58
-rw-r--r--spec/unit/provider_resolver_spec.rb36
2 files changed, 26 insertions, 68 deletions
diff --git a/lib/chef/platform/service_helpers.rb b/lib/chef/platform/service_helpers.rb
index 8365141c6e..ae95520e47 100644
--- a/lib/chef/platform/service_helpers.rb
+++ b/lib/chef/platform/service_helpers.rb
@@ -16,19 +16,12 @@
# limitations under the License.
#
-# XXX: mixing shellout into a mixin into classes has to be code smell
-require 'chef/mixin/shell_out'
-require 'chef/mixin/which'
require 'chef/chef_class'
class Chef
class Platform
class ServiceHelpers
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
# not necessarily need to linux-specific, but currently all our
@@ -53,23 +46,22 @@ class Chef
providers << :invokercd
end
+ if ::File.exist?(Chef.path_to("/sbin/initctl"))
+ providers << :upstart
+ end
+
if ::File.exist?(Chef.path_to("/sbin/insserv"))
providers << :insserv
end
- # debian >= 6.0 has /etc/init but does not have upstart
- if ::File.exist?(Chef.path_to("/etc/init")) && ::File.exist?(Chef.path_to("/sbin/start"))
- providers << :upstart
+ if systemd_is_init?
+ providers << :systemd
end
if ::File.exist?(Chef.path_to("/sbin/chkconfig"))
providers << :redhat
end
- if systemd_sanity_check?
- providers << :systemd
- end
-
providers
end
@@ -96,7 +88,7 @@ class Chef
configs << :usr_local_etc_rcd
end
- if systemd_sanity_check? && platform_has_systemd_unit?(service_name)
+ if has_systemd_service_unit?(service_name) || has_systemd_unit?(service_name)
configs << :systemd
end
@@ -105,34 +97,24 @@ class Chef
private
- def systemctl_path
- which("systemctl")
+ def systemd_is_init?
+ ::File.exist?(Chef.path_to("/proc/1/comm")) &&
+ ::File.open(Chef.path_to("/proc/1/comm")).gets.chomp == "systemd"
end
- def systemd_sanity_check?
- systemctl_path && File.exist?(Chef.path_to("/proc/1/comm")) && File.open(Chef.path_to("/proc/1/comm")).gets.chomp == "systemd"
- end
-
- def extract_systemd_services(command)
- output = shell_out!(command).stdout
- # first line finds e.g. "sshd.service"
- services = []
- output.each_line do |line|
- fields = line.split
- services << fields[0] if fields[1] == "loaded" || fields[1] == "not-found"
+ def has_systemd_service_unit?(svc_name)
+ %w( /etc /usr/lib /lib /run ).any? do |load_path|
+ ::File.exist?(
+ Chef.path_to("#{load_path}/systemd/system/#{svc_name.gsub(/@.*$/, '@')}.service")
+ )
end
- # this splits off the suffix after the last dot to return "sshd"
- services += services.select {|s| s.match(/\.service$/) }.map { |s| s.sub(/(.*)\.service$/, '\1') }
- rescue Mixlib::ShellOut::ShellCommandFailed
- []
end
- def platform_has_systemd_unit?(service_name)
- services = extract_systemd_services("#{systemctl_path} --all") +
- extract_systemd_services("#{systemctl_path} list-unit-files")
- services.include?(service_name)
- rescue Mixlib::ShellOut::ShellCommandFailed
- false
+ def has_systemd_unit?(svc_name)
+ # TODO: stop supporting non-service units with service resource
+ %w( /etc /usr/lib /lib /run ).any? do |load_path|
+ ::File.exist?(Chef.path_to("#{load_path}/systemd/system/#{svc_name}"))
+ end
end
end
end
diff --git a/spec/unit/provider_resolver_spec.rb b/spec/unit/provider_resolver_spec.rb
index 2fb99f610c..c1256180af 100644
--- a/spec/unit/provider_resolver_spec.rb
+++ b/spec/unit/provider_resolver_spec.rb
@@ -147,25 +147,17 @@ describe Chef::ProviderResolver do
services.each do |service|
case service
when :debian
- directory 'usr/sbin/update-rc.d'
+ file 'usr/sbin/update-rc.d', ''
when :invokercd
- directory 'usr/sbin/invoke-rc.d'
+ file 'usr/sbin/invoke-rc.d', ''
when :insserv
- directory 'sbin/insserv'
+ file 'sbin/insserv', ''
when :upstart
- directory 'etc/init'
- directory 'sbin/start'
+ file 'sbin/initctl', ''
when :redhat
- directory 'sbin/chkconfig'
+ file 'sbin/chkconfig', ''
when :systemd
- file 'bin/systemctl', ''
- # Make systemctl executable
- File.chmod(0755, path_to('bin/systemctl'))
- # Windows doesn't respect executable bit, do this to let Windows users see if they've broken the resolver
- allow(::File).to receive(:executable?) { |p| p == path_to('bin/systemctl') } if windows?
file 'proc/1/comm', "systemd\n"
- mock_shellout_command("/bin/systemctl --all", stdout: "")
- mock_shellout_command("/bin/systemctl list-unit-files", stdout: "")
else
raise ArgumentError, service
end
@@ -186,24 +178,8 @@ describe Chef::ProviderResolver do
when :usr_local_etc_rcd
file "usr/local/etc/rc.d/#{service_name}", ""
when :systemd
- file 'bin/systemctl', ''
- # Make systemctl executable
- File.chmod(0755, path_to("bin/systemctl"))
- # Windows doesn't respect executable bit, do this to let Windows users see if they've broken the resolver
- allow(::File).to receive(:executable?) { |p| p == path_to('bin/systemctl') } if windows?
file 'proc/1/comm', "systemd\n"
- mock_shellout_command("/bin/systemctl --all", stdout: <<-EOM)
- superv loaded
- stinky something-else
- #{service_name} loaded
- blargh not-found
- EOM
- mock_shellout_command("/bin/systemctl list-unit-files", stdout: <<-EOM)
- usuperv loaded
- ustinky something-else
- u#{service_name} loaded
- ublargh not-found
- EOM
+ file "etc/systemd/system/#{service_name}.service", ""
else
raise ArgumentError, config
end