diff options
Diffstat (limited to 'lib')
79 files changed, 190 insertions, 284 deletions
diff --git a/lib/chef/action_collection.rb b/lib/chef/action_collection.rb new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/lib/chef/action_collection.rb diff --git a/lib/chef/application.rb b/lib/chef/application.rb index 81637eabb8..ecc8578937 100644 --- a/lib/chef/application.rb +++ b/lib/chef/application.rb @@ -75,7 +75,7 @@ class Chef Chef::Application.fatal!("SIGTERM received, stopping", Chef::Exceptions::SigTerm.new) end - unless Chef::Platform.windows? + unless ChefHelpers.windows? trap("QUIT") do logger.info("SIGQUIT received, call stack:\n " + caller.join("\n ")) end @@ -288,7 +288,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) && !ChefHelpers.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 0e5584ded3..3b2de4e7e5 100644 --- a/lib/chef/application/client.rb +++ b/lib/chef/application/client.rb @@ -113,7 +113,7 @@ class Chef::Application::Client < Chef::Application description: "Group to set privilege to", proc: nil - unless Chef::Platform.windows? + unless ChefHelpers.windows? option :daemonize, short: "-d [WAIT]", long: "--daemonize [WAIT]", @@ -264,7 +264,7 @@ class Chef::Application::Client < Chef::Application description: "Set maximum duration to wait for another client run to finish, default is indefinitely.", proc: lambda { |s| s.to_i } - if Chef::Platform.windows? + if ChefHelpers.windows? option :fatal_windows_admin_check, short: "-A", long: "--fatal-windows-admin-check", @@ -366,7 +366,7 @@ class Chef::Application::Client < Chef::Application Chef::Config[:client_fork] = !!Chef::Config[:interval] end - if !Chef::Config[:client_fork] && Chef::Config[:interval] && !Chef::Platform.windows? + if !Chef::Config[:client_fork] && Chef::Config[:interval] && !ChefHelpers.windows? Chef::Application.fatal!(unforked_interval_error_message) end @@ -412,7 +412,7 @@ class Chef::Application::Client < Chef::Application def setup_signal_handlers super - unless Chef::Platform.windows? + unless ChefHelpers.windows? SELF_PIPE.replace IO.pipe trap("USR1") do diff --git a/lib/chef/application/solo.rb b/lib/chef/application/solo.rb index ee7d1530ed..bb4550a90e 100644 --- a/lib/chef/application/solo.rb +++ b/lib/chef/application/solo.rb @@ -75,7 +75,7 @@ class Chef::Application::Solo < Chef::Application option :color, long: "--[no-]color", boolean: true, - default: !Chef::Platform.windows?, + default: !ChefHelpers.windows?, description: "Use colored output, defaults to enabled" option :log_level, @@ -111,7 +111,7 @@ class Chef::Application::Solo < Chef::Application description: "Group to set privilege to", proc: nil - unless Chef::Platform.windows? + unless ChefHelpers.windows? option :daemonize, short: "-d", long: "--daemonize", diff --git a/lib/chef/audit/runner.rb b/lib/chef/audit/runner.rb index 847d5efbd5..96de022a23 100644 --- a/lib/chef/audit/runner.rb +++ b/lib/chef/audit/runner.rb @@ -146,7 +146,7 @@ class Chef # Set up the backend for Specinfra/Serverspec. :exec is the local system; on Windows, it is :cmd def configure_specinfra - if Chef::Platform.windows? + if ChefHelpers.windows? Specinfra.configuration.backend = :cmd Specinfra.configuration.os = { family: "windows" } else diff --git a/lib/chef/chef_class.rb b/lib/chef/chef_class.rb index 95b03f1c5c..dec9542d72 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 43a9efd5e2..31400895fc 100644 --- a/lib/chef/chef_fs.rb +++ b/lib/chef/chef_fs.rb @@ -53,7 +53,7 @@ require "chef/platform" class Chef module ChefFS def self.windows? - Chef::Platform.windows? + ChefHelpers.windows? end end end diff --git a/lib/chef/chef_fs/file_pattern.rb b/lib/chef/chef_fs/file_pattern.rb index 835e06eab7..41becca99e 100644 --- a/lib/chef/chef_fs/file_pattern.rb +++ b/lib/chef/chef_fs/file_pattern.rb @@ -239,7 +239,7 @@ class Chef end def self.pattern_special_characters - if Chef::ChefFS.windows? + if ChefHelpers.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 e4df7858a7..2eef494ba7 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 ChefHelpers.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 94c1a5b695..24dce29cf7 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 ChefHelpers.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 e07a73a118..26244e7fe4 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 @@ -44,7 +44,7 @@ require "chef/chef_fs/data_handler/role_data_handler" require "chef/chef_fs/data_handler/user_data_handler" require "chef/chef_fs/data_handler/group_data_handler" require "chef/chef_fs/data_handler/container_data_handler" -require "chef/win32/security" if Chef::Platform.windows? +require "chef/win32/security" if ChefHelpers.windows? class Chef module ChefFS @@ -111,7 +111,7 @@ class Chef child_paths[name].each do |path| begin Dir.mkdir(path, 0700) - if Chef::Platform.windows? + if ChefHelpers.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 a0dd0c9e51..41604f2de6 100644 --- a/lib/chef/chef_fs/file_system/repository/nodes_dir.rb +++ b/lib/chef/chef_fs/file_system/repository/nodes_dir.rb @@ -20,7 +20,7 @@ require "chef/chef_fs/file_system/repository/node" require "chef/chef_fs/file_system/repository/directory" require "chef/chef_fs/file_system/exceptions" -require "chef/win32/security" if Chef::Platform.windows? +require "chef/win32/security" if ChefHelpers.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 ChefHelpers.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/path_utils.rb b/lib/chef/chef_fs/path_utils.rb index 4de23f8266..3a76311586 100644 --- a/lib/chef/chef_fs/path_utils.rb +++ b/lib/chef/chef_fs/path_utils.rb @@ -57,7 +57,7 @@ class Chef end def self.regexp_path_separator - Chef::ChefFS.windows? ? '[\/\\\\]' : "/" + ChefHelpers.windows? ? '[\/\\\\]' : "/" end # Given a server path, determines if it is absolute. @@ -101,7 +101,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 + ChefHelpers.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 33d625c54e..5fd3a11562 100644 --- a/lib/chef/client.rb +++ b/lib/chef/client.rb @@ -20,7 +20,6 @@ require "chef/config" require "chef/mixin/params_validate" -require "chef/mixin/path_sanity" require "chef/log" require "chef/deprecated" require "chef/server_api" @@ -53,6 +52,7 @@ require "chef/policy_builder" require "chef/request_id" require "chef/platform/rebooter" require "chef/mixin/deprecation" +require "chef-helpers" require "ohai" require "rbconfig" @@ -61,8 +61,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 # @@ -256,7 +254,7 @@ class Chef logger.info("Platform: #{RUBY_PLATFORM}") logger.info "Chef-client pid: #{Process.pid}" logger.debug("Chef-client request_id: #{request_id}") - enforce_path_sanity + ENV['PATH'] = ChefHelpers::PathSanity.sanitized_path if Chef::Config[:enforce_path_sanity] run_ohai generate_guid @@ -825,7 +823,7 @@ class Chef # @api private # def do_windows_admin_check - if Chef::Platform.windows? + if ChefHelpers.windows? logger.trace("Checking for administrator privileges....") if !has_admin_privileges? diff --git a/lib/chef/config.rb b/lib/chef/config.rb index f5466ae145..d2d79b9266 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-2018, 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-helpers" require "chef/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 ChefHelpers.windows? && !Chef::Platform.windows_nano_server? evt_loggers << :win_evt end evt_loggers diff --git a/lib/chef/data_collector.rb b/lib/chef/data_collector.rb index 5cc92a34e4..04238afa89 100644 --- a/lib/chef/data_collector.rb +++ b/lib/chef/data_collector.rb @@ -2,7 +2,7 @@ # Author:: Adam Leff (<adamleff@chef.io>) # Author:: Ryan Cragun (<ryan@chef.io>) # -# Copyright:: Copyright 2012-2017, 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"); @@ -132,7 +132,7 @@ class Chef def run_started(current_run_status) update_run_status(current_run_status) - message = Chef::DataCollector::Messages.run_start_message(current_run_status) + message = Chef::DataCollector::Messages.construct_message(current_run_status) disable_reporter_on_error do send_to_data_collector(message) end diff --git a/lib/chef/dsl/core.rb b/lib/chef/dsl/core.rb index d7c5b6a006..77f9b567c9 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 e7f0c60f0d..7bd0fb0361 100644 --- a/lib/chef/dsl/platform_introspection.rb +++ b/lib/chef/dsl/platform_introspection.rb @@ -16,13 +16,15 @@ # limitations under the License. # +require 'chef-helpers' + class Chef module DSL - # == Chef::DSL::PlatformIntrospection # Provides the DSL for platform-dependent switch logic, such as # #value_for_platform. module PlatformIntrospection + include ChefHelpers # Implementation class for determining platform dependent values class PlatformDependentValue @@ -243,27 +245,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 COMMUNITY RFC 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/dsl/recipe.rb b/lib/chef/dsl/recipe.rb index 9da812cec0..b00502c186 100644 --- a/lib/chef/dsl/recipe.rb +++ b/lib/chef/dsl/recipe.rb @@ -1,7 +1,7 @@ # # Author:: Adam Jacob (<adam@chef.io>) # Author:: Christopher Walters (<cw@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"); @@ -27,6 +27,7 @@ require "chef/dsl/reboot_pending" require "chef/dsl/audit" require "chef/dsl/powershell" require "chef/dsl/core" +require "chef/dsl/universal" require "chef/mixin/lazy_module_include" class Chef diff --git a/lib/chef/event_loggers/windows_eventlog.rb b/lib/chef/event_loggers/windows_eventlog.rb index 950477556a..0ed16997a4 100644 --- a/lib/chef/event_loggers/windows_eventlog.rb +++ b/lib/chef/event_loggers/windows_eventlog.rb @@ -38,7 +38,7 @@ class Chef SOURCE = "Chef".freeze def self.available? - Chef::Platform.windows? + ChefHelpers.windows? end def initialize diff --git a/lib/chef/file_content_management/deploy.rb b/lib/chef/file_content_management/deploy.rb index 1e3ae55c21..751ca76b6b 100644 --- a/lib/chef/file_content_management/deploy.rb +++ b/lib/chef/file_content_management/deploy.rb @@ -18,7 +18,7 @@ require "chef/file_content_management/deploy/cp" require "chef/file_content_management/deploy/mv_unix" -if Chef::Platform.windows? +if ChefHelpers.windows? require "chef/file_content_management/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() + ChefHelpers.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 210400bc08..6e534c1ff0 100644 --- a/lib/chef/file_content_management/deploy/mv_windows.rb +++ b/lib/chef/file_content_management/deploy/mv_windows.rb @@ -22,7 +22,7 @@ # require "chef/platform/query_helpers" -if Chef::Platform.windows? +if ChefHelpers.windows? require "chef/win32/security" end diff --git a/lib/chef/file_content_management/tempfile.rb b/lib/chef/file_content_management/tempfile.rb index c3bf07d92b..4aa8add8e6 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 ChefHelpers.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 1cc147db7a..1cce0538d6 100644 --- a/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb +++ b/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb @@ -51,7 +51,7 @@ class Chef error_description.section("Template Context:", "#{exception.source_location}\n#{exception.source_listing}") end - if Chef::Platform.windows? + if ChefHelpers.windows? require "chef/win32/security" if !Chef::ReservedNames::Win32::Security.has_admin_privileges? diff --git a/lib/chef/http.rb b/lib/chef/http.rb index 3f815b9eff..c6e1530707 100644 --- a/lib/chef/http.rb +++ b/lib/chef/http.rb @@ -514,7 +514,7 @@ class Chef content_length = response["Content-Length"] if tf.nil? tf = Tempfile.open("chef-rest") - if Chef::Platform.windows? + if ChefHelpers.windows? tf.binmode # required for binary files on Windows platforms end end diff --git a/lib/chef/knife.rb b/lib/chef/knife.rb index 6e525bdf3d..13c397b180 100644 --- a/lib/chef/knife.rb +++ b/lib/chef/knife.rb @@ -440,7 +440,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'] = ChefHelpers::PathSanity.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 5630a54335..72d40d8c81 100644 --- a/lib/chef/local_mode.rb +++ b/lib/chef/local_mode.rb @@ -15,7 +15,7 @@ # See the License for the specific language governing permissions and # limitations under the License. require "chef/config" -if Chef::Platform.windows? +if ChefHelpers.windows? if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.1") require "chef/monkey_patches/webrick-utils" end diff --git a/lib/chef/mixin/file_class.rb b/lib/chef/mixin/file_class.rb index 166dd5796a..d27116014d 100644 --- a/lib/chef/mixin/file_class.rb +++ b/lib/chef/mixin/file_class.rb @@ -23,7 +23,7 @@ class Chef module FileClass def file_class - @host_os_file ||= if Chef::Platform.windows? + @host_os_file ||= if ChefHelpers.windows? require "chef/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..6705758c4b 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"); @@ -52,6 +52,7 @@ class Chef infector = Module.new do define_method(:included) do |subklass| super(subklass) + puts "infecting #{subclass}" subklass.extend(infector) parent_klass.descendants.push(subklass) end @@ -68,6 +69,7 @@ class Chef super classes.each do |klass| descendants.each do |descendant| + puts "recursively including #{klass} into #{descendant}" descendant.send(:include, klass) end end diff --git a/lib/chef/mixin/path_sanity.rb b/lib/chef/mixin/path_sanity.rb index c441d0770a..51f0595b4f 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-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,48 +19,12 @@ class Chef module Mixin module PathSanity - + # FIXME: deprecatad 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"] = ChefHelpers::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 eb59f184d1..a94cbc8056 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 ChefHelpers.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 25a8ee989c..169dbda1fc 100644 --- a/lib/chef/mixin/shell_out.rb +++ b/lib/chef/mixin/shell_out.rb @@ -16,12 +16,11 @@ # limitations under the License. require "mixlib/shellout" -require "chef/mixin/path_sanity" +require "chef-helpers/path_sanity" class Chef module Mixin module ShellOut - extend Chef::Mixin::PathSanity # PREFERRED APIS: # @@ -162,7 +161,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 => ChefHelpers::PathSanity.sanitized_path, }.update(options[env_key] || {}) end options @@ -244,7 +243,7 @@ class Chef end def self.env_path - if Chef::Platform.windows? + if ChefHelpers.windows? "Path" else "PATH" diff --git a/lib/chef/mixin/template.rb b/lib/chef/mixin/template.rb index bb811aa758..812d1b3025 100644 --- a/lib/chef/mixin/template.rb +++ b/lib/chef/mixin/template.rb @@ -173,7 +173,7 @@ class Chef # potential issues for the applications that will consume # this template. - if Chef::Platform.windows? + if ChefHelpers.windows? output = output.gsub(/\r?\n/, "\r\n") end diff --git a/lib/chef/mixin/user_context.rb b/lib/chef/mixin/user_context.rb index f5c2da1389..92003937c5 100644 --- a/lib/chef/mixin/user_context.rb +++ b/lib/chef/mixin/user_context.rb @@ -16,7 +16,7 @@ # limitations under the License. # -require "chef/util/windows/logon_session" if Chef::Platform.windows? +require "chef/util/windows/logon_session" if ChefHelpers.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 ChefHelpers.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 8a825f55c9..ae8f38eaf0 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-2017, Chef Software Inc. +# Copyright:: Copyright 2010-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -28,7 +28,7 @@ class Chef 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 = File.join(path, cmd) filename if valid_executable?(filename, &block) end.compact end.flatten diff --git a/lib/chef/mixin/windows_architecture_helper.rb b/lib/chef/mixin/windows_architecture_helper.rb index 67c34a85a1..f2906d110a 100644 --- a/lib/chef/mixin/windows_architecture_helper.rb +++ b/lib/chef/mixin/windows_architecture_helper.rb @@ -18,8 +18,8 @@ require "chef/exceptions" require "chef/platform/query_helpers" -require "chef/win32/process" if Chef::Platform.windows? -require "chef/win32/system" if Chef::Platform.windows? +require "chef/win32/process" if ChefHelpers.windows? +require "chef/win32/system" if ChefHelpers.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 ChefHelpers.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) && ::ChefHelpers.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) && ::ChefHelpers.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 610c0cd075..756ecd25e7 100644 --- a/lib/chef/mixin/windows_env_helper.rb +++ b/lib/chef/mixin/windows_env_helper.rb @@ -19,16 +19,16 @@ require "chef/exceptions" require "chef/mixin/wide_string" require "chef/platform/query_helpers" -require "chef/win32/error" if Chef::Platform.windows? -require "chef/win32/api/system" if Chef::Platform.windows? -require "chef/win32/api/unicode" if Chef::Platform.windows? +require "chef/win32/error" if ChefHelpers.windows? +require "chef/win32/api/system" if ChefHelpers.windows? +require "chef/win32/api/unicode" if ChefHelpers.windows? class Chef module Mixin module WindowsEnvHelper include Chef::Mixin::WideString - if Chef::Platform.windows? + if ChefHelpers.windows? include Chef::ReservedNames::Win32::API::System end diff --git a/lib/chef/node.rb b/lib/chef/node.rb index c31fc99279..635acb44fb 100644 --- a/lib/chef/node.rb +++ b/lib/chef/node.rb @@ -2,7 +2,7 @@ # Author:: Christopher Brown (<cb@chef.io>) # Author:: Christopher Walters (<cw@chef.io>) # Author:: Tim Hinderliter (<tim@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"); @@ -88,7 +88,6 @@ class Chef # after the run_context has been set on the node, go through the cookbook_collection # and setup the node[:cookbooks] attribute so that it is published in the node object def set_cookbook_attribute - return unless run_context.cookbook_collection run_context.cookbook_collection.each do |cookbook_name, cookbook| automatic_attrs[:cookbooks][cookbook_name][:version] = cookbook.version end diff --git a/lib/chef/node_map.rb b/lib/chef/node_map.rb index a4891234f5..875f6f9687 100644 --- a/lib/chef/node_map.rb +++ b/lib/chef/node_map.rb @@ -240,6 +240,11 @@ EOH private + def platform_family_query_helper?(node, m) + method = "#{m}?".to_sym + ChefHelpers::PlatformFamily.respond_to?(method) && ChefHelpers::PlatformFamily.send(method, node) + end + # # Succeeds if: # - no negative matches (!value) @@ -255,11 +260,19 @@ EOH # 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 b49010efc0..a8cadbde4a 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-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,16 +16,18 @@ # limitations under the License. # +require 'chef-helpers' + class Chef class Platform class << self def windows? - ChefConfig.windows? + ChefHelpers.windows? end def windows_nano_server? - return false unless windows? + return false unless ChefHelpers.windows? require "win32/registry" # This method may be called before ohai runs (e.g., it may be used to @@ -47,7 +49,7 @@ class Chef end def supports_msi? - return false unless windows? + return false unless ChefHelpers.windows? require "win32/registry" key = "System\\CurrentControlSet\\Services\\msiserver" diff --git a/lib/chef/platform/rebooter.rb b/lib/chef/platform/rebooter.rb index 430d175869..358507c5c8 100644 --- a/lib/chef/platform/rebooter.rb +++ b/lib/chef/platform/rebooter.rb @@ -34,7 +34,7 @@ class Chef reboot_info = node.run_context.reboot_info cmd = case - when Chef::Platform.windows? + when ChefHelpers.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 02d293449c..4f6810b3d9 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-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,50 +17,22 @@ # require "chef/chef_class" +require "chef-helpers" class Chef class Platform + # FIXME: deprecated, use ChefHelpers::Service instead class ServiceHelpers class << self - # 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 ChefHelpers::Service.debianrcd? + providers << :invokercd if ChefHelpers::Service.invokercd? + providers << :upstart if ChefHelpers::Service.upstart? + providers << :insserv if ChefHelpers::Service.insserv? + providers << :systemd if ChefHelpers.systemd? + providers << :redhat if ChefHelpers::Service.redhatrcd? providers end @@ -68,54 +40,14 @@ class Chef def config_for_service(service_name) configs = [] - if ::File.exist?(Chef.path_to("/etc/init.d/#{service_name}")) - configs += [ :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 ChefHelpers::Service.service_script_exist?(:initd, service_name) + configs << :upstart if ChefHelpers::Service.service_script_exist?(:upstart, service_name) + configs << :xinetd if ChefHelpers::Service.service_script_exist?(:xinetd, service_name) + configs << :systemd if ChefHelpers::Service.service_script_exist?(:systemd, service_name) + configs << :etc_rcd if ChefHelpers::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 1751c35129..f1dded4112 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 05522f1eb8..e7793a04aa 100644 --- a/lib/chef/provider/file.rb +++ b/lib/chef/provider/file.rb @@ -451,7 +451,7 @@ class Chef end def load_resource_attributes_from_file(resource) - if Chef::Platform.windows? + if ChefHelpers.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 70d78bcfe9..275207665b 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 783d52d09a..b2ab2515d7 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 + ChefHelpers.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 ChefHelpers.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 ChefHelpers.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 48936c8057..8f8aa77b11 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 e991e4541e..4dd3cb69f4 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 7c7b190b95..e6412858ce 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 ChefHelpers.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 94de68c557..7485a4bcbd 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 !ChefHelpers.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 ChefHelpers.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 ChefHelpers.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 3011dd80a0..5b3e256d24 100644 --- a/lib/chef/provider/remote_file/fetcher.rb +++ b/lib/chef/provider/remote_file/fetcher.rb @@ -24,7 +24,7 @@ class Chef def self.for_resource(uri, new_resource, current_resource) if network_share?(uri) - if !Chef::Platform.windows? + if !ChefHelpers.windows? raise Exceptions::UnsupportedPlatform, "Fetching the file on a network share is supported only on the Windows platform. Please change your source: #{uri}" end Chef::Provider::RemoteFile::NetworkFile.new(uri, new_resource, current_resource) diff --git a/lib/chef/provider/remote_file/local_file.rb b/lib/chef/provider/remote_file/local_file.rb index 0719e5dbf7..e8a521763d 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 + ChefHelpers.windows? ? fix_windows_path(path) : path end end diff --git a/lib/chef/provider/script.rb b/lib/chef/provider/script.rb index c5966370f7..3c0431e928 100644 --- a/lib/chef/provider/script.rb +++ b/lib/chef/provider/script.rb @@ -18,7 +18,7 @@ require "tempfile" require "chef/provider/execute" -require "chef/win32/security" if Chef::Platform.windows? +require "chef/win32/security" if ChefHelpers.windows? require "forwardable" class Chef @@ -67,7 +67,7 @@ class Chef end def set_owner_and_group - if Chef::Platform.windows? + if ChefHelpers.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 c116d321f1..4d3d640866 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-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,10 +18,13 @@ # require "chef/provider" +require "chef-helpers" class Chef class Provider class Service < Chef::Provider + include ChefHelpers::Service + extend ChefHelpers::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 e34227036a..da1c71ba8b 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 351075111b..61cae7421b 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 = /\/rc[\dS].d\/S|not installed/i UPDATE_RC_D_PRIORITIES = /\/rc([\dS]).d\/([SK])(\d\d)/i 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 6d150513cc..55f2d7d509 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 a8e841f8b3..2ea9dbd340 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 9477afec48..08c9f0ebc3 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 18ef245083..77b83aae1f 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/ CHKCONFIG_MISSING = /No such/ 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 6d5bc338c7..fac4686c4f 100644 --- a/lib/chef/provider/service/systemd.rb +++ b/lib/chef/provider/service/systemd.rb @@ -26,14 +26,14 @@ class Chef::Provider::Service::Systemd < Chef::Provider::Service::Simple include Chef::Mixin::Which - provides :service, os: "linux" do |node| - Chef::Platform::ServiceHelpers.service_resource_providers.include?(:systemd) + provides :service, os: "linux" do + 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 68f97d1ff2..f2ff182aa9 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 = /\S+ \(?(start|stop)?\)? ?[\/ ](\w+)/ # 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 dec9e06f87..99886961a9 100644 --- a/lib/chef/provider/subversion.rb +++ b/lib/chef/provider/subversion.rb @@ -212,7 +212,7 @@ class Chef def svn_binary new_resource.svn_binary || - (Chef::Platform.windows? ? "svn.exe" : "svn") + (ChefHelpers.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 1c78e20606..33b37804ed 100644 --- a/lib/chef/provider/windows_path.rb +++ b/lib/chef/provider/windows_path.rb @@ -16,14 +16,14 @@ # limitations under the License. # -require "chef/mixin/windows_env_helper" if Chef::Platform.windows? +require "chef/mixin/windows_env_helper" if ChefHelpers.windows? require "chef/mixin/wide_string" require "chef/exceptions" class Chef class Provider class WindowsPath < Chef::Provider - include Chef::Mixin::WindowsEnvHelper if Chef::Platform.windows? + include Chef::Mixin::WindowsEnvHelper if ChefHelpers.windows? provides :windows_path diff --git a/lib/chef/provider/windows_task.rb b/lib/chef/provider/windows_task.rb index 98dd8795fa..eb0404f55e 100644 --- a/lib/chef/provider/windows_task.rb +++ b/lib/chef/provider/windows_task.rb @@ -22,7 +22,7 @@ require "iso8601" require "chef/mixin/powershell_out" require "chef/provider" require "chef/util/path_helper" -require "win32/taskscheduler" if Chef::Platform.windows? +require "win32/taskscheduler" if ChefHelpers.windows? class Chef class Provider @@ -30,7 +30,7 @@ class Chef include Chef::Mixin::ShellOut include Chef::Mixin::PowershellOut - if Chef::Platform.windows? + if ChefHelpers.windows? include Win32 provides :windows_task diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 1440b2eb61..17133e0cdb 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 7c549fb236..fc76dcb676 100644 --- a/lib/chef/resource/build_essential.rb +++ b/lib/chef/resource/build_essential.rb @@ -1,5 +1,5 @@ # -# Copyright:: 2008-2018, Chef Software, Inc. +# Copyright:: 2008-2018, 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. @@ -39,7 +39,7 @@ class Chef case node["platform_family"] when "debian" declare_resource(:package, %w{ autoconf binutils-doc bison build-essential flex gettext ncurses-dev }) - when "amazon", "fedora", "rhel" + when fedora_derived? declare_resource(: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/execute.rb b/lib/chef/resource/execute.rb index 93f8063d5d..3bfdc8a07e 100644 --- a/lib/chef/resource/execute.rb +++ b/lib/chef/resource/execute.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"); @@ -98,7 +98,7 @@ class Chef description: "Windows only: The password of the user specified by the user property. This property is mandatory if user is specified on Windows and may only be specified if user is specified. The sensitive property for this resource will automatically be set to true if password is specified." # lazy used to set default value of sensitive to true if password is set - property :sensitive, [ TrueClass, FalseClass ], default: lazy { |r| r.password ? true : false } + property :sensitive, [ TrueClass, FalseClass ], default: lazy { password ? true : false } property :elevated, [ TrueClass, FalseClass ], default: false, introduced: "13.3" diff --git a/lib/chef/resource/file.rb b/lib/chef/resource/file.rb index cac20f2356..48c4e79cbb 100644 --- a/lib/chef/resource/file.rb +++ b/lib/chef/resource/file.rb @@ -1,7 +1,7 @@ # # Author:: Adam Jacob (<adam@chef.io>) # Author:: Seth Chisamore (<schisamo@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"); @@ -30,7 +30,7 @@ class Chef description "Use the file resource to manage files directly on a node." - if Platform.windows? + if ChefHelpers.windows? # Use Windows rights instead of standard *nix permissions state_attrs :checksum, :rights, :deny_rights else @@ -54,7 +54,7 @@ class Chef property :path, String, name_property: true, identity: true, description: "The full path to the file, including the file name and its extension. For example: /files/file.txt. Default value: the name of the resource block. Microsoft Windows: A path that begins with a forward slash (/) will point to the root of the current working directory of the chef-client process. This path can vary from system to system. Therefore, using a path that begins with a forward slash (/) is not recommended." - property :atomic_update, [ TrueClass, FalseClass ], desired_state: false, default: lazy { |r| r.docker? && r.special_docker_files?(r.path) ? false : Chef::Config[:file_atomic_update] }, + property :atomic_update, [ TrueClass, FalseClass ], desired_state: false, default: lazy { docker? && special_docker_files?(path) ? false : Chef::Config[:file_atomic_update] }, description: "Perform atomic file updates on a per-resource basis. Set to true for atomic file updates. Set to false for non-atomic file updates. This setting overrides file_atomic_update, which is a global setting found in the client.rb file." property :backup, [ Integer, FalseClass ], desired_state: false, default: 5, diff --git a/lib/chef/resource/link.rb b/lib/chef/resource/link.rb index 00ce69dddd..ddad945cce 100644 --- a/lib/chef/resource/link.rb +++ b/lib/chef/resource/link.rb @@ -78,7 +78,7 @@ class Chef # On certain versions of windows links are not supported. Make # sure we are not on such a platform. - if Chef::Platform.windows? + if ChefHelpers.windows? require "chef/win32/file" begin Chef::ReservedNames::Win32::File.verify_links_supported! diff --git a/lib/chef/resource/msu_package.rb b/lib/chef/resource/msu_package.rb index 85ab170ee0..f52cd15196 100644 --- a/lib/chef/resource/msu_package.rb +++ b/lib/chef/resource/msu_package.rb @@ -1,6 +1,6 @@ # # Author:: Nimisha Sharad (<nimisha.sharad@msystechnologies.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"); @@ -39,7 +39,7 @@ class Chef uri_scheme?(s) ? s : Chef::Util::PathHelper.canonical_path(s, false) end end), - default: lazy { |r| r.package_name } + default: lazy { package_name } property :checksum, String, desired_state: false end end diff --git a/lib/chef/resource/remote_directory.rb b/lib/chef/resource/remote_directory.rb index f03b13ef02..4786dc7502 100644 --- a/lib/chef/resource/remote_directory.rb +++ b/lib/chef/resource/remote_directory.rb @@ -45,10 +45,10 @@ class Chef @recursive = true @files_owner = nil @files_group = nil - @files_mode = 0644 unless Chef::Platform.windows? + @files_mode = 0644 unless ChefHelpers.windows? end - if Chef::Platform.windows? + if ChefHelpers.windows? # create a second instance of the 'rights' attribute rights_attribute(:files_rights) end diff --git a/lib/chef/resource/service.rb b/lib/chef/resource/service.rb index c5197d5f06..004eff8054 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-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"); @@ -18,11 +18,14 @@ # require "chef/resource" -require "shellwords" +require "chef-helpers" class Chef class Resource class Service < Chef::Resource + include ChefHelpers::Service + extend ChefHelpers::Service + identity_attr :service_name state_attrs :enabled, :running, :masked diff --git a/lib/chef/resource/solaris_package.rb b/lib/chef/resource/solaris_package.rb index 435a6ecc44..0a8e932b35 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 905cacc477..61487bbf8d 100644 --- a/lib/chef/resource/yum_package.rb +++ b/lib/chef/resource/yum_package.rb @@ -22,7 +22,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_lock.rb b/lib/chef/run_lock.rb index 08d58fd164..c6afa01c0c 100644 --- a/lib/chef/run_lock.rb +++ b/lib/chef/run_lock.rb @@ -17,7 +17,7 @@ require "chef/mixin/create_path" require "fcntl" -if Chef::Platform.windows? +if ChefHelpers.windows? require "chef/win32/mutex" end require "chef/config" @@ -96,7 +96,7 @@ class Chef # def wait Chef::Log.warn("Chef client #{runpid} is running, will wait for it to finish and then run.") - if Chef::Platform.windows? + if ChefHelpers.windows? mutex.wait else runlock.flock(File::LOCK_EX) @@ -115,7 +115,7 @@ class Chef # Release the system-wide lock. def release if runlock - if Chef::Platform.windows? + if ChefHelpers.windows? mutex.release else runlock.flock(File::LOCK_UN) @@ -137,7 +137,7 @@ class Chef # @api private solely for race condition tests def acquire_lock - if Chef::Platform.windows? + if ChefHelpers.windows? acquire_win32_mutex else # If we support FD_CLOEXEC, then use it. diff --git a/lib/chef/util/powershell/ps_credential.rb b/lib/chef/util/powershell/ps_credential.rb index 32810b98a6..97c94c6d93 100644 --- a/lib/chef/util/powershell/ps_credential.rb +++ b/lib/chef/util/powershell/ps_credential.rb @@ -16,7 +16,7 @@ # limitations under the License. # -require "chef/win32/crypto" if Chef::Platform.windows? +require "chef/win32/crypto" if ChefHelpers.windows? class Chef::Util::Powershell class PSCredential diff --git a/lib/chef/util/windows/logon_session.rb b/lib/chef/util/windows/logon_session.rb index afe58ae4f9..f70a2205b2 100644 --- a/lib/chef/util/windows/logon_session.rb +++ b/lib/chef/util/windows/logon_session.rb @@ -16,7 +16,7 @@ # limitations under the License. # -require "chef/win32/api/security" if Chef::Platform.windows? +require "chef/win32/api/security" if ChefHelpers.windows? require "chef/mixin/wide_string" class Chef diff --git a/lib/chef/win32/eventlog.rb b/lib/chef/win32/eventlog.rb index e8c63bf13a..308c1f6b33 100644 --- a/lib/chef/win32/eventlog.rb +++ b/lib/chef/win32/eventlog.rb @@ -16,7 +16,7 @@ # limitations under the License. # -if Chef::Platform.windows? +if ChefHelpers.windows? if !defined? Chef::Win32EventLogLoaded if defined? Windows::Constants [:INFINITE, :WAIT_FAILED, :FORMAT_MESSAGE_IGNORE_INSERTS, :ERROR_INSUFFICIENT_BUFFER].each do |c| |