Commit 4ae4fbdd by Jordan Hollinger

Support for running as a different user

parent 3a5b47e9
...@@ -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,7 +162,8 @@ namespace :sneakers do ...@@ -146,7 +162,8 @@ 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|
as_sneakers_user(role) do
if test("[ -d #{current_path} ]") # fixes #11 if test("[ -d #{current_path} ]") # fixes #11
for_each_sneakers_process(true) do |pid_file, idx| for_each_sneakers_process(true) do |pid_file, idx|
if sneakers_pid_process_exists?(pid_file) if sneakers_pid_process_exists?(pid_file)
...@@ -156,10 +173,12 @@ namespace :sneakers do ...@@ -156,10 +173,12 @@ namespace :sneakers do
end end
end end
end end
end
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|
as_sneakers_user(role) do
if test("[ -d #{current_path} ]") if test("[ -d #{current_path} ]")
for_each_sneakers_process(true) do |pid_file, idx| for_each_sneakers_process(true) do |pid_file, idx|
if sneakers_pid_process_exists?(pid_file) if sneakers_pid_process_exists?(pid_file)
...@@ -169,15 +188,18 @@ namespace :sneakers do ...@@ -169,15 +188,18 @@ namespace :sneakers do
end end
end end
end end
end
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|
as_sneakers_user(role) do
for_each_sneakers_process do |pid_file, idx| for_each_sneakers_process do |pid_file, idx|
start_sneakers(pid_file, idx) unless sneakers_pid_process_exists?(pid_file) start_sneakers(pid_file, idx) unless sneakers_pid_process_exists?(pid_file)
end end
end end
end end
end
desc 'Restart sneakers' desc 'Restart sneakers'
task :restart do task :restart do
...@@ -187,7 +209,8 @@ namespace :sneakers do ...@@ -187,7 +209,8 @@ 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|
as_sneakers_user(role) do
for_each_sneakers_process(true) do |pid_file, idx| for_each_sneakers_process(true) do |pid_file, idx|
if sneakers_pid_process_exists?(pid_file) if sneakers_pid_process_exists?(pid_file)
stop_sneakers(pid_file) stop_sneakers(pid_file)
...@@ -196,10 +219,12 @@ namespace :sneakers do ...@@ -196,10 +219,12 @@ namespace :sneakers do
end 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|
as_sneakers_user(role) do
for_each_sneakers_process do |pid_file, idx| for_each_sneakers_process do |pid_file, idx|
if sneakers_pid_file_exists?(pid_file) if sneakers_pid_file_exists?(pid_file)
execute "rm #{pid_file}" unless sneakers_pid_process_exists?(pid_file) execute "rm #{pid_file}" unless sneakers_pid_process_exists?(pid_file)
...@@ -207,12 +232,14 @@ namespace :sneakers do ...@@ -207,12 +232,14 @@ namespace :sneakers do
end end
end end
end end
end
# TODO : Don't start if all proccess are off, raise warning. # TODO : Don't start if all proccess are off, raise warning.
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|
as_sneakers_user(role) do
for_each_sneakers_process do |pid_file, idx| for_each_sneakers_process do |pid_file, idx|
unless sneakers_pid_file_exists?(pid_file) unless sneakers_pid_file_exists?(pid_file)
start_sneakers(pid_file, idx) start_sneakers(pid_file, idx)
...@@ -220,6 +247,7 @@ namespace :sneakers do ...@@ -220,6 +247,7 @@ namespace :sneakers do
end end
end end
end end
end
def template_sneakers(from, to, role) def template_sneakers(from, to, role)
[ [
......
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