Class: Jossh::CommandRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/jossh/command_runner.rb

Overview

CommandRunner is the primary class in the Jossh module. It is responsible for providing the backend for all api methods.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



11
12
13
# File 'lib/jossh/command_runner.rb', line 11

def self.instance
  @@instance ||= self.new
end

Instance Method Details

#active_hostfileObject



54
55
56
57
58
59
60
61
62
# File 'lib/jossh/command_runner.rb', line 54

def active_hostfile
  if File.exist? hostfile 
    hostfile
  elsif hostfile[0] != '/' and File.exist? user_hostfile
    user_hostfile
  else
    false
  end
end

#ssh(hostspec, script, callback: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jossh/command_runner.rb', line 19

def ssh(hostspec, script, callback: nil)
  callback ||= method :simple_puts
  script   = script.join "\n" if script.is_a? Array
  hostspec = load_spec(hostspec) if hostspec.is_a? Symbol
  host     = hostspec.delete :host
  user     = hostspec.delete :user

  Net::SSH.start( host, user, hostspec ) do |ssh| 
    ssh.exec! script do |_ch, stream, data|
      callback.call data, stream
    end
  end
end

#ssh!(hostspec, script) ⇒ Object



15
16
17
# File 'lib/jossh/command_runner.rb', line 15

def ssh!(hostspec, script)
  ssh hostspec, script, callback: OutputHandler.new.method(:pretty_puts)
end

#ssh_hostfile(file) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/jossh/command_runner.rb', line 41

def ssh_hostfile(file)
  file = 'ssh_hosts.yml' if file == :default
  @hostfile = file

  # force reload in case we were called after some ssh call was
  # initiated (e.g. in tests...)
  @ssh_hosts = nil 
end

#ssh_hostsObject



50
51
52
# File 'lib/jossh/command_runner.rb', line 50

def ssh_hosts
  @ssh_hosts ||= load_ssh_hosts
end

#ssh_script(hostspec, script, arguments: nil, callback: nil) ⇒ Object



37
38
39
# File 'lib/jossh/command_runner.rb', line 37

def ssh_script(hostspec, script, arguments: nil, callback: nil)
  ssh hostspec, load_script(script, arguments), callback: callback
end

#ssh_script!(hostspec, script, arguments: nil) ⇒ Object



33
34
35
# File 'lib/jossh/command_runner.rb', line 33

def ssh_script!(hostspec, script, arguments: nil)
  ssh! hostspec, load_script(script, arguments)
end