Skip to content

Instantly share code, notes, and snippets.

@antoniosmgatto
Last active June 7, 2021 22:17
Show Gist options
  • Save antoniosmgatto/c1362f962781bd6228769e0cc4149053 to your computer and use it in GitHub Desktop.
Save antoniosmgatto/c1362f962781bd6228769e0cc4149053 to your computer and use it in GitHub Desktop.
rails + postgres + sidekiq + capistrado
require "capistrano/setup"
require "capistrano/deploy"
require "capistrano/bundler"
require "capistrano/scm/git"
require "capistrano/rails/assets"
require "capistrano/rails/migrations"
require "capistrano/data_migrate"
require "capistrano/rbenv"
install_plugin Capistrano::SCM::Git
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
lock "~> 3.16"
set :application, "app"
set :deploy_to, "/srv/www"
set :repo_url, "git@github.com:antoniosmgatto/vehicle-example-api.git"
set :branch, ENV.fetch("BRANCH", "main")
set :user, "deploy"
set :ssh_options, { forward_agent: true, user: fetch(:user) }
set :format, :airbrussh
set :rbenv_type, :user
set :rbenv_ruby, File.read(".ruby-version").strip
append :linked_dirs, ".bundle", "log", "storage", "tmp/cache", "tmp/pids", "tmp/sockets", "public/system", "public/uploads"
before "deploy:compile_assets", "deploy:yarn:install"
after "deploy:publishing", "deploy:puma:restart"
after "deploy:publishing", "deploy:sidekiq:restart"
namespace :deploy do
namespace :puma do
desc "Stop puma"
task :stop do
on roles(:app) do
execute :sudo, :systemctl, :stop, :puma
end
end
desc "Restart puma"
task :restart do
on roles(:app) do
execute :sudo, :systemctl, :restart, :puma
end
end
desc "Start puma"
task :start do
on roles(:app) do
execute :sudo, :systemctl, :start, :puma
end
end
end
namespace :sidekiq do
desc "Restart sidekiq"
task :restart do
on roles(:app) do
execute :sudo, :systemctl, :restart, :sidekiq
end
end
end
namespace :yarn do
desc "Install yarn dependencies"
task :install do
on roles(:app) do
within release_path do
execute :yarn, :install
end
end
end
end
end
# put this in /etc/environment
RAILS_ENV="production"
RAILS_MASTER_KEY="your_key_here"
DATABASE_URL="postgres://postgres:password@localhost/siga_test"
REDIS_URL='redis://127.0.0.1:6379'
gem "capistrano", require: false
gem "capistrano-bundler", require: false
gem "capistrano-rails", require: false
gem "capistrano-rbenv", require: false
[Unit]
Description=sidekiq
After=syslog.target network.target
[Service]
Type=notify
WatchdogSec=10
EnvironmentFile=/etc/environment
Environment=MALLOC_ARENA_MAX=2
WorkingDirectory=/srv/www/current
ExecStart=/bin/bash -lc 'bundle exec sidekiq -e production'
User=deploy
Group=deploy
UMask=0002
RestartSec=1
Restart=on-failure
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=sidekiq
# Use `systemctl kill -s TSTP sidekiq` to quiet the Sidekiq process
[Install]
WantedBy=multi-user.target
upstream puma_production {
server unix:/srv/www/shared/tmp/sockets/puma.sock fail_timeout=0;
}
server {
server_name app.example.com;
root /srv/www/current/public;
try_files $uri/index.html $uri @puma_production;
client_max_body_size 10m;
keepalive_timeout 10;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
error_page 500 502 504 /500.html;
error_page 503 @503;
location @puma_production {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Photo $scheme;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://puma_production;
proxy_redirect off;
proxy_read_timeout 3600;
# limit_req zone=one;
access_log /srv/www/shared/log/nginx.access.log;
error_log /srv/www/shared/log/nginx.error.log;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location @503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
}
server {
if ($host = app.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name app.example.com;
return 404; # managed by Certbot
}
set :stage, :production
server "app.example.com", port: 22, roles: %i[web app db], primary: true
[Unit]
Description=Puma HTTP Server
After=network.target
[Service]
Type=simple
User=deploy
WorkingDirectory=/srv/www/current
EnvironmentFile=/etc/environment
ExecStart=/home/deploy/.rbenv/bin/rbenv exec bundle exec puma -C /srv/www/shared/config/puma_production.rb
ExecStop=/home/deploy/.rbenv/bin/rbenv exec bundle exec pumactl -S /srv/www/shared/tmp/pids/puma.state stop
TimeoutSec=15
Restart=always
[Install]
WantedBy=multi-user.target
workers 1
threads 1, 6
environment "production"
bind "unix:/srv/www/shared/tmp/sockets/puma.sock"
state_path "/srv/www/shared/tmp/pids/puma.state"
stdout_redirect "/srv/www/shared/log/production.log", "/srv/www/shared/log/production.log", true
on_worker_boot do
ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment