Retrieve LeafOutput operational
This commit is contained in:
@@ -11,7 +11,7 @@ namespace LeafWeb.Core.Remote
|
||||
public class PiscalSshClient : IPiscalClient
|
||||
{
|
||||
private const string BaseDirectory = "./LeafWeb";
|
||||
private const string RemoteScriptPath = "./piscal_manager.sh";
|
||||
private const string RemoteScriptPath = BaseDirectory + "/piscal_manager.sh";
|
||||
private readonly PasswordConnectionInfo _connectionInfo;
|
||||
|
||||
public PiscalSshClient(string connectionString)
|
||||
@@ -53,61 +53,76 @@ namespace LeafWeb.Core.Remote
|
||||
{
|
||||
ssh.Connect();
|
||||
var commandText = $"{RemoteScriptPath} -d {file.DirectoryName} -f {file.Filename}";
|
||||
var exeCommand = ssh.CreateCommand(commandText);
|
||||
exeCommand.Execute();
|
||||
var command = ssh.CreateCommand(commandText);
|
||||
command.Execute();
|
||||
ssh.Disconnect();
|
||||
|
||||
Console.Write(exeCommand.Result);
|
||||
if (command.ExitStatus != 0)
|
||||
throw new PiscalClientException(command.Error);
|
||||
|
||||
if (exeCommand.ExitStatus != 0)
|
||||
throw new PiscalClientException(exeCommand.Error);
|
||||
Console.Write(command.Result);
|
||||
}
|
||||
}
|
||||
|
||||
public PiscalStatus GetLeafInputStatus(PiscalLeafInputFile file)
|
||||
{
|
||||
var statusRaw = GetLeafInputStatusRaw(file);
|
||||
|
||||
switch (statusRaw[0])
|
||||
{
|
||||
case "running":
|
||||
return PiscalStatus.Running;
|
||||
case "success":
|
||||
return PiscalStatus.Success;
|
||||
default:
|
||||
return PiscalStatus.Error;
|
||||
}
|
||||
}
|
||||
|
||||
private string[] GetLeafInputStatusRaw(PiscalLeafInputFile file)
|
||||
{
|
||||
using (var ssh = GetSshClient())
|
||||
{
|
||||
ssh.Connect();
|
||||
var commandText = $"{RemoteScriptPath} -d {file.DirectoryName} -s";
|
||||
var exeCommand = ssh.CreateCommand(commandText);
|
||||
exeCommand.Execute();
|
||||
var command = ssh.CreateCommand(commandText);
|
||||
command.Execute();
|
||||
ssh.Disconnect();
|
||||
|
||||
Console.Write(exeCommand.Result);
|
||||
if (command.ExitStatus != 0)
|
||||
throw new PiscalClientException(command.Error);
|
||||
|
||||
return command.Result
|
||||
.SplitNewLine()
|
||||
.Where(s => s.Length > 0)
|
||||
.Select(s => s.Trim()).ToArray();
|
||||
}
|
||||
return PiscalStatus.Success;
|
||||
}
|
||||
|
||||
public IEnumerable<PiscalLeafOutputFile> RetrieveLeafInputResult(PiscalLeafInputFile file)
|
||||
public IEnumerable<PiscalLeafOutputFile> RetrieveLeafOutput(PiscalLeafInputFile file)
|
||||
{
|
||||
var outputDirectory = $"{BaseDirectory}/{file.DirectoryName}";
|
||||
|
||||
string[] filenames;
|
||||
|
||||
// get output files
|
||||
using (var ssh = GetSshClient())
|
||||
{
|
||||
ssh.Connect();
|
||||
var commandText = $"ls -1 {outputDirectory}";
|
||||
var lsCommand = ssh.CreateCommand(commandText);
|
||||
lsCommand.Execute();
|
||||
ssh.Disconnect();
|
||||
var status = GetLeafInputStatusRaw(file);
|
||||
if (status[0] != "success")
|
||||
throw new PiscalClientException("output not available, status is " + status[0]);
|
||||
|
||||
filenames = lsCommand.Result.SplitNewLine().Where(s => s.Length > 0).Select(s => s.Trim()).ToArray();
|
||||
Console.Write(lsCommand.Result);
|
||||
}
|
||||
var filePaths = status.Skip(1);
|
||||
|
||||
using (var scp = GetScpClient())
|
||||
{
|
||||
scp.Connect();
|
||||
foreach (var filename in filenames)
|
||||
foreach (var filePath in filePaths)
|
||||
{
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
var filePath = $"{BaseDirectory}/{file.DirectoryName}/{filename}";
|
||||
scp.Download(filePath, stream);
|
||||
yield return new PiscalLeafOutputFile {Contents = stream.ToArray(), Filename = filename};
|
||||
yield return
|
||||
new PiscalLeafOutputFile
|
||||
{
|
||||
Contents = stream.ToArray(),
|
||||
Filename = filePath.FilenameFromPath(),
|
||||
DirectoryName = file.DirectoryName
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user