Module: Chef::ResourceHelpers::PathHelpers

Overview

Helpers for path manipulation

Instance Method Summary collapse

Instance Method Details

#chef_client_hab_binary_pathString

This method returns the absolute path to the chef-client binary that is currently executing. In a Habitat environment, you might have multiple versions of chef-client installed, we want to ensure we get the path to the one currently running.

Examples:

chef_client_hab_binary_path
# => "/hab/pkgs/chef/chef-infra-client/19.10.0/20250822151044/bin/chef-client"
# Or on Windows:
# => "C:/hab/pkgs/chef/chef-infra-client/19.10.0/20250822151044/bin/chef-client"

Returns:

  • (String)

    The absolute path to the chef-client binary if found, or an empty string if no valid binary path is detected.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chef/resource/helpers/path_helpers.rb', line 21

def chef_client_hab_binary_path
  path = File.realpath($PROGRAM_NAME)
  bin = File.basename(path)

  # On Windows, temporarily use the c:\\hab\\bin\\*.bat binstubs
  bat_path = "C:\\hab\\bin\\#{ChefUtils::Dist::Infra::CLIENT}.bat"
  return bat_path if File.exist?(bat_path) && ChefUtils.windows?

  # return path for any bin/chef-* names
  return path.sub(bin, ChefUtils::Dist::Infra::CLIENT) if bin =~ /^chef-[a-z-]+$/

  # Return empty string if no valid path is found
  ""
end

#chef_client_hab_package_binary_pathObject

once the binstubs under hab package have been fixed, restore this as the chef_client_hab_binary_path method



38
39
40
41
42
43
44
45
46
# File 'lib/chef/resource/helpers/path_helpers.rb', line 38

def chef_client_hab_package_binary_path
  path = File.realpath($PROGRAM_NAME)
  bin = File.basename(path)

  return path.sub(bin, ChefUtils::Dist::Infra::CLIENT) if bin =~ /^chef-[a-z-]+$/

  # Return empty string if no valid path is found
  ""
end

#hab_executable_binary_pathObject



48
49
50
51
# File 'lib/chef/resource/helpers/path_helpers.rb', line 48

def hab_executable_binary_path
  # Find hab in PATH
  which("hab") || ""
end