Skip to content

Instantly share code, notes, and snippets.

@nobuti
Created January 29, 2013 09:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobuti/4662944 to your computer and use it in GitHub Desktop.
Save nobuti/4662944 to your computer and use it in GitHub Desktop.
Rakefile for small sinatra app, with a task to deploy to Webfaction.
require 'rubygems'
require 'bundler'
Bundler.require
require 'rspec/core/rake_task'
ENV['RACK_ENV'] ||= 'development'
task :default => :help
desc "Show help menu"
task :help do
puts "Available rake tasks: "
puts "rake thin:start - Start App"
puts "rake thin:stop - Stop App"
puts "rake thin:restart - Restart App"
puts "rake deploy - Deploy to webfaction"
puts "rake spec - Run specs and calculate coverage"
end
desc "Run specs"
task :spec do
RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = './spec/**/*_spec.rb'
end
end
namespace :thin do
desc 'Start the app'
task :start do
puts 'Starting...'
system "bundle exec thin -s 1 -C config/config-#{ENV['RACK_ENV']}.yaml -R 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 = 'webfaction_user'
app_name = 'webfaction_app'
app_dir = "/home/#{user}/webapps/#{app_name}"
desc 'Deploy to server'
task :deploy, :password do |t, args|
puts 'Deploying to server...'
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',
"export GEM_HOME=#{app_dir}/gems",
"export PATH=#{app_dir}/bin:$PATH",
"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