From 607b4f98b833ce6d5dd2cdacf9054de15affd3b7 Mon Sep 17 00:00:00 2001 From: Benoit Hiller Date: Fri, 16 Dec 2016 17:45:23 -0500 Subject: [PATCH 1/2] add systemd support --- lib/capistrano/dsl/unicorn_paths.rb | 4 ++ lib/capistrano/tasks/nginx.rake | 9 ++++- lib/capistrano/tasks/unicorn.rake | 40 +++++++++++++++---- lib/capistrano/unicorn_nginx/helpers.rb | 2 +- .../unicorn_nginx/templates/unicorn_init.erb | 2 +- .../templates/unicorn_service.erb | 20 ++++++++++ 6 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 lib/generators/capistrano/unicorn_nginx/templates/unicorn_service.erb diff --git a/lib/capistrano/dsl/unicorn_paths.rb b/lib/capistrano/dsl/unicorn_paths.rb index aed6411..02a475f 100644 --- a/lib/capistrano/dsl/unicorn_paths.rb +++ b/lib/capistrano/dsl/unicorn_paths.rb @@ -6,6 +6,10 @@ def unicorn_initd_file "/etc/init.d/#{fetch(:unicorn_service)}" end + def unicorn_systemd_file + "/etc/systemd/system/#{fetch(:unicorn_service)}.service" + end + def unicorn_default_config_file shared_path.join('config/unicorn.rb') end diff --git a/lib/capistrano/tasks/nginx.rake b/lib/capistrano/tasks/nginx.rake index abdcd5d..68309f1 100644 --- a/lib/capistrano/tasks/nginx.rake +++ b/lib/capistrano/tasks/nginx.rake @@ -6,6 +6,7 @@ include Capistrano::DSL::NginxPaths namespace :load do task :defaults do + set :init_system, :sysv set :templates_path, 'config/deploy/templates' set :nginx_config_name, -> { "#{fetch(:application)}_#{fetch(:stage)}" } set :nginx_pid, nginx_default_pid_file @@ -65,7 +66,13 @@ namespace :nginx do desc 'Reload nginx configuration' task :reload do on roles :web do - sudo nginx_service_path, 'reload' + case fetch(:init_system) + when :sysv + sudo nginx_service_path, 'reload' + when :systemd + sudo "systemctl", "start", "nginx" + sudo "systemctl", "reload", "nginx" + end end end diff --git a/lib/capistrano/tasks/unicorn.rake b/lib/capistrano/tasks/unicorn.rake index b7971f2..4a62f01 100644 --- a/lib/capistrano/tasks/unicorn.rake +++ b/lib/capistrano/tasks/unicorn.rake @@ -36,9 +36,15 @@ namespace :unicorn do desc 'Setup Unicorn initializer' task :setup_initializer do on roles :app do - sudo_upload! template('unicorn_init.erb'), unicorn_initd_file - execute :chmod, '+x', unicorn_initd_file - sudo 'update-rc.d', '-f', fetch(:unicorn_service), 'defaults' + case fetch(:init_system) + when :sysv + sudo_upload! template('unicorn_init.erb'), unicorn_initd_file + execute :chmod, '+x', unicorn_initd_file + sudo 'update-rc.d', '-f', fetch(:unicorn_service), 'defaults' + when :systemd + sudo_upload! template('unicorn_service.erb'), fetch(:unicorn_service) + sudo 'systemctl', 'daemon-reload' + end end end @@ -53,9 +59,11 @@ namespace :unicorn do desc 'Setup logrotate configuration' task :setup_logrotate do on roles :app do - sudo :mkdir, '-pv', File.dirname(fetch(:unicorn_logrotate_config)) - sudo_upload! template('unicorn-logrotate.rb.erb'), fetch(:unicorn_logrotate_config) - sudo 'chown', 'root:root', fetch(:unicorn_logrotate_config) + if fetch(:init_system) == :sysv + sudo :mkdir, '-pv', File.dirname(fetch(:unicorn_logrotate_config)) + sudo_upload! template('unicorn-logrotate.rb.erb'), fetch(:unicorn_logrotate_config) + sudo 'chown', 'root:root', fetch(:unicorn_logrotate_config) + end end end @@ -63,7 +71,23 @@ namespace :unicorn do desc "#{command} unicorn" task command do on roles :app do - sudo 'service', fetch(:unicorn_service), command + case fetch(:init_system) + when :sysv + sudo 'service', fetch(:unicorn_service), command + when :systemd + sudo 'systemctl', command, fetch(:unicorn_service) + end + end + end + end + + task :run_latest do + on roles :app do + case fetch(:init_system) + when :sysv + sudo 'service', fetch(:unicorn_service), "reload" + when :systemd + sudo 'systemctl', "restart", fetch(:unicorn_service) end end end @@ -74,7 +98,7 @@ namespace :unicorn do end namespace :deploy do - after :publishing, 'unicorn:reload' + after :publishing, 'unicorn:run_latest' end desc 'Server setup tasks' diff --git a/lib/capistrano/unicorn_nginx/helpers.rb b/lib/capistrano/unicorn_nginx/helpers.rb index d59cff8..8787b5a 100644 --- a/lib/capistrano/unicorn_nginx/helpers.rb +++ b/lib/capistrano/unicorn_nginx/helpers.rb @@ -5,7 +5,7 @@ module UnicornNginx module Helpers def bundle_unicorn(*args) - SSHKit::Command.new(:bundle, :exec, :unicorn, args).to_command + SSHKit::Command.new(:bundle, :exec, :unicorn, args) end # renders the ERB template specified by template_name to string. Use the locals variable to pass locals to the diff --git a/lib/generators/capistrano/unicorn_nginx/templates/unicorn_init.erb b/lib/generators/capistrano/unicorn_nginx/templates/unicorn_init.erb index f4e49e1..83f4928 100644 --- a/lib/generators/capistrano/unicorn_nginx/templates/unicorn_init.erb +++ b/lib/generators/capistrano/unicorn_nginx/templates/unicorn_init.erb @@ -17,7 +17,7 @@ PID=<%= fetch(:unicorn_pid) %> AS_USER=<%= fetch(:unicorn_user) %> UNICORN_ENV="<%= fetch(:unicorn_env) %>" -CMD="export HOME; true "${HOME:=$(getent passwd "$AS_USER" | cut -d: -f6;)}" ; cd $APP_ROOT && $UNICORN_ENV <%= bundle_unicorn("-D -c", fetch(:unicorn_config), "-E", fetch(:unicorn_app_env)) %>" +CMD="export HOME; true "${HOME:=$(getent passwd "$AS_USER" | cut -d: -f6;)}" ; cd $APP_ROOT && $UNICORN_ENV <%= bundle_unicorn("-D -c", fetch(:unicorn_config), "-E", fetch(:unicorn_app_env).to_command) %>" set -u diff --git a/lib/generators/capistrano/unicorn_nginx/templates/unicorn_service.erb b/lib/generators/capistrano/unicorn_nginx/templates/unicorn_service.erb new file mode 100644 index 0000000..a776f18 --- /dev/null +++ b/lib/generators/capistrano/unicorn_nginx/templates/unicorn_service.erb @@ -0,0 +1,20 @@ +<% command = bundle_unicorn("-D -c", fetch(:unicorn_config), "-E", fetch(:unicorn_app_env)) %> +[Unit] +Description=Unicorn Server +Wants=mysqld.service nginx.service postgresql.service +After=redis.service mysqld.service postgresql.service + +[Service] +User=<%= fetch(:unicorn_user) %> +WorkingDirectory=<%= current_path %> +Environment=RAILS_ENV=<%= fetch(:unicorn_app_env) %> +<% command.environment_hash.each_pair do |key, value| %> + Environment=<%= key %>=<%= value %> +<% end %> +SyslogIdentifier=unicorn +PIDFile=<%= fetch(:unicorn_pid) %> + +ExecStart=<%= command.to_s %> + +[Install] +WantedBy=multi-user.target From 94a0b5469c165198b3695930b33b87b5d7fa7c12 Mon Sep 17 00:00:00 2001 From: Benoit Hiller Date: Mon, 10 Apr 2017 19:33:36 -0400 Subject: [PATCH 2/2] use correct upload path in initializer --- lib/capistrano/tasks/unicorn.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/capistrano/tasks/unicorn.rake b/lib/capistrano/tasks/unicorn.rake index 4a62f01..bad5824 100644 --- a/lib/capistrano/tasks/unicorn.rake +++ b/lib/capistrano/tasks/unicorn.rake @@ -42,7 +42,7 @@ namespace :unicorn do execute :chmod, '+x', unicorn_initd_file sudo 'update-rc.d', '-f', fetch(:unicorn_service), 'defaults' when :systemd - sudo_upload! template('unicorn_service.erb'), fetch(:unicorn_service) + sudo_upload! template('unicorn_service.erb'), unicorn_systemd_file sudo 'systemctl', 'daemon-reload' end end