The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Switching users during a capistrano deploy

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Paul Gross

Posts: 152
Nickname: pgross
Registered: Sep, 2007

Paul Gross is a software developer for ThoughtWorks.
Switching users during a capistrano deploy Posted: Aug 5, 2008 8:41 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Paul Gross.
Original Post: Switching users during a capistrano deploy
Feed Title: Paul Gross's Blog - Home
Feed URL: http://feeds.feedburner.com/pgrs
Feed Description: Posts mainly about ruby on rails.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Paul Gross
Latest Posts From Paul Gross's Blog - Home

Advertisement

We have a complicated deployment on my current project which includes running several commands as a different user from the main deployment user. Normally, this wouldn’t be a problem since the sudo method provides an option called ‘as’:


task :run_command_as_another_user do
  sudo "whoami", :as => "another_user" 
end

Unfortunately, we do not have sudo access to our servers. Instead of using sudo, we wrote a new method called with_user which will execute a block as a different user:


task :try_another_user do
  with_user("another_user", "another_password") do
    run "whoami" 
  end
end

The with_user method will set the user and password back to the original values once the block is complete. For example:


task :whoami do
  set :user, 'original'
  set :password, 'original'

  run 'whoami'
  with_user("someone_else", 'password') do
    run "whoami" 
  end
  run 'whoami'
end

The output of the previous task (stripped down) is:


 ** [out :: 127.0.0.1] original
 ** [out :: 127.0.0.1] someone_else
 ** [out :: 127.0.0.1] original

The implementation of with_user is:


def with_user(new_user, new_pass, &block)
  old_user, old_pass = user, password
  set :user, new_user
  set :password, new_pass
  close_sessions
  yield
  set :user, old_user
  set :password, old_pass
  close_sessions
end

def close_sessions
  sessions.values.each { |session| session.close }
  sessions.clear
end

It saves the old user and password and then sets the new values. Then, it disconnects from all of the servers, which forces a reconnect on the next command with the new user and password. When the block is complete, it resets the user and password and disconnects again.

The repeated reconnecting is not the most efficient solution, but it is simple and works for us.

Read: Switching users during a capistrano deploy

Topic: Moving On Previous Topic   Next Topic Topic: Subversion Password Caching Problem Leads to TextMate svn Integration Problems

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use