This is heavily based on the capistrano-foreman gem but it only targets systemd
(default init system for Ubuntu since 16.04).
It works best with foreman-systemd (fork of 0.78 version of foreman gem).
gem 'capistrano', '~> 3.1'
gem 'capistrano-foreman-systemd'
And then execute:
$ bundle
Or install it yourself as:
$ gem install capistrano-foreman-systemd
Require in Capfile
:
require 'capistrano/foreman_systemd'
Export Procfile to process management format (defaults to upstart) and restart the application services:
$ cap foreman_systemd:setup
$ cap foreman_systemd:start
Configurable options, shown here with defaults:
set :foreman_systemd_roles, :all
set :foreman_systemd_export_format, 'systemd'
set :foreman_systemd_export_path, '/etc/init'
set :foreman_systemd_flags, "--root=#{current_path}" # optional, default is empty string
set :foreman_systemd_target_path, release_path
set :foreman_systemd_app, -> { fetch(:application) }
set :foreman_systemd_concurrency, 'web=2,worker=1' # optional, default is not set
set :foreman_systemd_log, -> { shared_path.join('log') }
set :foreman_systemd_port, 3000 # optional, default is not set
set :foreman_systemd_user, 'www-data' # optional, default is not set
See exporting options for an exhaustive list of foreman options.
This gem provides the following Capistrano tasks:
foreman_systemd:setup
exports the Procfile and starts application servicesforeman_systemd:export
exports the Procfile to process management formatforeman_systemd:enable
enables the application in systemdforeman_systemd:disable
disables the application in systemdforeman_systemd:restart
restarts the application servicesforeman_systemd:start
starts the application servicesforeman_systemd:stop
stops the application services
Commands have to be executed with root
or user with sudo
writes because foreman:setup
creates files in /etc/systemd/system
directory.
Configuration in deploy.rb
:
# Set the app with `sites/` prefix
set :foreman_app, -> { fetch(:application) }
# Set user to `deploy`, assuming this is your deploy user
set :foreman_user, 'deploy'
# Set root to `current_path` so exporting only have to be done once.
set :foreman_flags, "--root=#{current_path}"
Setup your init scripts by running foreman:setup
after your first deploy.
From this moment on you only have to run foreman:setup
when your Procfile
has changed or when you alter the foreman deploy configuration.
Finally you have to instruct Capistrano to run foreman:restart
after deploy:
You can control which process runs on which servers using server variable foreman_systemd_concurrency
:
server '123.123.123.1', { roles: [:web], foreman_systemd_concurrency: 'web=1,sidekiq=1' }
server '123.123.123.1', { roles: [:web], foreman_systemd_concurrency: 'web=1,sidekiq=0' }
Finally
# Hook foreman restart after publishing
after :'deploy:publishing', :'foreman:restart'
When using rbenv
, rvm
, chruby
and/or bundler
don't forget to add foreman
to the bins list:
fetch(:rbenv_map_bins, []).push 'foreman'
fetch(:rvm_map_bins, []).push 'foreman'
fetch(:chruby_map_bins, []).push 'foreman'
fetch(:bundle_bins, []).push 'foreman'
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request