From c683f22ba04c8134b2e2ca0e87dd8d8a7960f170 Mon Sep 17 00:00:00 2001 From: John Keiser Date: Thu, 15 Sep 2011 13:42:03 -0700 Subject: [PATCH] Figure out environment, branch and remote automatically and add --opscode-environment for Private Chef environments --- deploy.rb | 10 +++++++++- lib/opscode_deploy.rb | 30 ++++++++++++++++-------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/deploy.rb b/deploy.rb index 8ff7d54..3436646 100644 --- a/deploy.rb +++ b/deploy.rb @@ -49,6 +49,11 @@ class Deploy < Chef::Knife :boolean => true, :default => false + option :opscode_environment, + :short => "-E", + :long => "--opscode-environment ENVIRONMENT", + :description => "Opscode environment" + deps do require 'yajl' require 'chef/search/query' @@ -96,7 +101,10 @@ def git_remote end def deploy_config - @deploy_config ||= (Chef::Config[:deploy][environment] rescue nil) + @deploy_config ||= (Chef::Config[:deploy][environment] rescue Mash.new) + @deploy_config[:default_command] ||= "macterm" + @deploy_config[:branch] ||= `git branch`.each_line.grep(/^\*/).first[1..-1].strip + @deploy_config[:remote] ||= `git config --get branch.#{@deploy_config[:branch]}.remote`.strip if @deploy_config.nil? || !@deploy_config.is_a?(Hash) ui.error "missing deploy({#{environment} => {...}}) section in knife.rb" exit 1 diff --git a/lib/opscode_deploy.rb b/lib/opscode_deploy.rb index 91a28c7..99c5d08 100644 --- a/lib/opscode_deploy.rb +++ b/lib/opscode_deploy.rb @@ -1,37 +1,39 @@ module OpscodeDeploy module EnvironmentNames - OC_ENVS = {'rs-prod' => true, 'rs-preprod' => true} - attr_reader :environment def get_env_from_args! - unless @environment = guess_env_name + unless @environment = config[:opscode_environment] || guess_env_name ui.error "Environment to edit could not be determined by magic and you did not provide one" exit 1 end end def guess_env_name - pwd = File.basename(Dir.pwd) - if OC_ENVS.key?(pwd) - pwd - elsif OC_ENVS.key?(@name_args[0]) - @name_args[0] + env_dir = File.basename(repo_path) + if File.exist?(repo_file("data_bags/environments/#{env_dir}.json")) + env_dir else nil end end + def repo_path + cookbook_parent = File.expand_path("..", Chef::Config.cookbook_path.first) + if File.directory?(File.join(cookbook_parent, "data_bags")) + cookbook_parent + elsif File.directory?(File.join(Dir.pwd, "data_bags")) + Dir.pwd + else + File.join(Dir.pwd, 'chef-repo') + end + end + # Takes a relative path and expands it relative to your chef-repo # Looks for data_bags in cwd, if found then assume we are in the # chef-repo, otherwise look for 'chef-repo' def repo_file(relative_path) - if File.directory?("data_bags") - File.expand_path(relative_path, Dir.pwd) - else - repo_path = File.expand_path('chef-repo', Dir.pwd) - File.expand_path(relative_path, repo_path) - end + File.expand_path(relative_path, repo_path) end end end