Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@meineerde
Forked from gaffneyc/node_update_from_file.rb
Last active December 28, 2015 23:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save meineerde/7580432 to your computer and use it in GitHub Desktop.
Save meineerde/7580432 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
# 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("Updated Node #{update.name}!")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment