public
Last active — forked from jsierles/gist:84082

Custom Knife Plugin for adding various actions

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#!/usr/bin/ruby
#
# Joshua Sierles, 37signals, 2009
 
require 'rubygems'
require 'thor'
require 'chef'
require 'chef/node'
require 'chef/rest'
 
Chef::Config.from_file("/etc/chef/server.rb")
 
API_USERNAME='someuser'
API_PASSWORD='somepass'
 
class Knife < Thor
 
desc "register", "Register an openid for an API user"
method_options :username => :required, :password => :required
def register
@rest = Chef::REST.new(Chef::Config[:registration_url])
@rest.register(options[:username], options[:password])
end
desc "add_recipe", "Add a recipe to a node"
method_options :recipe => :required, :after => :optional, :node => :required
def add_recipe
authenticate
node = @rest.get_rest("nodes/#{options[:node]}")
node.recipes << options[:recipe] if !node.recipes.include?(options[:recipe])
@rest.put_rest("nodes/#{options[:node]}", node)
list_recipes
end
 
desc "remove_recipe", "Remove a recipe from a node"
method_options :recipe => :required, :node => :required
def remove_recipe
authenticate
node = @rest.get_rest("nodes/#{options[:node]}")
node.recipes.delete(options[:recipe]) if node.recipes.include?(options[:recipe])
@rest.put_rest("nodes/#{options[:node]}", node)
list_recipes
end
 
desc "list_recipes", "List a node's recipes"
method_options :node => :required
def list_recipes
authenticate
node = @rest.get_rest("nodes/#{options[:node]}")
puts node.recipes.inspect
end
 
def authenticate
@rest = Chef::REST.new(Chef::Config[:registration_url])
@rest.authenticate(API_USERNAME, API_PASSWORD)
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.