apply_staleness_check(commands)
click to toggle source
def apply_staleness_check(commands)
needs(:deploy_to, :repository)
check = "cd #{setting(:deploy_to)}; " +
"ml=\`git log -1 --pretty=format:%H\`; " +
"mr=\`git ls-remote #{setting(:repository)} refs/heads/#{branch}\`; "
if setting(:deploy_config_to)
check += "cd #{setting(:deploy_config_to)}; " +
"cl=\`git log -1 --pretty=format:%H\`; " +
"cr=\`git ls-remote #{setting(:config_repository)} refs/heads/#{config_branch}\`; "
end
check += "if [[ $ml != ${mr%%\t*} ]] " +
(setting(:deploy_config_to) ? "|| [[ $cl != ${cr%%\t*} ]]" : '') +
"; then #{commands}; else echo \"No changes to deploy.\"; fi"
end
branch()
click to toggle source
def branch
(setting(:branch) and setting(:branch) != '') ? setting(:branch) : 'master'
end
buffer()
click to toggle source
def buffer
@buffer ||= []
end
build_command(domain, cmd)
click to toggle source
def build_command(domain, cmd)
"#{'set -x; ' if debugging?}" + encode_roles(domain['roles']) + cmd
end
build_path(path)
click to toggle source
def build_path(path)
return path if path =~ %r{^/}
File.join(setting(:deploy_to), path)
end
bundle()
click to toggle source
def bundle
return '' if buffer.empty?
(staleness_checks_enabled? and check_staleness?) ? apply_staleness_check(join_commands) : join_commands
end
capture_git_changes()
click to toggle source
def capture_git_changes
needs(:deploy_to)
enqueue "git diff --name-only ${ml}..HEAD > #{setting(:deploy_to)}/.whiskey_disk_git_changes"
end
check_staleness?()
click to toggle source
def check_staleness?
config.check_staleness?
end
checkout_configuration_repository()
click to toggle source
def checkout_configuration_repository
needs(:deploy_config_to, :config_repository)
clone_repository(setting(:config_repository), setting(:deploy_config_to), config_branch)
end
checkout_main_repository()
click to toggle source
def checkout_main_repository
needs(:deploy_to, :repository)
clone_repository(setting(:repository), setting(:deploy_to), branch)
end
clone_repository(repo, path, my_branch)
click to toggle source
def clone_repository(repo, path, my_branch)
enqueue "cd #{parent_path(path)}"
enqueue("if [ -e #{path} ]; then echo 'Repository already cloned to [#{path}]. Skipping.'; " +
"else git clone #{repo} #{tail_path(path)} && #{safe_branch_checkout(path, my_branch)}; fi")
end
config()
click to toggle source
def config
@config ||= WhiskeyDisk::Config.new
end
config_branch()
click to toggle source
def config_branch
(setting(:config_branch) and setting(:config_branch) != '') ? setting(:config_branch) : 'master'
end
configuration()
click to toggle source
def configuration
@configuration ||= config.fetch
end
debugging?()
click to toggle source
def debugging?
config.debug?
end
domain_limit_match?(domain, limit)
click to toggle source
def domain_limit_match?(domain, limit)
domain.sub(%r{^.*@}, '') == limit
end
domain_of_interest?(domain)
click to toggle source
def domain_of_interest?(domain)
return true unless limit = config.domain_limit
domain_limit_match?(domain, limit)
end
encode_roles(roles)
click to toggle source
def encode_roles(roles)
return '' unless roles and !roles.empty?
"export WD_ROLES='#{roles.join(':')}'; "
end
enqueue(command)
click to toggle source
def enqueue(command)
buffer << command
end
ensure_config_parent_path_is_present()
click to toggle source
def ensure_config_parent_path_is_present
needs(:deploy_config_to)
enqueue "mkdir -p #{parent_path(setting(:deploy_config_to))}"
end
ensure_main_parent_path_is_present()
click to toggle source
def ensure_main_parent_path_is_present
needs(:deploy_to)
enqueue "mkdir -p #{parent_path(setting(:deploy_to))}"
end
env_vars()
click to toggle source
def env_vars
return '' unless setting(:rake_env)
setting(:rake_env).keys.inject('') do |buffer,k|
buffer += "#{k}='#{setting(:rake_env)[k]}' "
buffer
end
end
flush()
click to toggle source
def flush
needs(:domain)
setting(:domain).each do |domain|
next unless domain_of_interest?(domain['name'])
puts "Deploying #{domain['name']}..."
status = remote?(domain['name']) ? run(domain, bundle) : shell(domain, bundle)
record_result(domain['name'], status)
end
end
has_config_repo?()
click to toggle source
def has_config_repo?
! (setting(:config_repository).nil? or setting(:config_repository) == '')
end
if_file_present(path, cmd)
click to toggle source
def if_file_present(path, cmd)
"if [ -e #{path} ]; then #{cmd}; fi"
end
if_task_defined(task, cmd)
click to toggle source
def if_task_defined(task, cmd)
%Q(rakep=`#{env_vars} rake -P` && if [[ `echo "${rakep}" | grep #{task}` != "" ]]; then #{cmd}; fi )
end
initialize_all_changes()
click to toggle source
def initialize_all_changes
needs(:deploy_to)
initialize_git_changes
initialize_rsync_changes
end
initialize_git_changes()
click to toggle source
def initialize_git_changes
needs(:deploy_to)
enqueue "rm -f #{setting(:deploy_to)}/.whiskey_disk_git_changes"
snapshot_git_revision
end
initialize_rsync_changes()
click to toggle source
def initialize_rsync_changes
needs(:deploy_to)
enqueue "rm -f #{setting(:deploy_to)}/.whiskey_disk_rsync_changes"
end
join_commands()
click to toggle source
def join_commands
buffer.collect {|c| "{ #{c} ; }"}.join(' && ')
end
needs(*keys)
click to toggle source
def needs(*keys)
keys.each do |key|
raise "No value for '#{key}' declared in configuration files [#{config.configuration_file}]" unless setting(key)
end
end
parent_path(path)
click to toggle source
def parent_path(path)
File.split(path).first
end
project_name_specified?()
click to toggle source
def project_name_specified?
setting(:project) != 'unnamed_project'
end
record_result(domain, status)
click to toggle source
def record_result(domain, status)
@results ||= []
@results << { 'domain' => domain, 'status' => status }
end
refresh_checkout(path, repo_branch)
click to toggle source
def refresh_checkout(path, repo_branch)
enqueue "cd #{path}"
enqueue "git fetch origin +refs/heads/#{repo_branch}:refs/remotes/origin/#{repo_branch} #{'&>/dev/null' unless debugging?}"
enqueue "git checkout #{repo_branch} #{'&>/dev/null' unless debugging?}"
enqueue "git reset --hard origin/#{repo_branch} #{'&>/dev/null' unless debugging?}"
end
refresh_configuration()
click to toggle source
def refresh_configuration
needs(:deploy_to, :deploy_config_to)
raise "Must specify project name when using a configuration repository." unless project_name_specified?
enqueue "echo Rsyncing configuration..."
enqueue("rsync -a#{'v --progress' if debugging?} " + '--log-format="%t [%p] %i %n" ' +
"#{setting(:deploy_config_to)}/#{setting(:project)}/#{setting(:config_target)}/ #{setting(:deploy_to)}/ " +
"> #{setting(:deploy_to)}/.whiskey_disk_rsync_changes")
end
remote?(domain)
click to toggle source
def remote?(domain)
return false unless domain
return false if domain == 'local'
limit = config.domain_limit
return false if limit and domain_limit_match?(domain, limit)
true
end
run(domain, cmd)
click to toggle source
def run(domain, cmd)
ssh(domain, cmd)
end
run_post_deploy_hooks()
click to toggle source
def run_post_deploy_hooks
needs(:deploy_to)
run_script(setting(:post_deploy_script))
run_rake_task(setting(:deploy_to), "deploy:post_deploy")
end
run_post_setup_hooks()
click to toggle source
def run_post_setup_hooks
needs(:deploy_to)
run_script(setting(:post_setup_script))
run_rake_task(setting(:deploy_to), "deploy:post_setup")
end
run_rake_task(path, task_name)
click to toggle source
def run_rake_task(path, task_name)
enqueue "echo Running rake #{task_name}..."
enqueue "cd #{path}"
enqueue(if_file_present("#{setting(:deploy_to)}/Rakefile",
if_task_defined(task_name, "#{env_vars} rake #{'--trace' if debugging?} #{task_name} to=#{setting(:environment)}")))
end
run_script(script)
click to toggle source
def run_script(script)
return unless script
enqueue(%Q<cd #{setting(:deploy_to)}; echo "Running post script..."; #{env_vars} bash #{'-x' if debugging?} #{build_path(script)}>)
end
safe_branch_checkout(path, my_branch)
click to toggle source
def safe_branch_checkout(path, my_branch)
%Q(cd #{path} && git checkout -b #{my_branch} origin/#{my_branch} || git checkout #{my_branch} origin/#{my_branch} || git checkout #{my_branch})
end
setting(key)
click to toggle source
def setting(key)
configuration[key.to_s]
end
shell(domain, cmd)
click to toggle source
def shell(domain, cmd)
puts "Running command locally: [#{cmd}]" if debugging?
system('bash', '-c', build_command(domain, cmd))
end
snapshot_git_revision()
click to toggle source
def snapshot_git_revision
needs(:deploy_to)
enqueue "cd #{setting(:deploy_to)}"
enqueue %Q{ml=\`git log -1 --pretty=format:%H\`}
end
ssh(domain, cmd)
click to toggle source
def ssh(domain, cmd)
args = []
args << domain['name']
args << '-v' if debugging?
args += domain['ssh_options'] if domain['ssh_options']
args << build_command(domain, cmd)
puts "Running: ssh #{args.join(' ')}" if debugging?
system('ssh', *args)
end
staleness_checks_enabled?()
click to toggle source
def staleness_checks_enabled?
!!@staleness_checks
end
success?()
click to toggle source
def success?
return true if !results or results.empty?
results.all? {|result| result['status'] }
end
summarize()
click to toggle source
def summarize
puts "\nResults:"
if results and not results.empty?
total, successes, failures = summarize_results(results)
puts "Total: #{total} deployment#{total == 1 ? '' : 's'}, " +
"#{successes} success#{successes == 1 ? '' : 'es'}, " +
"#{failures} failure#{failures == 1 ? '' : 's'}."
else
puts "No deployments to report."
end
end
summarize_results(results)
click to toggle source
def summarize_results(results)
successes = failures = 0
results.each do |result|
puts "#{result['domain']} => #{result['status'] ? 'succeeded' : 'failed'}."
if result['status']
successes += 1
else
failures += 1
end
end
[successes + failures, successes, failures]
end
tail_path(path)
click to toggle source
def tail_path(path)
File.split(path).last
end
update_configuration_repository_checkout()
click to toggle source
def update_configuration_repository_checkout
needs(:deploy_config_to)
initialize_rsync_changes
refresh_checkout(setting(:deploy_config_to), config_branch)
end
update_main_repository_checkout()
click to toggle source
def update_main_repository_checkout
needs(:deploy_to)
initialize_git_changes
refresh_checkout(setting(:deploy_to), branch)
capture_git_changes
end