Class: ChefDK::Command::Provision

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

Constant Summary collapse

OPT_SEPARATOR =
/[=\s]+/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ChefDK::Configurable

#chef_config, #chefdk_config, #config_loader, #generator_config, #knife_config

Methods inherited from Base

#needs_help?, #needs_version?, #run_with_default_options

Methods included from Helpers

#chefdk_home, #err, #git_bin_dir, #git_windows_bin_dir, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_chefdk_location, #omnibus_embedded_bin_dir, #omnibus_env, #omnibus_install?, #omnibus_root, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix

Constructor Details

#initialize(*args) ⇒ Provision

Returns a new instance of Provision.



216
217
218
219
220
221
222
223
224
225
226
# File 'lib/chef-dk/command/provision.rb', line 216

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.



210
211
212
# File 'lib/chef-dk/command/provision.rb', line 210

def params
  @params
end

#policy_groupObject (readonly)

Returns the value of attribute policy_group.



212
213
214
# File 'lib/chef-dk/command/provision.rb', line 212

def policy_group
  @policy_group
end

#policyfile_relative_pathObject (readonly)

Returns the value of attribute policyfile_relative_path.



211
212
213
# File 'lib/chef-dk/command/provision.rb', line 211

def policyfile_relative_path
  @policyfile_relative_path
end

#uiObject

Returns the value of attribute ui.



214
215
216
# File 'lib/chef-dk/command/provision.rb', line 214

def ui
  @ui
end

Class Method Details

.split_opt(key_value) ⇒ Object



190
191
192
193
# File 'lib/chef-dk/command/provision.rb', line 190

def self.split_opt(key_value)
  key, _separator, value = key_value.partition(OPT_SEPARATOR)
  [key, value]
end

Instance Method Details

#apply_params!(params) ⇒ Object



335
336
337
338
339
340
341
342
# File 'lib/chef-dk/command/provision.rb', line 335

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.



251
252
253
# File 'lib/chef-dk/command/provision.rb', line 251

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

#cookbook_pathObject



327
328
329
# File 'lib/chef-dk/command/provision.rb', line 327

def cookbook_path
  config[:cookbook]
end

#debug?Boolean

Returns:

  • (Boolean)


344
345
346
# File 'lib/chef-dk/command/provision.rb', line 344

def debug?
  !!config[:debug]
end

#default_actionObject



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

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

#enable_policyfile?Boolean

Returns:

  • (Boolean)


331
332
333
# File 'lib/chef-dk/command/provision.rb', line 331

def enable_policyfile?
  config[:enable_policyfile]
end

#node_nameObject



298
299
300
# File 'lib/chef-dk/command/provision.rb', line 298

def node_name
  config[:node_name]
end

#policy_nameObject



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

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



322
323
324
325
# File 'lib/chef-dk/command/provision.rb', line 322

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.



316
317
318
319
# File 'lib/chef-dk/command/provision.rb', line 316

def provisioning_cookbook_path
  detect_provisioning_cookbook_name_and_path! unless @provisioning_cookbook_path
  @provisioning_cookbook_path
end

#pushObject



255
256
257
258
259
260
261
# File 'lib/chef-dk/command/provision.rb', line 255

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



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

def recipe
  config[:machine_recipe]
end

#run(params = []) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/chef-dk/command/provision.rb', line 228

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



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/chef-dk/command/provision.rb', line 263

def setup_context
  ProvisioningData.context.tap do |c|

    c.action = default_action
    c.node_name = node_name
    c.target = target

    c.set_user_opts(user_opts)

    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)


348
349
350
# File 'lib/chef-dk/command/provision.rb', line 348

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

#targetObject



302
303
304
# File 'lib/chef-dk/command/provision.rb', line 302

def target
  config[:target]
end

#user_optsObject



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

def user_opts
  config[:opts]
end