Skip to content

Instantly share code, notes, and snippets.

@dwayne
Created June 11, 2012 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save dwayne/2909075 to your computer and use it in GitHub Desktop.
Save dwayne/2909075 to your computer and use it in GitHub Desktop.
Deploying Sinatra/Thin to WebFaction
# Exclude files that don't need to be on the server
# Used by rsync when deploying code to the server
.excludes
.git
.gitignore
log/
tmp/
# These directories are created by Thin when `rake:thin start` is invoked
log/
tmp/
# Server options:
address: 127.0.0.1
port: 5000
# Adapter options:
environment: development
# See `thin -h` for more options
# Server options:
port: 12345
# Adapter options:
environment: production
# Daemon options:
daemonize: true
user: user
group: group
# See `thin -h` for more options
# Assuming a classic style app
require './app'
run Sinatra::Application
# Modular style app
# require './awesome-app'
# run AwesomeApp
# or
# map('/end/point') { run AwesomeApp }
require 'rake'
ENV['RACK_ENV'] ||= 'development'
namespace :thin do
desc 'Start the app'
task :start do
puts 'Starting...'
system "bundle exec thin -s 1 -C config/config-#{ENV['RACK_ENV']}.yml -R config/config.ru start"
puts 'Started!'
end
desc 'Stop the app'
task :stop do
puts 'Stopping...'
pids = File.join(File.dirname(__FILE__), 'tmp/pids')
if File.directory?(pids)
Dir.new(pids).each do |file|
prefix = file.to_s
if prefix[0, 4] == 'thin'
puts "Stopping the server on port #{file[/\d+/]}..."
system "bundle exec thin stop -Ptmp/pids/#{file}"
end
end
end
puts 'Stopped!'
end
desc 'Restart the application'
task :restart do
puts 'Restarting...'
Rake::Task['thin:stop'].invoke
Rake::Task['thin:start'].invoke
puts 'Restarted!'
end
end
user = 'username'
app_name = 'app'
app_dir = "/home/#{user}/webapps/#{app_name}"
desc 'Deploy to server'
task :deploy, :password do |t, args|
puts 'Deploying to server...'
# http://linux.die.net/man/1/rsync
# Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
success = system "rsync --exclude-from .excludes -rltvz -e ssh . #{user}@#{user}.webfactional.com:#{app_dir}"
if success
require 'net/ssh'
Net::SSH.start("#{user}.webfactional.com", user, :password => args[:password]) do |ssh|
commands = [
'export RACK_ENV=production',
"cd #{app_dir}",
'bundle install --without=development',
'rake thin:restart'
].join ' && '
ssh.exec commands
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment