diff --git a/README.md b/README.md index c417604..d37a28b 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ Configurable options, shown here with defaults: :sidekiq_options_per_process => nil :sidekiq_concurrency => nil :sidekiq_monit_templates_path => 'config/deploy/templates' + :sidekiq_monit_use_sudo => true :sidekiq_cmd => "#{fetch(:bundle_cmd, "bundle")} exec sidekiq" # Only for capistrano2.5 :sidekiqctl_cmd => "#{fetch(:bundle_cmd, "bundle")} exec sidekiqctl" # Only for capistrano2.5 ``` @@ -90,6 +91,12 @@ If you need change some config in redactor, you can ``` +If your deploy user has no need in `sudo` for using monit, you can disable it as follows: + +```ruby +set :sidekiq_monit_use_sudo, false +``` + ## Changelog - 0.5.3: Custom count of processes per each host - 0.5.0: Multiple processes @mrsimo diff --git a/lib/capistrano/tasks/monit.cap b/lib/capistrano/tasks/monit.cap index 6d597b4..c80de90 100644 --- a/lib/capistrano/tasks/monit.cap +++ b/lib/capistrano/tasks/monit.cap @@ -1,6 +1,7 @@ namespace :load do task :defaults do set :sidekiq_monit_conf_dir, -> { '/etc/monit/conf.d' } + set :sidekiq_monit_use_sudo, -> { true } set :monit_bin, -> { '/usr/bin/monit' } set :sidekiq_monit_default_hooks, -> { true } end @@ -29,15 +30,9 @@ namespace :sidekiq do template_sidekiq 'sidekiq_monit', "#{fetch(:tmp_dir)}/monit.conf", @role mv_command = "mv #{fetch(:tmp_dir)}/monit.conf #{fetch(:sidekiq_monit_conf_dir)}/#{sidekiq_service_name}.conf" + sudo_if_needed mv_command - # Try execute in case the deploy user doesn't have sudo to mv - begin - execute mv_command - rescue - sudo mv_command - end - - sudo "#{fetch(:monit_bin)} reload" + sudo_if_needed "#{fetch(:monit_bin)} reload" end end @@ -46,10 +41,10 @@ namespace :sidekiq do on roles(fetch(:sidekiq_role)) do fetch(:sidekiq_processes).times do |idx| begin - sudo "#{fetch(:monit_bin)} monitor #{sidekiq_service_name(idx)}" + sudo_if_needed "#{fetch(:monit_bin)} monitor #{sidekiq_service_name(idx)}" rescue invoke 'sidekiq:monit:config' - sudo "#{fetch(:monit_bin)} monitor #{sidekiq_service_name(idx)}" + sudo_if_needed "#{fetch(:monit_bin)} monitor #{sidekiq_service_name(idx)}" end end end @@ -60,7 +55,7 @@ namespace :sidekiq do on roles(fetch(:sidekiq_role)) do fetch(:sidekiq_processes).times do |idx| begin - sudo "#{fetch(:monit_bin)} unmonitor #{sidekiq_service_name(idx)}" + sudo_if_needed "#{fetch(:monit_bin)} unmonitor #{sidekiq_service_name(idx)}" rescue # no worries here end @@ -72,7 +67,7 @@ namespace :sidekiq do task :start do on roles(fetch(:sidekiq_role)) do fetch(:sidekiq_processes).times do |idx| - sudo "#{fetch(:monit_bin)} start #{sidekiq_service_name(idx)}" + sudo_if_needed "#{fetch(:monit_bin)} start #{sidekiq_service_name(idx)}" end end end @@ -81,7 +76,7 @@ namespace :sidekiq do task :stop do on roles(fetch(:sidekiq_role)) do fetch(:sidekiq_processes).times do |idx| - sudo "#{fetch(:monit_bin)} stop #{sidekiq_service_name(idx)}" + sudo_if_needed "#{fetch(:monit_bin)} stop #{sidekiq_service_name(idx)}" end end end @@ -90,7 +85,7 @@ namespace :sidekiq do task :restart do on roles(fetch(:sidekiq_role)) do fetch(:sidekiq_processes).times do |idx| - sudo "#{fetch(:monit_bin)} restart #{sidekiq_service_name(idx)}" + sudo_if_needed"#{fetch(:monit_bin)} restart #{sidekiq_service_name(idx)}" end end end @@ -123,5 +118,13 @@ namespace :sidekiq do end end + def sudo_if_needed(command) + send(use_sudo? ? :sudo : :execute, command) + end + + def use_sudo? + fetch(:sidekiq_monit_use_sudo) + end + end end