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



139
140
141
142
143
144
145
146
147
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 139

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



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

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

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

  JSON.parse(Misc.ssh_run(server, script))
end

.get_raw(url, params) ⇒ Object



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

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



70
71
72
73
74
75
76
77
78
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 70

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

.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



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

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

.path_script(path) ⇒ Object



20
21
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
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 20

def self.path_script(path) 

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

  script =<<-EOF
require 'rbbt/workflow'
wf = Workflow.require_workflow "#{workflow}"
  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



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

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



215
216
217
218
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 215

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

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

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

end



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 184

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

  search_path ||= 'user'

  produce = true if migrate

  workflow_name = job.workflow.to_s
  inputs = job.inputs.to_hash
  job.dependencies.each do |dep|
    dep.produce if options[:produce_dependencies]
    next unless dep.done?

    Step.migrate(dep.path, search_path, :target => server)
  end

  remote_workflow = RemoteWorkflow.new("ssh://#{server}:#{job.workflow.to_s}", "#{job.workflow.to_s}")
  rjob = remote_workflow.job(job.task_name.to_s, job.clean_name, inputs)

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

  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



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

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.produce
STDOUT.write job.path
  EOF
  Misc.ssh_run(server, script)
end

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



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

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

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

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



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 149

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|
        path = Open.read(file).strip
        new = Step.migrate(path, :user, :target => server)
        Open.write(file, new)
      end
      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



224
225
226
227
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 224

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

#init_remote_tasksObject



269
270
271
272
273
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 269

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



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

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



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 229

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

  if @task_info[task].nil?
    task_info = RemoteWorkflow::SSH.get_json(File.join(@base_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



247
248
249
250
251
252
253
254
255
256
257
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 247

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



220
221
222
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 220

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