Class: NodeUpdate::NodeUpdateFromFile

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/update_from_file.rb

Instance Method Summary collapse

Instance Method Details

#loaderObject



30
31
32
# File 'lib/chef/knife/update_from_file.rb', line 30

def loader
  @loader ||= ::Chef::Knife::Core::ObjectLoader.new(Chef::Node, ui)
end

#runObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chef/knife/update_from_file.rb', line 46

def run
  file_path = @name_args[0]
  updated = loader.load_from("nodes", file_path)
  node_name = updated.name

  ui.info("Looking for #{node_name}")

  result = searcher.search(:node, "name:#{node_name}")

  node = result.first.first

  if node.nil?
    ui.warn("Could not find a node named #{node_name}")
    result = ui.ask_question("Create a new one? (Y/N)", :default => "y").upcase
    if ['Y', 'YES'].include?(result)
      system( *%W{knife node from file #{file_path}})
    else
      ui.info("Exiting to system")
      exit 1
    end

  else
    update_node(node, updated)

    ui.info("Saving the updated node #{@node_name}")
    node.save
  end
end

#searcherObject



34
35
36
# File 'lib/chef/knife/update_from_file.rb', line 34

def searcher
  @searcher ||= ::Chef::Search::Query.new
end

#update_node(node, updated) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/chef/knife/update_from_file.rb', line 38

def update_node(node, updated)
  node.normal_attrs = Chef::Mixin::DeepMerge.merge(node.normal_attrs, updated.normal_attrs)
  node.override_attrs = Chef::Mixin::DeepMerge.merge(node.override_attrs, updated.override_attrs)
  node.run_list(updated.run_list)
  node.chef_environment(updated.chef_environment)
  node
end