Class: KnifeTopo::TopoBootstrap

Inherits:
Chef::Knife
  • Object
show all
Includes:
BootstrapHelper, Loader
Defined in:
lib/chef/knife/topo_bootstrap.rb

Overview

knife topo bootstrap

Direct Known Subclasses

TopoCreate

Instance Attribute Summary collapse

Attributes included from Loader

#loader, #ui

Instance Method Summary collapse

Methods included from Loader

#auto_detect_format, #check_file, #create_topo_bag, #get_local_topo_path, #list_topo_bag, #load_local_topo_or_exit, #load_node_data, #load_topo_from_file_or_exit, #load_topo_from_server, #load_topo_from_server_or_exit, #priority_attrs, #topo_bag_name, #topologies_path

Methods included from BootstrapHelper

#attributes_for_bootstrap, #delete_client_node, #run_bootstrap, #setup_bootstrap_args

Methods included from CommandHelper

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

Constructor Details

#initialize(args) ⇒ TopoBootstrap

Returns a new instance of TopoBootstrap.



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/chef/knife/topo_bootstrap.rb', line 67

def initialize(args)
  super
  @bootstrap_args = initialize_cmd_args(args, name_args, ['bootstrap', ''])
  @results = {
    bootstrapped: [], skipped: [], skipped_ssh: [], existed: [], failed: []
  }
  @msgs = bootstrap_msgs
  @bootstrap = true

  # All called commands need to accept union of options
  Chef::Knife::Bootstrap.options = options
end

Instance Attribute Details

#msgsObject

Returns the value of attribute msgs.



52
53
54
# File 'lib/chef/knife/topo_bootstrap.rb', line 52

def msgs
  @msgs
end

#resultsObject

Returns the value of attribute results.



52
53
54
# File 'lib/chef/knife/topo_bootstrap.rb', line 52

def results
  @results
end

Instance Method Details

#bootstrap_msgsObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chef/knife/topo_bootstrap.rb', line 54

def bootstrap_msgs
  {
    bootstrapped: 'Bootstrapped %{num} nodes [ %{list} ]',
    skipped: 'Unexpected error',
    skipped_ssh: 'Did not bootstrap %{num} nodes [ %{list} ] ' \
      'because they do not have an ssh_host',
    existed: 'Did not bootstrap %{num} nodes [ %{list} ] because '\
      "they already exist.\n"\
      "Specify --overwrite to re-bootstrap existing nodes. \n",
    failed: '%{num} nodes [ %{list} ] failed to bootstrap due to errors'
  }
end

#node_bootstrap(node_data) ⇒ Object

rubocop:disable Metrics/MethodLength



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef/knife/topo_bootstrap.rb', line 104

def node_bootstrap(node_data)
  node_name = node_data['name']
  state = :skipped_ssh
  if node_data['ssh_host']
    exists = resource_exists?("nodes/#{node_name}")
    if config[:overwrite] || !exists
      success = run_bootstrap(node_data, @bootstrap_args, exists)
      state = success ? :bootstrapped : :failed
    else
      state = :existed
    end
  end
  @results[state] << node_name
  success
end

#reportObject

Report is used by create, update and bootstrap commands



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/chef/knife/topo_bootstrap.rb', line 122

def report
  if @topo['nodes'].empty?
    ui.info 'No nodes found'
  else
    report_msg(:bootstrapped, :info, false) if @bootstrap
    report_msg(:skipped, :info, true)
    report_msg(:skipped_ssh, :info, true)
    report_msg(:existed, :info, true)
    report_msg(:failed, :warn, true) if @bootstrap
  end
  ui.info("Topology: #{@topo.display_info}")
end

#report_msg(state, level, only_non_zero = true) ⇒ Object



135
136
137
138
139
140
# File 'lib/chef/knife/topo_bootstrap.rb', line 135

def report_msg(state, level, only_non_zero = true)
  nodes = @results[state]
  return if only_non_zero && nodes.empty?
  ui.send(level, @msgs[state] %
    { num: nodes.length, list: nodes.join(', ') })
end

#runObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chef/knife/topo_bootstrap.rb', line 80

def run
  validate_args

  # load and bootstrap each node that has a ssh_host
  @topo = load_topo_from_server_or_exit(@topo_name)
  @processor = KnifeTopo::Processor.for_topo(@topo)
  nodes = @processor.generate_nodes
  nodes.each do |node_data|
    node_bootstrap(node_data)
  end

  report
end

#validate_argsObject



94
95
96
97
98
99
100
101
# File 'lib/chef/knife/topo_bootstrap.rb', line 94

def validate_args
  unless @name_args[0]
    show_usage
    ui.fatal('You must specify the name of a topology')
    exit 1
  end
  @topo_name = @name_args[0]
end