Skip to content

Instantly share code, notes, and snippets.

@arturaz
Created December 5, 2012 15:32
Show Gist options
  • Save arturaz/4216581 to your computer and use it in GitHub Desktop.
Save arturaz/4216581 to your computer and use it in GitHub Desktop.
Deploying Play application with capistrano
load 'deploy'
# Uncomment if you are using Rails' asset pipeline
# load 'deploy/assets'
Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'deploy/config'
require 'fileutils'
set :application, "your-app-name" # Application name.
# Server urls
set :locations, ["your.server.com"]
set :user, "yourusername" # Remote user name. Must be able to log in via SSH.
set :port, 22 # SSH port. Only required if non default ssh port used.
# Remove or set the true if all commands should be run through sudo.
set :use_sudo, false
set :deploy_to, "/home/#{user}/#{application}"
set :shared_path, "#{deploy_to}/shared"
# Copy the files across as an archive rather than
# using Subversion on the remote machine.
set :deploy_via, :copy
# Use without Subversion on local machine.
set :repository, File.expand_path(
File.join(File.dirname(__FILE__), '..', 'target', 'deploy')
)
set :scm, :none
locations.each { |location| role :app, location }
desc "Cleans play application"
task :clean do
puts "Cleaning Play application."
system "play clean"
end
this_dir = File.expand_path("#{File.dirname(__FILE__)}")
root_dir = File.expand_path("#{this_dir}/..")
local_deploy = File.expand_path("#{root_dir}/target/deploy")
def info(*args)
print " "
puts *args
end
# Override default tasks which are not relevant to a non-rails app.
namespace :deploy do
desc "Prepares Play application for deployment in target/deploy"
task :prepare do
info "Compiling Play application."
system "play compile stage"
info "Ensuring #{local_deploy} is fresh."
FileUtils.rm_rf local_deploy if File.exists? local_deploy
FileUtils.mkdir local_deploy
info "Copying NewRelic"
FileUtils.cp_r "#{this_dir}/newrelic", local_deploy
info "Moving files."
%w{target}.each do |dir|
FileUtils.mkdir "#{local_deploy}/#{dir}"
end
FileUtils.cp "#{this_dir}/daemon", local_deploy
["#{root_dir}/target/start", "#{root_dir}/target/staged"].each do |f|
FileUtils.mv f, "#{local_deploy}/target"
end
end
task :migrate do
info " not doing migrate because not a Rails application."
end
task :finalize_update do
info " not doing finalize_update because not a Rails application."
end
task :start do
info "Starting remote Play application"
run "cd #{release_path} && ./daemon start"
info "Application started."
end
task :stop do
info "Stopping remote Play application"
run "cd #{release_path} && ./daemon stop"
info "Application stopped."
end
task :restart do
stop
start
end
end
# Custom tasks for our hosting environment.
namespace :remote do
task :prepare do
info "creating #{release_path}."
run "mkdir -p #{release_path}"
info "creating logs & run."
run "mkdir -p #{shared_path}/logs #{shared_path}/run"
run "ln -s #{shared_path}/logs #{release_path}/logs"
run "ln -s #{shared_path}/run #{release_path}/run"
run "ln -s #{shared_path}/conf #{release_path}/conf"
end
end
# Callbacks.
before 'deploy:update_code', 'deploy:prepare'
before 'deploy:update', 'remote:prepare'
after 'deploy:setup', 'remote:create_symlink'
after 'deploy:finalize_update', 'deploy:cleanup'
#!/usr/bin/env bash
set -e
app="Play application"
# Cannot be "logs/application.log", because play erases that on start.
logfile="logs/daemon.log"
# Max numbers of secs to wait for app to come up/down.
max_wait=10
# Line count from logfile to output on failure.
linecount=30
pidfile="run/RUNNING_PID"
echoerr() { echo "$@" 1>&2; }
checkpid() { kill -0 $1 > /dev/null 2>&1; }
fail() {
echo
echoerr "Cannot start $app! Here's last $linecount lines from $logfile:"
tail -n $linecount $logfile 1>&2
exit 1
}
checkpidfile() {
if [ -e $pidfile ]; then
if ! checkpid `cat $pidfile`; then
echo "Dangling pidfile found, removing."
rm $pidfile
fi
fi
}
if [ "$1" == "start" ]; then
checkpidfile
if [ ! -e "$pidfile" ]; then
echo -n "Launching $app"
echo "Starting $app @ `date`" >> $logfile
nohup target/start \
-Dpidfile.path=$pidfile \
-Dconfig.file=conf/production.conf \
-Dlogger.resource=prod-logger.xml \
-javaagent:newrelic/agent.jar \
-Xmx1G \
>> $logfile 2>&1 &
# Wait for app to come up.
cur_wait=0
while [ ! -f "$pidfile" ]; do
echo -n .
sleep 1
cur_wait=$((cur_wait+1))
if [ "$cur_wait" -gt "$max_wait" ]; then
fail
fi
done
echo ". Running!"
else
echo "!!! $app already running with PID `cat $pidfile`"
fi
elif [ "$1" == "stop" ]; then
checkpidfile
if [ -e "$pidfile" ]; then
echo -n "Stopping $app"
kill `cat $pidfile`
# Wait for app to come down
cur_wait=0
while [ -f "$pidfile" ]; do
echo -n .
sleep 1
cur_wait=$((cur_wait+1))
if [ "$cur_wait" -gt "$max_wait" ]; then
fail
fi
done
echo ". Stopped."
else
echo "!!! $app not found."
fi
else
echo "Usage: $0 start|stop"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment