3

I'd like to keep my unit files in source control (e.g. in config), such that after a capistrano deploy, the unit will be copied to the systemd dir, and the service (e.g. puma) will be restarted. What would be the best way to achieve this?

I've thought simply adding a post-deploy task such as (untested code)

namespace :deploy do
[...]

  before :published, :systemd
  desc 'systemd'
  task :systemd do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
      within release_path do
        execute "sudo cp config/puma.service /etc/systemd/system/puma.service"
        execute "sudo cp config/puma-init /usr/bin/puma-init"
        execute "sudo systemctl daemon-reload"
        execute "sudo systemctl restart puma"
      end
    end
  end
end
Community
  • 1
  • 1
dimid
  • 7,285
  • 1
  • 46
  • 85

2 Answers2

1

This seems to work, but I still wonder if there's a more elegant solution

namespace :deploy do
  [...]

  before :published, :systemd
  desc 'systemd integration'
  task :systemd do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
      within shared_path do
        execute "sudo cp #{shared_path}/config/puma.service /etc/systemd/system/puma.service"
        execute "sudo cp #{shared_path}/config/puma-init /usr/bin/puma-init"
        execute "sudo systemctl daemon-reload"
        execute "sudo systemctl restart puma"
      end
    end
  end
end

Also, note that you need to

set :pty, true

Otherwise sudo won't work.

dimid
  • 7,285
  • 1
  • 46
  • 85
0

Something like what you've described in your example will work. As an alternative, what I've done is create a symlink.

To do so, I'd suggest moving your service files to something like config/systemd/ in your repo. Deploy once, then create a symlink on the server like ln -s $CURRENT_PATH/config/systemd/ /etc/systemd/system/myapp. Now when you deploy, the path the symlink is pointing to will change (due to the Capistrano managed current symlink), and you just have to restart the processes.

will_in_wi
  • 2,623
  • 1
  • 16
  • 21
  • Thanks, could you share the full code for your task? I recall systemd having issues with symlinks, but perhaps it was resolved in recent versions. – dimid Feb 15 '17 at 08:54