summaryrefslogtreecommitdiff
path: root/chef-utils
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2020-07-21 17:39:32 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2020-07-21 17:39:32 -0700
commit2227059987a81e87763ecca25f3e63519d659e6e (patch)
treeea163cb822177520f4e8ea243e88c0a9219de6c0 /chef-utils
parent6d1ce408e055a4904b9a875c47b0b2a0b21a977c (diff)
downloadchef-2227059987a81e87763ecca25f3e63519d659e6e.tar.gz
just code changes, no spec modifications
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
Diffstat (limited to 'chef-utils')
-rw-r--r--chef-utils/lib/chef-utils/dsl/path_sanity.rb24
-rw-r--r--chef-utils/lib/chef-utils/dsl/service.rb5
-rw-r--r--chef-utils/lib/chef-utils/internal.rb42
3 files changed, 54 insertions, 17 deletions
diff --git a/chef-utils/lib/chef-utils/dsl/path_sanity.rb b/chef-utils/lib/chef-utils/dsl/path_sanity.rb
index a3b71c14d9..921c666124 100644
--- a/chef-utils/lib/chef-utils/dsl/path_sanity.rb
+++ b/chef-utils/lib/chef-utils/dsl/path_sanity.rb
@@ -30,27 +30,27 @@ module ChefUtils
path_separator = ChefUtils.windows? ? ";" : ":"
# ensure the Ruby and Gem bindirs are included for omnibus chef installs
new_paths = env_path.split(path_separator)
- [ ChefUtils::DSL::PathSanity.ruby_bindir, ChefUtils::DSL::PathSanity.gem_bindir ].compact.each do |path|
+ [ __ruby_bindir, __gem_bindir ].compact.each do |path|
new_paths = [ path ] + new_paths unless new_paths.include?(path)
end
- ChefUtils::DSL::PathSanity.sane_paths.each do |path|
+ __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
- class << self
- def sane_paths
- ChefUtils.windows? ? %w{} : %w{/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin}
- end
+ private
- def ruby_bindir
- RbConfig::CONFIG["bindir"]
- end
+ def __sane_paths
+ ChefUtils.windows? ? %w{} : %w{/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin}
+ end
- def gem_bindir
- Gem.bindir
- end
+ def __ruby_bindir
+ RbConfig::CONFIG["bindir"]
+ end
+
+ def __gem_bindir
+ Gem.bindir
end
extend self
diff --git a/chef-utils/lib/chef-utils/dsl/service.rb b/chef-utils/lib/chef-utils/dsl/service.rb
index faa5e96467..ed7805806c 100644
--- a/chef-utils/lib/chef-utils/dsl/service.rb
+++ b/chef-utils/lib/chef-utils/dsl/service.rb
@@ -25,6 +25,7 @@ module ChefUtils
module Service
include Internal
include TrainHelpers
+ include Introspection
# Returns if debian's old rc.d manager is installed (not necessarily the primary init system).
#
@@ -97,8 +98,8 @@ module ChefUtils
file_exist?("/etc/rc.d/#{script}")
when :systemd
file_exist?("/etc/init.d/#{script}") ||
- ChefUtils::DSL::Introspection.has_systemd_service_unit?(script) ||
- ChefUtils::DSL::Introspection.has_systemd_unit?(script)
+ has_systemd_service_unit?(script) ||
+ has_systemd_unit?(script)
else
raise ArgumentError, "type of service must be one of :initd, :upstart, :xinetd, :etc_rcd, or :systemd"
end
diff --git a/chef-utils/lib/chef-utils/internal.rb b/chef-utils/lib/chef-utils/internal.rb
index 731a0d1712..6986b5250a 100644
--- a/chef-utils/lib/chef-utils/internal.rb
+++ b/chef-utils/lib/chef-utils/internal.rb
@@ -40,11 +40,18 @@ module ChefUtils
private
- # FIXME: include a `__config` method so we can wire up Chef::Config automatically or allow other consumers to
- # inject a config hash without having to take a direct dep on the chef-config gem
-
+ # This should be set to a Chef::Node instance or to some Hash/Mash-like configuration object with the same keys. It needs to
+ # expose keys like `:os`, `:platform`, `:platform_version` and `:platform_family` at least to be useful. It will automatically
+ # pick up a `node` method when mixed into an object that has that as a method (which is the encouraged "public" API to use
+ # for dependency injection rather than overriding the method in this case.
+ #
+ # @return [Hash] hash-like config object
+ #
# @api private
def __getnode(skip_global = false)
+ # Software developers should feel free to rely on the default wiring here to the node method by implementing the node method in their
+ # own class. For anything more complicated they should completely override the method (overriding the whole method is never wrong and
+ # is safer).
return node if respond_to?(:node) && node
return run_context&.node if respond_to?(:run_context) && run_context&.node
@@ -56,7 +63,10 @@ module ChefUtils
nil
end
+ # Just a helper to pull the ENV["PATH"] in a train-independent way
+ #
# @api private
+ #
def __env_path
if __transport_connection
__transport_connection.run_command("echo $PATH").stdout || ""
@@ -65,13 +75,39 @@ module ChefUtils
end
end
+ # This should be set to a Train::FIXME instance. You should wire this up to nil for not using a train transport connection.
+ #
+ # @return [Train::FIXME]
+ #
# @api private
+ #
def __transport_connection
+ # Software consumers MUST override this method with their own implementation. The default behavior here is subject to change.
return Chef.run_context.transport_connection if defined?(Chef) && Chef.respond_to?(:run_context) && Chef&.run_context&.transport_connection
nil
end
+ # This should be set to Chef::Config or to some Hash/Mash-like configuration object with the same keys. It must not be nil.
+ #
+ # @return [Hash] hash-like config object
+ #
+ # @api private
+ #
+ def __config
+ raise NotImplementedError
+ end
+
+ # This should be set to Chef::Log or something that duck-types like it. It must not be nil.
+ #
+ # @return [Chef::Log] logger-like logging object
+ #
+ # @api private
+ #
+ def __log
+ raise NotImplementedError
+ end
+
extend self
end
end