Module: ChefUtils::DSL::Introspection

Extended by:
Introspection
Includes:
TrainHelpers
Included in:
ChefUtils, Introspection, Service
Defined in:
lib/chef-utils/dsl/introspection.rb

Overview

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.

Instance Method Summary collapse

Methods included from TrainHelpers

#dir_exist?, #file_directory?, #file_exist?, #file_open, #file_read

Instance Method Details

#ci?(node = __getnode) ⇒ Boolean

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

Parameters:

  • node (Chef::Node) (defaults to: __getnode)

    the node to check

Returns:

  • (Boolean)

Since:

  • 15.5



85
86
87
# File 'lib/chef-utils/dsl/introspection.rb', line 85

def ci?(node = __getnode)
  ENV.key?("CI")
end

#docker?(node = __getnode) ⇒ Boolean

Determine if the node is a docker container.

Parameters:

  • node (Chef::Node) (defaults to: __getnode)

    the node to check

Returns:

  • (Boolean)

Since:

  • 12.11



50
51
52
53
54
# 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) ⇒ Boolean

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

Parameters:

  • node (Chef::Node) (defaults to: __getnode)

    the node to check

Returns:

  • (Boolean)

Since:

  • 17.0



39
40
41
# 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) ⇒ Boolean

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

Parameters:

  • svc_name (String)

Returns:

  • (Boolean)

Since:

  • 15.5



96
97
98
99
100
101
102
# 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) ⇒ Boolean

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

Parameters:

  • svc_name (String)

Returns:

  • (Boolean)

Since:

  • 15.5



111
112
113
114
115
116
# 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

#includes_recipe?(recipe_name, node = __getnode) ⇒ Boolean Also known as: include_recipe?

Determine if the current node includes the given recipe name.

Parameters:

  • recipe_name (String)

Returns:

  • (Boolean)

Since:

  • 15.8



125
126
127
# File 'lib/chef-utils/dsl/introspection.rb', line 125

def includes_recipe?(recipe_name, node = __getnode)
  node.recipe?(recipe_name)
end

#kitchen?(node = __getnode) ⇒ Boolean

Determine if the node is running in Test Kitchen.

Parameters:

  • node (Chef::Node) (defaults to: __getnode)

    the node to check

Returns:

  • (Boolean)

Since:

  • 15.5



74
75
76
# File 'lib/chef-utils/dsl/introspection.rb', line 74

def kitchen?(node = __getnode)
  ENV.key?("TEST_KITCHEN")
end

#systemd?(node = __getnode) ⇒ Boolean

Determine if the node uses the systemd init system.

Parameters:

  • node (Chef::Node) (defaults to: __getnode)

    the node to check

Returns:

  • (Boolean)

Since:

  • 15.5



63
64
65
# 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