Skip to content

Instantly share code, notes, and snippets.

@dux
Last active August 9, 2017 14:18
Show Gist options
  • Save dux/657e812df32b4f78e38e9bdbc8a57c0c to your computer and use it in GitHub Desktop.
Save dux/657e812df32b4f78e38e9bdbc8a57c0c to your computer and use it in GitHub Desktop.
simple remote control of single production vps server
#!/usr/bin/env ruby
require 'thor'
require 'dotenv'
require 'awesome_print'
require 'colorize'
Dotenv.load
def Thor.define name, desc, &block
block ||= -> { Cli.bash name }
Thor.desc name, desc
Thor.send :define_method, name, &block
end
module Cli
extend self
def run command
command = command.gsub(/\$([A-Z_]+)/) { ENV[$1] }
puts command.green
system command
end
def bash name
run 'bash bin/bash/%s.bash' % name
end
def remote command
run %[ssh -t $PRODUCTION_SERVER "cd $PRODUCTION_PATH; echo '$PRODUCTION_PASS' | sudo -S echo; %s"] % command
end
end
###
class Remote < Thor
define :assets, 'Deploy js and css assets to production'
define :deploy, 'Deploy app to production'
define :get_db, 'Get and load production database'
define :check, 'Check ENV' do
for name in [:DB_NAME, :PRODUCTION_SERVER, :PRODUCTION_PATH, :PRODUCTION_PASS, :PRODUCTION_DB_NAME, :PRODUCTION_DB_USER, :PRODUCTION_DB_PASS]
puts '%s - %s' % [name.to_s.ljust(20), ENV.fetch(name.to_s)]
end
end
define :bash, 'Get production bash' do
Cli.remote 'bash;'
end
define :cli, 'Get production app cli' do
Cli.remote '~/.rbenv/shims/ruby lux/bin/lux c'
end
define :psql, 'get psql cli for remote db' do
puts '\l - list databases'
puts '\q - quit'
Cli.remote 'sudo -u postgres psql -d %s' % ENV.fetch('PRODUCTION_DB_NAME')
end
define :restart, 'Restart services' do
Cli.remote 'service memcached restart; service nginx restart'
end
desc 'upload FILE', 'Upload file to production server'
def upload name
Cli.run 'scp "%s" $PRODUCTION_SERVER:~' % name
Cli.remote 'ls -al; bash'
end
end
Remote.start(ARGV)
#!/usr/bin/env bash
source .env
lux assets
# assets deploy
rsync -vrh ./public/assets/ "$PRODUCTION_SERVER:$PRODUCTION_PATH/public/assets"
# copy app
rsync -vrh \
--executability \
--delete \
--exclude 'log/*' \
--exclude '.env' \
--exclude 'tmp/*' \
--exclude '.git/*' \
--exclude 'public/assets/*' \
--exclude 'node_modules/*' \
--exclude 'lux/*' \
. $PRODUCTION_SERVER:$PRODUCTION_PATH
# copy lux
rsync -vrh \
--executability \
--delete \
--exclude 'log/*' \
--exclude '.env' \
--exclude 'tmp/*' \
--exclude '.git/*' \
../lux/ "$PRODUCTION_SERVER:$PRODUCTION_PATH/lux"
# install and login
ssh -t $PRODUCTION_SERVER "
echo '$PRODUCTION_PASS' | sudo -S echo;
set -x;
source ~/.bashrc;
cd $PRODUCTION_PATH;
~.rbenv/shims/bundle install;
~.rbenv/shims/bundle exec $PRODUCTION_PATH/lux/bin/lux am;
sudo service nginx restart;"
#!/usr/bin/env bash
source .env
# get remote DB
ssh -t $PRODUCTION_SERVER "
echo '$PRODUCTION_PASS' | sudo -S echo;
cd $PRODUCTION_PATH;
set -x;
sudo -u postgres pg_dump $DB_NAME > tmp/db.sql
gzip -f tmp/db.sql"
set -x;
scp $PRODUCTION_SERVER:$PRODUCTION_PATH/tmp/db.sql.gz tmp/
gunzip -f tmp/db.sql.gz
dropdb $DB_NAME
createdb $DB_NAME
psql $DB_NAME < tmp/db.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment