Skip to content

Commit

Permalink
Merge pull request seuros#89 from dreyks/sudo_if_needed
Browse files Browse the repository at this point in the history
added ability to operate without sudo
  • Loading branch information
seuros committed Jun 25, 2015
2 parents 94e08ab + 6c44b5f commit 1c4005e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down Expand Up @@ -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
Expand Down
31 changes: 17 additions & 14 deletions lib/capistrano/tasks/monit.cap
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 1c4005e

Please sign in to comment.