summaryrefslogtreecommitdiff
path: root/lib/leap_cli/commands/run.rb
blob: cad9b7a0a0cde73d540915022a6d35bd9bec4511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module LeapCli; module Commands

  desc 'Run a shell command remotely'
  long_desc "Runs the specified command COMMAND on each node in the FILTER set. " +
            "For example, `leap run 'uname -a' webapp`"
  arg_name 'COMMAND FILTER'
  command :run do |c|
    c.switch 'stream', :default => false, :desc => 'If set, stream the output as it arrives. (default: --stream for a single node, --no-stream for multiple nodes)'
    c.flag 'port', :arg_name => 'SSH_PORT', :desc => 'Override default SSH port used when trying to connect to the server.'
    c.action do |global, options, args|
      run_shell_command(global, options, args)
    end
  end

  private

  def run_shell_command(global, options, args)
    require 'leap_cli/ssh'
    cmd    = args[0]
    filter = args[1..-1]
    cmd    = global[:force] ? cmd : LeapCli::SSH::Options.sanitize_command(cmd)
    nodes  = manager.filter!(filter)
    if nodes.size == 1 || options[:stream]
      stream_command(nodes, cmd, options)
    else
      capture_command(nodes, cmd, options)
    end
  end

  def capture_command(nodes, cmd, options)
    SSH.remote_command(nodes, options) do |ssh, host|
      output = ssh.capture(cmd, :log_output => false)
      if output
        logger = LeapCli.new_logger
        logger.log(:ran, "`" + cmd + "`", host: host.hostname, color: :green) do
          logger.log(output, wrap: true)
        end
      end
    end
  end

  def stream_command(nodes, cmd, options)
    SSH.remote_command(nodes, options) do |ssh, host|
      ssh.stream(cmd, :log_cmd => true, :log_finish => true, :fail_msg => 'oops')
    end
  end

end; end