Commit 68d192b1 by Karl Kloppenborg

Merge pull request #2 from consolo/run-as

Support for running as a different user
parents 3a5b47e9 4ae4fbdd
...@@ -55,7 +55,7 @@ namespace :sneakers do ...@@ -55,7 +55,7 @@ namespace :sneakers do
end end
def sneakers_pid_process_exists?(pid_file) def sneakers_pid_process_exists?(pid_file)
sneakers_pid_file_exists?(pid_file) and test(*("kill -0 $( cat #{pid_file} )").split(' ')) sneakers_pid_file_exists?(pid_file) and test(:kill, "-0 $( cat #{pid_file} )")
end end
def sneakers_pid_file_exists?(pid_file) def sneakers_pid_file_exists?(pid_file)
...@@ -64,11 +64,11 @@ namespace :sneakers do ...@@ -64,11 +64,11 @@ namespace :sneakers do
def stop_sneakers(pid_file) def stop_sneakers(pid_file)
if fetch(:sneakers_run_config) == true if fetch(:sneakers_run_config) == true
execute "kill -SIGTERM `cat #{pid_file}`" execute :kill, "-SIGTERM `cat #{pid_file}`"
else else
if fetch(:stop_sneakers_in_background, fetch(:sneakers_run_in_background)) if fetch(:stop_sneakers_in_background, fetch(:sneakers_run_in_background))
if fetch(:sneakers_use_signals) if fetch(:sneakers_use_signals)
background "kill -TERM `cat #{pid_file}`" background :kill, "-TERM `cat #{pid_file}`"
else else
background :bundle, :exec, :sneakersctl, 'stop', "#{pid_file}", fetch(:sneakers_timeout) background :bundle, :exec, :sneakersctl, 'stop', "#{pid_file}", fetch(:sneakers_timeout)
end end
...@@ -80,7 +80,7 @@ namespace :sneakers do ...@@ -80,7 +80,7 @@ namespace :sneakers do
def quiet_sneakers(pid_file) def quiet_sneakers(pid_file)
if fetch(:sneakers_use_signals) || fetch(:sneakers_run_config) if fetch(:sneakers_use_signals) || fetch(:sneakers_run_config)
background "kill -USR1 `cat #{pid_file}`" background :kill, "-USR1 `cat #{pid_file}`"
else else
begin begin
execute :bundle, :exec, :sneakersctl, 'quiet', "#{pid_file}" execute :bundle, :exec, :sneakersctl, 'quiet', "#{pid_file}"
...@@ -137,6 +137,22 @@ namespace :sneakers do ...@@ -137,6 +137,22 @@ namespace :sneakers do
end end
end end
def as_sneakers_user(role, &block)
user = sneakers_user(role)
if user == role.user
block.call
else
as user do
block.call
end
end
end
def sneakers_user(role)
properties = role.properties
properties.fetch(:sneakers_user) || fetch(:sneakers_user) || properties.fetch(:run_as) || role.user
end
task :add_default_hooks do task :add_default_hooks do
after 'deploy:starting', 'sneakers:quiet' after 'deploy:starting', 'sneakers:quiet'
after 'deploy:updated', 'sneakers:stop' after 'deploy:updated', 'sneakers:stop'
...@@ -146,11 +162,13 @@ namespace :sneakers do ...@@ -146,11 +162,13 @@ namespace :sneakers do
desc 'Quiet sneakers (stop processing new tasks)' desc 'Quiet sneakers (stop processing new tasks)'
task :quiet do task :quiet do
on roles fetch(:sneakers_role) do on roles fetch(:sneakers_role) do |role|
if test("[ -d #{current_path} ]") # fixes #11 as_sneakers_user(role) do
for_each_sneakers_process(true) do |pid_file, idx| if test("[ -d #{current_path} ]") # fixes #11
if sneakers_pid_process_exists?(pid_file) for_each_sneakers_process(true) do |pid_file, idx|
quiet_sneakers(pid_file) if sneakers_pid_process_exists?(pid_file)
quiet_sneakers(pid_file)
end
end end
end end
end end
...@@ -159,11 +177,13 @@ namespace :sneakers do ...@@ -159,11 +177,13 @@ namespace :sneakers do
desc 'Stop sneakers' desc 'Stop sneakers'
task :stop do task :stop do
on roles fetch(:sneakers_role) do on roles fetch(:sneakers_role) do |role|
if test("[ -d #{current_path} ]") as_sneakers_user(role) do
for_each_sneakers_process(true) do |pid_file, idx| if test("[ -d #{current_path} ]")
if sneakers_pid_process_exists?(pid_file) for_each_sneakers_process(true) do |pid_file, idx|
stop_sneakers(pid_file) if sneakers_pid_process_exists?(pid_file)
stop_sneakers(pid_file)
end
end end
end end
end end
...@@ -172,9 +192,11 @@ namespace :sneakers do ...@@ -172,9 +192,11 @@ namespace :sneakers do
desc 'Start sneakers' desc 'Start sneakers'
task :start do task :start do
on roles fetch(:sneakers_role) do on roles fetch(:sneakers_role) do |role|
for_each_sneakers_process do |pid_file, idx| as_sneakers_user(role) do
start_sneakers(pid_file, idx) unless sneakers_pid_process_exists?(pid_file) for_each_sneakers_process do |pid_file, idx|
start_sneakers(pid_file, idx) unless sneakers_pid_process_exists?(pid_file)
end
end end
end end
end end
...@@ -187,22 +209,26 @@ namespace :sneakers do ...@@ -187,22 +209,26 @@ namespace :sneakers do
desc 'Rolling-restart sneakers' desc 'Rolling-restart sneakers'
task :rolling_restart do task :rolling_restart do
on roles fetch(:sneakers_role) do on roles fetch(:sneakers_role) do |role|
for_each_sneakers_process(true) do |pid_file, idx| as_sneakers_user(role) do
if sneakers_pid_process_exists?(pid_file) for_each_sneakers_process(true) do |pid_file, idx|
stop_sneakers(pid_file) if sneakers_pid_process_exists?(pid_file)
stop_sneakers(pid_file)
end
start_sneakers(pid_file, idx)
end end
start_sneakers(pid_file, idx)
end end
end end
end end
# Delete any pid file not in use # Delete any pid file not in use
task :cleanup do task :cleanup do
on roles fetch(:sneakers_role) do on roles fetch(:sneakers_role) do |role|
for_each_sneakers_process do |pid_file, idx| as_sneakers_user(role) do
if sneakers_pid_file_exists?(pid_file) for_each_sneakers_process do |pid_file, idx|
execute "rm #{pid_file}" unless sneakers_pid_process_exists?(pid_file) if sneakers_pid_file_exists?(pid_file)
execute "rm #{pid_file}" unless sneakers_pid_process_exists?(pid_file)
end
end end
end end
end end
...@@ -212,10 +238,12 @@ namespace :sneakers do ...@@ -212,10 +238,12 @@ namespace :sneakers do
desc 'Respawn missing sneakers proccesses' desc 'Respawn missing sneakers proccesses'
task :respawn do task :respawn do
invoke 'sneakers:cleanup' invoke 'sneakers:cleanup'
on roles fetch(:sneakers_role) do on roles fetch(:sneakers_role) do |role|
for_each_sneakers_process do |pid_file, idx| as_sneakers_user(role) do
unless sneakers_pid_file_exists?(pid_file) for_each_sneakers_process do |pid_file, idx|
start_sneakers(pid_file, idx) unless sneakers_pid_file_exists?(pid_file)
start_sneakers(pid_file, idx)
end
end end
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment