Skip to content

Instantly share code, notes, and snippets.

@cwjohnston
Created April 3, 2012 16:49
Show Gist options
  • Save cwjohnston/2293568 to your computer and use it in GitHub Desktop.
Save cwjohnston/2293568 to your computer and use it in GitHub Desktop.
knife exec script for setting the value of a particular node attribute (i.e. core.revision or haystack.revision)
# knife-exec script to set git revision to be deployed for specified application
# Example usage: knife exec /path/to/this/script/setrevision haystack i-fb106a9f v69.10
require 'chef/knife'
require 'chef/shef/ext'
# - ARGV[2] expected to be the app name (e.g. haystack, core)
# - ARGV[3] expected to be the node name
# - ARGV[4] expected to be the revision id
abort("usage: knife exec setrevision APP NODE REVISION") unless ARGV[2]
begin
nodes.find(:name => ARGV[3]).each do |n|
case ARGV[2]
when 'haystack'
n['haystack']['revision'] = ARGV[4]
begin
if n.save
puts "Set haystack.revision to #{ARGV[4]} on node #{n['name']}."
end
rescue => e
puts "Failed to modify node #{n['name']}: #{e.inspect}"
end
when 'core'
n['core']['revision'] = ARGV[4]
begin
if n.save
puts "Set core.revision to #{ARGV[4]} on node #{n['name']}."
end
rescue => e
puts "Failed to modify node #{n['name']}: #{e.inspect}"
end
else
puts "Did not recognize #{ARGV[2]} as a valid application."
exit 1
end
end
rescue => e
puts "Error encountered when searching for node #{ARGV[3]}: #{e.inspect}"
end
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment