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



241
242
243
244
245
246
247
248
249
250
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 241

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?
    iif [dep, dep.inputs, dep.real_inputs]
    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_slurm_job(url, input_id, jobname = nil, slurm_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_slurm_job(url, input_id, jobname = nil, slurm_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 "SLURM"
slurm_options = JSON.parse(%q(#{slurm_options.to_json}))
job.clean if job.error? and job.recoverable_error?
HPC::BATCH_MODULE.orchestrate_job(job, slurm_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



282
283
284
285
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 282

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



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 252

def self.relay_job(job, server, options = {})
  migrate, produce, produce_dependencies, search_path, run_type, slurm_options = Misc.process_options options.dup,
    :migrate, :produce, :produce_dependencies, :search_path, :run_type, :slurm_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.slurm_options = slurm_options || {}

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

  rjob
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

.run_slurm_job(url, input_id, jobname = nil, slurm_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_slurm_job(url, input_id, jobname = nil, slurm_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 "SLURM"
slurm_options = JSON.parse(%q(#{slurm_options.to_json}))
job.clean if job.error? and job.recoverable_error?
HPC::BATCH_MODULE.run_job(job, slurm_options) unless job.done? || job.error?
STDOUT.write job.path
  EOF
  Misc.ssh_run(server, script)
end

.upload_dependencies(job, 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



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 223

def self.upload_dependencies(job, server, search_path = 'user', produce_dependencies = false)
  server, path = parse_url(server) if server =~ /^ssh:\/\//
  job.dependencies.each do |dep|
    Log.medium "Producing #{dep.workflow}:#{dep.short_path} dependency for #{job.workflow}:#{job.short_path}"
    dep.produce
  end if produce_dependencies

  job.input_dependencies.each do |dep|
    Log.medium "Producing #{dep.workflow}:#{dep.short_path} dependency for #{job.workflow}:#{job.short_path}"
    dep.produce
  end 

  migrate_dependencies = job.rec_dependencies.select{|d| d.done? }.collect{|d| d.path }
  migrate_dependencies += job.input_dependencies.select{|d| d.done? }.collect{|d| d.path }
  Log.medium "Migrating #{migrate_dependencies.length} dependencies from #{job.path} 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
# 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) }

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

Instance Method Details

#documentationObject



291
292
293
294
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 291

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

#init_remote_tasksObject



336
337
338
339
340
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 336

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



326
327
328
329
330
331
332
333
334
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 326

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



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 296

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



314
315
316
317
318
319
320
321
322
323
324
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 314

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



287
288
289
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 287

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