Skip to content

Instantly share code, notes, and snippets.

@arthurmolina
Created February 14, 2018 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthurmolina/3130b773e7f025c59132cfc2a5bc6bdf to your computer and use it in GitHub Desktop.
Save arthurmolina/3130b773e7f025c59132cfc2a5bc6bdf to your computer and use it in GitHub Desktop.
mina + puma + nginx
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rvm' # for rvm support. (http://rvm.io)
require 'mina/puma'
# Basic settings:
# domain - The hostname to SSH to.
# deploy_to - Path to deploy into.
# repository - Git repo to clone from. (needed by mina/git)
# branch - Branch name to deploy. (needed by mina/git)
#set :domain, 'bots.codium.com.br'
set :domain, 'rails_app.net'
set :deploy_to, '/home/deployer/apps/rails_app'
set :repository, 'git@bitbucket.org:artz/rails_app.git'
set :branch, 'master'
set :rails_env, 'production'
set :user, 'deployer'
set :port, '6622'
set :ssh_options, '-A'
# For system-wide RVM install.
set :rvm_path, '/usr/local/rvm/scripts/rvm'
set :app_path, lambda { "#{deploy_to}/#{current_path}" }
# Manually create these paths in shared/ (eg: shared/config/database.yml) in your server.
# They will be linked in the 'deploy:link_shared_paths' step.
set :shared_paths, ['config/database.yml', 'config/secrets.yml', 'log', 'public/system']
# Optional settings:
# set :user, 'foobar' # Username in the server to SSH to.
# set :port, '30000' # SSH port number.
set :forward_agent, true # SSH forward_agent.
set :term_mode, nil
set :keep_releases, 3
# This task is the environment that is loaded for most commands, such as
# `mina deploy` or `mina rake`.
task :environment do
# If you're using rbenv, use this to load the rbenv environment.
# Be sure to commit your .ruby-version or .rbenv-version to your repository.
# invoke :'rbenv:load'
# For those using RVM, use this to load an RVM version@gemset.
invoke :'rvm:use[ruby-2.2.1@rails4.2]'
end
# Put any custom mkdir's in here for when `mina setup` is ran.
# For Rails apps, we'll make some of the shared paths that are shared between
# all releases.
task :setup => :environment do
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/log"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/log"]
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/config"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/config"]
queue! %[touch "#{deploy_to}/#{shared_path}/config/database.yml"]
queue! %[touch "#{deploy_to}/#{shared_path}/config/secrets.yml"]
queue %[echo "-----> Be sure to edit '#{deploy_to}/#{shared_path}/config/database.yml' and 'secrets.yml'."]
# Puma needs a place to store its pid file and socket file.
queue! %(mkdir -p "#{deploy_to}/#{shared_path}/tmp/sockets")
queue! %(chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/tmp/sockets")
queue! %(mkdir -p "#{deploy_to}/#{shared_path}/tmp/pids")
queue! %(chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/tmp/pids")
queue! %(mkdir -p "#{deploy_to}/#{shared_path}/uploads")
queue! %(chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/uploads")
end
desc "Load environment variables"
task :env do
queue %{
echo "-----> Loading environment"
#{echo_cmd %[source ~/.bash_profile]}
}
end
desc "Deploys the current version to the server."
task :deploy => :environment do
to :before_hook do
# Put things to run locally before ssh
end
invoke :env
deploy do
# Put things that will set up an empty directory into a fully set-up
# instance of your project.
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'rails:db_migrate'
invoke :'rails:assets_precompile'
invoke :'deploy:cleanup'
to :launch do
queue "mkdir -p #{deploy_to}/#{current_path}/tmp/"
queue "touch #{deploy_to}/#{current_path}/tmp/restart.txt"
queue "echo #{pumactl_cmd}"
queue! %[
if [ -e '#{pumactl_socket}' ]; then
PID=`cat #{deploy_to}/shared/tmp/sockets/puma.state |grep pid| sed "s/[^0-9]//g"`
cd #{deploy_to}/#{current_path} && #{pumactl_cmd} -p $PID phased-restart
else
cd #{deploy_to}/#{current_path} && #{puma_cmd} -q -d -e #{puma_env} -b 'unix://#{puma_socket}' -S #{puma_state} --pidfile #{puma_pid} --control 'unix://#{pumactl_socket}' >> /tmp/prod.log
fi
]
queu "echo ---------> #{puma_cmd}"
invoke :'cleanup'
end
end
end
task :down do
invoke :restart
invoke :logs
end
task :restart do
queue 'sudo service nginx restart'
end
task :logs do
queue 'tail -f /var/log/nginx/error.log'
end
task :admin_user do
queue 'RAILS_ENV=production bundle exec rake casein:users:create_admin email=arthur@arthur password=senha'
end
# For help in making your deploy script, see the Mina documentation:
#
# - http://nadarei.co/mina
# - http://nadarei.co/mina/tasks
# - http://nadarei.co/mina/settings
# - http://nadarei.co/mina/helpers
desc "Clean up old releases."
task :cleanup do
queue %{
echo "-----> Cleaning up old releases (keeping #{keep_releases!})"
#{echo_cmd %{cd "#{deploy_to!}/#{releases_path!}" || exit 15}}
#{echo_cmd %{count=`ls -1d [0-9]* | sort -rn | wc -l`}}
#{echo_cmd %{remove=$((count > #{keep_releases} ? count - #{keep_releases} : 0))}}
#{echo_cmd %{ls -1d [0-9]* | sort -rn | tail -n $remove | xargs rm -rf {}}}
}
end
server {
#listen *:80 | *:8000;
listen 80;
server_name www.rails_app.net;
return 301 $scheme://rails_app$request_uri;
}
upstream emetropolis {
server unix:///home/deployer/apps/rails_app/shared/tmp/sockets/puma.sock;
}
server {
listen 80;
server_name rails_app.net;
keepalive_timeout 5;
root /home/deployer/apps/rails_app/current/public;
access_log /home/deployer/apps/rails_app/current/log/production.access.log;
error_log /home/deployer/apps/rails_app/current/log/production.error.log info;
client_max_body_size 50M;
if (-f $document_root/maintenance.html) {
rewrite ^(.*)$ /maintenance.html last;
break;
}
location / {
proxy_pass http://rails_app;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ ^/(assets)/ {
root /home/deployer/apps/rails_app/current/public;
gzip_static on;
expires max;
add_header Cache-Control public;
# access_log /dev/null;
}
location ~* \.(pdf|ico|css|gif|jpe?g|png|js|doc|docx)(\?[0-9]+)?$ {
expires max;
break;
}
location ~ ^/javascripts/.*\.js(\?[0-9]+)?$ {
expires max;
break;
}
location ~* ^/assets|fonts/ {
expires max;
add_header Cache-Control public;
add_header ETag "";
break;
}
# Error pages
# error_page 500 502 503 504 /500.html;
location = /500.html {
root /rails_app/current/public;
}
}
#!/usr/bin/env puma
directory '/home/deployer/apps/emetropolis.net/current'
daemonize true
threads 0, 3
stdout_redirect 'log/puma.stdout.log', 'log/puma.stderr.log', true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment