Class: Dk::Remote::BaseCmd

Inherits:
Object
  • Object
show all
Defined in:
lib/dk/remote.rb

Direct Known Subclasses

Cmd, CmdSpy

Defined Under Namespace

Classes: OutputLine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_cmd_or_spy_klass, cmd_str, opts) ⇒ BaseCmd

Returns a new instance of BaseCmd.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dk/remote.rb', line 17

def initialize(local_cmd_or_spy_klass, cmd_str, opts)
  opts ||= {}
  if nil_or_empty_or_missing_hosts(opts[:hosts])
    raise NoHostsError, "no hosts to run cmd on (#{opts[:hosts].inspect})"
  end

  @hosts         = opts[:hosts].sort
  @ssh_args      = opts[:ssh_args]      || Dk::Config::DEFAULT_SSH_ARGS.dup
  @host_ssh_args = opts[:host_ssh_args] || Dk::Config::DEFAULT_HOST_SSH_ARGS.dup
  @cmd_str       = cmd_str

  @local_cmds = @hosts.inject({}) do |cmds, host|
    cmds[host] = local_cmd_or_spy_klass.new(self.ssh_cmd_str(host), {
      :env          => opts[:env],
      :dry_tree_run => opts[:dry_tree_run]
    })
    cmds
  end
end

Instance Attribute Details

#cmd_strObject (readonly)

Returns the value of attribute cmd_str.



15
16
17
# File 'lib/dk/remote.rb', line 15

def cmd_str
  @cmd_str
end

#host_ssh_argsObject (readonly)

Returns the value of attribute host_ssh_args.



15
16
17
# File 'lib/dk/remote.rb', line 15

def host_ssh_args
  @host_ssh_args
end

#hostsObject (readonly)

Returns the value of attribute hosts.



15
16
17
# File 'lib/dk/remote.rb', line 15

def hosts
  @hosts
end

#local_cmdsObject (readonly)

Returns the value of attribute local_cmds.



15
16
17
# File 'lib/dk/remote.rb', line 15

def local_cmds
  @local_cmds
end

#ssh_argsObject (readonly)

Returns the value of attribute ssh_args.



15
16
17
# File 'lib/dk/remote.rb', line 15

def ssh_args
  @ssh_args
end

Instance Method Details

#output_linesObject



67
68
69
70
71
# File 'lib/dk/remote.rb', line 67

def output_lines
  self.hosts.inject([]) do |lines, host|
    lines + build_output_lines(host, @local_cmds[host].output_lines)
  end
end

#run(input = nil) ⇒ Object



43
44
45
46
47
# File 'lib/dk/remote.rb', line 43

def run(input = nil)
  self.hosts.each{ |host| @local_cmds[host].scmd.start(input) }
  self.hosts.each{ |host| @local_cmds[host].scmd.wait }
  self
end

#ssh_cmd_str(host) ⇒ Object



39
40
41
# File 'lib/dk/remote.rb', line 39

def ssh_cmd_str(host)
  build_ssh_cmd_str(@cmd_str, host, @ssh_args, @host_ssh_args)
end

#stderrObject



55
56
57
58
59
# File 'lib/dk/remote.rb', line 55

def stderr
  self.hosts.inject('') do |err, host|
    err.empty? ? @local_cmds[host].stderr.to_s : err
  end
end

#stdoutObject



49
50
51
52
53
# File 'lib/dk/remote.rb', line 49

def stdout
  self.hosts.inject('') do |out, host|
    out.empty? ? @local_cmds[host].stdout.to_s : out
  end
end

#success?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/dk/remote.rb', line 61

def success?
  self.hosts.inject(true) do |success, host|
    success && @local_cmds[host].success?
  end
end

#to_sObject



37
# File 'lib/dk/remote.rb', line 37

def to_s; self.cmd_str; end