module ChefUtils::DSL::Introspection

This is for “introspection” helpers in the sense that we are inspecting the actual server or image under management to determine running state (duck-typing the system). The helpers here may use the node object state from ohai, but typically not the big 5: platform, platform_family, platform_version, arch, os. The helpers here should infer somewhat higher level facts about the system.

Public Instance Methods

ci?(node = __getnode) click to toggle source

Determine if the node is running in a CI system that sets the CI env var.

@param [Chef::Node] node the node to check @since 15.5

@return [Boolean]

# File lib/chef-utils/dsl/introspection.rb, line 85
def ci?(node = __getnode)
  ENV.key?("CI")
end
docker?(node = __getnode) click to toggle source

Determine if the node is a docker container.

@param [Chef::Node] node the node to check @since 12.11

@return [Boolean]

# File lib/chef-utils/dsl/introspection.rb, line 50
def docker?(node = __getnode)
  # Using "File.exist?('/.dockerinit') || File.exist?('/.dockerenv')" makes Travis sad,
  # and that makes us sad too.
  !!(node && node.read("virtualization", "systems", "docker") == "guest")
end
effortless?(node = __getnode) click to toggle source

Determine if the node is using the Chef Effortless pattern in which the Chef Infra Client is packaged using Chef Habitat

@param [Chef::Node] node the node to check @since 17.0

@return [Boolean]

# File lib/chef-utils/dsl/introspection.rb, line 39
def effortless?(node = __getnode)
  !!(node && node.read("chef_packages", "chef", "chef_effortless"))
end
has_systemd_service_unit?(svc_name) click to toggle source

Determine if the a systemd service unit is present on the system.

@param [String] svc_name @since 15.5

@return [Boolean]

# File lib/chef-utils/dsl/introspection.rb, line 96
def has_systemd_service_unit?(svc_name)
  %w{ /etc /usr/lib /lib /run }.any? do |load_path|
    file_exist?(
      "#{load_path}/systemd/system/#{svc_name.gsub(/@.*$/, "@")}.service"
    )
  end
end
has_systemd_unit?(svc_name) click to toggle source

Determine if the a systemd unit of any type is present on the system.

@param [String] svc_name @since 15.5

@return [Boolean]

# File lib/chef-utils/dsl/introspection.rb, line 111
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?("#{load_path}/systemd/system/#{svc_name}")
  end
end
include_recipe?(recipe_name, node = __getnode)

chef-sugar backcompat method

Alias for: includes_recipe?
includes_recipe?(recipe_name, node = __getnode) click to toggle source

Determine if the current node includes the given recipe name.

@param [String] recipe_name @since 15.8

@return [Boolean]

# File lib/chef-utils/dsl/introspection.rb, line 125
def includes_recipe?(recipe_name, node = __getnode)
  node.recipe?(recipe_name)
end
Also aliased as: include_recipe?
kitchen?(node = __getnode) click to toggle source

Determine if the node is running in Test Kitchen.

@param [Chef::Node] node the node to check @since 15.5

@return [Boolean]

# File lib/chef-utils/dsl/introspection.rb, line 74
def kitchen?(node = __getnode)
  ENV.key?("TEST_KITCHEN")
end
systemd?(node = __getnode) click to toggle source

Determine if the node uses the systemd init system.

@param [Chef::Node] node the node to check @since 15.5

@return [Boolean]

# File lib/chef-utils/dsl/introspection.rb, line 63
def systemd?(node = __getnode)
  file_exist?("/proc/1/comm") && file_open("/proc/1/comm").gets.chomp == "systemd"
end