Skip to content

Commit

Permalink
Fix bug in switch_user and dry up common methods to a helpers module (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscz authored Apr 3, 2021
1 parent 554d8f7 commit a434606
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 109 deletions.
1 change: 1 addition & 0 deletions lib/capistrano/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def set_defaults
end
end

require_relative 'sidekiq/helpers'
require_relative 'sidekiq/systemd'
require_relative 'sidekiq/upstart'
require_relative 'sidekiq/monit'
54 changes: 54 additions & 0 deletions lib/capistrano/sidekiq/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module Capistrano
module Sidekiq::Helpers
def sidekiq_require
if fetch(:sidekiq_require)
"--require #{fetch(:sidekiq_require)}"
end
end

def sidekiq_config
if fetch(:sidekiq_config)
"--config #{fetch(:sidekiq_config)}"
end
end

def sidekiq_concurrency
if fetch(:sidekiq_concurrency)
"--concurrency #{fetch(:sidekiq_concurrency)}"
end
end

def sidekiq_queues
Array(fetch(:sidekiq_queue)).map do |queue|
"--queue #{queue}"
end.join(' ')
end

def sidekiq_logfile
fetch(:sidekiq_log)
end

def switch_user(role)
su_user = sidekiq_user(role)
if su_user == role.user
yield
else
as su_user do
yield
end
end
end

def sidekiq_user(role = nil)
if role.nil?
fetch(:sidekiq_user)
else
properties = role.properties
properties.fetch(:sidekiq_user) || # local property for sidekiq only
fetch(:sidekiq_user) ||
properties.fetch(:run_as) || # global property across multiple capistrano gems
role.user
end
end
end
end
2 changes: 2 additions & 0 deletions lib/capistrano/sidekiq/monit.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Capistrano
class Sidekiq::Monit < Capistrano::Plugin
include Sidekiq::Helpers

def set_defaults
set_if_empty :monit_bin, '/usr/bin/monit'
set_if_empty :sidekiq_monit_conf_dir, '/etc/monit/conf.d'
Expand Down
2 changes: 2 additions & 0 deletions lib/capistrano/sidekiq/systemd.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Capistrano
class Sidekiq::Systemd < Capistrano::Plugin
include Sidekiq::Helpers

def set_defaults
set_if_empty :sidekiq_service_unit_name, 'sidekiq'
set_if_empty :sidekiq_service_unit_user, :user # :system
Expand Down
2 changes: 2 additions & 0 deletions lib/capistrano/sidekiq/upstart.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Capistrano
class Sidekiq::Upstart < Capistrano::Plugin
include Sidekiq::Helpers

def set_defaults
set_if_empty :sidekiq_service_unit_name, 'sidekiq'
end
Expand Down
55 changes: 2 additions & 53 deletions lib/capistrano/tasks/monit.rake
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ namespace :sidekiq do
on roles(fetch(:sidekiq_roles)) do |role|
@role = role
git_plugin.upload_sidekiq_template 'sidekiq_monit', "#{fetch(:tmp_dir)}/monit.conf", @role

git_plugin.switch_user(role) do
mv_command = "mv #{fetch(:tmp_dir)}/monit.conf #{fetch(:sidekiq_monit_conf_dir)}/#{fetch(:sidekiq_monit_conf_file)}"

git_plugin.sudo_if_needed mv_command
git_plugin.sudo_if_needed "#{fetch(:monit_bin)} reload"
end
Expand Down Expand Up @@ -87,57 +87,6 @@ namespace :sidekiq do
fetch(:sidekiq_service_name, "sidekiq_#{fetch(:application)}_#{fetch(:sidekiq_env)}")
end

def sidekiq_config
if fetch(:sidekiq_config)
"--config #{fetch(:sidekiq_config)}"
end
end

def sidekiq_concurrency
if fetch(:sidekiq_concurrency)
"--concurrency #{fetch(:sidekiq_concurrency)}"
end
end

def sidekiq_queues
Array(fetch(:sidekiq_queue)).map do |queue|
"--queue #{queue}"
end.join(' ')
end

def sidekiq_logfile
fetch(:sidekiq_log)
end

def sidekiq_require
if fetch(:sidekiq_require)
"--require #{fetch(:sidekiq_require)}"
end
end

def switch_user(role)
su_user = sidekiq_user(role)
if su_user != role.user
yield
else
backend.as su_user do
yield
end
end
end

def sidekiq_user(role = nil)
if role.nil?
fetch(:sidekiq_user)
else
properties = role.properties
properties.fetch(:sidekiq_user) ||
fetch(:sidekiq_user) ||
properties.fetch(:run_as) ||
role.user
end
end

def sudo_if_needed(command)
if use_sudo?
backend.execute :sudo, command
Expand Down
15 changes: 0 additions & 15 deletions lib/capistrano/tasks/systemd.rake
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,4 @@ namespace :sidekiq do
backend.execute :systemctl, "--user", "daemon-reload"
end
end

def switch_user(role)
su_user = sidekiq_user
if su_user != role.user
yield
else
backend.as su_user do
yield
end
end
end

def sidekiq_user
fetch(:sidekiq_user, fetch(:run_as))
end
end
41 changes: 0 additions & 41 deletions lib/capistrano/tasks/upstart.rake
Original file line number Diff line number Diff line change
Expand Up @@ -98,49 +98,8 @@ namespace :sidekiq do
end
end

def switch_user(role)
su_user = sidekiq_user(role)
if su_user == role.user
yield
else
as su_user do
yield
end
end
end

def sidekiq_user(role = nil)
if role.nil?
fetch(:sidekiq_user)
else
properties = role.properties
properties.fetch(:sidekiq_user) || # local property for sidekiq only
fetch(:sidekiq_user) ||
properties.fetch(:run_as) || # global property across multiple capistrano gems
role.user
end
end

def num_workers
fetch(:sidekiq_upstart_num_workers, nil)
end

def sidekiq_config
if fetch(:sidekiq_config)
"--config #{fetch(:sidekiq_config)}"
end
end

def sidekiq_concurrency
if fetch(:sidekiq_concurrency)
"--concurrency #{fetch(:sidekiq_concurrency)}"
end
end

def sidekiq_queues
Array(fetch(:sidekiq_queue)).map do |queue|
"--queue #{queue}"
end.join(' ')
end

end

0 comments on commit a434606

Please sign in to comment.