How do I run a rake task from Capistrano?

前端 未结 16 2153
忘了有多久
忘了有多久 2020-11-28 02:18

I already have a deploy.rb that can deploy my app on my production server.

My app contains a custom rake task (a .rake file in the lib/tasks directory).

I\

16条回答
  •  情书的邮戳
    2020-11-28 02:27

    So I have been working on this. it seams to work well. However you need a formater to really take advantage of the code.

    If you don't want to use a formatter just set the log level to to debug mode. These semas to h

    SSHKit.config.output_verbosity = Logger::DEBUG
    

    Cap Stuff

    namespace :invoke do
      desc 'Run a bash task on a remote server. cap environment invoke:bash[\'ls -la\'] '
      task :bash, :execute do |_task, args|
        on roles(:app), in: :sequence do
          SSHKit.config.format = :supersimple
          execute args[:execute]
        end
      end
    
      desc 'Run a rake task on a remote server. cap environment invoke:rake[\'db:migrate\'] '
      task :rake, :task do |_task, args|
        on primary :app do
          within current_path do
            with rails_env: fetch(:rails_env) do
              SSHKit.config.format = :supersimple
              rake args[:task]
            end
          end
        end
      end
    end
    

    This is the formatter I built to work with the code above. It is based off the :textsimple built into the sshkit but it is not a bad way to invoke custom tasks. Oh this many not works with the newest version of sshkit gem. I know it works with 1.7.1. I say this because the master branch has changed the SSHKit::Command methods that are available.

    module SSHKit
      module Formatter
        class SuperSimple < SSHKit::Formatter::Abstract
          def write(obj)
            case obj
            when SSHKit::Command    then write_command(obj)
            when SSHKit::LogMessage then write_log_message(obj)
            end
          end
          alias :<< :write
    
          private
    
          def write_command(command)
            unless command.started? && SSHKit.config.output_verbosity == Logger::DEBUG
              original_output << "Running #{String(command)} #{command.host.user ? "as #{command.host.user}@" : "on "}#{command.host}\n"
              if SSHKit.config.output_verbosity == Logger::DEBUG
                original_output << "Command: #{command.to_command}" + "\n"
              end
            end
    
            unless command.stdout.empty?
              command.stdout.lines.each do |line|
                original_output << line
                original_output << "\n" unless line[-1] == "\n"
              end
            end
    
            unless command.stderr.empty?
              command.stderr.lines.each do |line|
                original_output << line
                original_output << "\n" unless line[-1] == "\n"
              end
            end
    
          end
    
          def write_log_message(log_message)
            original_output << log_message.to_s + "\n"
          end
        end
      end
    end
    

提交回复
热议问题