Skip to content

Instantly share code, notes, and snippets.

@rmoriz
Created February 24, 2011 17:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmoriz/842491 to your computer and use it in GitHub Desktop.
Save rmoriz/842491 to your computer and use it in GitHub Desktop.
# FIRST: Environment variables
knife_env = ENV['KNIFE_ENV']
knife_org = ENV['KNIFE_ORG']
knife_config_dir = ENV['KNIFE_CONFIG_DIR']
knife_config_file = ENV['KNIFE_CONFIG_FILE']
if knife_env.blank?
`git branch 2> /dev/null` =~ /^\*\s(.*)$/
git_branch = $1
if git_branch && git_branch == 'production'
knife_env = 'production'
else
knife_env = 'development'
end
end
# Defaults
knife_org ||= 'moriz'
knife_config_dir ||= '~/.chef'
knife_config_file ||= 'KNIFE_ORG-KNIFE_ENV-knife.rb'
knife_config_file.gsub!(/KNIFE_ENV/, knife_env ).gsub!(/KNIFE_ORG/, knife_org)
# the "real" knife file e.g.:
# ~/.chef/myorganization-development-knife.rb
# OR ~/.chef/myorganization-production-knife.rb
#
knife_file = File.expand_path knife_config_dir + '/' + knife_config_file
unless File.exists? knife_file
puts "*" * 80
puts "!!! Could not read #{knife_file} !!!"
puts
puts " => please create this file OR provide settings through the environment."
puts "*" * 80
puts " (see: #{__FILE__} for the loading logic)"
exit 1
else
puts knife_file
# check this url for more details:
# https://github.com/opscode/mixlib-config/blob/master/lib/mixlib/config.rb#L36
Chef::Config.from_file knife_file
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment