From 0d4954a1d4a8656be920a83615b93306c4387717 Mon Sep 17 00:00:00 2001 From: Nathan Williams Date: Fri, 28 Aug 2015 09:20:33 -0700 Subject: simplify service helpers --- lib/chef/platform/service_helpers.rb | 49 +++++++----------------------------- 1 file changed, 9 insertions(+), 40 deletions(-) diff --git a/lib/chef/platform/service_helpers.rb b/lib/chef/platform/service_helpers.rb index d50812e687..1fc0f6b130 100644 --- a/lib/chef/platform/service_helpers.rb +++ b/lib/chef/platform/service_helpers.rb @@ -16,18 +16,10 @@ # 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' - 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 @@ -56,8 +48,7 @@ class Chef service_resource_providers << :insserv end - # debian >= 6.0 has /etc/init but does not have upstart - if ::File.exist?("/etc/init") && ::File.exist?("/sbin/start") + if ::File.exist?("/sbin/initctl") service_resource_providers << :upstart end @@ -65,7 +56,7 @@ class Chef service_resource_providers << :redhat end - if systemd_sanity_check? + if systemd_is_init? service_resource_providers << :systemd end @@ -95,7 +86,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) configs << :systemd end @@ -104,37 +95,15 @@ class Chef private - def systemctl_path - if @systemctl_path.nil? - @systemctl_path = which("systemctl") - end - @systemctl_path - end - - def systemd_sanity_check? - systemctl_path && File.exist?("/proc/1/comm") && File.open("/proc/1/comm").gets.chomp == "systemd" + def systemd_is_init? + File.exist?("/proc/1/comm") && + IO.read("/proc/1/comm").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 /run /usr/lib ).any? do |cfg_base| + ::File.exist?("#{cfg_base}/systemd/system/#{svc_name}.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 - false - 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 end end end -- cgit v1.2.1 From ed89cf566f0f4f6c33c9c0a77bb557f990607bf5 Mon Sep 17 00:00:00 2001 From: Nathan Williams Date: Fri, 28 Aug 2015 09:22:10 -0700 Subject: ::File --- lib/chef/platform/service_helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/chef/platform/service_helpers.rb b/lib/chef/platform/service_helpers.rb index 1fc0f6b130..ca0ed063da 100644 --- a/lib/chef/platform/service_helpers.rb +++ b/lib/chef/platform/service_helpers.rb @@ -96,7 +96,7 @@ class Chef private def systemd_is_init? - File.exist?("/proc/1/comm") && + ::File.exist?("/proc/1/comm") && IO.read("/proc/1/comm").chomp == "systemd" end -- cgit v1.2.1 From 5a78ea32aaaee75265086c27d273f5c7dc1c111e Mon Sep 17 00:00:00 2001 From: Nathan Williams Date: Fri, 28 Aug 2015 09:33:30 -0700 Subject: include systemd in configs if init.d on systemd-based system --- lib/chef/platform/service_helpers.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/chef/platform/service_helpers.rb b/lib/chef/platform/service_helpers.rb index ca0ed063da..f7680fa8c5 100644 --- a/lib/chef/platform/service_helpers.rb +++ b/lib/chef/platform/service_helpers.rb @@ -68,6 +68,7 @@ class Chef if ::File.exist?("/etc/init.d/#{service_name}") configs << :initd + configs << :systemd if systemd_is_init? end if ::File.exist?("/etc/init/#{service_name}.conf") -- cgit v1.2.1 From 9eecb12994f13f45a8cdcc35e649574c071d9b66 Mon Sep 17 00:00:00 2001 From: Nathan Williams Date: Fri, 28 Aug 2015 09:33:53 -0700 Subject: Revert "include systemd in configs if init.d on systemd-based system" I'm not actually sure where this is used, so maybe leave it be. This reverts commit 5a78ea32aaaee75265086c27d273f5c7dc1c111e. --- lib/chef/platform/service_helpers.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/chef/platform/service_helpers.rb b/lib/chef/platform/service_helpers.rb index f7680fa8c5..ca0ed063da 100644 --- a/lib/chef/platform/service_helpers.rb +++ b/lib/chef/platform/service_helpers.rb @@ -68,7 +68,6 @@ class Chef if ::File.exist?("/etc/init.d/#{service_name}") configs << :initd - configs << :systemd if systemd_is_init? end if ::File.exist?("/etc/init/#{service_name}.conf") -- cgit v1.2.1 From 266873574dca9b6d9122b1f9647847bcd1c16d42 Mon Sep 17 00:00:00 2001 From: Nathan Williams Date: Sat, 29 Aug 2015 07:46:41 -0700 Subject: support service names like redis@6380 derived from units like redis@.service --- lib/chef/platform/service_helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/chef/platform/service_helpers.rb b/lib/chef/platform/service_helpers.rb index ca0ed063da..1fcea44bcd 100644 --- a/lib/chef/platform/service_helpers.rb +++ b/lib/chef/platform/service_helpers.rb @@ -102,7 +102,7 @@ class Chef def has_systemd_service_unit?(svc_name) %w( /etc /run /usr/lib ).any? do |cfg_base| - ::File.exist?("#{cfg_base}/systemd/system/#{svc_name}.service") + ::File.exist?("#{cfg_base}/systemd/system/#{svc_name.gsub(/@.*$/, '@')}.service") end end end -- cgit v1.2.1 From e6b2a07634f8eef374e1edb84387d69bca3044c1 Mon Sep 17 00:00:00 2001 From: Nathan Williams Date: Tue, 1 Sep 2015 09:24:31 -0700 Subject: add support for non-service units --- lib/chef/platform/service_helpers.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/chef/platform/service_helpers.rb b/lib/chef/platform/service_helpers.rb index 1fcea44bcd..92efe24da2 100644 --- a/lib/chef/platform/service_helpers.rb +++ b/lib/chef/platform/service_helpers.rb @@ -102,7 +102,8 @@ class Chef def has_systemd_service_unit?(svc_name) %w( /etc /run /usr/lib ).any? do |cfg_base| - ::File.exist?("#{cfg_base}/systemd/system/#{svc_name.gsub(/@.*$/, '@')}.service") + ::File.exist?("#{cfg_base}/systemd/system/#{svc_name.gsub(/@.*$/, '@')}.service") || + ::File.exist?("#{cfg_base}/systemd/system/#{svc_name}") # TODO: Stop supporting non-service units end end end -- cgit v1.2.1 From 12f7293dea16a566b52fbf15e50f56b4ef80a213 Mon Sep 17 00:00:00 2001 From: Nathan Williams Date: Fri, 23 Oct 2015 22:27:42 -0700 Subject: move gross non-service "services" to separate detection method --- lib/chef/platform/service_helpers.rb | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/chef/platform/service_helpers.rb b/lib/chef/platform/service_helpers.rb index f97eed2581..3ef6ee4ae3 100644 --- a/lib/chef/platform/service_helpers.rb +++ b/lib/chef/platform/service_helpers.rb @@ -88,7 +88,7 @@ class Chef configs << :usr_local_etc_rcd end - if has_systemd_service_unit?(service_name) + if has_systemd_service_unit?(service_name) || has_systemd_unit?(service_name) configs << :systemd end @@ -98,14 +98,22 @@ class Chef private def systemd_is_init? - ::File.exist?("/proc/1/comm") && + ::File.exist?(Chef.path_to("/proc/1/comm")) && IO.read("/proc/1/comm").chomp == "systemd" end def has_systemd_service_unit?(svc_name) - %w( /etc /run /usr/lib ).any? do |cfg_base| - ::File.exist?("#{cfg_base}/systemd/system/#{svc_name.gsub(/@.*$/, '@')}.service") || - ::File.exist?("#{cfg_base}/systemd/system/#{svc_name}") # TODO: Stop supporting non-service units + %w( /etc /run /usr/lib ).any? do |load_path| + ::File.exist?( + Chef.path_to("#{load_path}/systemd/system/#{svc_name.gsub(/@.*$/, '@')}.service") + ) + end + end + + def has_systemd_unit?(svc_name) + # TODO: stop supporting non-service units with service resource + %w( /etc /run /usr/lib ).any? do |load_path| + ::File.exist?(Chef.path_to("#{load_path}/systemd/system/#{svc_name}")) end end end -- cgit v1.2.1 From aaa127a084099c53e54e1ccd01f4ef54f99acf68 Mon Sep 17 00:00:00 2001 From: Nathan Williams Date: Fri, 23 Oct 2015 22:51:48 -0700 Subject: i have no idea what is happening --- spec/unit/provider_resolver_spec.rb | 36 ++++++------------------------------ 1 file changed, 6 insertions(+), 30 deletions(-) diff --git a/spec/unit/provider_resolver_spec.rb b/spec/unit/provider_resolver_spec.rb index 2fb99f610c..773e29a6d7 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 -- cgit v1.2.1 From acf5c365ca3e5c221af386a5ffa7bb1c84a50c31 Mon Sep 17 00:00:00 2001 From: Nathan Williams Date: Fri, 23 Oct 2015 23:20:43 -0700 Subject: fix specs --- lib/chef/platform/service_helpers.rb | 4 ++-- spec/unit/provider_resolver_spec.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/chef/platform/service_helpers.rb b/lib/chef/platform/service_helpers.rb index 3ef6ee4ae3..b392152651 100644 --- a/lib/chef/platform/service_helpers.rb +++ b/lib/chef/platform/service_helpers.rb @@ -47,7 +47,7 @@ class Chef end if ::File.exist?(Chef.path_to("/sbin/initctl")) - service_resource_providers << :upstart + providers << :upstart end if ::File.exist?(Chef.path_to("/sbin/insserv")) @@ -99,7 +99,7 @@ class Chef def systemd_is_init? ::File.exist?(Chef.path_to("/proc/1/comm")) && - IO.read("/proc/1/comm").chomp == "systemd" + ::File.open(Chef.path_to("/proc/1/comm")).gets.chomp == "systemd" end def has_systemd_service_unit?(svc_name) diff --git a/spec/unit/provider_resolver_spec.rb b/spec/unit/provider_resolver_spec.rb index 773e29a6d7..c1256180af 100644 --- a/spec/unit/provider_resolver_spec.rb +++ b/spec/unit/provider_resolver_spec.rb @@ -179,7 +179,7 @@ describe Chef::ProviderResolver do file "usr/local/etc/rc.d/#{service_name}", "" when :systemd file 'proc/1/comm', "systemd\n" - file "/etc/systemd/system/#{service_name}.service", "" + file "etc/systemd/system/#{service_name}.service", "" else raise ArgumentError, config end -- cgit v1.2.1 From 71f1f3b75dfa7c4aea6bd23a468d1a6662a71cef Mon Sep 17 00:00:00 2001 From: Nathan Williams Date: Fri, 23 Oct 2015 23:23:28 -0700 Subject: support split-usr on ubuntu platforms --- lib/chef/platform/service_helpers.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/chef/platform/service_helpers.rb b/lib/chef/platform/service_helpers.rb index b392152651..ae95520e47 100644 --- a/lib/chef/platform/service_helpers.rb +++ b/lib/chef/platform/service_helpers.rb @@ -103,7 +103,7 @@ class Chef end def has_systemd_service_unit?(svc_name) - %w( /etc /run /usr/lib ).any? do |load_path| + %w( /etc /usr/lib /lib /run ).any? do |load_path| ::File.exist?( Chef.path_to("#{load_path}/systemd/system/#{svc_name.gsub(/@.*$/, '@')}.service") ) @@ -112,7 +112,7 @@ class Chef def has_systemd_unit?(svc_name) # TODO: stop supporting non-service units with service resource - %w( /etc /run /usr/lib ).any? do |load_path| + %w( /etc /usr/lib /lib /run ).any? do |load_path| ::File.exist?(Chef.path_to("#{load_path}/systemd/system/#{svc_name}")) end end -- cgit v1.2.1