Class: Chef::Provider::MachineBatch

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

Defined Under Namespace

Classes: MachineBatchError

Instance Method Summary collapse

Instance Method Details

#action_handlerObject



13
14
15
# File 'lib/chef/provider/machine_batch.rb', line 13

def action_handler
  @action_handler ||= Provisioning::ChefProviderActionHandler.new(self)
end

#by_current_driverObject



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/chef/provider/machine_batch.rb', line 178

def by_current_driver
  result = {}
  drivers = {}
  @machines.each do |m|
    if m[:spec].driver_url
      drivers[m[:spec].driver_url] ||= run_context.chef_provisioning.driver_for(m[:spec].driver_url)
      driver = drivers[m[:spec].driver_url]
      result[driver] ||= {}
      result[driver][m[:spec]] = m[:machine_options].call(driver)
    end
  end
  result
end

#by_idObject



142
143
144
# File 'lib/chef/provider/machine_batch.rb', line 142

def by_id
  @by_id ||= @machines.inject({}) { |hash,m| hash[m[:spec].id] = m; hash }
end

#by_new_driverObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/chef/provider/machine_batch.rb', line 153

def by_new_driver
  result = {}
  drivers = {}
  @machines.each do |m|
    if m[:desired_driver]
      drivers[m[:desired_driver]] ||= run_context.chef_provisioning.driver_for(m[:desired_driver])
      driver = drivers[m[:desired_driver]]
      # Check whether the current driver is same or different; we disallow
      # moving a machine from one place to another.
      if m[:spec].driver_url
        drivers[m[:spec].driver_url] ||= run_context.chef_provisioning.driver_for(m[:spec].driver_url)
        current_driver = drivers[m[:spec].driver_url]
        if driver.driver_url != current_driver.driver_url
          raise "Cannot move '#{m[:spec].name}' from #{current_driver.driver_url} to #{driver.driver_url}: machine moving is not supported.  Destroy and recreate."
        end
      end
      result[driver] ||= {}
      result[driver][m[:spec]] = m[:machine_options].call(driver)
    else
      raise "No driver specified for #{m[:spec].name}"
    end
  end
  result
end

#chef_managed_entry_storeObject



230
231
232
# File 'lib/chef/provider/machine_batch.rb', line 230

def chef_managed_entry_store
  @chef_managed_entry_store ||= Provisioning.chef_managed_entry_store(new_resource.chef_server)
end

#load_current_resourceObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/chef/provider/machine_batch.rb', line 192

def load_current_resource
  # Load nodes in parallel
  @machines = parallel_do(new_resource.machines) do |machine|
    if machine.is_a?(Chef::Resource::Machine)
      machine_resource = machine
      provider = Chef::Provider::Machine.new(machine_resource, machine_resource.run_context)
      provider.load_current_resource
      {
        :resource => machine_resource,
        :spec => provider.machine_spec,
        :desired_driver => machine_resource.driver,
        :files => machine_resource.files,
        :machine_options => proc { |driver| provider.machine_options(driver) },
        :action_handler => Provisioning::AddPrefixActionHandler.new(action_handler, "[#{machine_resource.name}] ")
      }
    elsif machine.is_a?(Provisioning::ManagedEntry)
      machine_spec = machine
      {
        :spec => machine_spec,
        :desired_driver => new_resource.driver,
        :files => new_resource.files,
        :machine_options => proc { |driver| machine_options(driver) },
        :action_handler => Provisioning::AddPrefixActionHandler.new(action_handler, "[#{machine_spec.name}] ")
      }
    else
      name = machine
      machine_spec = chef_managed_entry_store.get_or_new(:machine, name)
      {
        :spec => machine_spec,
        :desired_driver => new_resource.driver,
        :files => new_resource.files,
        :machine_options => proc { |driver| machine_options(driver) },
        :action_handler => Provisioning::AddPrefixActionHandler.new(action_handler, "[#{name}] ")
      }
    end
  end.to_a
end

#machine_options(driver) ⇒ Object



234
235
236
237
238
239
240
# File 'lib/chef/provider/machine_batch.rb', line 234

def machine_options(driver)
  result = { :convergence_options => { :chef_server => new_resource.chef_server } }
  result = Chef::Mixin::DeepMerge.hash_only_merge(result, run_context.chef_provisioning.config[:machine_options]) if run_context.chef_provisioning.config[:machine_options]
  result = Chef::Mixin::DeepMerge.hash_only_merge(result, driver.config[:machine_options]) if driver.config && driver.config[:machine_options]
  result = Chef::Mixin::DeepMerge.hash_only_merge(result, new_resource.machine_options)
  result
end

#parallel_do(enum, options = {}, &block) ⇒ Object

TODO in many of these cases, the order of the results only matters because you want to match it up with the input. Make a parallelize method that doesn’t care about order and spits back results as quickly as possible.



149
150
151
# File 'lib/chef/provider/machine_batch.rb', line 149

def parallel_do(enum, options = {}, &block)
  parallelizer.parallelize(enum, options, &block).to_a
end

#parallelizerObject



23
24
25
# File 'lib/chef/provider/machine_batch.rb', line 23

def parallelizer
  @parallelizer ||= Chef::ChefFS::Parallelizer.new(new_resource.max_simultaneous || 100)
end

#whyrun_supported?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/chef/provider/machine_batch.rb', line 19

def whyrun_supported?
  true
end

#with_ready_machines(ready_only: false) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/chef/provider/machine_batch.rb', line 117

def with_ready_machines(ready_only: false)
  action_allocate unless ready_only
  parallel_do(by_new_driver) do |driver, specs_and_options|
    driver.ready_machines(action_handler, specs_and_options, parallelizer) do |machine|
      machine.machine_spec.save(action_handler)

      m = by_id[machine.machine_spec.id]

      m[:machine] = machine
      begin
        yield m if block_given?
      rescue StandardError => error
        Chef::Log.debug("Chef provisioning failed on machine #{machine.name}")
        raise MachineBatchError.new(machine, error.message)
      ensure
        machine.disconnect
      end
    end
  end
end

#with_ready_machines_onlyObject



138
139
140
# File 'lib/chef/provider/machine_batch.rb', line 138

def with_ready_machines_only
  with_ready_machines(ready_only: true)
end