Skip to content

Instantly share code, notes, and snippets.

@jtimberman
Forked from jsierles/gist:84082
Created March 28, 2014 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtimberman/9837538 to your computer and use it in GitHub Desktop.
Save jtimberman/9837538 to your computer and use it in GitHub Desktop.
The original "knife" - a Thor task file for interacting with a Chef Server API, ca. the Stone Age of Chef :).
#!/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