-
-
Save davidlee/77904 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 | |
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