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, #getCommandOutput, #getContentTree, #listFileHashes

Constructor Details

#initialize(host, hashCommand, shell, scpProgram) ⇒ SshContentHost

Returns a new instance of SshContentHost.



199
200
201
202
203
204
205
# File 'lib/synqa.rb', line 199

def initialize(host, hashCommand, shell, scpProgram)
  super(hashCommand)
  @host = host
  @shell = shell.is_a?(String) ? [shell] : shell
  @scpProgram = scpProgram.is_a?(String) ? [scpProgram] : scpProgram
  @scpCommandString = @scpProgram.join(" ")
end

Instance Attribute Details

#hostObject (readonly)

The remote host, e.g. “[email protected]



194
195
196
# File 'lib/synqa.rb', line 194

def host
  @host
end

#scpCommandStringObject (readonly)

The SCP command as a string



197
198
199
# File 'lib/synqa.rb', line 197

def scpCommandString
  @scpCommandString
end

#scpProgramObject (readonly)

The SCP client, e.g. [“scp”] or [“pscp”,“-pw”,“mysecretpassword”] (i.e. command + args as an array)



191
192
193
# File 'lib/synqa.rb', line 191

def scpProgram
  @scpProgram
end

#shellObject (readonly)

The SSH client, e.g. [“ssh”] or [“plink”,“-pw”,“mysecretpassword”] (i.e. command + args as an array)



188
189
190
# File 'lib/synqa.rb', line 188

def shell
  @shell
end

Instance Method Details

#executeRemoteCommand(commandString, dryRun = false) ⇒ Object

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



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/synqa.rb', line 215

def executeRemoteCommand(commandString, dryRun = false)
  puts "SSH #{host} (#{shell.join(" ")}): executing #{commandString}"
  if not dryRun
    output = getCommandOutput(shell + [host, commandString])
    while (line = output.gets)
      yield line.chomp
    end
    output.close()
    checkProcessStatus("SSH #{host} #{commandString}")
  end
end

#getScpPath(path) ⇒ Object

Get the remote path of the directory or file on the host, in the format required by SCP



272
273
274
# File 'lib/synqa.rb', line 272

def getScpPath(path)
  return host + ":" + path
end

#listDirectories(baseDir) ⇒ Object

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



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/synqa.rb', line 236

def listDirectories(baseDir)
  baseDir = normalisedDir(baseDir)
  puts "Listing directories ..."
  directories = []
  baseDirLen = baseDir.length
  executeRemoteCommand(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



254
255
256
257
258
259
260
261
# File 'lib/synqa.rb', line 254

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

#listFiles(baseDir) ⇒ Object

List all files within the base directory to stdout



264
265
266
267
268
269
# File 'lib/synqa.rb', line 264

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

#locationDescriptor(baseDir) ⇒ Object

Return readable description of base directory on remote system



208
209
210
211
# File 'lib/synqa.rb', line 208

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

#ssh(commandString, dryRun = false) ⇒ Object

execute an SSH command on the remote system, displaying output to stdout, (or don’t actually execute, if dryRun is true)



229
230
231
232
233
# File 'lib/synqa.rb', line 229

def ssh(commandString, dryRun = false)
  executeRemoteCommand(commandString, dryRun) do |line|
    puts line
  end
end