Is there a way to use capistrano (or similar) to remotely interact with rails console

后端 未结 6 643
情话喂你
情话喂你 2021-02-03 11:50

I\'m loving how capistrano has simplified my deployment workflow, but often times a pushed change will run into issues that I need to log into the server to troubleshoot via the

6条回答
  •  广开言路
    2021-02-03 12:27

    I found pretty nice solution based on https://github.com/codesnik/rails-recipes/blob/master/lib/rails-recipes/console.rb

    desc "Remote console" 
    task :console, :roles => :app do
      env = stage || "production"
      server = find_servers(:roles => [:app]).first
      run_with_tty server, %W( ./script/rails console #{env} )
    end
    
    desc "Remote dbconsole" 
    task :dbconsole, :roles => :app do
      env = stage || "production"
      server = find_servers(:roles => [:app]).first
      run_with_tty server, %W( ./script/rails dbconsole #{env} )
    end
    
    def run_with_tty(server, cmd)
      # looks like total pizdets
      command = []
      command += %W( ssh -t #{gateway} -l #{self[:gateway_user] || self[:user]} ) if     self[:gateway]
      command += %W( ssh -t )
      command += %W( -p #{server.port}) if server.port
      command += %W( -l #{user} #{server.host} )
      command += %W( cd #{current_path} )
      # have to escape this once if running via double ssh
      command += [self[:gateway] ? '\&\&' : '&&']
      command += Array(cmd)
      system *command
    end
    

提交回复
热议问题