Skip to content

Instantly share code, notes, and snippets.

@gaffneyc
Created September 28, 2012 16:54
Show Gist options
  • Save gaffneyc/3800934 to your computer and use it in GitHub Desktop.
Save gaffneyc/3800934 to your computer and use it in GitHub Desktop.
Replace attributes, run_list, and chef_environment for an existing node without losing automatic attributes.
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])
node = ::Chef::Node.load(update.name)
# 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)
# Expand the run_list in case it has changed
node.expand!
node.save
output(format_for_display(node)) if config[:print_after]
ui.info("update Node #{update.name}!")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment