Class: SSHLine

Inherits:
Object
  • Object
show all
Defined in:
lib/scout/offsite/ssh.rb,
lib/scout/offsite/sync.rb

Direct Known Subclasses

Mock

Defined Under Namespace

Classes: Mock

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = :default, user = nil) ⇒ SSHLine

Returns a new instance of SSHLine.



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
# File 'lib/scout/offsite/ssh.rb', line 14

def initialize(host = :default, user = nil)
  host = SSHLine.default_server if host.nil? || host == :default
  @host = host
  @user = user

  @ssh = Net::SSH.start(@host, @user)

  @ch = @ssh.open_channel do |ch|
    ch.exec 'bash -l'
  end

  @ch.send_data("[[ -f ~/.scout/environment ]] && source ~/.scout/environment\n")
  @ch.send_data("[[ -f ~/.rbbt/environment ]] && source ~/.rbbt/environment\n")

  @ch.on_data do |_,data|
    if m = data.match(/DONECMD: (\d+)\n/)
      @exit_status = m[1].to_i
      @output << data.sub(m[0],'')
      serve_output 
    else
      @output << data
    end
  end

  @ch.on_extended_data do |_,c,err|
    STDERR.write err 
  end
end

Class Attribute Details

.default_serverObject

Returns the value of attribute default_server.



6
7
8
# File 'lib/scout/offsite/ssh.rb', line 6

def default_server
  @default_server
end

Class Method Details

.command(server, command, argv = [], options = nil) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/scout/offsite/ssh.rb', line 154

def self.command(server, command, argv = [], options = nil)
  command = "scout #{command}" unless command && command.include?('scout')
  argv_str = (argv - ["--"]).collect{|v| '"' + v.to_s + '"' } * " "
  command = "#{command} #{argv_str}"
  Log.debug "Offsite #{server} running: #{command}"
  run(server, command, options)
end

.locate(server, paths, map: :user) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/scout/offsite/sync.rb', line 2

def self.locate(server, paths, map: :user)
  SSHLine.scout server, <<-EOF
map = :#{map}
paths = [#{paths.collect{|p| "'" + p + "'" } * ", " }]
located = paths.collect{|p| Path.setup(p).find(map) }
identified = paths.collect{|p| Resource.identify(p) }
[located, identified]
  EOF
end

.mkdir(server, path) ⇒ Object



162
163
164
# File 'lib/scout/offsite/ssh.rb', line 162

def self.mkdir(server, path)
  self.run server, "mkdir -p '#{path}'"
end

.open(host, user = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/scout/offsite/ssh.rb', line 125

def self.open(host, user = nil)
  @connections[[host, user]] ||=
    begin
      if host == 'localhost'
        SSHLine::Mock.new
      else
        SSHLine.new host, user
      end
    end
end

.reach?(server = SSHLine.default_server) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
# File 'lib/scout/offsite/ssh.rb', line 44

def self.reach?(server = SSHLine.default_server)
  Persist.memory(server, :key => "Reach server") do
    begin
      CMD.cmd("ssh #{server} bash -l -c \"scout\"") 
      true
    rescue Exception
      false
    end
  end
end

.rsync(source_path, target_path, directory: false, source: nil, target: nil, dry_run: false, hard_link: false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/scout/offsite/sync.rb', line 12

def self.rsync(source_path, target_path, directory: false, source: nil, target: nil, dry_run: false, hard_link: false)
  rsync_args = "-avztHP --copy-unsafe-links --omit-dir-times "

  rsync_args << "--link-dest '#{source_path}' " if hard_link && ! source

  source_path = source_path + "/" if directory && ! source_path.end_with?("/")
  target_path = target_path + "/" if directory && ! target_path.end_with?("/")
  if target
    SSHLine.mkdir target, File.dirname(target_path)
  else
    Open.mkdir(File.dirname(target_path))
  end

  cmd = 'rsync '
  cmd << rsync_args
  cmd << '-nv ' if dry_run
  cmd << (source ? [source, source_path] * ":" : source_path) << " "
  cmd << (target ? [target, target_path] * ":" : target_path) << " "

  CMD.cmd_log(cmd, :log => Log::HIGH)
end

.ruby(server, script) ⇒ Object



142
143
144
# File 'lib/scout/offsite/ssh.rb', line 142

def self.ruby(server, script)
  open(server).ruby(script)
end

.run(server, cmd, options = nil) ⇒ Object



136
137
138
139
140
# File 'lib/scout/offsite/ssh.rb', line 136

def self.run(server, cmd, options = nil)
  cmd = cmd * " " if Array === cmd
  cmd += " " + CMD.process_cmd_options(options) if options
  open(server).run(cmd)
end

.run_local(&block) ⇒ Object



166
167
168
169
170
171
172
173
174
# File 'lib/scout/offsite/ssh.rb', line 166

def self.run_local(&block)
  res = begin
          old_stdout = STDOUT.dup; STDOUT.reopen(STDERR)
          block.call
        ensure
          STDOUT.reopen(old_stdout)
        end
  puts Marshal.dump(res)
end

.scout(server, script) ⇒ Object



146
147
148
# File 'lib/scout/offsite/ssh.rb', line 146

def self.scout(server, script)
  open(server).scout(script)
end

.sync(paths, source: nil, target: nil, map: :user, **kwargs) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/scout/offsite/sync.rb', line 34

def self.sync(paths, source: nil, target: nil, map: :user, **kwargs)
  source = nil if source == 'localhost'
  target = nil if target == 'localhost'

  if source
    source_paths, identified_paths = SSHLine.locate(source, paths)
  else
    source_paths = paths.collect{|p| Path === p ? p.find : p }
    identified_paths = paths.collect{|p| Resource.identify(p) }
  end

  if target
    target_paths = SSHLine.locate(target, identified_paths, map: map)
  else
    target_paths = identified_paths.collect{|p| p.find(map) }
  end

  source_paths.zip(target_paths).each do |source_path,target_path|
    rsync(source_path, target_path, source: source, target: target, **kwargs)
  end
end

.workflow(server, workflow, script) ⇒ Object



150
151
152
# File 'lib/scout/offsite/ssh.rb', line 150

def self.workflow(server, workflow, script)
  open(server).workflow(workflow, script)
end

Instance Method Details

#ruby(script) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/scout/offsite/ssh.rb', line 75

def ruby(script)
  @output = ""
  @complete_output = false
  cmd = "ruby -e \"#{script.gsub('"','\\"')}\"\n"
  Log.debug "Running ruby on #{@host}:\n#{ script }"
  @ch.send_data(cmd)
  @ch.send_data("echo DONECMD: $?\n")
  @ssh.loop{ !@complete_output }
  if @exit_status.to_i == 0
    return @output
  else
    raise SSHProcessFailed.new @host, "Ruby script:\n#{script}"
  end
end

#run(command) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/scout/offsite/ssh.rb', line 65

def run(command)
  send_cmd(command)
  @ssh.loop{ ! @complete_output}
  if @exit_status.to_i == 0
    return @output
  else
    raise SSHProcessFailed.new @host, command
  end
end

#scout(script) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/scout/offsite/ssh.rb', line 90

def scout(script)
  scout_script =<<-EOF
require 'scout'
SSHLine.run_local do
#{script.strip}
end
  EOF

  m = ruby(scout_script)
  Marshal.load m
end

#send_cmd(command) ⇒ Object



55
56
57
58
59
# File 'lib/scout/offsite/ssh.rb', line 55

def send_cmd(command)
  @output = ""
  @complete_output = false
  @ch.send_data(command+"\necho DONECMD: $?\n")
end

#serve_outputObject



61
62
63
# File 'lib/scout/offsite/ssh.rb', line 61

def serve_output
  @complete_output = true
end

#workflow(workflow, script) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/scout/offsite/ssh.rb', line 102

def workflow(workflow, script)
  preamble =<<-EOF
wf = Workflow.require_workflow('#{workflow}')
  EOF

  scout(preamble + "\n" + script)
end