Module: Chef::DSL::PlatformIntrospection

Included in:
RebootPending, Universal
Defined in:
lib/chef/dsl/platform_introspection.rb

Overview

Chef::DSL::PlatformIntrospection

Provides the DSL for platform-dependent switch logic, such as #value_for_platform.

Defined Under Namespace

Classes: PlatformDependentValue, PlatformFamilyDependentValue

Instance Method Summary collapse

Instance Method Details

#docker?(node = run_context.nil? ? nil : run_context.node) ⇒ Boolean

Shamelessly stolen from 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

Returns:

  • (Boolean)

257
258
259
260
261
262
# File 'lib/chef/dsl/platform_introspection.rb', line 257

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

#older_than_win_2012_or_8?(node = run_context.nil? ? nil : run_context.node) ⇒ Boolean

a simple helper to determine if we're on a windows release pre-2012 / 8

Returns:

  • (Boolean)

    Is the system older than Windows 8 / 2012


266
267
268
# File 'lib/chef/dsl/platform_introspection.rb', line 266

def older_than_win_2012_or_8?(node = run_context.nil? ? nil : run_context.node)
  node["platform_version"].to_f < 6.2
end

#platform?(*args) ⇒ Boolean

Given a list of platforms, returns true if the current recipe is being run on a node with that platform, false otherwise.

Parameters

args

A list of platforms. Each platform can be in string or symbol format.

Returns

true

If the current platform is in the list

false

If the current platform is not in the list

Returns:

  • (Boolean)

159
160
161
162
163
164
165
166
167
# File 'lib/chef/dsl/platform_introspection.rb', line 159

def platform?(*args)
  has_platform = false

  args.flatten.each do |platform|
    has_platform = true if platform.to_s == node[:platform]
  end

  has_platform
end

#platform_family?(*args) ⇒ Boolean

Given a list of platform families, returns true if the current recipe is being run on a node within that platform family, false otherwise.

Parameters

args

A list of platform families. Each platform family can be in string or symbol format.

Returns

true

if the current node platform family is in the list.

false

if the current node platform family is not in the list.

Returns:

  • (Boolean)

242
243
244
245
246
# File 'lib/chef/dsl/platform_introspection.rb', line 242

def platform_family?(*args)
  args.flatten.any? do |platform_family|
    platform_family.to_s == node[:platform_family]
  end
end

#value_for_platform(platform_hash) ⇒ Object

Given a hash similar to the one we use for Platforms, select a value from the hash. Supports per platform defaults, along with a single base default. Arrays may be passed as hash keys and will be expanded.

Parameters

platform_hash

A platform-style hash.

Returns

value

Whatever the most specific value of the hash is.


146
147
148
# File 'lib/chef/dsl/platform_introspection.rb', line 146

def value_for_platform(platform_hash)
  PlatformDependentValue.new(platform_hash).value_for_node(node)
end

#value_for_platform_family(platform_family_hash) ⇒ Object

Given a hash mapping platform families to values, select a value from the hash. Supports a single base default if platform family is not in the map. Arrays may be passed as hash keys and will be expanded

Parameters

platform_family_hash

A hash in the form { platform_family_name => value }

Returns

value

Whatever the most specific value of the hash is.


229
230
231
# File 'lib/chef/dsl/platform_introspection.rb', line 229

def value_for_platform_family(platform_family_hash)
  PlatformFamilyDependentValue.new(platform_family_hash).value_for_node(node)
end