Module: KnifeTopo::BootstrapHelper

Includes:
CommandHelper
Included in:
TopoBootstrap
Defined in:
lib/chef/knife/topo/bootstrap_helper.rb

Overview

Node update helper for knife topo

Instance Method Summary collapse

Methods included from CommandHelper

#check_chef_env, #initialize_cmd_args, #most_common, #resource_exists?, #run_cmd

Instance Method Details

#attributes_for_bootstrap(data) ⇒ Object

for bootstrap, attributes have to include tags



60
61
62
63
64
# File 'lib/chef/knife/topo/bootstrap_helper.rb', line 60

def attributes_for_bootstrap(data)
  attrs = data['normal'] || {}
  attrs['tags'] = data['tags'] if data['tags']
  attrs
end

#delete_client_node(node_name) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/chef/knife/topo/bootstrap_helper.rb', line 66

def delete_client_node(node_name)
  ui.info("Node #{node_name} exists and will be overwritten")
  # delete node first so vault refresh does not pick up existing node
  rest.delete("nodes/#{node_name}")
  rest.delete("clients/#{node_name}")
rescue Net::HTTPServerException => e
  raise unless e.response.code == '404'
end

#run_bootstrap(data, bootstrap_args, overwrite = false) ⇒ Object

Setup the bootstrap args and run the bootstrap command



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/chef/knife/topo/bootstrap_helper.rb', line 28

def run_bootstrap(data, bootstrap_args, overwrite = false)
  node_name = data['name']
  args = setup_bootstrap_args(bootstrap_args, data)
  delete_client_node(node_name) if overwrite

  ui.info "Bootstrapping node #{node_name}"
  run_cmd(Chef::Knife::Bootstrap, args)
rescue StandardError => e
  raise if Chef::Config[:verbosity] == 2
  ui.warn "bootstrap of node #{node_name} exited with error"
  humanize_exception(e)
  false
end

#setup_bootstrap_args(args, data) ⇒ Object

rubocop:disable Metrics/AbcSize



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chef/knife/topo/bootstrap_helper.rb', line 43

def setup_bootstrap_args(args, data)
  # We need to remove the --bootstrap option, if it exists
  args -= ['--bootstrap']
  args[1] = data['ssh_host']

  # And set up the node-specific data but ONLY if defined
  args += ['-N', data['name']] if data['name']
  args += ['-E', data['chef_environment']] if data['chef_environment']
  args += ['--ssh-port', data['ssh_port']] if data['ssh_port']
  args += ['--run-list', data['run_list'].join(',')] if data['run_list']
  attrs = attributes_for_bootstrap(data)
  args += ['--json-attributes', attrs.to_json] unless attrs.empty?
  args
end