Class: Synqa::SshContentHost

Inherits:
DirContentHost show all
Defined in:
lib/synqa.rb

Overview

Representation of a remote system accessible via SSH

Instance Attribute Summary collapse

Attributes inherited from DirContentHost

#hashCommand, #pathPrefix

Instance Method Summary collapse

Methods inherited from DirContentHost

#findDirectoriesCommand, #findFilesCommand, #getContentTree, #listFileHashes

Constructor Details

#initialize(userAtHost, hashCommand, sshAndScp = nil) ⇒ SshContentHost

Returns a new instance of SshContentHost.



330
331
332
333
334
# File 'lib/synqa.rb', line 330

def initialize(userAtHost, hashCommand, sshAndScp = nil)
  super(hashCommand)
  @sshAndScp = sshAndScp != nil ?  sshAndScp : InternalSshScp.new()
  @sshAndScp.setUserAtHost(userAtHost)
end

Instance Attribute Details

#sshAndScpObject (readonly)

The remote SSH/SCP login, e.g. SSH via “[email protected]



328
329
330
# File 'lib/synqa.rb', line 328

def sshAndScp
  @sshAndScp
end

Instance Method Details

#closeConnectionsObject



340
341
342
# File 'lib/synqa.rb', line 340

def closeConnections()
  @sshAndScp.close()
end

#copyLocalFileToRemoteDirectory(sourcePath, destinationPath, dryRun) ⇒ Object

copy a local file to a remote directory, if dryRun is false



374
375
376
# File 'lib/synqa.rb', line 374

def copyLocalFileToRemoteDirectory(sourcePath, destinationPath, dryRun)
  sshAndScp.copyLocalFileToRemoteDirectory(sourcePath, destinationPath, dryRun)
end

#copyLocalToRemoteDirectory(sourcePath, destinationPath, dryRun) ⇒ Object

copy a local directory to a remote directory, if dryRun is false



369
370
371
# File 'lib/synqa.rb', line 369

def copyLocalToRemoteDirectory(sourcePath, destinationPath, dryRun)
  sshAndScp.copyLocalToRemoteDirectory(sourcePath, destinationPath, dryRun)
end

#deleteDirectory(dirPath, dryRun) ⇒ Object

delete a remote directory, if dryRun is false



359
360
361
# File 'lib/synqa.rb', line 359

def deleteDirectory(dirPath, dryRun)
  sshAndScp.deleteDirectory(dirPath, dryRun)
end

#deleteFile(filePath, dryRun) ⇒ Object

delete a remote file, if dryRun is false



364
365
366
# File 'lib/synqa.rb', line 364

def deleteFile(filePath, dryRun)
  sshAndScp.deleteFile(filePath, dryRun)
end

#listDirectories(baseDir) ⇒ Object

Return a list of all subdirectories of the base directory (as paths relative to the base directory)



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/synqa.rb', line 379

def listDirectories(baseDir)
  baseDir = normalisedDir(baseDir)
  puts "Listing directories ..."
  directories = []
  baseDirLen = baseDir.length
  ssh(findDirectoriesCommand(baseDir).join(" ")) do |line|
    puts " #{line}"
    if line.start_with?(baseDir)
      directories << line[baseDirLen..-1]
    else
      raise "Directory #{line} is not a sub-directory of base directory #{baseDir}"
    end
  end
  return directories
end

#listFileHashLines(baseDir) ⇒ Object

Yield lines of output from the command to display hash values and file names of all files within the base directory



397
398
399
400
401
402
403
404
# File 'lib/synqa.rb', line 397

def listFileHashLines(baseDir)
  baseDir = normalisedDir(baseDir)
  remoteFileHashLinesCommand = findFilesCommand(baseDir) + ["|", "xargs", "-r"] + @hashCommand.command
  ssh(remoteFileHashLinesCommand.join(" ")) do |line| 
    puts " #{line}"
    yield line 
  end
end

#listFiles(baseDir) ⇒ Object

List all files within the base directory to stdout



407
408
409
410
411
412
# File 'lib/synqa.rb', line 407

def listFiles(baseDir)
  baseDir = normalisedDir(baseDir)
  ssh(findFilesCommand(baseDir).join(" ")) do |line| 
    puts " #{line}"
  end
end

#locationDescriptor(baseDir) ⇒ Object

Return readable description of base directory on remote system



345
346
347
348
# File 'lib/synqa.rb', line 345

def locationDescriptor(baseDir)
  baseDir = normalisedDir(baseDir)
  return "#{userAtHost}:#{baseDir} (connect = #{shell}/#{scpProgram}, hashCommand = #{hashCommand})"
end

#ssh(commandString, dryRun = false) ⇒ Object

execute an SSH command on the remote system, yielding lines of output (or don’t actually execute, if dryRun is false)



352
353
354
355
356
# File 'lib/synqa.rb', line 352

def ssh(commandString, dryRun = false)
  sshAndScp.ssh(commandString, dryRun) do |line|
    yield line
  end
end

#userAtHostObject



336
337
338
# File 'lib/synqa.rb', line 336

def userAtHost
  return @sshAndScp.userAtHost
end