Module: Chef::DSL::Recipe

Defined in:
lib/chef_metal/recipe_dsl.rb

Constant Summary collapse

NOT_PASSED =
Object.new
@@next_machine_batch_index =
0

Instance Method Summary collapse

Instance Method Details

#add_machine_options(options, &block) ⇒ Object



30
31
32
# File 'lib/chef_metal/recipe_dsl.rb', line 30

def add_machine_options(options, &block)
  run_context.chef_metal.add_machine_options(options, &block)
end

#auto_batch_machines(value = NOT_PASSED) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/chef_metal/recipe_dsl.rb', line 44

def auto_batch_machines(value = NOT_PASSED)
  if value == NOT_PASSED
    run_context.chef_metal.auto_batch_machines
  else
    run_context.chef_metal.auto_batch_machines = value
  end
end

#current_image_optionsObject



38
39
40
# File 'lib/chef_metal/recipe_dsl.rb', line 38

def current_image_options
  run_context.chef_metal.current_image_options
end

#current_machine_optionsObject



26
27
28
# File 'lib/chef_metal/recipe_dsl.rb', line 26

def current_machine_options
  run_context.chef_metal.current_machine_options
end

#machine(name, &block) ⇒ Object

When the machine resource is first declared, create a machine_batch (if there isn’t one already)



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chef_metal/recipe_dsl.rb', line 74

def machine(name, &block)
  resource = build_resource(:machine, name, caller[0], &block)

  # Grab the previous resource so we can decide whether to batch this or make it its own resource.
  previous_index = run_context.resource_collection.previous_index
  previous = previous_index >= 0 ? run_context.resource_collection[previous_index] : nil
  if run_context.chef_metal.auto_batch_machines &&
     previous &&
     Array(resource.action).size == 1 &&
     Array(previous.action) == Array(resource.action)

    # Handle batching similar machines (with similar actions)
    if previous.is_a?(Chef::Resource::MachineBatch)
      # If we see a machine declared after a previous machine_batch with the same action, add it to the batch.
      previous.machines << resource
    elsif previous.is_a?(Chef::Resource::Machine)
      # If we see two machines in a row with the same action, batch them.
      _self = self
      batch = build_resource(:machine_batch, machine_batch_default_name) do
        action resource.action
        machines [ previous, resource ]
      end
      batch.from_recipe self
      run_context.resource_collection[previous_index] = batch
    else
      run_context.resource_collection.insert(resource)
    end

  else
    run_context.resource_collection.insert(resource)
  end
  resource
end

#machine_batch(name = nil, &block) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/chef_metal/recipe_dsl.rb', line 63

def machine_batch(name = nil, &block)
  name ||= machine_batch_default_name
  recipe = self
  declare_resource(:machine_batch, name, caller[0]) do
    from_recipe recipe
    instance_eval(&block)
  end
end

#machine_batch_default_nameObject



54
55
56
57
58
59
60
61
# File 'lib/chef_metal/recipe_dsl.rb', line 54

def machine_batch_default_name
  @@next_machine_batch_index += 1
  if @@next_machine_batch_index > 1
    "default#{@@next_machine_batch_index}"
  else
    "default"
  end
end

#with_driver(driver, options = nil, &block) ⇒ Object



18
19
20
# File 'lib/chef_metal/recipe_dsl.rb', line 18

def with_driver(driver, options = nil, &block)
  run_context.chef_metal.with_driver(driver, options, &block)
end

#with_image_options(image_options, &block) ⇒ Object



34
35
36
# File 'lib/chef_metal/recipe_dsl.rb', line 34

def with_image_options(image_options, &block)
  run_context.chef_metal.with_image_options(image_options, &block)
end

#with_machine_options(machine_options, &block) ⇒ Object



22
23
24
# File 'lib/chef_metal/recipe_dsl.rb', line 22

def with_machine_options(machine_options, &block)
  run_context.chef_metal.with_machine_options(machine_options, &block)
end