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



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

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.name
  EOF
  @name = Misc.ssh_run(server, script)
end

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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 156

def self.relay(workflow, task, jobname, inputs, server, options = {})
  options = Misc.add_defaults options, :search_path => 'user'
  search_path = options[:search_path]

  job = workflow.job(task, jobname, inputs)

  job.dependencies.each do |dep| 
    dep.produce 
  end

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

  job.dependencies.each do |dep| 
    Step.migrate(dep.path, search_path, :target => server)
  end

  remote = RemoteWorkflow.new("ssh://#{server}:#{workflow.to_s}", "#{workflow.to_s}")
  rjob = remote.job(task, jobname, {})
  rjob.override_dependencies = override_dependencies
  rjob.run
end

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



114
115
116
117
118
119
120
121
122
123
124
# 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
job.produce
STDOUT.write job.path
  EOF
  Misc.ssh_run(server, script)
end

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



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

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



148
149
150
151
152
153
154
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 148

def self.upload_inputs(server, inputs, input_types, input_id)
  TmpFile.with_file do |dir|
    if Step.save_inputs(inputs, input_types, dir)
      CMD.cmd("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



182
183
184
185
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 182

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

#init_remote_tasksObject



227
228
229
230
231
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 227

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



217
218
219
220
221
222
223
224
225
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 217

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



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 187

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



205
206
207
208
209
210
211
212
213
214
215
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 205

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



178
179
180
# File 'lib/rbbt/workflow/remote_workflow/driver/ssh.rb', line 178

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