Class: Chef::GuardInterpreter::ResourceGuardInterpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/guard_interpreter/resource_guard_interpreter.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent_resource, command, opts) ⇒ ResourceGuardInterpreter

Returns a new instance of ResourceGuardInterpreter.



24
25
26
27
28
29
30
# File 'lib/chef/guard_interpreter/resource_guard_interpreter.rb', line 24

def initialize(parent_resource, command, opts)
  @command = command
  @opts = opts

  @parent_resource = parent_resource
  @resource = get_interpreter_resource(parent_resource)
end

Instance Method Details

#evaluateObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/chef/guard_interpreter/resource_guard_interpreter.rb', line 38

def evaluate
  # Add attributes inherited from the parent class
  # to the resource
  merge_inherited_attributes

  @opts.each do |attribute, value|
    @resource.send(attribute, value)
  end

  # Only execute and script resources and use guard attributes.
  # The command to be executed on them are passed via different attributes.
  # Script resources use code attribute and execute resources use
  # command property. Moreover script resources are also execute
  # resources. Here we make sure @command is assigned to the right
  # attribute by checking the type of the resources.
  # We need to make sure we check for Script first because any resource
  # that can get to here is an Execute resource.
  if @resource.is_a? Chef::Resource::Script
    @resource.code @command
  else
    @resource.command @command
  end

  # Handles cases like powershell_script where default
  # attributes are different when used in a guard vs. not. For
  # powershell_script in particular, this will go away when
  # the one attribute that causes this changes its default to be
  # the same after some period to prepare for deprecation
  if @resource.class.respond_to?(:get_default_attributes)
    @resource.class.send(:get_default_attributes).each do |attribute, value|
      @resource.send(attribute, value)
    end
  end

  begin
    # Coerce to an array to be safe. This could happen with a legacy
    # resource or something overriding the default_action code in a
    # subclass.
    Array(@resource.action).each { |action_to_run| @resource.run_action(action_to_run) }
    @resource.updated
  rescue Mixlib::ShellOut::ShellCommandFailed
    nil
  end
end

#outputObject

This class used to inherit from DefaultGuardInterpreter and it responds to #output, so leave this in for potential backwards compatibility.



34
35
36
# File 'lib/chef/guard_interpreter/resource_guard_interpreter.rb', line 34

def output
  nil
end