How to enter rails console on production via capistrano?

前端 未结 7 1498
借酒劲吻你
借酒劲吻你 2021-02-02 11:25

I want to enter the rails console on production server from my local machine via capistrano. I found some gists, e.g. https://gist.github.com/813291 and when I enter console via

7条回答
  •  礼貌的吻别
    2021-02-02 11:45

    I have a somewhat difficult environment, which is influx ... So bash -lc isn't really an option right now. My solution is similar to @Rocco, but it's a bit more refined.

    # run a command in the `current` directory of `deploy_to`
    def run_interactively(command)
      # select a random server to run on
      server = find_servers_for_task(current_task).sample
      # cobble together a shell environment
      app_env = fetch("default_environment", {}).map{|k,v| "#{k}=\"#{v}\""}.join(' ')
      # Import the default environment, cd to the currently deployed app, run the command
      command = %Q(ssh -tt -i #{ssh_options[:keys]} #{user}@#{server} "env #{app_env} bash -c 'cd #{deploy_to}/current; #{command}'")
      puts command
      exec command
    end
    
    namespace :rails do
      desc "rails console on a sidekiq worker"
      task :console, role: :sidekiq_normal do
        run_interactively "bundle exec rails console #{rails_env}"
      end
    end
    

提交回复
热议问题