diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2019-11-08 14:06:33 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2019-11-08 14:06:33 -0800 |
commit | 8a7e29d52882c39d0c903eef83bd3472e4f334af (patch) | |
tree | a25a7b76416e07ebdb54e569fb0006079cf012db /lib/chef/platform | |
parent | c31625406530b560fab02b11fa2447955ed2faef (diff) | |
download | chef-8a7e29d52882c39d0c903eef83bd3472e4f334af.tar.gz |
Add chef-utils gem with various recipe DSL helpers
This is the implementation of [RFC-087](https://github.com/chef-boneyard/chef-rfc/blob/master/rfc087-distro-sugar-helpers.md)
although some of the specifics have been iterated on and changed.
The documentation will be in the [README.md](https://github.com/chef/chef/tree/master/chef-utils/README.md) once this is merged.
While this PR mostly moves chef-sugar utilities into core-chef via this chef-utils gem, the scope of the chef-utils gem
should be considered larger than just that. As an example this PR moves the Mash class into this gem for reuse in ohai
as well.
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
Diffstat (limited to 'lib/chef/platform')
-rw-r--r-- | lib/chef/platform/query_helpers.rb | 6 | ||||
-rw-r--r-- | lib/chef/platform/rebooter.rb | 2 | ||||
-rw-r--r-- | lib/chef/platform/service_helpers.rb | 99 |
3 files changed, 19 insertions, 88 deletions
diff --git a/lib/chef/platform/query_helpers.rb b/lib/chef/platform/query_helpers.rb index ffcd7e0bd9..3cce4b5d2c 100644 --- a/lib/chef/platform/query_helpers.rb +++ b/lib/chef/platform/query_helpers.rb @@ -1,6 +1,6 @@ # # Author:: Adam Jacob (<adam@chef.io>) -# Copyright:: Copyright 2008-2016, Chef Software Inc. +# Copyright:: Copyright 2008-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,12 +16,14 @@ # limitations under the License. # +require "chef-utils" unless defined?(ChefUtils::CANARY) + class Chef class Platform class << self def windows? - ChefConfig.windows? + ChefUtils.windows? end def windows_nano_server? diff --git a/lib/chef/platform/rebooter.rb b/lib/chef/platform/rebooter.rb index ad17ffad8c..c3378e6313 100644 --- a/lib/chef/platform/rebooter.rb +++ b/lib/chef/platform/rebooter.rb @@ -35,7 +35,7 @@ class Chef reboot_info = node.run_context.reboot_info cmd = case - when Chef::Platform.windows? + when ChefUtils.windows? # should this do /f as well? do we then need a minimum delay to let apps quit? # Use explicit path to shutdown.exe, to protect against https://github.com/chef/chef/issues/5594 windows_shutdown_path = "#{ENV["SYSTEMROOT"]}/System32/shutdown.exe" diff --git a/lib/chef/platform/service_helpers.rb b/lib/chef/platform/service_helpers.rb index 851c3ebffc..a97a6f2ef4 100644 --- a/lib/chef/platform/service_helpers.rb +++ b/lib/chef/platform/service_helpers.rb @@ -1,6 +1,6 @@ # # Author:: Lamont Granquist (<lamont@chef.io>) -# Copyright:: Copyright 2014-2016, Chef Software, Inc. +# Copyright:: Copyright 2014-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,53 +17,22 @@ # require_relative "../chef_class" -require_relative "../mixin/train_helpers" +require "chef-utils" if defined?(ChefUtils::CANARY) class Chef class Platform + # @deprecated, use ChefUtils::DSL::Service instead (via the ChefUtils Universal DSL) class ServiceHelpers class << self - include Chef::Mixin::TrainHelpers - - # 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 - # other service providers are narrowly platform-specific with no - # alternatives. - # - # NOTE: if a system has (for example) chkconfig installed then we - # should report that chkconfig is installed. The fact that a system - # may also have systemd installed does not mean that we do not - # report that systemd is also installed. This module is purely for - # discovery of all the alternatives, handling the priority of the - # different services is NOT a design concern of this module. - # def service_resource_providers providers = [] - if file_exist?(Chef.path_to("/usr/sbin/update-rc.d")) - providers << :debian - end - - if file_exist?(Chef.path_to("/usr/sbin/invoke-rc.d")) - 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 - - if systemd_is_init? - providers << :systemd - end - - if file_exist?(Chef.path_to("/sbin/chkconfig")) - providers << :redhat - end + providers << :debian if ChefUtils::DSL::Service.debianrcd? + providers << :invokercd if ChefUtils::DSL::Service.invokercd? + providers << :upstart if ChefUtils::DSL::Service.upstart? + providers << :insserv if ChefUtils::DSL::Service.insserv? + providers << :systemd if ChefUtils.systemd? + providers << :redhat if ChefUtils::DSL::Service.redhatrcd? providers end @@ -71,54 +40,14 @@ class Chef def config_for_service(service_name) configs = [] - if file_exist?(Chef.path_to("/etc/init.d/#{service_name}")) - configs += %i{initd systemd} - end - - if file_exist?(Chef.path_to("/etc/init/#{service_name}.conf")) - configs << :upstart - end - - if file_exist?(Chef.path_to("/etc/xinetd.d/#{service_name}")) - configs << :xinetd - end - - if file_exist?(Chef.path_to("/etc/rc.d/#{service_name}")) - configs << :etc_rcd - end - - if file_exist?(Chef.path_to("/usr/local/etc/rc.d/#{service_name}")) - configs << :usr_local_etc_rcd - end - - if has_systemd_service_unit?(service_name) || has_systemd_unit?(service_name) - configs << :systemd - end + configs << :initd if ChefUtils::DSL::Service.service_script_exist?(:initd, service_name) + configs << :upstart if ChefUtils::DSL::Service.service_script_exist?(:upstart, service_name) + configs << :xinetd if ChefUtils::DSL::Service.service_script_exist?(:xinetd, service_name) + configs << :systemd if ChefUtils::DSL::Service.service_script_exist?(:systemd, service_name) + configs << :etc_rcd if ChefUtils::DSL::Service.service_script_exist?(:etc_rcd, service_name) configs end - - private - - 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 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 - end - - 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 end |