Module: RemoteWorkflow::SSH

Defined in:
lib/rbbt/workflow/remote_workflow/driver/ssh.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clean(url, input_id, jobname = nil) ⇒ Object



171
172
173
174
175
176
177
178
179
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 171

def self.clean(url, input_id, jobname = nil)
  server, path = parse_url(url)

  script = path_script(path)
  script +=<<-EOF
job.clean
  EOF
  Misc.ssh_run(server, script)
end

.get_json(url, params = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 89

def self.get_json(url, params = {})
  server, path = parse_url(url)
  script = path_script(path)

  script +=<<-EOF
STDOUT.write res.to_json
  EOF

  json = Misc.ssh_run(server, script)
  Log.debug "JSON (#{ url }): #{json}" if RBBT_DEBUG_REMOTE_JSON
  JSON.parse(json)
end

.get_raw(url, params = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 102

def self.get_raw(url, params = {})
  server, path = parse_url(url)
  script = path_script(path)

  script +=<<-EOF
STDOUT.write res
  EOF

  Misc.ssh_run(server, script)
end

.job_script(inputs_id, jobname = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 79

def self.job_script(inputs_id, jobname = nil)
  script =<<-EOF
jobname = #{jobname.nil? ? 'nil' : "'#{jobname}'"}
path = File.join(ENV["HOME"], '.rbbt/tmp/tmp-ssh_job_inputs/#{inputs_id}')
job_inputs = Workflow.load_inputs(path, task_info[:inputs], task_info[:input_types])
job = wf.job(task, jobname, job_inputs)
  EOF
  script
end

.missing_dep_inputs(job) ⇒ Object



260
261
262
263
264
265
266
267
268
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 260

def self.missing_dep_inputs(job)
  inputs = job.inputs.to_hash.slice(*job.real_inputs.map{|i| i.to_s})
  job.dependencies.each do |dep|
    next if dep.done?
    inputs = dep.inputs.to_hash.slice(*dep.real_inputs.map{|i| i.to_s}).merge(inputs)
    inputs = missing_dep_inputs(dep).merge(inputs)
  end
  inputs
end

.orchestrate_batch_job(url, input_id, jobname = nil, batch_options = {}) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 155

def self.orchestrate_batch_job(url, input_id, jobname = nil, batch_options = {})
  server, path = parse_url(url)

  script = path_script(path)
  script += job_script(input_id, jobname)
  script +=<<-EOF
require 'rbbt/hpc'
HPC::BATCH_MODULE = HPC.batch_system
batch_options = JSON.parse(%q(#{batch_options.to_json}))
job.clean if job.error? and job.recoverable_error?
HPC::BATCH_MODULE.orchestrate_job(job, batch_options) unless job.done? || job.error?
STDOUT.write job.path
  EOF
  Misc.ssh_run(server, script)
end

.parse_url(url) ⇒ Object

def self.run_log(server, script)

Log.debug "Run and monitor ssh script in #{server}:\n#{script}"
CMD.cmd("ssh '#{server}' 'shopt -s expand_aliases; bash -ic \"ruby\"' ", :in => script, :log => true)

end



15
16
17
18
19
20
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 15

def self.parse_url(url)
  m = url.match(/ssh:\/\/([^:]+):(.*)/)
  server = m.captures[0]
  path = m.captures[1]
  [server, path]
end

.path_script(path) ⇒ Object



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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 22

def self.path_script(path) 

  workflow, task, job, *rest = path.split("/")

  workflow_name = begin
                    wf = Kernel.const_get(workflow) if String === workflow && ! workflow.empty?
                    wf.respond_to?(:complete_name) ? (wf.complete_name || workflow) : workflow
                  rescue
                    workflow
                  end

  script =<<-EOF
require 'rbbt/workflow'
wf = Workflow.require_workflow "#{workflow_name}"
  EOF

  case task
  when nil
    script +=<<-EOF
task_info = {}
wf.tasks.keys.each do |task|
  task_info[task] = wf.task_info(task)
end
res = task_info
    EOF
  when 'documentation'
    script +=<<-EOF
res = documentation = wf.documentation
    EOF
  else
    if job.nil?
      script +=<<-EOF
task = '#{task}'
res = task_info = wf.task_info(task)
      EOF
    else
      case rest.first
      when nil
        script +=<<-EOF
task = '#{task}'
jobname = '#{job}'
res = job = wf.fast_load_id(File.join(task, jobname))
        EOF
      when "info"
        script +=<<-EOF
task = '#{task}'
jobname = '#{job}'
job = wf.fast_load_id(File.join(task, jobname))
res = job_info = job.info
        EOF
      else
        raise "Unkown path: #{[path, rest].inspect}"
      end
    end
  end
end

.post_job(url, inputs_id, jobname = nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 113

def self.post_job(url, inputs_id, jobname = nil)
  server, path = parse_url(url)

  script = path_script(path)
  script += job_script(inputs_id, jobname)
  script +=<<-EOF
job.init_info
STDOUT.write job.path
  EOF
  Misc.ssh_run(server, script)
end

.relay(workflow, task, jobname, inputs, server, options = {}) ⇒ Object



341
342
343
344
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 341

def self.relay(workflow, task, jobname, inputs, server, options = {})
  job = workflow.job(task, jobname, inputs)
  relay_job(job, server, options)
end

.relay_job(job, server, options = {}) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 270

def self.relay_job(job, server, options = {})
  migrate, produce, produce_dependencies, search_path, run_type, batch_options = Misc.process_options options.dup,
    :migrate, :produce, :produce_dependencies, :search_path, :run_type, :batch_options

  search_path ||= 'user'

  produce = true if migrate

  workflow_name = job.workflow.to_s
  remote_workflow = RemoteWorkflow.new("ssh://#{server}:#{workflow_name}", "#{workflow_name}")
  inputs = job.recursive_inputs.to_hash.slice(*job.real_inputs.map{|i| i.to_s})
  Log.medium "Relaying dependency #{job.workflow}:#{job.short_path} to #{server} (#{inputs.keys * ", "})"

  upload_dependencies(job, server, search_path, options[:produce_dependencies])
  rjob = remote_workflow.job(job.task_name.to_s, job.clean_name, inputs)

  override_dependencies = job.rec_dependencies.select{|dep| dep.done? }.collect{|dep| [dep.workflow.to_s, dep.task_name.to_s] * "#" << "=" << Rbbt.identify(dep.path)}
  rjob.override_dependencies = override_dependencies

  rjob.run_type = run_type
  rjob.batch_options = batch_options || {}

  if options[:migrate]
    rjob.produce
    Step.migrate(Rbbt.identify(job.path), 'user', :source => server) 
  end

  rjob
end

.relay_job_list(job_list, server, options = {}) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 300

def self.relay_job_list(job_list, server, options = {})
  migrate, produce, produce_dependencies, search_path, run_type, batch_options = Misc.process_options options.dup,
    :migrate, :produce, :produce_dependencies, :search_path, :run_type, :batch_options

  search_path ||= 'user'

  produce = true if migrate

  upload_dependencies(job_list, server, search_path, options[:produce_dependencies])

  rjobs_job = job_list.collect do |job|
    
    workflow_name = job.workflow.to_s
    remote_workflow = RemoteWorkflow.new("ssh://#{server}:#{workflow_name}", "#{workflow_name}")
    inputs = IndiferentHash.setup(job.recursive_inputs.to_hash).slice(*job.real_inputs.map{|i| i.to_s})
    Log.medium "Relaying dependency #{job.workflow}:#{job.short_path} to #{server} (#{inputs.keys * ", "})"

    rjob = remote_workflow.job(job.task_name.to_s, job.clean_name, inputs)

    override_dependencies = job.rec_dependencies.select{|dep| dep.done? }.collect{|dep| [dep.workflow.to_s, dep.task_name.to_s] * "#" << "=" << Rbbt.identify(dep.path)}
    rjob.override_dependencies = override_dependencies

    rjob.run_type = run_type
    rjob.batch_options = batch_options || {}

    rjob.run(true)

    [rjob, job]
  end

  if options[:migrate]
    rjobs_job.each do |rjob,job|
      rjob.produce
      Step.migrate(Rbbt.identify(job.path), 'user', :source => server) 
    end
  end

  rjobs_job.collect{|p| p.first }
end

.run_batch_job(url, input_id, jobname = nil, batch_options = {}) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 139

def self.run_batch_job(url, input_id, jobname = nil, batch_options = {})
  server, path = parse_url(url)

  script = path_script(path)
  script += job_script(input_id, jobname)
  script +=<<-EOF
require 'rbbt/hpc'
HPC::BATCH_MODULE = HPC.batch_system 
batch_options = JSON.parse(%q(#{batch_options.to_json}))
job.clean if job.error? and job.recoverable_error?
HPC::BATCH_MODULE.run_job(job, batch_options) unless job.done? || job.error?
STDOUT.write job.path
  EOF
  Misc.ssh_run(server, script)
end

.run_job(url, input_id, jobname = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 125

def self.run_job(url, input_id, jobname = nil)
  server, path = parse_url(url)

  script = path_script(path)
  script += job_script(input_id, jobname)
  script +=<<-EOF
ENV["RBBT_UPDATE"]="#{(ENV["RBBT_UPDATE"] || false).to_s}"
job.clean if job.error? and job.recoverable_error?
job.run unless job.done? || job.error?
STDOUT.write job.path
  EOF
  Misc.ssh_run(server, script)
end

.upload_dependencies(job_list, server, search_path = 'user', produce_dependencies = false) ⇒ Object

remote = RemoteWorkflow.new(“ssh://#server:#workflowworkflow.to_s”, “#workflowworkflow.to_s”)

rjob = remote.job(task, jobname, {})
rjob.override_dependencies = override_dependencies
rjob.run

end



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 224

def self.upload_dependencies(job_list, server, search_path = 'user', produce_dependencies = false)
  server, path = parse_url(server) if server =~ /^ssh:\/\//

  job_list = [job_list] unless Array === job_list

  all_deps = {}
  if produce_dependencies
    job_list.each do |job|
      job.dependencies.each do |dep|
        all_deps[dep] ||= []
        all_deps[dep] << job
      end
    end
  end

  job_list.each do |job|
    job.input_dependencies.each do |dep|
      all_deps[dep] ||= []
      all_deps[dep] << job
    end
  end

  missing_deps = []
  all_deps.each do |dep,jobs|
    next if dep.done?
    Log.medium "Producing #{dep.workflow}:#{dep.short_path} dependency for #{Misc.fingerprint jobs}"
    dep.run(true)
    missing_deps << dep
  end if produce_dependencies
  Step.wait_for_jobs missing_deps

  migrate_dependencies = all_deps.keys.collect{|d| [d] + d.rec_dependencies + d.input_dependencies }.flatten.select{|d| d.done? }.collect{|d| d.path }
  Log.low "Migrating #{migrate_dependencies.length} dependencies from #{Misc.fingerprint job_list} to #{ server }" 
  Step.migrate(migrate_dependencies, search_path, :target => server) if migrate_dependencies.any?
end

.upload_inputs(server, inputs, input_types, input_id) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 181

def self.upload_inputs(server, inputs, input_types, input_id)
  TmpFile.with_file do |dir|
    if Step.save_inputs(inputs, input_types, dir)
      # Dir.glob(File.join(dir, "*.as_step")).each do |file|
      #   Log.medium "Migrating Step input #{file} #{ server }" 
      #   path = Open.read(file).strip
      #   new = Step.migrate(path, :user, :target => server)
      #   Open.write(file, new)
      # end

      files = Dir.glob(File.join(dir, "*.as_step"))
      paths = files.collect{|f| Open.read(f).strip }
      new = Step.migrate(paths, :user, :target => server)
      files.zip(new).each{|file,new| Open.write(file, new) }

      SSHLine.run(server, "mkdir -p .rbbt/tmp/tmp-ssh_job_inputs/")
      CMD.cmd_log("scp -r '#{dir}' #{server}:.rbbt/tmp/tmp-ssh_job_inputs/#{input_id}")
    end
  end
end

Instance Method Details

#documentationObject



350
351
352
353
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 350

def documentation
  @documention ||= IndiferentHash.setup(RemoteWorkflow::SSH.get_json(File.join(url, "documentation")))
  @documention
end

#init_remote_tasksObject



395
396
397
398
399
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 395

def init_remote_tasks
  @task_info = IndiferentHash.setup(RemoteWorkflow::SSH.get_json(url))
  @exec_exports = @stream_exports = @synchronous_exports = []
  @asynchronous_exports = @task_info.keys
end

#task_dependenciesObject



385
386
387
388
389
390
391
392
393
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 385

def task_dependencies
  @task_dependencies ||= Hash.new do |hash,task| 
    hash[task] = if exported_tasks.include? task
                   RemoteWorkflow::SSH.get_json(File.join(url, task.to_s, 'dependencies'))
                 else
                   []
                 end
  end
end

#task_info(task) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 355

def task_info(task)
  @task_info ||= IndiferentHash.setup({})

  if @task_info[task].nil?
    task_info = RemoteWorkflow::SSH.get_json(File.join(@base_url || @url, task.to_s))
    task_info = RemoteWorkflow::SSH.fix_hash(task_info)

    task_info[:result_type] = task_info[:result_type].to_sym if task_info[:result_type]
    task_info[:export] = task_info[:export].to_sym if task_info[:export]
    task_info[:input_types] = RemoteWorkflow::SSH.fix_hash(task_info[:input_types], true)
    task_info[:inputs] = task_info[:inputs].collect{|input| input.to_sym }

    @task_info[task] = IndiferentHash.setup(task_info)
  end

  IndiferentHash.setup(@task_info[task])
end

#tasksObject



373
374
375
376
377
378
379
380
381
382
383
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 373

def tasks
  @tasks ||= Hash.new do |hash,task_name| 
    raise Workflow::TaskNotFoundException, "Task #{task_name} not found in workflow #{self.to_s}" unless @task_info.include?(task_name)
    info = @task_info[task_name]
    task = Task.setup info do |*args|
      raise "This is a remote task" 
    end
    task.name = task_name.to_sym
    hash[task_name] = task
  end
end

#workflow_descriptionObject



346
347
348
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 346

def workflow_description
  RemoteWorkflow::SSH.get_raw(File.join(url, 'description'))
end