Jump to content

The ultimate community for Ruby on Rails developers.


Photo

automate Capistrano deployment question + have ssh process open in background

capistrano ssh

  • Please log in to reply
3 replies to this topic

#1 ericfrancis

ericfrancis

    Passenger

  • Members
  • 4 posts

Posted 27 January 2014 - 10:52 PM

Hello,

I am trying to automate parts of my deployment process.

 

I am managing my company's deployment process and we use Capistrano.

 

My process right now makes use of RemoteForwad in my .ssh/config file in order for my staging servers to access my git repos hosted locally.

 

Right now,  I am having to:

  1. open a terminal
  2. ssh into my staging server
  3. open a new terminal
  4. `cap staging deploy`

I would like to have capistrano perform step 2 for me and also close the ssh session after deployment is completed. I would also like an exception to be raised if port forwarding did not work.

 

Is there a recommended technology that will help me achieve my goal?

 

Thanks!

Eric



#2 ericfrancis

ericfrancis

    Passenger

  • Members
  • 4 posts

Posted 27 January 2014 - 11:16 PM

Update, I have found the run_locally option in capistrano. My task so far looks like this:

namespace :deploy do

  desc 'open ssh session in background'
  task :ssh_in_background do
    run_locally do
      execute 
        `puts "ssh #{fetch(:rails_env)}-#{fetch(:application)}"; 
        ssh #{fetch(:rails_env)}-#{fetch(:application)}`
    end
  end
...
...
end
 

For some reason the puts statement is not able to print out the variables.. 



#3 ericfrancis

ericfrancis

    Passenger

  • Members
  • 4 posts

Posted 28 January 2014 - 12:29 AM

Update,

 

This is what I have now:

 

namespace :deploy do

  desc 'open ssh session in background'
  task :ssh_in_background do
    run_locally do
      puts "environment: #{fetch(:rails_env)}
            application: #{fetch(:application)}"
      # -t -t is for error: Pseudo-terminal will not be allocated because stdin is not a terminal
      execute " ssh -N -t -t -o 'ExitOnForwardFailure yes' #{fetch(:rails_env)}-#{fetch(:application)}"
    end
  end
  ...
  ...

I am getting the connection to close when remote forward does not work. 

 

After reading around... I think what I need is:

  1. ssh into remote server
  2. save the pid of the ssh session to a file
  3. leave the session open

Then, I need another task that runs after deploy is completed `deploy:close_ssh_in_background` that:

  1. ssh into remote server
  2. open file and kill pid
  3. exit session

Thoughts?



#4 ericfrancis

ericfrancis

    Passenger

  • Members
  • 4 posts

Posted 28 January 2014 - 09:26 PM

Found this solution to my problem as well: 

 

Add this to deploy.rb:

 

http://www.halkeye.n...nal-git-server/

module SSHKit; module Backend
 class Netssh < Printer
  alias_method :orig_ssh, :ssh
  def ssh
   ret = orig_ssh
   @@created_remote ||= ret.forward.remote(22, 'gitserver', 9000)
   return ret
  end
 end
end; end

  • james likes this





Also tagged with one or more of these keywords: capistrano, ssh

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users