Skip to content

Instantly share code, notes, and snippets.

@nathenharvey
Created July 25, 2013 16:07
Show Gist options
  • Save nathenharvey/6081247 to your computer and use it in GitHub Desktop.
Save nathenharvey/6081247 to your computer and use it in GitHub Desktop.
A Chef knife.rb that uses the git gem to determine which git branch you're in and then updates the Chef server endpoint accordingly.
require 'rubygems'
require 'git'
g = Git.open('./')
current_dir = File.dirname(__FILE__)
log_level :info
log_location STDOUT
node_name 'myworkstation'
cache_type 'BasicFile'
cache_options( :path => "#{ENV['HOME']}/.chef/checksums" )
cookbook_path ["#{current_dir}/../cookbooks"]
if g.current_branch == "production"
puts "Using Production"
chef_server_url "https://api.opscode.com/organizations/myproduction"
validation_client_name 'mychef-validator'
validation_key "#{ENV['HOME']}/.chef/myproduction-validator.pem"
client_key "#{ENV['HOME']}/.chef/myproduction.pem"
elsif g.current_branch == "staging"
puts "Using Staging"
chef_server_url "https://api.opscode.com/organizations/mystaging"
validation_client_name 'mychef-validator'
validation_key "#{ENV['HOME']}/.chef/mystaging-validator.pem"
client_key "#{ENV['HOME']}/.chef/mystaging.pem"
else
puts "Using Development"
chef_server_url "https://api.opscode.com/organizations/mydevelopment"
validation_client_name 'mychef-validator'
validation_key "#{ENV['HOME']}/.chef/mydevelopment-validator.pem"
client_key "#{ENV['HOME']}/.chef/mydevelopment.pem"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment