Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
30 changes: 16 additions & 14 deletions lib/opscode_deploy.rb
Original file line number Diff line number Diff line change
@@ -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