Skip to content

Instantly share code, notes, and snippets.

@antonydenyer
Created September 21, 2012 10:07
Show Gist options
  • Save antonydenyer/3760738 to your computer and use it in GitHub Desktop.
Save antonydenyer/3760738 to your computer and use it in GitHub Desktop.
Capistrano depoly mono with fastcgi
set :stages, %w(devtest systest uat prod)
set :default_stage, "devtest"
set :keep_releases, 5
require 'capistrano/ext/multistage'
require 'nokogiri'
set :use_sudo, false
set :normalize_asset_timestamps, false
set :deploy_via, :copy
set :copy_dir, "."
set :copy_remote_dir, "/tmp/capistrano"
set :repository, "./artifacts"
set :scm, :none
ssh_options[:keys] = Dir["./ssh/*id_rsa"]
namespace :remote do
desc <<-DESC
Create directory required by copy_remote_dir.
DESC
task :create_copy_remote_dir, :roles => :app do
print "creating #{copy_remote_dir}.\n"
run "mkdir -p #{copy_remote_dir}"
run "mkdir -p #{deploy_to}/releases"
end
desc <<-DESC
delete the extra public folder that capistrano creates
DESC
task :delete_public_folder, :roles => :app do
print "deleteing public folder.\n"
run "rm -r #{latest_release}/public"
run "rm -r #{latest_release}/tmp"
run "rm -r #{latest_release}/log"
end
end
namespace :sdigital do
task :start_temp_fastcgi_server, :roles => :app do
run "nohup fastcgi-mono-server4 /applications=/:#{latest_release} /filename=/tmp/SOCK-#{fqdn}-prerelease /socket=unix >/dev/null 2>&1&"
end
desc <<-DESC
(backup production socket and) move staging to production
DESC
task :release, :roles => :app do
run "ln /tmp/SOCK-#{fqdn} /tmp/SOCK-#{fqdn}-backup || true" # we don't care if there was no site before
run "mv /tmp/SOCK-#{fqdn}-prerelease /tmp/SOCK-#{fqdn}"
end
task :stop_previous_release_of_fastcgi_server, :roles => :app do
begin
# FIXME: wait 10 secs
run "if [ -e /tmp/SOCK-#{fqdn}-backup ]; then kill `lsof -t /tmp/SOCK-#{fqdn}-backup`; else echo 'no backup found'; fi"
rescue
#dont care
end
end
desc <<-DESC
check staging is up and running
DESC
task :hit_temp_status, :roles => :app do
attempt_number = 0
begin
attempt_number = attempt_number + 1
current_host = capture("echo $CAPISTRANO:HOST$").strip
curl_flags = "--verbose --silent -w %{http_code} --retry 5 --fail -X GET -o curl.output -H 'Host: #{fqdn}-prerelease'"
curl_command = "curl #{curl_flags} http://#{current_host}/status"
status_code = capture(curl_command)
if status_code != "200"
raise "error, status code was not 200 but #{status_code}"
end
rescue Exception => ex
retry if attempt_number < 10
puts "##teamcity[message text='Could not curl site.' errorDetails='Failed bring site up. See output above in the logs' status='ERROR']"
error = CommandError.new("Failed to curl the site on #{current_host}")
raise error
end
end
task :indicate_success, :roles => :app do
run("curl --silent -w %{http_code} --retry 2 -X GET -o curl.output -H 'Host: #{fqdn}' http://localhost/status")
end
end
before 'deploy', 'remote:create_copy_remote_dir', 'sdigital:replace_config'
after 'deploy:update_code', 'sdigital:start_temp_fastcgi_server'
before 'deploy:create_symlink', 'sdigital:hit_temp_status'
after 'deploy', 'remote:delete_public_folder', 'deploy:cleanup', 'sdigital:release', 'sdigital:indicate_success', 'sdigital:stop_previous_release_of_fastcgi_server'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment