Skip to content

Instantly share code, notes, and snippets.

@cwjohnston
Created April 3, 2012 16:49
Show Gist options
  • Select an option

  • Save cwjohnston/2293568 to your computer and use it in GitHub Desktop.

Select an option

Save cwjohnston/2293568 to your computer and use it in GitHub Desktop.

Revisions

  1. cwjohnston created this gist Apr 3, 2012.
    43 changes: 43 additions & 0 deletions setrevision.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    # 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