Class: Chef::Resource::MachineBatch

Inherits:
LWRPBase
  • Object
show all
Defined in:
lib/chef/resource/machine_batch.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ MachineBatch

Returns a new instance of MachineBatch.



10
11
12
13
14
15
16
# File 'lib/chef/resource/machine_batch.rb', line 10

def initialize(*args)
  super
  @machines = []
  @driver = run_context.chef_provisioning.current_driver
  @chef_server = run_context.cheffish.current_chef_server
  @machine_options = run_context.chef_provisioning.current_machine_options
end

Instance Method Details

#add_machine_options(options) ⇒ Object



44
45
46
# File 'lib/chef/resource/machine_batch.rb', line 44

def add_machine_options(options)
  @machine_options = Chef::Mixin::DeepMerge.hash_only_merge(@machine_options, options)
end

#machine(name, &block) ⇒ Object



40
41
42
# File 'lib/chef/resource/machine_batch.rb', line 40

def machine(name, &block)
  machines << from_recipe.build_resource(:machine, name, &block)
end

#machines(*values) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/chef/resource/machine_batch.rb', line 32

def machines(*values)
  if values.size == 0
    @machines
  else
    @machines += values.flatten
  end
end

#to_textObject

We override this because we want to hide @from_recipe and shorten @machines in error output.



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
# File 'lib/chef/resource/machine_batch.rb', line 50

def to_text
  ivars = instance_variables.map { |ivar| ivar.to_sym } - HIDDEN_IVARS - [ :@from_recipe, :@machines ]
  text = "# Declared in #{@source_line}\n\n"
  text << self.class.resource_name.to_s + "(\"#{name}\") do\n"
  ivars.each do |ivar|
    if (value = instance_variable_get(ivar)) && !(value.respond_to?(:empty?) && value.empty?)
      value_string = value.respond_to?(:to_text) ? value.to_text : value.inspect
      text << "  #{ivar.to_s.sub(/^@/,'')} #{value_string}\n"
    end
  end
  machine_names = @machines.map do |m|
    if m.is_a?(Chef::Provisioning::ManagedEntry)
      m.name
    elsif m.is_a?(Chef::Resource::Machine)
      m.name
    else
      m
    end
  end
  text << "  machines #{machine_names.inspect}\n"
  [@not_if, @only_if].flatten.each do |conditional|
    text << "  #{conditional.to_text}\n"
  end
  text << "end\n"
end