Skip to content

Instantly share code, notes, and snippets.

@aglarond
Created April 11, 2013 11:13
Show Gist options
  • Save aglarond/5362569 to your computer and use it in GitHub Desktop.
Save aglarond/5362569 to your computer and use it in GitHub Desktop.
Knife exec script for editing 'normal' attributes from the command line. Call with: knife exec node_edit.rb <node> <JSON>
#
# Author:: Dimitri Aivaliotis <dna@everyware.ch>
# Copyright:: Copyright (c) 2013 Every Ware Ltd
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Learned much from node_editor.rb (source) and knife-hacks (github)
# Author:: Daniel DeLeo (<dan@opscode.com>)
# Author:: Steven Danna (steve@opcsode.com)
# Copyright:: Copyright (c) 2011 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Quick hack to edit 'normal' attributes from the command line.
# Call with:
# knife exec node_edit.rb <node> <JSON>
#
require 'chef/json_compat'
require 'chef/mixin/deep_merge'
updated_data = Chef::JSONCompat.from_json(ARGV[3])
nodes.find(:name => ARGV[2]).each do |n|
puts "Editing #{n.name}"
normal_attrs = Chef::Mixin::DeepMerge.merge(n.normal_attrs,updated_data)
n.normal_attrs = normal_attrs
if Chef::Config[:why_run]
puts "normal attributes that would be saved: "
puts Chef::JSONCompat.to_json_pretty(normal_attrs)
else
n.save
end
end
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment