-
-
Save thommay/92044 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment