Skip to content

Instantly share code, notes, and snippets.

@augieschwer
Forked from meineerde/node_update_from_file.rb
Last active August 29, 2015 14:08
Show Gist options
  • Save augieschwer/5ac1eedaaaafa11ce4d8 to your computer and use it in GitHub Desktop.
Save augieschwer/5ac1eedaaaafa11ce4d8 to your computer and use it in GitHub Desktop.
class NodeUpdateFromFile < ::Chef::Knife
deps do
require "chef/node"
require "chef/json_compat"
require "chef/knife/core/object_loader"
end
banner "knife node update from file FILE (options)"
def loader
@loader ||= ::Chef::Knife::Core::ObjectLoader.new(Chef::Node, ui)
end
def run
update = loader.load_from("nodes", @name_args[0])
begin
node = ::Chef::Node.load(update.name)
rescue Net::HTTPServerException
ui.info("Could not load existing node #{update.name}, assuming new node.")
node = ::Chef::Node.new
node.name(update.name)
end
ui.info("Save lastrun info.")
lastrun_attrs = node.normal_attrs["lastrun"]
# Replace attributes, run_list, and chef_environment from the new node
# definition.
node.normal_attrs = update.normal_attrs
node.override_attrs = update.override_attrs
node.default_attrs = update.default_attrs
node.run_list.reset!(update.run_list)
node.chef_environment(update.chef_environment)
# Put the lastrun attributes back in.
node.normal_attrs["lastrun"] = lastrun_attrs
# Expand the run_list in case it has changed
node.expand!
node.save
output(format_for_display(node)) if config[:print_after]
ui.info("Updated Node #{update.name}!")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment