From e2908e4361961137243534a2e96fc574dd183fe0 Mon Sep 17 00:00:00 2001 From: "Michael C. Beck" Date: Thu, 30 Jul 2015 15:31:18 +0200 Subject: [PATCH] intial support for sidekiq_user --- lib/capistrano/tasks/capistrano2.rb | 11 +++-- lib/capistrano/tasks/sidekiq.cap | 64 ++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/lib/capistrano/tasks/capistrano2.rb b/lib/capistrano/tasks/capistrano2.rb index 595f728..3ceb87f 100644 --- a/lib/capistrano/tasks/capistrano2.rb +++ b/lib/capistrano/tasks/capistrano2.rb @@ -56,12 +56,17 @@ def for_each_role end end + def run_as(cmd) + su_user = fetch(:sidekiq_user) + run cdm, roles: sidekiq_role, shell: "su - #{su_user}" + end + def quiet_process(pid_file, idx, sidekiq_role) - run "if [ -d #{current_path} ] && [ -f #{pid_file} ] && kill -0 `cat #{pid_file}`> /dev/null 2>&1; then cd #{current_path} && #{fetch(:sidekiqctl_cmd)} quiet #{pid_file} ; else echo 'Sidekiq is not running'; fi", roles: sidekiq_role + run_as "if [ -d #{current_path} ] && [ -f #{pid_file} ] && kill -0 `cat #{pid_file}`> /dev/null 2>&1; then cd #{current_path} && #{fetch(:sidekiqctl_cmd)} quiet #{pid_file} ; else echo 'Sidekiq is not running'; fi" end def stop_process(pid_file, idx, sidekiq_role) - run "if [ -d #{current_path} ] && [ -f #{pid_file} ] && kill -0 `cat #{pid_file}`> /dev/null 2>&1; then cd #{current_path} && #{fetch(:sidekiqctl_cmd)} stop #{pid_file} #{fetch :sidekiq_timeout} ; else echo 'Sidekiq is not running'; fi", roles: sidekiq_role + run_as "if [ -d #{current_path} ] && [ -f #{pid_file} ] && kill -0 `cat #{pid_file}`> /dev/null 2>&1; then cd #{current_path} && #{fetch(:sidekiqctl_cmd)} stop #{pid_file} #{fetch :sidekiq_timeout} ; else echo 'Sidekiq is not running'; fi" end def start_process(pid_file, idx, sidekiq_role) @@ -90,7 +95,7 @@ def start_process(pid_file, idx, sidekiq_role) args.push '--daemon' end - run "if [ -d #{current_path} ] && [ ! -f #{pid_file} ] || ! kill -0 `cat #{pid_file}` > /dev/null 2>&1; then cd #{current_path} ; #{fetch(:sidekiq_cmd)} #{args.compact.join(' ')} ; else echo 'Sidekiq is already running'; fi", pty: false, roles: sidekiq_role + run_as "if [ -d #{current_path} ] && [ ! -f #{pid_file} ] || ! kill -0 `cat #{pid_file}` > /dev/null 2>&1; then cd #{current_path} ; #{fetch(:sidekiq_cmd)} #{args.compact.join(' ')} ; else echo 'Sidekiq is already running'; fi", pty: false end desc 'Quiet sidekiq (stop accepting new work)' diff --git a/lib/capistrano/tasks/sidekiq.cap b/lib/capistrano/tasks/sidekiq.cap index c37b513..6ac4724 100644 --- a/lib/capistrano/tasks/sidekiq.cap +++ b/lib/capistrano/tasks/sidekiq.cap @@ -1,5 +1,6 @@ namespace :load do task :defaults do + set :sidekiq_user, nil set :sidekiq_default_hooks, -> { true } set :sidekiq_pid, -> { File.join(shared_path, 'tmp', 'pids', 'sidekiq.pid') } @@ -129,10 +130,12 @@ namespace :sidekiq do desc 'Quiet sidekiq (stop processing new tasks)' task :quiet do on roles fetch(:sidekiq_role) do - if test("[ -d #{release_path} ]") # fixes #11 - for_each_process(true) do |pid_file, idx| - if pid_process_exists?(pid_file) - quiet_sidekiq(pid_file) + switch_user do + if test("[ -d #{release_path} ]") # fixes #11 + for_each_process(true) do |pid_file, idx| + if pid_process_exists?(pid_file) + quiet_sidekiq(pid_file) + end end end end @@ -142,10 +145,12 @@ namespace :sidekiq do desc 'Stop sidekiq' task :stop do on roles fetch(:sidekiq_role) do - if test("[ -d #{release_path} ]") - for_each_process(true) do |pid_file, idx| - if pid_process_exists?(pid_file) - stop_sidekiq(pid_file) + switch_user do + if test("[ -d #{release_path} ]") + for_each_process(true) do |pid_file, idx| + if pid_process_exists?(pid_file) + stop_sidekiq(pid_file) + end end end end @@ -155,8 +160,10 @@ namespace :sidekiq do desc 'Start sidekiq' task :start do on roles fetch(:sidekiq_role) do - for_each_process do |pid_file, idx| - start_sidekiq(pid_file, idx) unless pid_process_exists?(pid_file) + switch_user do + for_each_process do |pid_file, idx| + start_sidekiq(pid_file, idx) unless pid_process_exists?(pid_file) + end end end end @@ -170,11 +177,13 @@ namespace :sidekiq do desc 'Rolling-restart sidekiq' task :rolling_restart do on roles fetch(:sidekiq_role) do - for_each_process(true) do |pid_file, idx| - if pid_process_exists?(pid_file) - stop_sidekiq(pid_file) + switch_user do + for_each_process(true) do |pid_file, idx| + if pid_process_exists?(pid_file) + stop_sidekiq(pid_file) + end + start_sidekiq(pid_file, idx) end - start_sidekiq(pid_file, idx) end end end @@ -182,9 +191,11 @@ namespace :sidekiq do # Delete any pid file not in use task :cleanup do on roles fetch(:sidekiq_role) do - for_each_process do |pid_file, idx| - if pid_file_exists?(pid_file) - execute "rm #{pid_file}" unless pid_process_exists?(pid_file) + switch_user do + for_each_process do |pid_file, idx| + if pid_file_exists?(pid_file) + execute "rm #{pid_file}" unless pid_process_exists?(pid_file) + end end end end @@ -195,14 +206,27 @@ namespace :sidekiq do task :respawn do invoke 'sidekiq:cleanup' on roles fetch(:sidekiq_role) do - for_each_process do |pid_file, idx| - unless pid_file_exists?(pid_file) - start_sidekiq(pid_file, idx) + switch_user do + for_each_process do |pid_file, idx| + unless pid_file_exists?(pid_file) + start_sidekiq(pid_file, idx) + end end end end end + def switch_user(&block) + su_user = fetch(:puma_user) + if su_user + as su_user do + yield + end + end + + yield + end + def upload_sidekiq_template(from, to, role) template = sidekiq_template(from, role) upload!(StringIO.new(ERB.new(template).result(binding)), to)