Class: KnifeTopo::TopoCreate

Inherits:
TopoBootstrap show all
Includes:
CommandHelper, Loader, NodeUpdateHelper
Defined in:
lib/chef/knife/topo_create.rb

Overview

knife topo create

Direct Known Subclasses

TopoUpdate

Instance Attribute Summary

Attributes included from Loader

#loader, #ui

Attributes inherited from TopoBootstrap

#msgs, #results

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 NodeUpdateHelper

#do_node_updates, #update_attrs, #update_chef_env, #update_node, #update_node_with_values, #update_runlist, #update_tags

Methods included from CommandHelper

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

Methods inherited from TopoBootstrap

#node_bootstrap, #report, #report_msg

Methods included from BootstrapHelper

#attributes_for_bootstrap, #delete_client_node, #run_bootstrap, #setup_bootstrap_args

Constructor Details

#initialize(args) ⇒ TopoCreate

Returns a new instance of TopoCreate.



60
61
62
63
# File 'lib/chef/knife/topo_create.rb', line 60

def initialize(args)
  super
  @args = args
end

Instance Method Details

#bootstrap_msgsObject



65
66
67
68
69
70
# File 'lib/chef/knife/topo_create.rb', line 65

def bootstrap_msgs
  msgs = super.dup
  msgs[:existed] = 'Updated but did not bootstrap %{num} existing nodes '\
  "[ %{list} ].\n Specify --overwrite to re-bootstrap existing nodes. \n"
  msgs
end

#bootstrap_or_update_node(node_data, merge) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/chef/knife/topo_create.rb', line 134

def bootstrap_or_update_node(node_data, merge)
  node_name = node_data['name']
  if @bootstrap
    update_node(node_data, merge) unless node_bootstrap(node_data)
  elsif update_node(node_data, merge)
    @results[:existed] << node_name
  else
    @results[:skipped] << node_name
  end
end

#confirm_and_update_topoObject



125
126
127
128
129
130
131
132
# File 'lib/chef/knife/topo_create.rb', line 125

def confirm_and_update_topo
  version = @topo.topo_version
  to_version_str = " to version #{version}"
  msg = "Topology #{@topo_name} already exists - do you want to " \
    "update it#{to_version_str  if version}"
  ui.confirm(msg, true, false)
  @topo.save
end

#create_or_update_topoObject



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/chef/knife/topo_create.rb', line 105

def create_or_update_topo
  # Load the topology data & create the topology bag
  @topo = load_local_topo_or_exit(@topo_name)
  create_topo_bag

  # Add topology item to the data bag on the server
  @topo.create
rescue Net::HTTPServerException => e
  raise unless e.to_s =~ /^409/
  confirm_and_update_topo
end

#non_bootstrap_msgsObject



72
73
74
75
76
77
78
79
80
# File 'lib/chef/knife/topo_create.rb', line 72

def non_bootstrap_msgs
  {
    existed: 'Applied updates (if any) to %{num} nodes [ %{list} ]',
    skipped_ssh: 'Unexpected error skipped_ssh',
    skipped: 'Skipped %{num} nodes [ %{list} ] because they do not exist',
    bootstrapped: 'Unexpected error bootstrapped',
    failed: 'Unexpected error failed'
  }
end

#processorObject



94
95
96
# File 'lib/chef/knife/topo_create.rb', line 94

def processor
  @processor ||= KnifeTopo::Processor.for_topo(@topo)
end

#runObject



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

def run
  validate_args
  create_or_update_topo

  # make sure env and cookbooks are in place
  check_chef_env(@topo['chef_environment'])
  upload_artifacts unless config[:disable_upload]
  update_nodes

  report
end

#update_nodesObject



117
118
119
120
121
122
123
# File 'lib/chef/knife/topo_create.rb', line 117

def update_nodes
  nodes = processor.generate_nodes
  merge = @topo.merge_attrs
  nodes.each do |node_data|
    bootstrap_or_update_node(node_data, merge)
  end
end

#upload_artifactsObject



145
146
147
# File 'lib/chef/knife/topo_create.rb', line 145

def upload_artifacts
  processor.upload_artifacts('cmd' => self, 'cmd_args' => @args)
end

#validate_argsObject



98
99
100
101
102
103
# File 'lib/chef/knife/topo_create.rb', line 98

def validate_args
  super
  @bootstrap = config[:bootstrap]
  @msgs = @bootstrap ? bootstrap_msgs : non_bootstrap_msgs
  config[:disable_editing] = true
end