diff options
Diffstat (limited to 'lib/chef')
79 files changed, 232 insertions, 624 deletions
diff --git a/lib/chef/application.rb b/lib/chef/application.rb index 36cb4401c9..a793a91909 100644 --- a/lib/chef/application.rb +++ b/lib/chef/application.rb @@ -78,7 +78,7 @@ class Chef Chef::Application.fatal!("SIGTERM received, stopping", Chef::Exceptions::SigTerm.new) end - unless Chef::Platform.windows? + unless ChefUtils.windows? trap("QUIT") do logger.info("SIGQUIT received, call stack:\n " + caller.join("\n ")) end @@ -305,7 +305,7 @@ class Chef # win32-process gem exposes some form of :fork for Process # class. So we are separately ensuring that the platform we're # running on is not windows before forking. - chef_config[:client_fork] && Process.respond_to?(:fork) && !Chef::Platform.windows? + chef_config[:client_fork] && Process.respond_to?(:fork) && !ChefUtils.windows? end # Run chef-client once and then exit. If TERM signal is received, ignores the diff --git a/lib/chef/application/client.rb b/lib/chef/application/client.rb index 890ecbd385..ea768a7fc6 100644 --- a/lib/chef/application/client.rb +++ b/lib/chef/application/client.rb @@ -33,7 +33,7 @@ class Chef::Application::Client < Chef::Application::Base long: "--config CONFIG", description: "The configuration file to use." - unless Chef::Platform.windows? + unless ChefUtils.windows? option :daemonize, short: "-d [WAIT]", long: "--daemonize [WAIT]", diff --git a/lib/chef/application/solo.rb b/lib/chef/application/solo.rb index bffe970257..de34387fa4 100644 --- a/lib/chef/application/solo.rb +++ b/lib/chef/application/solo.rb @@ -36,7 +36,7 @@ class Chef::Application::Solo < Chef::Application::Base default: Chef::Config.platform_specific_path("#{Chef::Dist::CONF_DIR}/solo.rb"), description: "The configuration file to use." - unless Chef::Platform.windows? + unless ChefUtils.windows? option :daemonize, short: "-d", long: "--daemonize", diff --git a/lib/chef/chef_class.rb b/lib/chef/chef_class.rb index dccab726ed..dc87ebd271 100644 --- a/lib/chef/chef_class.rb +++ b/lib/chef/chef_class.rb @@ -1,6 +1,6 @@ # # Author:: Lamont Granquist (<lamont@chef.io>) -# Copyright:: Copyright 2015-2016, Chef Software, Inc. +# Copyright:: Copyright 2015-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -239,10 +239,5 @@ class Chef end end - # @api private Only for test dependency injection; not evenly implemented as yet. - def self.path_to(path) - path - end - reset! end diff --git a/lib/chef/chef_fs.rb b/lib/chef/chef_fs.rb index dbf02bd103..bb3408b781 100644 --- a/lib/chef/chef_fs.rb +++ b/lib/chef/chef_fs.rb @@ -53,7 +53,7 @@ require_relative "platform" class Chef module ChefFS def self.windows? - Chef::Platform.windows? + ChefUtils.windows? end end end diff --git a/lib/chef/chef_fs/file_pattern.rb b/lib/chef/chef_fs/file_pattern.rb index f4889fbd86..32c5e80bac 100644 --- a/lib/chef/chef_fs/file_pattern.rb +++ b/lib/chef/chef_fs/file_pattern.rb @@ -245,7 +245,7 @@ class Chef end def self.pattern_special_characters - if Chef::ChefFS.windows? + if ChefUtils.windows? @pattern_special_characters ||= /(\*\*|\*|\?|[\*\?\.\|\(\)\[\]\{\}\+\\\\\^\$])/ else # Unix also supports character regexes and backslashes diff --git a/lib/chef/chef_fs/file_system/chef_server/cookbook_artifacts_dir.rb b/lib/chef/chef_fs/file_system/chef_server/cookbook_artifacts_dir.rb index c99c689a71..974bbc91fd 100644 --- a/lib/chef/chef_fs/file_system/chef_server/cookbook_artifacts_dir.rb +++ b/lib/chef/chef_fs/file_system/chef_server/cookbook_artifacts_dir.rb @@ -86,7 +86,7 @@ class Chef # the symlink without removing the original contents if we # are running on windows # - if Chef::Platform.windows? + if ChefUtils.windows? Dir.rmdir proxy_cookbook_path end end diff --git a/lib/chef/chef_fs/file_system/chef_server/versioned_cookbooks_dir.rb b/lib/chef/chef_fs/file_system/chef_server/versioned_cookbooks_dir.rb index 739a42b124..9a65f70f33 100644 --- a/lib/chef/chef_fs/file_system/chef_server/versioned_cookbooks_dir.rb +++ b/lib/chef/chef_fs/file_system/chef_server/versioned_cookbooks_dir.rb @@ -91,7 +91,7 @@ class Chef # the symlink without removing the original contents if we # are running on windows # - if Chef::Platform.windows? + if ChefUtils.windows? Dir.rmdir proxy_cookbook_path end end diff --git a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb b/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb index de1f071fb3..f156faf817 100644 --- a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb +++ b/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb @@ -1,6 +1,6 @@ # # Author:: John Keiser (<jkeiser@chef.io>) -# Copyright:: Copyright 2012-2016, Chef Software, Inc. +# Copyright:: Copyright 2012-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -44,7 +44,7 @@ require_relative "../../data_handler/role_data_handler" require_relative "../../data_handler/user_data_handler" require_relative "../../data_handler/group_data_handler" require_relative "../../data_handler/container_data_handler" -require_relative "../../../win32/security" if Chef::Platform.windows? +require_relative "../../../win32/security" if ChefUtils.windows? class Chef module ChefFS @@ -112,7 +112,7 @@ class Chef begin ::FileUtils.mkdir_p(path) ::FileUtils.chmod(0700, path) - if Chef::Platform.windows? + if ChefUtils.windows? all_mask = Chef::ReservedNames::Win32::API::Security::GENERIC_ALL administrators = Chef::ReservedNames::Win32::Security::SID.Administrators owner = Chef::ReservedNames::Win32::Security::SID.default_security_object_owner diff --git a/lib/chef/chef_fs/file_system/repository/nodes_dir.rb b/lib/chef/chef_fs/file_system/repository/nodes_dir.rb index 3edd442121..56118ffcd1 100644 --- a/lib/chef/chef_fs/file_system/repository/nodes_dir.rb +++ b/lib/chef/chef_fs/file_system/repository/nodes_dir.rb @@ -1,7 +1,7 @@ # # Author:: John Keiser (<jkeiser@chef.io>) # Author:: Ho-Sheng Hsiao (<hosh@chef.io>) -# Copyright:: Copyright 2012-2016, Chef Software Inc. +# Copyright:: Copyright 2012-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,7 +20,7 @@ require_relative "node" require_relative "directory" require_relative "../exceptions" -require_relative "../../../win32/security" if Chef::Platform.windows? +require_relative "../../../win32/security" if ChefUtils.windows? class Chef module ChefFS @@ -35,7 +35,7 @@ class Chef def create_child(child_name, file_contents = nil) child = super File.chmod(0600, child.file_path) - if Chef::Platform.windows? + if ChefUtils.windows? read_mask = Chef::ReservedNames::Win32::API::Security::GENERIC_READ write_mask = Chef::ReservedNames::Win32::API::Security::GENERIC_WRITE administrators = Chef::ReservedNames::Win32::Security::SID.Administrators diff --git a/lib/chef/chef_fs/knife.rb b/lib/chef/chef_fs/knife.rb index 288acac10a..84301c92e4 100644 --- a/lib/chef/chef_fs/knife.rb +++ b/lib/chef/chef_fs/knife.rb @@ -18,7 +18,7 @@ require_relative "../knife" require "pathname" unless defined?(Pathname) -require "chef/dist" +require_relative "../dist" class Chef module ChefFS diff --git a/lib/chef/chef_fs/path_utils.rb b/lib/chef/chef_fs/path_utils.rb index c490f50c17..b894359e2a 100644 --- a/lib/chef/chef_fs/path_utils.rb +++ b/lib/chef/chef_fs/path_utils.rb @@ -58,7 +58,7 @@ class Chef end def self.regexp_path_separator - Chef::ChefFS.windows? ? '[\/\\\\]' : "/" + ChefUtils.windows? ? '[\/\\\\]' : "/" end # Given a server path, determines if it is absolute. @@ -103,7 +103,7 @@ class Chef # Compares two path fragments according to the case-sentitivity of the host platform. def self.os_path_eq?(left, right) - Chef::ChefFS.windows? ? left.casecmp(right) == 0 : left == right + ChefUtils.windows? ? left.casecmp(right) == 0 : left == right end # Given two general OS-dependent file paths, determines the relative path of the diff --git a/lib/chef/client.rb b/lib/chef/client.rb index 06370f5d07..9e1859eba8 100644 --- a/lib/chef/client.rb +++ b/lib/chef/client.rb @@ -20,7 +20,7 @@ require_relative "config" require_relative "mixin/params_validate" -require_relative "mixin/path_sanity" +require "chef-utils/dsl/path_sanity" unless defined?(ChefUtils::DSL::PathSanity) require_relative "log" require_relative "deprecated" require_relative "server_api" @@ -52,6 +52,7 @@ require_relative "policy_builder" require_relative "request_id" require_relative "platform/rebooter" require_relative "mixin/deprecation" +require "chef-utils" unless defined?(ChefUtils::CANARY) require "ohai" unless defined?(Ohai::System) require "rbconfig" require_relative "dist" @@ -62,8 +63,6 @@ class Chef # The main object in a Chef run. Preps a Chef::Node and Chef::RunContext, # syncs cookbooks if necessary, and triggers convergence. class Client - include Chef::Mixin::PathSanity - extend Chef::Mixin::Deprecation extend Forwardable @@ -251,7 +250,7 @@ class Chef logger.info "#{Chef::Dist::CLIENT.capitalize} pid: #{Process.pid}" logger.info "Targeting node: #{Chef::Config.target_mode.host}" if Chef::Config.target_mode? logger.debug("#{Chef::Dist::CLIENT.capitalize} request_id: #{request_id}") - enforce_path_sanity + ENV["PATH"] = ChefUtils::PathSanity.sanitized_path if Chef::Config[:enforce_path_sanity] if Chef::Config.target_mode? get_ohai_data_remotely @@ -749,7 +748,7 @@ class Chef # @api private # def do_windows_admin_check - if Chef::Platform.windows? + if ChefUtils.windows? logger.trace("Checking for administrator privileges....") if !has_admin_privileges? diff --git a/lib/chef/config.rb b/lib/chef/config.rb index 135ae622f7..6964d37abe 100644 --- a/lib/chef/config.rb +++ b/lib/chef/config.rb @@ -4,7 +4,7 @@ # Author:: AJ Christensen (<aj@chef.io>) # Author:: Mark Mzyk (<mmzyk@chef.io>) # Author:: Kyle Goodwin (<kgoodwin@primerevenue.com>) -# 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"); @@ -28,6 +28,7 @@ require "chef-config/logger" ChefConfig.logger = Chef::Log require "chef-config/config" +require "chef-utils" unless defined?(ChefUtils::CANARY) require_relative "platform/query_helpers" # Ohai::Config defines its own log_level and log_location. When loaded, it will @@ -55,7 +56,7 @@ class Chef default :event_loggers do evt_loggers = [] - if ChefConfig.windows? && !Chef::Platform.windows_nano_server? + if ChefUtils.windows? && !Chef::Platform.windows_nano_server? evt_loggers << :win_evt end evt_loggers diff --git a/lib/chef/dsl/core.rb b/lib/chef/dsl/core.rb index a06adc28d0..f564dd0418 100644 --- a/lib/chef/dsl/core.rb +++ b/lib/chef/dsl/core.rb @@ -1,7 +1,7 @@ #-- # Author:: Adam Jacob (<adam@chef.io>) # Author:: Christopher Walters (<cw@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"); diff --git a/lib/chef/dsl/platform_introspection.rb b/lib/chef/dsl/platform_introspection.rb index 3f79782798..c0856c15cc 100644 --- a/lib/chef/dsl/platform_introspection.rb +++ b/lib/chef/dsl/platform_introspection.rb @@ -1,6 +1,6 @@ # # Author:: Adam Jacob (<adam@chef.io>) -# Copyright:: Copyright 2008-2018, 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,13 +16,15 @@ # limitations under the License. # +require "chef-utils" unless defined?(ChefUtils::CANARY) + class Chef module DSL - # == Chef::DSL::PlatformIntrospection # Provides the DSL for platform-dependent switch logic, such as # #value_for_platform. module PlatformIntrospection + include ChefUtils # Implementation class for determining platform dependent values class PlatformDependentValue @@ -245,27 +247,16 @@ class Chef end end - # Shamelessly stolen from https://github.com/sethvargo/chef-sugar/blob/master/lib/chef/sugar/docker.rb - # Given a node object, returns whether the node is a docker container. - # - # === Parameters - # node:: [Chef::Node] The node to check. - # - # === Returns - # true:: if the current node is a docker container - # false:: if the current node is not a docker container - def docker?(node = run_context.nil? ? nil : run_context.node) - # Using "File.exist?('/.dockerinit') || File.exist?('/.dockerenv')" makes Travis sad, - # and that makes us sad too. - !!(node && node[:virtualization] && node[:virtualization][:systems] && - node[:virtualization][:systems][:docker] && node[:virtualization][:systems][:docker] == "guest") - end - # a simple helper to determine if we're on a windows release pre-2012 / 8 # @return [Boolean] Is the system older than Windows 8 / 2012 def older_than_win_2012_or_8?(node = run_context.nil? ? nil : run_context.node) node["platform_version"].to_f < 6.2 end + + # ^^^^^^ NOTE: PLEASE DO NOT CONTINUE TO ADD THESE KINDS OF PLATFORM_VERSION APIS WITHOUT ^^^^^^^ + # ^^^^^^ GOING THROUGH THE DESIGN REVIEW PROCESS AND ADDRESS THE EXISTING CHEF-SUGAR ONES ^^^^^^^ + # ^^^^^^ DO "THE HARD RIGHT THING" AND ADDRESS THE BROADER PROBLEM AND FIX IT ALL. ^^^^^^^ + end end end diff --git a/lib/chef/event_loggers/windows_eventlog.rb b/lib/chef/event_loggers/windows_eventlog.rb index 7111d975d5..3ace9e6f44 100644 --- a/lib/chef/event_loggers/windows_eventlog.rb +++ b/lib/chef/event_loggers/windows_eventlog.rb @@ -39,7 +39,7 @@ class Chef SOURCE = "#{Chef::Dist::PRODUCT}".freeze def self.available? - Chef::Platform.windows? + ChefUtils.windows? end def initialize diff --git a/lib/chef/file_content_management/deploy.rb b/lib/chef/file_content_management/deploy.rb index 1f158059e6..b048cfb454 100644 --- a/lib/chef/file_content_management/deploy.rb +++ b/lib/chef/file_content_management/deploy.rb @@ -1,6 +1,6 @@ # # Author:: Lamont Granquist (<lamont@chef.io>) -# Copyright:: Copyright 2013-2016, Chef Software Inc. +# Copyright:: Copyright 2013-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,7 +18,7 @@ require_relative "deploy/cp" require_relative "deploy/mv_unix" -if Chef::Platform.windows? +if ChefUtils.windows? require_relative "deploy/mv_windows" end @@ -27,7 +27,7 @@ class Chef class Deploy def self.strategy(atomic_update) if atomic_update - Chef::Platform.windows? ? MvWindows.new : MvUnix.new + ChefUtils.windows? ? MvWindows.new : MvUnix.new else Cp.new end diff --git a/lib/chef/file_content_management/deploy/mv_windows.rb b/lib/chef/file_content_management/deploy/mv_windows.rb index 132e9635d0..0043d5ce22 100644 --- a/lib/chef/file_content_management/deploy/mv_windows.rb +++ b/lib/chef/file_content_management/deploy/mv_windows.rb @@ -1,6 +1,6 @@ # # Author:: Lamont Granquist (<lamont@chef.io>) -# Copyright:: Copyright 2013-2016, Chef Software Inc. +# Copyright:: Copyright 2013-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,7 +22,7 @@ # require_relative "../../platform/query_helpers" -if Chef::Platform.windows? +if ChefUtils.windows? require_relative "../../win32/security" end diff --git a/lib/chef/file_content_management/tempfile.rb b/lib/chef/file_content_management/tempfile.rb index 3e57a131a6..0ec1563f97 100644 --- a/lib/chef/file_content_management/tempfile.rb +++ b/lib/chef/file_content_management/tempfile.rb @@ -67,7 +67,7 @@ class Chef basename = ::File.basename(@new_resource.path, tempfile_extension) # the leading "[.]chef-" here should be considered a public API and should not be changed basename.insert 0, "chef-" - basename.insert 0, "." unless Chef::Platform.windows? # dotfile if we're not on windows + basename.insert 0, "." unless ChefUtils.windows? # dotfile if we're not on windows basename.scrub end diff --git a/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb b/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb index 5141ed89fe..320a03c8aa 100644 --- a/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb +++ b/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb @@ -1,7 +1,7 @@ #-- # Author:: Daniel DeLeo (<dan@chef.io>) # Author:: Tyler Cloke (<tyler@chef.io>) -# Copyright:: Copyright 2012-2016, Chef Software Inc. +# Copyright:: Copyright 2012-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -52,7 +52,7 @@ class Chef error_description.section("Template Context:", "#{exception.source_location}\n#{exception.source_listing}") end - if Chef::Platform.windows? + if ChefUtils.windows? require_relative "../../win32/security" unless Chef::ReservedNames::Win32::Security.has_admin_privileges? diff --git a/lib/chef/http.rb b/lib/chef/http.rb index 5546156031..2c28c8a871 100644 --- a/lib/chef/http.rb +++ b/lib/chef/http.rb @@ -519,7 +519,7 @@ class Chef content_length = response["Content-Length"] if tf.nil? tf = Tempfile.open("chef-rest") - if Chef::Platform.windows? + if ChefUtils.windows? tf.binmode # required for binary files on Windows platforms end end diff --git a/lib/chef/http/socketless_chef_zero_client.rb b/lib/chef/http/socketless_chef_zero_client.rb index b83fdbd41b..fc65b584dd 100644 --- a/lib/chef/http/socketless_chef_zero_client.rb +++ b/lib/chef/http/socketless_chef_zero_client.rb @@ -44,7 +44,7 @@ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. require "chef_zero/server" -require "chef/dist" +require_relative "../dist" class Chef class HTTP diff --git a/lib/chef/knife.rb b/lib/chef/knife.rb index bc25d48f90..e040de9d57 100644 --- a/lib/chef/knife.rb +++ b/lib/chef/knife.rb @@ -1,7 +1,7 @@ # # Author:: Adam Jacob (<adam@chef.io>) # Author:: Christopher Brown (<cb@chef.io>) -# Copyright:: Copyright 2009-2018, Chef Software Inc. +# Copyright:: Copyright 2009-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,6 +20,7 @@ require "forwardable" unless defined?(Forwardable) require_relative "version" require "mixlib/cli" unless defined?(Mixlib::CLI) +require "chef-utils/dsl/path_sanity" unless defined?(ChefUtils::DSL::PathSanity) require_relative "workstation_config_loader" require_relative "mixin/convert_to_class_name" require_relative "mixin/path_sanity" @@ -39,7 +40,7 @@ class Chef Chef::HTTP::HTTPRequest.user_agent = "#{Chef::Dist::PRODUCT} Knife#{Chef::HTTP::HTTPRequest::UA_COMMON}" include Mixlib::CLI - include Chef::Mixin::PathSanity + include ChefUtils::DSL::PathSanity extend Chef::Mixin::ConvertToClassName extend Forwardable @@ -479,7 +480,7 @@ class Chef unless respond_to?(:run) ui.error "You need to add a #run method to your knife command before you can use it" end - enforce_path_sanity + ENV["PATH"] = sanitized_path if Chef::Config[:enforce_path_sanity] maybe_setup_fips Chef::LocalMode.with_server_connectivity do run diff --git a/lib/chef/local_mode.rb b/lib/chef/local_mode.rb index fb41cf8b27..8e83d8fa89 100644 --- a/lib/chef/local_mode.rb +++ b/lib/chef/local_mode.rb @@ -16,7 +16,7 @@ # limitations under the License. require_relative "config" -require_relative "monkey_patches/webrick-utils" if Chef::Platform.windows? +require_relative "monkey_patches/webrick-utils" if ChefUtils.windows? require_relative "dist" class Chef diff --git a/lib/chef/mash.rb b/lib/chef/mash.rb index 404d6d622f..64adfa8672 100644 --- a/lib/chef/mash.rb +++ b/lib/chef/mash.rb @@ -1,232 +1,21 @@ -# Copyright 2009-2016, Dan Kubb - -# Permission is hereby granted, free of charge, to any person obtaining -# a copy of this software and associated documentation files (the -# "Software"), to deal in the Software without restriction, including -# without limitation the rights to use, copy, modify, merge, publish, -# distribute, sublicense, and/or sell copies of the Software, and to -# permit persons to whom the Software is furnished to do so, subject to -# the following conditions: - -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. - -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -# --- -# --- - -# Some portions of blank.rb and mash.rb are verbatim copies of software -# licensed under the MIT license. That license is included below: - -# Copyright 2005-2016, David Heinemeier Hansson - -# Permission is hereby granted, free of charge, to any person obtaining -# a copy of this software and associated documentation files (the -# "Software"), to deal in the Software without restriction, including -# without limitation the rights to use, copy, modify, merge, publish, -# distribute, sublicense, and/or sell copies of the Software, and to -# permit persons to whom the Software is furnished to do so, subject to -# the following conditions: - -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. - -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -# This class has dubious semantics and we only have it so that people can write -# params[:key] instead of params['key']. -class Mash < Hash - - # @param constructor<Object> - # The default value for the mash. Defaults to an empty hash. - # - # @details [Alternatives] - # If constructor is a Hash, a new mash will be created based on the keys of - # the hash and no default value will be set. - def initialize(constructor = {}) - if constructor.is_a?(Hash) - super() - update(constructor) - else - super(constructor) - end - end - - # @param orig<Object> Mash being copied - # - # @return [Object] A new copied Mash - def initialize_copy(orig) - super - # Handle nested values - each do |k, v| - if v.is_a?(Mash) || v.is_a?(Array) - self[k] = v.dup - end - end - self - end - - # @param key<Object> The default value for the mash. Defaults to nil. - # - # @details [Alternatives] - # If key is a Symbol and it is a key in the mash, then the default value will - # be set to the value matching the key. - def default(key = nil) - if key.is_a?(Symbol) && include?(key = key.to_s) - self[key] - else - super - end - end - - alias_method :regular_writer, :[]= unless method_defined?(:regular_writer) - alias_method :regular_update, :update unless method_defined?(:regular_update) - - # @param key<Object> The key to set. - # @param value<Object> - # The value to set the key to. - # - # @see Mash#convert_key - # @see Mash#convert_value - def []=(key, value) - regular_writer(convert_key(key), convert_value(value)) - end - - # internal API for use by Chef's deep merge cache - # @api private - def internal_set(key, value) - regular_writer(key, convert_value(value)) - end - - # @param other_hash<Hash> - # A hash to update values in the mash with. The keys and the values will be - # converted to Mash format. - # - # @return [Mash] The updated mash. - def update(other_hash) - other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } - self - end - - alias_method :merge!, :update - - # @param key<Object> The key to check for. This will be run through convert_key. - # - # @return [Boolean] True if the key exists in the mash. - def key?(key) - super(convert_key(key)) - end - - # def include? def has_key? def member? - alias_method :include?, :key? - alias_method :has_key?, :key? - alias_method :member?, :key? - - # @param key<Object> The key to fetch. This will be run through convert_key. - # @param *extras<Array> Default value. - # - # @return [Object] The value at key or the default value. - def fetch(key, *extras) - super(convert_key(key), *extras) - end - - # @param *indices<Array> - # The keys to retrieve values for. These will be run through +convert_key+. - # - # @return [Array] The values at each of the provided keys - def values_at(*indices) - indices.collect { |key| self[convert_key(key)] } - end - - # @param hash<Hash> The hash to merge with the mash. - # - # @return [Mash] A new mash with the hash values merged in. - def merge(hash) - dup.update(hash) - end - - # @param key<Object> - # The key to delete from the mash.\ - def delete(key) - super(convert_key(key)) - end - - # @param *rejected<Array[(String, Symbol)] The mash keys to exclude. - # - # @return [Mash] A new mash without the selected keys. - # - # @example - # { :one => 1, :two => 2, :three => 3 }.except(:one) - # #=> { "two" => 2, "three" => 3 } - def except(*keys) - super(*keys.map { |k| convert_key(k) }) - end - - # Used to provide the same interface as Hash. - # - # @return [Mash] This mash unchanged. - def stringify_keys!; self end - - # @return [Hash] The mash as a Hash with symbolized keys. - def symbolize_keys - h = Hash.new(default) - each { |key, val| h[key.to_sym] = val } - h - end - - # @return [Hash] The mash as a Hash with string keys. - def to_hash - Hash.new(default).merge(self) - end - - # @return [Mash] Convert a Hash into a Mash - # The input Hash's default value is maintained - def self.from_hash(hash) - mash = Mash.new(hash) - mash.default = hash.default - mash - end - - protected - - # @param key<Object> The key to convert. - # - # @param [Object] - # The converted key. If the key was a symbol, it will be converted to a - # string. - # - # @api private - def convert_key(key) - key.is_a?(Symbol) ? key.to_s : key - end - - # @param value<Object> The value to convert. - # - # @return [Object] - # The converted value. A Hash or an Array of hashes, will be converted to - # their Mash equivalents. - # - # @api private - def convert_value(value) - if value.class == Hash - Mash.from_hash(value) - elsif value.is_a?(Array) - value.collect { |e| convert_value(e) } - else - value - end - end -end +# +# Copyright:: Copyright 2018-2019, Chef Software 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. +# + +require "chef-utils/mash" unless defined?(ChefUtils::Mash) + +# For historical reasons we inject Mash directly into the top level class namespace +Mash = ChefUtils::Mash unless defined?(Mash) diff --git a/lib/chef/mixin/file_class.rb b/lib/chef/mixin/file_class.rb index 5c7e690db4..9ca7b0d770 100644 --- a/lib/chef/mixin/file_class.rb +++ b/lib/chef/mixin/file_class.rb @@ -2,7 +2,7 @@ # Author:: Mark Mzyk <mmzyk@chef.io> # Author:: Seth Chisamore <schisamo@chef.io> # Author:: Bryan McLellan <btm@chef.io> -# Copyright:: Copyright 2011-2016, Chef Software, Inc. +# Copyright:: Copyright 2011-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,7 +23,7 @@ class Chef module FileClass def file_class - @host_os_file ||= if Chef::Platform.windows? + @host_os_file ||= if ChefUtils.windows? require_relative "../win32/file" Chef::ReservedNames::Win32::File else diff --git a/lib/chef/mixin/lazy_module_include.rb b/lib/chef/mixin/lazy_module_include.rb index 34e1fce4f1..4989493d9c 100644 --- a/lib/chef/mixin/lazy_module_include.rb +++ b/lib/chef/mixin/lazy_module_include.rb @@ -1,5 +1,5 @@ # -# Copyright:: Copyright 2011-2016, Chef Software Inc. +# Copyright:: Copyright 2011-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/lib/chef/mixin/path_sanity.rb b/lib/chef/mixin/path_sanity.rb index c441d0770a..de59c063e9 100644 --- a/lib/chef/mixin/path_sanity.rb +++ b/lib/chef/mixin/path_sanity.rb @@ -1,6 +1,6 @@ # # Author:: Seth Chisamore (<schisamo@chef.io>) -# Copyright:: Copyright 2011-2017, Chef Software Inc. +# Copyright:: Copyright 2011-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,49 +18,13 @@ class Chef module Mixin + # @ deprecated module PathSanity - def enforce_path_sanity(env = ENV) if Chef::Config[:enforce_path_sanity] - env["PATH"] = sanitized_path(env) - end - end - - def sanitized_path(env = ENV) - env_path = env["PATH"].nil? ? "" : env["PATH"].dup - path_separator = Chef::Platform.windows? ? ";" : ":" - # ensure the Ruby and Gem bindirs are included - # mainly for 'full-stack' Chef installs - new_paths = env_path.split(path_separator) - [ ruby_bindir, gem_bindir ].compact.each do |path| - new_paths = [ path ] + new_paths unless new_paths.include?(path) - end - sane_paths.each do |path| - new_paths << path unless new_paths.include?(path) - end - new_paths.join(path_separator).encode("utf-8", invalid: :replace, undef: :replace) - end - - private - - def sane_paths - @sane_paths ||= begin - if Chef::Platform.windows? - %w{} - else - %w{/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin} - end + env["PATH"] = ChefUtils::DSL::PathSanity.sanitized_path(env) end end - - def ruby_bindir - RbConfig::CONFIG["bindir"] - end - - def gem_bindir - Gem.bindir - end - end end end diff --git a/lib/chef/mixin/securable.rb b/lib/chef/mixin/securable.rb index d90b906f91..e6dd5043c8 100644 --- a/lib/chef/mixin/securable.rb +++ b/lib/chef/mixin/securable.rb @@ -49,7 +49,7 @@ class Chef end # Windows does not support the sticky or setuid bits - if Chef::Platform.windows? + if ChefUtils.windows? Integer(m) <= 0777 && Integer(m) >= 0 else Integer(m) <= 07777 && Integer(m) >= 0 diff --git a/lib/chef/mixin/shell_out.rb b/lib/chef/mixin/shell_out.rb index 5fc7c249b9..72aa82fc6c 100644 --- a/lib/chef/mixin/shell_out.rb +++ b/lib/chef/mixin/shell_out.rb @@ -1,6 +1,6 @@ #-- # Author:: Daniel DeLeo (<dan@chef.io>) -# Copyright:: Copyright 2010-2018, Chef Software Inc. +# Copyright:: Copyright 2010-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,12 +16,11 @@ # limitations under the License. require "mixlib/shellout" unless defined?(Mixlib::ShellOut::DEFAULT_READ_TIMEOUT) -require_relative "path_sanity" +require "chef-utils/dsl/path_sanity" unless defined?(ChefUtils::DSL::PathSanity) class Chef module Mixin module ShellOut - extend Chef::Mixin::PathSanity # PREFERRED APIS: # @@ -97,7 +96,7 @@ class Chef "LC_ALL" => Chef::Config[:internal_locale], "LANGUAGE" => Chef::Config[:internal_locale], "LANG" => Chef::Config[:internal_locale], - env_path => sanitized_path, + env_path => ChefUtils::DSL::PathSanity.sanitized_path, }.update(options[env_key] || {}) end options @@ -182,7 +181,7 @@ class Chef end def self.env_path - if Chef::Platform.windows? + if ChefUtils.windows? "Path" else "PATH" diff --git a/lib/chef/mixin/template.rb b/lib/chef/mixin/template.rb index 488a80acdb..d584d47d06 100644 --- a/lib/chef/mixin/template.rb +++ b/lib/chef/mixin/template.rb @@ -175,7 +175,7 @@ class Chef # potential issues for the applications that will consume # this template. - if Chef::Platform.windows? + if ChefUtils.windows? output = output.gsub(/\r?\n/, "\r\n") end diff --git a/lib/chef/mixin/train_helpers.rb b/lib/chef/mixin/train_helpers.rb deleted file mode 100644 index 1b031ab338..0000000000 --- a/lib/chef/mixin/train_helpers.rb +++ /dev/null @@ -1,60 +0,0 @@ -#-- -# Author:: Lamont Granquist <lamont@chef.io> -# Copyright:: Copyright 2019, Chef Software 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. - -require "stringio" unless defined?(StringIO) - -class Chef - module Mixin - module TrainHelpers - - # - # FIXME: generally these helpers all use the pattern of checking for target_mode? - # and then if it is we use train. That approach should likely be flipped so that - # even when we're running without target mode we still use inspec in its local - # mode. - # - - # Train wrapper around File.exist? to make it local mode aware. - # - # @param filename filename to check - # @return [Boolean] if it exists - # - def file_exist?(filename) - if Chef::Config.target_mode? - Chef.run_context.transport_connection.file(filename).exist? - else - File.exist?(filename) - end - end - - # XXX: modifications to the StringIO won't get written back - # FIXME: this is very experimental and may be a bad idea and may break at any time - # @api private - # - def file_open(*args, &block) - if Chef::Config.target_mode? - content = Chef.run_context.transport_connection.file(args[0]).content - string_io = StringIO.new content - yield string_io if block_given? - string_io - else - File.open(*args, &block) - end - end - end - end -end diff --git a/lib/chef/mixin/user_context.rb b/lib/chef/mixin/user_context.rb index bd73e818b0..82e8e91669 100644 --- a/lib/chef/mixin/user_context.rb +++ b/lib/chef/mixin/user_context.rb @@ -1,6 +1,6 @@ # # Author:: Adam Edwards (<adamed@chef.io>) -# Copyright:: Copyright (c) 2016 Chef Software, Inc. +# Copyright:: Copyright (c) 2016-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,7 +16,7 @@ # limitations under the License. # -require_relative "../util/windows/logon_session" if Chef::Platform.windows? +require_relative "../util/windows/logon_session" if ChefUtils.windows? class Chef module Mixin @@ -26,7 +26,7 @@ class Chef # When authentication = :local, we use the credentials to create a logon session against the local system, and then try to access the files. # When authentication = :remote, we continue with the current user but pass the provided credentials to the remote system. def with_user_context(user, password, domain = nil, authentication = :remote, &block) - unless Chef::Platform.windows? + unless ChefUtils.windows? raise Exceptions::UnsupportedPlatform, "User context impersonation is supported only on the Windows platform" end diff --git a/lib/chef/mixin/which.rb b/lib/chef/mixin/which.rb index a51963b6c2..6f0d096c1b 100644 --- a/lib/chef/mixin/which.rb +++ b/lib/chef/mixin/which.rb @@ -1,6 +1,6 @@ #-- # Author:: Lamont Granquist <lamont@chef.io> -# Copyright:: Copyright 2010-2018, Chef Software Inc. +# Copyright:: Copyright 2010-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,49 +15,21 @@ # See the License for the specific language governing permissions and # limitations under the License. +require "chef-utils/dsl/which" unless defined?(ChefUtils::DSL::Which) +require "chef-utils/dsl/path_sanity" unless defined?(ChefUtils::DSL::PathSanity) + class Chef module Mixin module Which - require_relative "../chef_class" - - def which(*cmds, extra_path: nil, &block) - where(*cmds, extra_path: extra_path, &block).first || false - end - - def where(*cmds, extra_path: nil, &block) - # NOTE: unnecessarily duplicates function of path_sanity - extra_path ||= [ "/bin", "/usr/bin", "/sbin", "/usr/sbin" ] - paths = env_path.split(File::PATH_SEPARATOR) + Array(extra_path) - cmds.map do |cmd| - paths.map do |path| - filename = Chef.path_to(File.join(path, cmd)) - filename if valid_executable?(filename, &block) - end.compact - end.flatten - end + include ChefUtils::DSL::Which private - # for test stubbing - def env_path - if Chef::Config.target_mode? - Chef.run_context.transport_connection.run_command("echo $PATH").stdout - else - ENV["PATH"] - end - end - - def valid_executable?(filename, &block) - is_executable = - if Chef::Config.target_mode? - connection = Chef.run_context.transport_connection - connection.file(filename).stat[:mode] & 1 && !connection.file(filename).directory? - else - File.executable?(filename) && !File.directory?(filename) - end - return false unless is_executable - - block ? yield(filename) : true + # we dep-inject path sanity into this API for historical reasons + # + # @api private + def __extra_path + ChefUtils::DSL::PathSanity.sane_paths end end end diff --git a/lib/chef/mixin/windows_architecture_helper.rb b/lib/chef/mixin/windows_architecture_helper.rb index cf05a682b5..87118dfe04 100644 --- a/lib/chef/mixin/windows_architecture_helper.rb +++ b/lib/chef/mixin/windows_architecture_helper.rb @@ -1,6 +1,6 @@ # # Author:: Adam Edwards (<adamed@chef.io>) -# Copyright:: Copyright 2013-2016, Chef Software Inc. +# Copyright:: Copyright 2013-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,8 +18,8 @@ require_relative "../exceptions" require_relative "../platform/query_helpers" -require_relative "../win32/process" if Chef::Platform.windows? -require_relative "../win32/system" if Chef::Platform.windows? +require_relative "../win32/process" if ChefUtils.windows? +require_relative "../win32/system" if ChefUtils.windows? class Chef module Mixin @@ -89,7 +89,7 @@ class Chef end def is_i386_process_on_x86_64_windows? - if Chef::Platform.windows? + if ChefUtils.windows? Chef::ReservedNames::Win32::Process.is_wow64_process else false @@ -97,13 +97,13 @@ class Chef end def disable_wow64_file_redirection( node ) - if ( node_windows_architecture(node) == :x86_64) && ::Chef::Platform.windows? + if ( node_windows_architecture(node) == :x86_64) && ::ChefUtils.windows? Chef::ReservedNames::Win32::System.wow64_disable_wow64_fs_redirection end end def restore_wow64_file_redirection( node, original_redirection_state ) - if (node_windows_architecture(node) == :x86_64) && ::Chef::Platform.windows? + if (node_windows_architecture(node) == :x86_64) && ::ChefUtils.windows? Chef::ReservedNames::Win32::System.wow64_revert_wow64_fs_redirection(original_redirection_state) end end diff --git a/lib/chef/mixin/windows_env_helper.rb b/lib/chef/mixin/windows_env_helper.rb index 8036fa2bbe..012b789855 100644 --- a/lib/chef/mixin/windows_env_helper.rb +++ b/lib/chef/mixin/windows_env_helper.rb @@ -1,6 +1,6 @@ # # Author:: Adam Edwards (<adamed@chef.io>) -# Copyright:: Copyright 2013-2016, Chef Software Inc. +# Copyright:: Copyright 2013-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,16 +19,16 @@ require_relative "../exceptions" require_relative "wide_string" require_relative "../platform/query_helpers" -require_relative "../win32/error" if Chef::Platform.windows? -require_relative "../win32/api/system" if Chef::Platform.windows? -require_relative "../win32/api/unicode" if Chef::Platform.windows? +require_relative "../win32/error" if ChefUtils.windows? +require_relative "../win32/api/system" if ChefUtils.windows? +require_relative "../win32/api/unicode" if ChefUtils.windows? class Chef module Mixin module WindowsEnvHelper include Chef::Mixin::WideString - if Chef::Platform.windows? + if ChefUtils.windows? include Chef::ReservedNames::Win32::API::System end diff --git a/lib/chef/node_map.rb b/lib/chef/node_map.rb index dd118c26cf..3c78d6cb9b 100644 --- a/lib/chef/node_map.rb +++ b/lib/chef/node_map.rb @@ -219,6 +219,11 @@ class Chef private + def platform_family_query_helper?(node, m) + method = "#{m}?".to_sym + ChefUtils::DSL::PlatformFamily.respond_to?(method) && ChefUtils::DSL::PlatformFamily.send(method, node) + end + # # Succeeds if: # - no negative matches (!value) @@ -235,11 +240,19 @@ class Chef # Split the blacklist and whitelist blacklist, whitelist = filter_values.partition { |v| v.is_a?(String) && v.start_with?("!") } - # If any blacklist value matches, we don't match - return false if blacklist.any? { |v| v[1..-1] == value } + if attribute == :platform_family + # If any blacklist value matches, we don't match + return false if blacklist.any? { |v| v[1..-1] == value || platform_family_query_helper?(node, v[1..-1]) } - # If the whitelist is empty, or anything matches, we match. - whitelist.empty? || whitelist.any? { |v| v == :all || v == value } + # If the whitelist is empty, or anything matches, we match. + whitelist.empty? || whitelist.any? { |v| v == :all || v == value || platform_family_query_helper?(node, v) } + else + # If any blacklist value matches, we don't match + return false if blacklist.any? { |v| v[1..-1] == value } + + # If the whitelist is empty, or anything matches, we match. + whitelist.empty? || whitelist.any? { |v| v == :all || v == value } + end end def matches_version_list?(node, filters, attribute) 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 diff --git a/lib/chef/provider.rb b/lib/chef/provider.rb index b028f3f276..93d2578414 100644 --- a/lib/chef/provider.rb +++ b/lib/chef/provider.rb @@ -46,6 +46,8 @@ class Chef # includes the "core" DSL and not the "recipe" DSL by design include Chef::DSL::Core + # the class only gets the Universal DSL (no resource_collection at class parsing time) + extend Chef::DSL::Universal # supports the given resource and action (late binding) def self.supports?(resource, action) diff --git a/lib/chef/provider/file.rb b/lib/chef/provider/file.rb index 7f4de1eaef..a2b956c282 100644 --- a/lib/chef/provider/file.rb +++ b/lib/chef/provider/file.rb @@ -453,7 +453,7 @@ class Chef end def load_resource_attributes_from_file(resource) - if Chef::Platform.windows? + if ChefUtils.windows? # This is a work around for CHEF-3554. # OC-6534: is tracking the real fix for this workaround. # Add support for Windows equivalent, or implicit resource diff --git a/lib/chef/provider/ifconfig/redhat.rb b/lib/chef/provider/ifconfig/redhat.rb index 9e34ec897b..44cfe31ea7 100644 --- a/lib/chef/provider/ifconfig/redhat.rb +++ b/lib/chef/provider/ifconfig/redhat.rb @@ -22,7 +22,7 @@ class Chef class Provider class Ifconfig class Redhat < Chef::Provider::Ifconfig - provides :ifconfig, platform_family: %w{fedora rhel amazon} + provides :ifconfig, platform_family: "fedora_derived" def initialize(new_resource, run_context) super(new_resource, run_context) diff --git a/lib/chef/provider/link.rb b/lib/chef/provider/link.rb index 7b17891900..f69d5cb01e 100644 --- a/lib/chef/provider/link.rb +++ b/lib/chef/provider/link.rb @@ -82,7 +82,7 @@ class Chef end def canonicalize(path) - Chef::Platform.windows? ? path.tr("/", '\\') : path + ChefUtils.windows? ? path.tr("/", '\\') : path end def action_create @@ -101,7 +101,7 @@ class Chef # However if the new symlink will point to a file and the current symlink is pointing at a # directory we want to throw an exception and calling ::File.unlink on the directory symlink # will throw the correct ones. - if Chef::Platform.windows? && ::File.directory?(new_resource.to) && + if ChefUtils.windows? && ::File.directory?(new_resource.to) && ::File.directory?(current_resource.target_file) converge_by("unlink existing windows symlink to dir at #{new_resource.target_file}") do ::Dir.unlink(new_resource.target_file) @@ -143,7 +143,7 @@ class Chef def action_delete if current_resource.to # Exists - if Chef::Platform.windows? && ::File.directory?(current_resource.target_file) + if ChefUtils.windows? && ::File.directory?(current_resource.target_file) converge_by("delete link to dir at #{new_resource.target_file}") do ::Dir.delete(new_resource.target_file) logger.info("#{new_resource} deleted") diff --git a/lib/chef/provider/mount/solaris.rb b/lib/chef/provider/mount/solaris.rb index 24f3a1f7ec..293d377079 100644 --- a/lib/chef/provider/mount/solaris.rb +++ b/lib/chef/provider/mount/solaris.rb @@ -27,7 +27,7 @@ class Chef class Mount # Mount Solaris File systems class Solaris < Chef::Provider::Mount - provides :mount, platform: %w{openindiana opensolaris nexentacore omnios solaris2 smartos} + provides :mount, platform_family: "solaris_based" extend Forwardable diff --git a/lib/chef/provider/package/yum.rb b/lib/chef/provider/package/yum.rb index e32457cc0c..ca24ea2f5b 100644 --- a/lib/chef/provider/package/yum.rb +++ b/lib/chef/provider/package/yum.rb @@ -37,7 +37,7 @@ class Chef use_multipackage_api use_package_name_for_source - provides :package, platform_family: %w{fedora amazon rhel} + provides :package, platform_family: "fedora_derived" provides :yum_package diff --git a/lib/chef/provider/registry_key.rb b/lib/chef/provider/registry_key.rb index 5dcb9f1959..c7489caea8 100644 --- a/lib/chef/provider/registry_key.rb +++ b/lib/chef/provider/registry_key.rb @@ -36,7 +36,7 @@ class Chef include Chef::Mixin::Checksum def running_on_windows! - unless Chef::Platform.windows? + unless ChefUtils.windows? raise Chef::Exceptions::Win32NotWindows, "Attempt to manipulate the windows registry on a non-windows node" end end diff --git a/lib/chef/provider/remote_directory.rb b/lib/chef/provider/remote_directory.rb index e7d470916b..4d6039751d 100644 --- a/lib/chef/provider/remote_directory.rb +++ b/lib/chef/provider/remote_directory.rb @@ -111,7 +111,7 @@ class Chef next if managed_files.include?(file) if ::File.directory?(file) - if !Chef::Platform.windows? && file_class.symlink?(file.dup) + if !ChefUtils.windows? && file_class.symlink?(file.dup) # Unix treats dir symlinks as files purge_file(file) else @@ -208,7 +208,7 @@ class Chef # Set the sensitivity level res.sensitive(new_resource.sensitive) res.source(::File.join(source, relative_source_path)) - if Chef::Platform.windows? && files_rights + if ChefUtils.windows? && files_rights files_rights.each_pair do |permission, *args| res.rights(permission, *args) end @@ -244,7 +244,7 @@ class Chef def directory_resource(dir) res = Chef::Resource::Directory.new(dir, run_context) res.cookbook_name = resource_cookbook - if Chef::Platform.windows? && rights + if ChefUtils.windows? && rights # rights are only meant to be applied to the toppest-level directory; # Windows will handle inheritance. if dir == path diff --git a/lib/chef/provider/remote_file/fetcher.rb b/lib/chef/provider/remote_file/fetcher.rb index f007fe40db..93a5618d4a 100644 --- a/lib/chef/provider/remote_file/fetcher.rb +++ b/lib/chef/provider/remote_file/fetcher.rb @@ -1,7 +1,7 @@ # # Author:: Jesse Campbell (<hikeit@gmail.com>) # Author:: Lamont Granquist (<lamont@chef.io>) -# Copyright:: Copyright 2013-2016, Chef Software Inc. +# Copyright:: Copyright 2013-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,7 +24,7 @@ class Chef def self.for_resource(uri, new_resource, current_resource) if network_share?(uri) - unless Chef::Platform.windows? + unless ChefUtils.windows? raise Exceptions::UnsupportedPlatform, "Fetching the file on a network share is supported only on the Windows platform. Please change your source: #{uri}" end diff --git a/lib/chef/provider/remote_file/local_file.rb b/lib/chef/provider/remote_file/local_file.rb index d4b71e24fa..f76c475b1e 100644 --- a/lib/chef/provider/remote_file/local_file.rb +++ b/lib/chef/provider/remote_file/local_file.rb @@ -41,7 +41,7 @@ class Chef def source_path @source_path ||= begin path = URI.unescape(uri.path) - Chef::Platform.windows? ? fix_windows_path(path) : path + ChefUtils.windows? ? fix_windows_path(path) : path end end diff --git a/lib/chef/provider/script.rb b/lib/chef/provider/script.rb index 4282825650..6124615eaa 100644 --- a/lib/chef/provider/script.rb +++ b/lib/chef/provider/script.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"); @@ -18,7 +18,7 @@ require "tempfile" unless defined?(Tempfile) require_relative "execute" -require_relative "../win32/security" if Chef::Platform.windows? +require_relative "../win32/security" if ChefUtils.windows? require "forwardable" unless defined?(Forwardable) class Chef @@ -63,7 +63,7 @@ class Chef end def set_owner_and_group - if Chef::Platform.windows? + if ChefUtils.windows? # And on Windows also this is a no-op if there is no user specified. grant_alternate_user_read_access else diff --git a/lib/chef/provider/service.rb b/lib/chef/provider/service.rb index af96e08b20..c9662b7ba4 100644 --- a/lib/chef/provider/service.rb +++ b/lib/chef/provider/service.rb @@ -1,7 +1,7 @@ # # Author:: AJ Christensen (<aj@hjksolutions.com>) # Author:: Davide Cavalca (<dcavalca@fb.com>) -# Copyright:: Copyright 2008-2017, 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"); @@ -18,10 +18,13 @@ # require_relative "../provider" +require "chef-utils" unless defined?(ChefUtils::CANARY) class Chef class Provider class Service < Chef::Provider + include ChefUtils::DSL::Service + extend ChefUtils::DSL::Service def supports @supports ||= new_resource.supports.dup @@ -245,7 +248,7 @@ class Chef Chef.set_provider_priority_array :service, [ Systemd, Arch ], platform_family: "arch" Chef.set_provider_priority_array :service, [ Systemd, Gentoo ], platform_family: "gentoo" Chef.set_provider_priority_array :service, [ Systemd, Upstart, Insserv, Debian, Invokercd ], platform_family: "debian" - Chef.set_provider_priority_array :service, [ Systemd, Insserv, Redhat ], platform_family: %w{rhel fedora suse amazon} + Chef.set_provider_priority_array :service, [ Systemd, Insserv, Redhat ], platform_family: "rpm_based" end end end diff --git a/lib/chef/provider/service/arch.rb b/lib/chef/provider/service/arch.rb index bb209e22ae..ee6d3dfacd 100644 --- a/lib/chef/provider/service/arch.rb +++ b/lib/chef/provider/service/arch.rb @@ -23,7 +23,7 @@ class Chef::Provider::Service::Arch < Chef::Provider::Service::Init provides :service, platform_family: "arch" def self.supports?(resource, action) - Chef::Platform::ServiceHelpers.config_for_service(resource.service_name).include?(:etc_rcd) + service_script_exist?(:etc_rcd, resource.service_name) end def initialize(new_resource, run_context) diff --git a/lib/chef/provider/service/debian.rb b/lib/chef/provider/service/debian.rb index 9f48504d5e..19e8823fb6 100644 --- a/lib/chef/provider/service/debian.rb +++ b/lib/chef/provider/service/debian.rb @@ -1,6 +1,6 @@ # # Author:: AJ Christensen (<aj@hjksolutions.com>) -# Copyright:: Copyright 2008-2017, Chef Software Inc. +# Copyright:: Copyright 2008-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,15 +22,15 @@ class Chef class Provider class Service class Debian < Chef::Provider::Service::Init - provides :service, platform_family: "debian" do |node| - Chef::Platform::ServiceHelpers.service_resource_providers.include?(:debian) + provides :service, platform_family: "debian" do + debianrcd? end UPDATE_RC_D_ENABLED_MATCHES = %r{/rc[\dS].d/S|not installed}i.freeze UPDATE_RC_D_PRIORITIES = %r{/rc([\dS]).d/([SK])(\d\d)}i.freeze def self.supports?(resource, action) - Chef::Platform::ServiceHelpers.config_for_service(resource.service_name).include?(:initd) + service_script_exist?(:initd, resource.service_name) end def load_current_resource diff --git a/lib/chef/provider/service/init.rb b/lib/chef/provider/service/init.rb index 3911ae3216..a413aeb3f3 100644 --- a/lib/chef/provider/service/init.rb +++ b/lib/chef/provider/service/init.rb @@ -29,7 +29,7 @@ class Chef provides :service, os: "!windows" def self.supports?(resource, action) - Chef::Platform::ServiceHelpers.config_for_service(resource.service_name).include?(:initd) + service_script_exist?(:initd, resource.service_name) end def initialize(new_resource, run_context) diff --git a/lib/chef/provider/service/insserv.rb b/lib/chef/provider/service/insserv.rb index 270ce2c274..be4c52b5ca 100644 --- a/lib/chef/provider/service/insserv.rb +++ b/lib/chef/provider/service/insserv.rb @@ -1,6 +1,6 @@ # # Author:: Bryan McLellan <btm@loftninjas.org> -# Copyright:: Copyright 2011-2017, Chef Software Inc. +# Copyright:: Copyright 2011-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,12 +24,12 @@ class Chef class Service class Insserv < Chef::Provider::Service::Init - provides :service, platform_family: %w{debian rhel fedora suse amazon} do |node| - Chef::Platform::ServiceHelpers.service_resource_providers.include?(:insserv) + provides :service, platform_family: %w{debian rhel fedora suse amazon} do + insserv? end def self.supports?(resource, action) - Chef::Platform::ServiceHelpers.config_for_service(resource.service_name).include?(:initd) + service_script_exist?(:initd, resource.service_name) end def load_current_resource diff --git a/lib/chef/provider/service/invokercd.rb b/lib/chef/provider/service/invokercd.rb index a6e744ba8e..d22c91611b 100644 --- a/lib/chef/provider/service/invokercd.rb +++ b/lib/chef/provider/service/invokercd.rb @@ -1,6 +1,6 @@ # # Author:: AJ Christensen (<aj@hjksolutions.com>) -# Copyright:: Copyright 2008-2016, Chef Software Inc. +# Copyright:: Copyright 2008-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,12 +23,12 @@ class Chef class Service class Invokercd < Chef::Provider::Service::Init - provides :service, platform_family: "debian", override: true do |node| - Chef::Platform::ServiceHelpers.service_resource_providers.include?(:invokercd) + provides :service, platform_family: "debian", override: true do + invokercd? end def self.supports?(resource, action) - Chef::Platform::ServiceHelpers.config_for_service(resource.service_name).include?(:initd) + service_script_exist?(:initd, resource.service_name) end def initialize(new_resource, run_context) diff --git a/lib/chef/provider/service/redhat.rb b/lib/chef/provider/service/redhat.rb index 8751090d44..7e27bce290 100644 --- a/lib/chef/provider/service/redhat.rb +++ b/lib/chef/provider/service/redhat.rb @@ -1,6 +1,6 @@ # # Author:: AJ Christensen (<aj@hjksolutions.com>) -# Copyright:: Copyright 2008-2017, Chef Software Inc. +# Copyright:: Copyright 2008-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -28,15 +28,15 @@ class Chef # @api private attr_accessor :current_run_levels - provides :service, platform_family: %w{rhel fedora suse amazon} do |node| - Chef::Platform::ServiceHelpers.service_resource_providers.include?(:redhat) + provides :service, platform_family: "rpm_based" do + redhatrcd? end CHKCONFIG_ON = /\d:on/.freeze CHKCONFIG_MISSING = /No such/.freeze def self.supports?(resource, action) - Chef::Platform::ServiceHelpers.config_for_service(resource.service_name).include?(:initd) + service_script_exist?(:initd, resource.service_name) end def initialize(new_resource, run_context) diff --git a/lib/chef/provider/service/systemd.rb b/lib/chef/provider/service/systemd.rb index 3b190f0153..c19a3166a4 100644 --- a/lib/chef/provider/service/systemd.rb +++ b/lib/chef/provider/service/systemd.rb @@ -1,7 +1,7 @@ # # Author:: Stephen Haynes (<sh@nomitor.com>) # Author:: Davide Cavalca (<dcavalca@fb.com>) -# Copyright:: Copyright 2011-2018, Chef Software Inc. +# Copyright:: Copyright 2011-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -27,13 +27,13 @@ class Chef::Provider::Service::Systemd < Chef::Provider::Service::Simple include Chef::Mixin::Which provides :service, os: "linux", target_mode: true do |node| - Chef::Platform::ServiceHelpers.service_resource_providers.include?(:systemd) + systemd? end attr_accessor :status_check_success def self.supports?(resource, action) - Chef::Platform::ServiceHelpers.config_for_service(resource.service_name).include?(:systemd) + service_script_exist?(:systemd, resource.service_name) end def load_current_resource diff --git a/lib/chef/provider/service/upstart.rb b/lib/chef/provider/service/upstart.rb index 8b2db84f35..2feb0edd70 100644 --- a/lib/chef/provider/service/upstart.rb +++ b/lib/chef/provider/service/upstart.rb @@ -28,15 +28,15 @@ class Chef # to maintain a local state of service across restart's internal calls attr_accessor :upstart_service_running - provides :service, platform_family: "debian", override: true do |node| - Chef::Platform::ServiceHelpers.service_resource_providers.include?(:upstart) + provides :service, platform_family: "debian", override: true do + upstart? end UPSTART_STATE_FORMAT = %r{\S+ \(?(start|stop)?\)? ?[/ ](\w+)}.freeze # Returns true if the configs for the service name has upstart variable def self.supports?(resource, action) - Chef::Platform::ServiceHelpers.config_for_service(resource.service_name).include?(:upstart) + service_script_exist?(:upstart, resource.service_name) end # Upstart does more than start or stop a service, creating multiple 'states' [1] that a service can be in. diff --git a/lib/chef/provider/subversion.rb b/lib/chef/provider/subversion.rb index dd3ece4786..2f25ed5fc7 100644 --- a/lib/chef/provider/subversion.rb +++ b/lib/chef/provider/subversion.rb @@ -215,7 +215,7 @@ class Chef def svn_binary new_resource.svn_binary || - (Chef::Platform.windows? ? "svn.exe" : "svn") + (ChefUtils.windows? ? "svn.exe" : "svn") end def assert_target_directory_valid! diff --git a/lib/chef/provider/windows_path.rb b/lib/chef/provider/windows_path.rb index 859efdde38..865aefed20 100644 --- a/lib/chef/provider/windows_path.rb +++ b/lib/chef/provider/windows_path.rb @@ -1,6 +1,6 @@ # # Author:: Nimisha Sharad (<nimisha.sharad@msystechnologies.com>) -# Copyright:: Copyright 2008-2017, 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,14 +16,14 @@ # limitations under the License. # -require_relative "../mixin/windows_env_helper" if Chef::Platform.windows? +require_relative "../mixin/windows_env_helper" if ChefUtils.windows? require_relative "../mixin/wide_string" require_relative "../exceptions" class Chef class Provider class WindowsPath < Chef::Provider - include Chef::Mixin::WindowsEnvHelper if Chef::Platform.windows? + include Chef::Mixin::WindowsEnvHelper if ChefUtils.windows? provides :windows_path diff --git a/lib/chef/provider/windows_task.rb b/lib/chef/provider/windows_task.rb index b94b6b875c..93347027a5 100644 --- a/lib/chef/provider/windows_task.rb +++ b/lib/chef/provider/windows_task.rb @@ -1,6 +1,6 @@ # # Author:: Nimisha Sharad (<nimisha.sharad@msystechnologies.com>) -# Copyright:: Copyright 2008-2018, 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"); @@ -18,11 +18,11 @@ require_relative "../mixin/shell_out" require "rexml/document" unless defined?(REXML::Document) -require "iso8601" if Chef::Platform.windows? +require "iso8601" if ChefUtils.windows? require_relative "../mixin/powershell_out" require_relative "../provider" require_relative "../util/path_helper" -require "win32/taskscheduler" if Chef::Platform.windows? +require "win32/taskscheduler" if ChefUtils.windows? class Chef class Provider @@ -30,7 +30,7 @@ class Chef include Chef::Mixin::ShellOut include Chef::Mixin::PowershellOut - if Chef::Platform.windows? + if ChefUtils.windows? include Win32 provides :windows_task diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index a413e3e8d4..6c7214fdd9 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -59,6 +59,7 @@ class Chef extend Chef::Mixin::Provides include Chef::DSL::Universal + extend Chef::DSL::Universal # Bring in `property` and `property_type` include Chef::Mixin::Properties diff --git a/lib/chef/resource/build_essential.rb b/lib/chef/resource/build_essential.rb index 105173bc88..0fb06486d9 100644 --- a/lib/chef/resource/build_essential.rb +++ b/lib/chef/resource/build_essential.rb @@ -1,5 +1,5 @@ # -# Copyright:: 2008-2019, Chef Software, Inc. +# Copyright:: 2008-2019, Chef Software Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -52,7 +52,7 @@ class Chef case node["platform_family"] when "debian" package %w{ autoconf binutils-doc bison build-essential flex gettext ncurses-dev } - when "amazon", "fedora", "rhel" + when fedora_derived? package %w{ autoconf bison flex gcc gcc-c++ gettext kernel-devel make m4 ncurses-devel patch } # Ensure GCC 4 is available on older pre-6 EL diff --git a/lib/chef/resource/file.rb b/lib/chef/resource/file.rb index 57dc6c42f9..a8e3bd3a88 100644 --- a/lib/chef/resource/file.rb +++ b/lib/chef/resource/file.rb @@ -32,7 +32,7 @@ class Chef description "Use the file resource to manage files directly on a node." - if Platform.windows? + if ChefUtils.windows? # Use Windows rights instead of standard *nix permissions state_attrs :checksum, :rights, :deny_rights else diff --git a/lib/chef/resource/link.rb b/lib/chef/resource/link.rb index b6b2601b6e..df4ab81dd1 100644 --- a/lib/chef/resource/link.rb +++ b/lib/chef/resource/link.rb @@ -1,7 +1,7 @@ # # Author:: Adam Jacob (<adam@chef.io>) # Author:: Tyler Cloke (<tyler@chef.io>) -# Copyright:: Copyright 2008-2018, 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"); @@ -79,7 +79,7 @@ class Chef # On certain versions of windows links are not supported. Make # sure we are not on such a platform. def verify_links_supported! - if Chef::Platform.windows? + if ChefUtils.windows? require_relative "../win32/file" begin Chef::ReservedNames::Win32::File.verify_links_supported! diff --git a/lib/chef/resource/remote_directory.rb b/lib/chef/resource/remote_directory.rb index 0d26d169b3..d05c719ac1 100644 --- a/lib/chef/resource/remote_directory.rb +++ b/lib/chef/resource/remote_directory.rb @@ -1,7 +1,7 @@ # # Author:: Adam Jacob (<adam@chef.io>) # Author:: Tyler Cloke (<tyler@chef.io>) -# Copyright:: Copyright 2008-2018, 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"); @@ -37,7 +37,7 @@ class Chef @delete = false end - if Chef::Platform.windows? + if ChefUtils.windows? # create a second instance of the 'rights' attribute (property) rights_attribute(:files_rights) end diff --git a/lib/chef/resource/service.rb b/lib/chef/resource/service.rb index 89f2a7184b..46054f21d9 100644 --- a/lib/chef/resource/service.rb +++ b/lib/chef/resource/service.rb @@ -1,7 +1,7 @@ # # Author:: AJ Christensen (<aj@hjksolutions.com>) # Author:: Tyler Cloke (<tyler@chef.io>) -# Copyright:: Copyright 2008-2018, 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"); @@ -17,6 +17,7 @@ # limitations under the License. # +require "chef-utils/dsl/service" unless defined?(ChefUtils::DSL::Service) require_relative "../resource" require "shellwords" unless defined?(Shellwords) require_relative "../dist" @@ -24,7 +25,11 @@ require_relative "../dist" class Chef class Resource class Service < Chef::Resource + include ChefUtils::DSL::Service + extend ChefUtils::DSL::Service + provides :service, target_mode: true + identity_attr :service_name description "Use the service resource to manage a service." diff --git a/lib/chef/resource/solaris_package.rb b/lib/chef/resource/solaris_package.rb index 163621ec5b..c6cda0ef36 100644 --- a/lib/chef/resource/solaris_package.rb +++ b/lib/chef/resource/solaris_package.rb @@ -1,7 +1,7 @@ # # Author:: Toomas Pelberg (<toomasp@gmx.net>) # Author:: Prabhu Das (<prabhu.das@clogeny.com>) -# Copyright:: Copyright 2013-2016, Chef Software Inc. +# Copyright:: Copyright 2013-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/lib/chef/resource/yum_package.rb b/lib/chef/resource/yum_package.rb index ce9a5654b5..d1d63e64c8 100644 --- a/lib/chef/resource/yum_package.rb +++ b/lib/chef/resource/yum_package.rb @@ -23,7 +23,7 @@ class Chef class Resource class YumPackage < Chef::Resource::Package resource_name :yum_package - provides :package, platform_family: %w{rhel fedora amazon} + provides :package, platform_family: "fedora_derived" description "Use the yum_package resource to install, upgrade, and remove packages with Yum"\ " for the Red Hat and CentOS platforms. The yum_package resource is able to resolve"\ diff --git a/lib/chef/run_context.rb b/lib/chef/run_context.rb index d849679680..70f403ddb6 100644 --- a/lib/chef/run_context.rb +++ b/lib/chef/run_context.rb @@ -635,7 +635,7 @@ class Chef # @return [Train::Plugins::Transport::BaseConnection] # def transport_connection - @transport_connection ||= transport.connection + @transport_connection ||= transport&.connection end # diff --git a/lib/chef/run_lock.rb b/lib/chef/run_lock.rb index 5f9b1ae06f..b25fb567d0 100644 --- a/lib/chef/run_lock.rb +++ b/lib/chef/run_lock.rb @@ -1,6 +1,6 @@ # # Author:: Daniel DeLeo (<dan@chef.io>) -# Copyright:: Copyright 2012-2016, Chef Software Inc. +# Copyright:: Copyright 2012-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,7 +17,7 @@ require_relative "mixin/create_path" require "fcntl" -if Chef::Platform.windows? +if ChefUtils.windows? require_relative "win32/mutex" end require_relative "config" @@ -97,7 +97,7 @@ class Chef # def wait Chef::Log.warn("#{Chef::Dist::PRODUCT} #{runpid} is running, will wait for it to finish and then run.") - if Chef::Platform.windows? + if ChefUtils.windows? mutex.wait else runlock.flock(File::LOCK_EX) @@ -116,7 +116,7 @@ class Chef # Release the system-wide lock. def release if runlock - if Chef::Platform.windows? + if ChefUtils.windows? mutex.release else runlock.flock(File::LOCK_UN) @@ -138,7 +138,7 @@ class Chef # @api private solely for race condition tests def acquire_lock - if Chef::Platform.windows? + if ChefUtils.windows? acquire_win32_mutex else # If we support FD_CLOEXEC, then use it. diff --git a/lib/chef/train_transport.rb b/lib/chef/train_transport.rb index 6a82e8620e..55fc9068fa 100644 --- a/lib/chef/train_transport.rb +++ b/lib/chef/train_transport.rb @@ -1,5 +1,5 @@ # Author:: Bryan McLellan <btm@loftninjas.org> -# Copyright:: Copyright 2018, Chef Software, Inc. +# Copyright:: Copyright 2018-2019, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -92,6 +92,8 @@ class Chef end def self.build_transport(logger = Chef::Log.with_child(subsystem: "transport")) + return nil unless Chef::Config.target_mode? + # TODO: Consider supporting parsing the protocol from a URI passed to `--target` # train_config = {} diff --git a/lib/chef/util/powershell/ps_credential.rb b/lib/chef/util/powershell/ps_credential.rb index 25159e3486..23e8572a61 100644 --- a/lib/chef/util/powershell/ps_credential.rb +++ b/lib/chef/util/powershell/ps_credential.rb @@ -1,7 +1,7 @@ # # Author:: Jay Mundrawala (<jdm@chef.io>) # -# Copyright:: Copyright 2015-2016, Chef Software, Inc. +# Copyright:: Copyright 2015-2019, Chef Software Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -16,7 +16,7 @@ # limitations under the License. # -require_relative "../../win32/crypto" if Chef::Platform.windows? +require_relative "../../win32/crypto" if ChefUtils.windows? class Chef::Util::Powershell class PSCredential diff --git a/lib/chef/util/selinux.rb b/lib/chef/util/selinux.rb index 70fa9298b8..7e477b016c 100644 --- a/lib/chef/util/selinux.rb +++ b/lib/chef/util/selinux.rb @@ -3,7 +3,7 @@ # Author:: Kevin Keane # Author:: Lamont Granquist (<lamont@chef.io>) # -# Copyright:: Copyright 2011-2016, Chef Software Inc. +# Copyright:: Copyright 2011-2019, Chef Software Inc. # Copyright:: Copyright 2013-2016, North County Tech Center, LLC # # License:: Apache License, Version 2.0 diff --git a/lib/chef/util/windows/logon_session.rb b/lib/chef/util/windows/logon_session.rb index b7db9da51c..b24104d61b 100644 --- a/lib/chef/util/windows/logon_session.rb +++ b/lib/chef/util/windows/logon_session.rb @@ -1,7 +1,7 @@ # # Author:: Adam Edwards (<adamed@chef.io>) # -# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# Copyright:: Copyright (c) 2015-2019, Chef Software Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -16,7 +16,7 @@ # limitations under the License. # -require_relative "../../win32/api/security" if Chef::Platform.windows? +require_relative "../../win32/api/security" if ChefUtils.windows? require_relative "../../mixin/wide_string" class Chef diff --git a/lib/chef/win32/eventlog.rb b/lib/chef/win32/eventlog.rb index ca15acf3d8..da6f487ab1 100644 --- a/lib/chef/win32/eventlog.rb +++ b/lib/chef/win32/eventlog.rb @@ -1,7 +1,7 @@ # # Author:: Jay Mundrawala (<jdm@chef.io>) # -# Copyright:: Copyright 2015-2016, Chef Software, Inc. +# Copyright:: Copyright 2015-2019, Chef Software Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -16,7 +16,7 @@ # limitations under the License. # -if Chef::Platform.windows? +if ChefUtils.windows? unless defined? Chef::Win32EventLogLoaded if defined? Windows::Constants %i{INFINITE WAIT_FAILED FORMAT_MESSAGE_IGNORE_INSERTS ERROR_INSUFFICIENT_BUFFER}.each do |c| |