Skip to content

Instantly share code, notes, and snippets.

@dux
Last active August 29, 2015 14:05
Show Gist options
  • Save dux/919f682c01a3647ad196 to your computer and use it in GitHub Desktop.
Save dux/919f682c01a3647ad196 to your computer and use it in GitHub Desktop.
simple replacement for Rails capistrano
#!/usr/bin/ruby
require 'open3'
require 'colorize'
# CODE
@actions = {}
def action(id, name, &block)
@actions[id] = { name:name, func:block }
end
# use this if you do not have to enter passwords and interact with output
def run(what)
puts what.green
stdin, stdout, stderr, wait_thread = Open3.popen3(what)
stdout.each_line { |line| print line }
end
# use ths if you have to ennter password or interact with output
def input(what)
puts what.green
`#{what}`
end
def remote(what)
run %[ssh #{@server} -t "cd #{@remote_path}; #{what}"]
end
def exec(name)
act = @actions[name.to_sym]
puts ">>> #{act[:name]} <<<".yellow
act[:func].call
end
def run!
opt = ARGV.shift || 'nil'
if @actions[opt.to_sym]
exec opt
else
print "@server remote by @dux for #{@server.green}\nUsage: ./remote [option]\n"
print "Options:".rjust(12)
print "\n"
for k,v in @actions
print " #{k.to_s.rjust(10).yellow}: #{v[:name]}\n"
end
end
end
# OPTS
@server = '[email protected]'
@sudo_pass = 'server_pass'
@remote_path = '/home/deployer/apps/stashbuckets.com'
@db_name = 'stash_bucket'
@db_user = 'db_user'
@db_pass = 'db_pass'
@db_local = 'stash-bucket'
# ACTIONS
action :log, 'Get remote log' do
remote 'tail -n 300 -f log/production.log'
end
action :deploy, 'Deploy pushed code' do
remote "git stash; git pull origin master"
remote "bundle install; rake db:migrate RAILS_ENV=production"
remote "rake assets:clean RAILS_ENV=production; rake assets:precompile RAILS_ENV=production"
remote "echo #{@sudo_pass} | sudo -S service nginx restart"
end
action :db, 'Get and import remote database' do
run %[echo "#{@db_pass}" | pbcopy]
print 'Just PASTE CMD+V pass -> '
input %[ssh #{@server} "pg_dump --host localhost --port 5432 --username #{@db_user} --password #{@db_name}" > prod_database.sql]
run "dropdb #{@db_local}"
run "createdb -T template1 #{@db_local}"
run "psql -d #{@db_local} -f prod_database.sql"
run "rm -rf prod_database.sql"
end
action :push, 'Commit and push to production in one step (./remote push added some shit)' do
print "Commit msg: "
msg = gets.chomp
unless msg =~/\w/
print "No push message!\n".red
exit
end
run 'git add .'
run 'git add -A'
run %[git commit -m "#{msg}"]
run 'git push origin master'
exec :deploy
end
run!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment