Class: ChefDK::Command::Provision

Inherits:
Base
  • Object
show all
Includes:
ChefDK::Configurable
Defined in:
lib/chef-dk/command/provision.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ChefDK::Configurable

#chef_config, #chefdk_config, #config_loader

Methods inherited from Base

#needs_help?, #needs_version?, #run_with_default_options

Methods included from Helpers

#err, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_chefdk_location, #omnibus_embedded_bin_dir, #omnibus_install?, #omnibus_root, #stderr, #stdout, #system_command

Constructor Details

#initialize(*args) ⇒ Provision

Returns a new instance of Provision.



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/chef-dk/command/provision.rb', line 189

def initialize(*args)
  super

  @ui = UI.new

  @policyfile_relative_path = nil
  @policy_group = nil

  @provisioning_cookbook_path = nil
  @provisioning_cookbook_name = nil
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



183
184
185
# File 'lib/chef-dk/command/provision.rb', line 183

def params
  @params
end

#policy_groupObject (readonly)

Returns the value of attribute policy_group.



185
186
187
# File 'lib/chef-dk/command/provision.rb', line 185

def policy_group
  @policy_group
end

#policyfile_relative_pathObject (readonly)

Returns the value of attribute policyfile_relative_path.



184
185
186
# File 'lib/chef-dk/command/provision.rb', line 184

def policyfile_relative_path
  @policyfile_relative_path
end

#uiObject

Returns the value of attribute ui.



187
188
189
# File 'lib/chef-dk/command/provision.rb', line 187

def ui
  @ui
end

Instance Method Details

#apply_params!(params) ⇒ Object



297
298
299
300
301
302
303
304
# File 'lib/chef-dk/command/provision.rb', line 297

def apply_params!(params)
  remaining_args = parse_options(params)
  if enable_policyfile?
    handle_policy_argv(remaining_args)
  else
    handle_no_policy_argv(remaining_args)
  end
end

#chef_runnerObject

An instance of ChefRunner. Calling ChefRunner#converge will trigger convergence and generate the desired code.



224
225
226
# File 'lib/chef-dk/command/provision.rb', line 224

def chef_runner
  @chef_runner ||= ChefRunner.new(provisioning_cookbook_path, ["recipe[#{provisioning_cookbook_name}::#{recipe}]"])
end

#cookbook_pathObject



289
290
291
# File 'lib/chef-dk/command/provision.rb', line 289

def cookbook_path
  config[:cookbook]
end

#debug?Boolean

Returns:

  • (Boolean)


306
307
308
# File 'lib/chef-dk/command/provision.rb', line 306

def debug?
  !!config[:debug]
end

#default_actionObject



260
261
262
263
264
265
266
# File 'lib/chef-dk/command/provision.rb', line 260

def default_action
  if config[:destroy]
    :destroy
  else
    :converge
  end
end

#enable_policyfile?Boolean

Returns:

  • (Boolean)


293
294
295
# File 'lib/chef-dk/command/provision.rb', line 293

def enable_policyfile?
  config[:enable_policyfile]
end

#node_nameObject



268
269
270
# File 'lib/chef-dk/command/provision.rb', line 268

def node_name
  config[:node_name]
end

#policy_nameObject



252
253
254
255
256
257
258
# File 'lib/chef-dk/command/provision.rb', line 252

def policy_name
  if sync_policy?
    push.policy_data["name"]
  else
    config[:policy_name]
  end
end

#provisioning_cookbook_nameObject

The name of the provisioning cookbook



284
285
286
287
# File 'lib/chef-dk/command/provision.rb', line 284

def provisioning_cookbook_name
  detect_provisioning_cookbook_name_and_path! unless @provisioning_cookbook_name
  @provisioning_cookbook_name
end

#provisioning_cookbook_pathObject

Gives the ‘cookbook_path` in the chef-client sense, which is the directory that contains the provisioning cookbook.



278
279
280
281
# File 'lib/chef-dk/command/provision.rb', line 278

def provisioning_cookbook_path
  detect_provisioning_cookbook_name_and_path! unless @provisioning_cookbook_path
  @provisioning_cookbook_path
end

#pushObject



228
229
230
231
232
233
234
# File 'lib/chef-dk/command/provision.rb', line 228

def push
  @push ||= PolicyfileServices::Push.new(policyfile: policyfile_relative_path,
                                         ui: ui,
                                         policy_group: policy_group,
                                         config: chef_config,
                                         root_dir: Dir.pwd)
end

#recipeObject



272
273
274
# File 'lib/chef-dk/command/provision.rb', line 272

def recipe
  config[:machine_recipe]
end

#run(params = []) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/chef-dk/command/provision.rb', line 201

def run(params = [])
  return 1 unless apply_params!(params)
  chef_config # force chef config to load
  return 1 unless check_cookbook_and_recipe_path

  push.run if sync_policy?

  setup_context

  chef_runner.converge
  0
rescue ChefRunnerError, PolicyfileServiceError => e
  handle_error(e)
  1
  # Chef Provisioning doesn't fail gracefully when a driver is missing:
  # https://github.com/chef/chef-provisioning/issues/338
rescue StandardError, LoadError => error
  ui.err("Error: #{error.message}")
  1
end

#setup_contextObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/chef-dk/command/provision.rb', line 236

def setup_context
  ProvisioningData.context.tap do |c|

    c.action = default_action
    c.node_name = node_name

    c.enable_policyfile = enable_policyfile?

    if enable_policyfile?
      c.policy_group = policy_group
      c.policy_name = policy_name
    end

  end
end

#sync_policy?Boolean

Returns:

  • (Boolean)


310
311
312
# File 'lib/chef-dk/command/provision.rb', line 310

def sync_policy?
  config.key?(:sync)
end