Skip to content

Commit

Permalink
Merge pull request #98 from BenoitHiller/systemd
Browse files Browse the repository at this point in the history
add preliminary systemd support
  • Loading branch information
BenoitHiller authored Apr 11, 2017
2 parents a7f2824 + 94a0b54 commit 14b7c91
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 11 deletions.
4 changes: 4 additions & 0 deletions lib/capistrano/dsl/unicorn_paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion lib/capistrano/tasks/nginx.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
40 changes: 32 additions & 8 deletions lib/capistrano/tasks/unicorn.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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'), unicorn_systemd_file
sudo 'systemctl', 'daemon-reload'
end
end
end

Expand All @@ -53,17 +59,35 @@ 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

%w[start stop restart reload].each do |command|
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
Expand All @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion lib/capistrano/unicorn_nginx/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 14b7c91

Please sign in to comment.