Skip to content

Instantly share code, notes, and snippets.

@sdeming
Created May 3, 2012 04:01
Show Gist options
  • Save sdeming/2583041 to your computer and use it in GitHub Desktop.
Save sdeming/2583041 to your computer and use it in GitHub Desktop.
Really simple script for installing rails apps when you can't deploy with Capistrano. Just zip up the app and run ./install.rb zipfile
#!/bin/env ruby
ENV['RAILS_ENV'] = 'production'
require 'fileutils'
def sudo(cmd, as = 'root')
puts "sudo: #{cmd}"
system("sudo -u #{as} #{cmd}")
end
def sys(cmd)
puts "exec: #{cmd}"
system(cmd)
end
begin
if ARGV.length != 1
puts "Usage: #{File.basename(__FILE__)} ziparchive"
exit 1
end
file = ARGV[0]
app_id = file.gsub(/.zip$/i, '')
raise "Missing file: #{file}" if file.nil? or !File.exist?(file)
raise "Expecting zip file: #{filename}" unless file =~ /.zip$/i
app_root = File.expand_path(File.dirname(__FILE__))
app_name = File.basename(app_root)
release = File.join(app_root, 'releases')
app_path = File.join(release, app_id, app_name)
shared = File.join(app_root, 'shared')
current = File.join(app_root, 'current')
sys("mkdir -p #{shared} >/dev/null 2>&1")
sys("mkdir -p #{shared}/vendor/bundle >/dev/null 2>&1")
sys("mkdir -p #{shared}/log >/dev/null 2>&1")
sys("mkdir -p #{shared}/uploads >/dev/null 2>&1")
sys("mkdir -p #{shared}/tmp/pids >/dev/null 2>&1")
sys("mkdir -p #{release} >/dev/null 2>&1")
sys("mkdir -p #{release}/#{app_id} >/dev/null 2>&1")
sys("unzip #{file} -d #{release}/#{app_id}")
raise "Failed to unzip to the right location. Expected: #{app_path}" unless File.exist?(app_path)
sys("rm -f #{current}")
sys("ln -s #{app_path} #{current}")
sys("rm -rf #{current}/log")
sys("ln -s #{shared}/log #{current}/log")
sys("ln -s #{shared}/uploads #{current}/public/uploads")
sys("ln -s #{shared}/tmp #{current}/tmp")
sys("ln -s #{shared}/vendor/bundle #{current}/vendor/bundle")
FileUtils.chdir(current) do
sys("rm -f .env")
File.open(".env", "w") do |f|
f.puts "RAILS_ENV=production"
end
sys("bundle install --deployment --without test development")
sys("rake db:migrate")
sys("rake assets:precompile")
sudo("stop erapp")
while (`/usr/sbin/lsof -t -itcp:3000`.length > 0)
puts "Waiting ..."
sleep 1
end
sudo("bundle exec foreman export upstart /etc/init --app=erapp --user=appadm --procfile=Procfile")
sudo("start erapp")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment