Class: NERA::RemoteConnector
- Inherits:
-
Object
- Object
- NERA::RemoteConnector
- Defined in:
- lib/nera/nera_remote_connector.rb
Overview
A class for remote connection
Instance Method Summary collapse
- #check_completion(hostname, passphrase = nil) ⇒ Object
- #check_completion_all(passphrase = nil) ⇒ Object
- #download(filenames, hostname, passphrase = nil) ⇒ Object
- #hostnames ⇒ Object
-
#initialize(path_db_folder) ⇒ RemoteConnector
constructor
A new instance of RemoteConnector.
- #show_status(hostname, passphrase = nil) ⇒ Object
- #submit(jobids, hostname, passphrase = nil) ⇒ Object
- #submittable_hostnames ⇒ Object
- #transfer(jobids, hostname, passphrase = nil) ⇒ Object
Constructor Details
#initialize(path_db_folder) ⇒ RemoteConnector
Returns a new instance of RemoteConnector.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/nera/nera_remote_connector.rb', line 23 def initialize( path_db_folder) raise ArgumentError unless path_db_folder.is_a?(String) @db_folders = NERA::DbFolders.new( path_db_folder) if File.exist?( @db_folders.path_to_hosts_yaml) File.open( @db_folders.path_to_hosts_yaml, 'r') do |io| @hosts = YAML.load( io) end else @hosts = [] end raise "Invalid format of hosts.yml" unless valid_hosts? @job_records = NERA::JobRecords.new( @db_folders.path_to_jobs_table) end |
Instance Method Details
#check_completion(hostname, passphrase = nil) ⇒ Object
262 263 264 265 266 267 268 269 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 299 300 301 |
# File 'lib/nera/nera_remote_connector.rb', line 262 def check_completion( hostname, passphrase = nil) raise ArgumentError unless hostname.is_a?(String) host = @hosts.find do |h| h[:name] == hostname end return :connection_failed unless host h = { :port => host[:port] } h[:passphrase] = passphrase if passphrase h[:keys] = host[:keys] if host.has_key?(:keys) ls_str = '' begin $stderr.print "Connecting with #{host[:name]}.....\t" Net::SSH.start( host[:host_url], host[:user], h) do |ssh| ls_str = ssh.exec!("ls #{host[:job_path]}") end rescue Errno::ECONNREFUSED => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { ex.to_s } return :connection_failed rescue Errno::ENETUNREACH => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { ex.to_s } return :connection_failed rescue SocketError => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { ex.to_s } return :connection_failed rescue Net::SSH::Exception => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { ex.to_s } return :connection_failed rescue OpenSSL::PKey::RSAError => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { Net::SSH::Exception } return :connection_failed end $stderr.puts "done" return finished_list( ls_str) end |
#check_completion_all(passphrase = nil) ⇒ Object
316 317 318 319 320 321 322 |
# File 'lib/nera/nera_remote_connector.rb', line 316 def check_completion_all( passphrase = nil) h = {} hostnames.each do |hname| h[hname] = check_completion(hname, passphrase) end return h end |
#download(filenames, hostname, passphrase = nil) ⇒ Object
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/nera/nera_remote_connector.rb', line 325 def download( filenames, hostname, passphrase = nil) raise ArgumentError unless filenames.is_a?(Array) filenames.each do |fname| raise ArgumentError unless fname.is_a?(String) raise ArgumentError unless fname =~ /.tar.bz2$/ end raise ArgumentError unless hostname.is_a?(String) host = @hosts.find do |h| h[:name] == hostname end return :connection_failed unless host h = { :port => host[:port] } h[:passphrase] = passphrase if passphrase h[:keys] = host[:keys] if host.has_key?(:keys) downloaded = [] begin $stderr.puts "Connecting with #{host[:name]}.....\t" Net::SSH.start( host[:host_url], host[:user], h) do |ssh| target_home = ssh.exec!('echo $HOME').chomp target_dir = host[:job_path].sub(/^~/,target_home) ls_str = ssh.exec!("ls #{target_dir}") d_list = filenames.find_all do |fname| ls_str.include?( fname) end ssh.sftp.connect do |sftp| d_list.each do |d_file| #download $stderr.print " downloading #{d_file}.....\t" sftp.download!( target_dir + '/' + d_file, @db_folders.path_to_include_layer + d_file) $stderr.puts "done" downloaded << d_file #delete data file and job script $stderr.print " deleting #{d_file}.....\t" sftp.remove!(target_dir + '/' + d_file) $stderr.puts "done" sh_file = d_file.sub(/tar.bz2$/,'sh') $stderr.print " deleting #{sh_file}.....\t" sftp.remove!(target_dir + '/' + sh_file) $stderr.puts "done" @db_folders.logger.info(self.class.to_s) { "downloaded a data file (#{d_file}) from #{host[:name]}" } end end end rescue Errno::ECONNREFUSED => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { ex.inspect } return :connection_failed rescue Errno::ENETUNREACH => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { ex.inspect } return :connection_failed rescue SocketError => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { ex.inspect } return :connection_failed rescue Net::SSH::Exception => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { ex.inspect } return :connection_failed rescue OpenSSL::PKey::RSAError => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { Net::SSH::Exception } return :connection_failed end $stderr.puts "done" return downloaded end |
#hostnames ⇒ Object
53 54 55 |
# File 'lib/nera/nera_remote_connector.rb', line 53 def hostnames @hosts.map do |host| host[:name] end end |
#show_status(hostname, passphrase = nil) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/nera/nera_remote_connector.rb', line 65 def show_status( hostname, passphrase = nil) found = @hosts.find do |host| host[:name] == hostname end return :connection_failed unless found h = { :port => found[:port], :timeout => 10 } h[:keys] = found[:keys] if found.has_key?(:keys) h[:passphrase] = passphrase if passphrase cmd = 'ps au' cmd = found[:show_status_command] if found.has_key?(:show_status_command) str = '' begin $stderr.print "Connecting with #{found[:name]}.....\t" Net::SSH.start( found[:host_url], found[:user], h) do |ssh| str = ssh.exec!( cmd) end rescue Errno::ECONNREFUSED => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { Errno::ECONNREFUSED } return :connection_failed rescue Errno::ENETUNREACH => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { Errno::ENETUNREACH } return :connection_failed rescue SocketError => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { SocketError } return :connection_failed rescue Net::SSH::Exception => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { Net::SSH::Exception } return :connection_failed rescue OpenSSL::PKey::RSAError => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { Net::SSH::Exception } return :connection_failed end $stderr.puts "done" return str end |
#submit(jobids, hostname, passphrase = nil) ⇒ Object
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 214 215 216 217 218 219 220 221 222 223 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 259 260 |
# File 'lib/nera/nera_remote_connector.rb', line 185 def submit( jobids, hostname, passphrase = nil) raise ArgumentError unless jobids.is_a?(Array) jobids.each do |jid| raise ArgumentError unless jid.is_a?(Integer) end raise ArgumentError unless hostname.is_a?(String) host = @hosts.find do |h| h[:name] == hostname end return :connection_failed unless host return :no_submission_command unless host.has_key?(:submission_command) succeeded_jobids = [] @job_records.transaction { jids = [] jobids.each do |jid| jids << jid if @job_records.find_by_id(jid) end return :no_such_jobs if jids.size == 0 h = { :port => host[:port] } h[:passphrase] = passphrase if passphrase h[:keys] = host[:keys] if host.has_key?(:keys) begin $stderr.print "Connecting with #{host[:name]}.....\t" Net::SSH.start( host[:host_url], host[:user], h) do |ssh| ssh.exec("mkdir -p #{host[:job_path]}") target_home = ssh.exec!('echo $HOME').chomp target_dir = host[:job_path].sub(/^~/,target_home) ssh.sftp.connect do |sftp| jids.each do |jid| pjs = @db_folders.path_to_job_script(jid) next unless File.exist?( pjs) target_path = target_dir + '/' + File.basename(pjs) contents = '' File.open( pjs, 'r') do |io| contents = io.readlines end contents[0] += "cd #{target_dir}\n" sftp.file.open(target_path, "w") do |f| f.puts contents end ssh.exec!("chmod +x #{target_path}") @job_records.update_to_state_copied( jid, host[:name]) ssh.exec!("cd #{target_dir} && #{host[:submission_command]} #{target_path}") @job_records.update_to_state_submitted( jid, host[:name]) @db_folders.logger.info(self.class.to_s) { "submitted a file (#{File.basename(pjs)}) to #{host[:name]}" } succeeded_jobids << jid end end end rescue Errno::ECONNREFUSED => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { ex.inspect } return :connection_failed rescue Errno::ENETUNREACH => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { ex.inspect } return :connection_failed rescue SocketError => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { ex.inspect } return :connection_failed rescue Net::SSH::Exception => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { ex.inspect } return :connection_failed rescue OpenSSL::PKey::RSAError => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { Net::SSH::Exception } return :connection_failed end $stderr.puts "done" } return succeeded_jobids end |
#submittable_hostnames ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/nera/nera_remote_connector.rb', line 57 def submittable_hostnames arr = [] @hosts.each do |host| arr << host[:name] if host[:submission_command] end return arr end |
#transfer(jobids, hostname, passphrase = nil) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/nera/nera_remote_connector.rb', line 106 def transfer( jobids, hostname, passphrase = nil) raise ArgumentError unless jobids.is_a?(Array) jobids.each do |jid| raise ArgumentError unless jid.is_a?(Integer) end raise ArgumentError unless hostname.is_a?(String) host = @hosts.find do |h| h[:name] == hostname end return :connection_failed unless host succeeded_jobids = [] @job_records.transaction { jids = [] jobids.each do |jid| jids << jid if @job_records.find_by_id(jid) end return :no_such_jobs if jids.size == 0 h = { :port => host[:port] } h[:passphrase] = passphrase if passphrase h[:keys] = host[:keys] if host.has_key?(:keys) begin $stderr.print "Connecting with #{host[:name]}.....\t" Net::SSH.start( host[:host_url], host[:user], h) do |ssh| ssh.exec("mkdir -p #{host[:job_path]}") target_home = ssh.exec!('echo $HOME').chomp target_dir = host[:job_path].sub(/^~/,target_home) ssh.sftp.connect do |sftp| jids.each do |jid| pjs = @db_folders.path_to_job_script(jid) target_path = target_dir + '/' + File.basename(pjs) next unless File.exist?( pjs) contents = '' File.open( pjs, 'r') do |io| contents = io.readlines end contents[0] += "cd #{target_dir}\n" sftp.file.open(target_path, "w") do |f| f.puts contents end ssh.exec!("chmod +x #{target_path}") @job_records.update_to_state_copied( jid, host[:name]) @db_folders.logger.info(self.class.to_s) { "transfered a file (#{File.basename(pjs)}) to #{host[:name]}" } succeeded_jobids << jid end end end rescue Errno::ECONNREFUSED => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { ex.inspect } return :connection_failed rescue Errno::ENETUNREACH => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { ex.inspect } return :connection_failed rescue SocketError => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { ex.inspect } return :connection_failed rescue Net::SSH::Exception => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { ex.inspect } return :connection_failed rescue OpenSSL::PKey::RSAError => ex $stderr.puts "failed" @db_folders.logger.error(self.class.to_s) { Net::SSH::Exception } return :connection_failed end $stderr.puts "done" } return succeeded_jobids end |