Skip to content

Instantly share code, notes, and snippets.

@davidlee
Forked from jsierles/gist:60789
Created March 12, 2009 04:28
Show Gist options
  • Save davidlee/77904 to your computer and use it in GitHub Desktop.
Save davidlee/77904 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'rubygems'
require 'thor'
require 'chef'
require 'chef/node'
require 'chef/rest'
Chef::Config.from_file("/etc/chef/server.rb")
API_USERNAME='user'
API_PASSWORD='pass'
class Knife < Thor
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
Knife.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment