Files
LeafWeb/Core/Remote/PiscalSshClient.cs
T
poprhythm 10cd2986cf Organize Piscal Service items
Add nlog, PiscalQueueManager

	modified:   Core.Tests/Remote/PiscalSshClientTests.cs
	modified:   Core/DAL/DataService.cs
	modified:   Core/Remote/IPiscalClient.cs
	modified:   Core/Remote/PiscalSshClient.cs
	new file:   Web/Attributes/ActionLogAttribute.cs
	modified:   Web/Controllers/ControllerBase.cs
	modified:   Web/Controllers/LeafInputController.cs
	modified:   Web/Controllers/LeafOutputController.cs
	new file:   Web/NLog.config
	new file:   Web/NLog.xsd
	new file:   Web/Services/PiscalQueueManager.cs
	modified:   Web/Services/PiscalService.cs
	modified:   Web/Startup.cs
	modified:   Web/Web.csproj
	modified:   Web/packages.config

Ignore logs

Organize piscal queue manager

	modified:   Core/Entities/LeafInputFile.cs
	modified:   Web/Controllers/LeafInputController.cs
	renamed:    Web/Startup.cs -> Web/HangfireStartup.cs
	modified:   Web/Services/PiscalQueueManager.cs
	modified:   Web/Web.csproj

cleanup usings in leafinputcontroller
2016-03-01 07:35:58 -05:00

136 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using LeafWeb.Core.Utility;
using Renci.SshNet;
namespace LeafWeb.Core.Remote
{
public class PiscalSshClient : IPiscalClient
{
private const string BaseDirectory = "./LeafWeb";
private const string RemoteScriptPath = BaseDirectory + "/piscal_manager.sh";
private readonly PasswordConnectionInfo _connectionInfo;
public PiscalSshClient(string connectionString)
{
var conn = new DbConnectionStringBuilder {ConnectionString = connectionString};
var host = conn["host"] as string;
var username = conn["username"] as string;
var password = conn["password"] as string;
_connectionInfo = new PasswordConnectionInfo(host, username, password);
}
private SshClient GetSshClient()
{
return new SshClient(_connectionInfo);
}
private ScpClient GetScpClient()
{
return new ScpClient(_connectionInfo);
}
public void RunLeafInputFile(PiscalLeafInputFile file)
{
var inputPath = $"{BaseDirectory}/{file.DirectoryName}/{file.Filename}";
// copy file
using (var scp = GetScpClient())
using (var stream = new MemoryStream(file.Contents))
{
Console.WriteLine(inputPath);
scp.Connect();
scp.Upload(stream, inputPath);
scp.Disconnect();
}
// begin processing
using (var ssh = GetSshClient())
{
ssh.Connect();
var commandText = $"{RemoteScriptPath} -d {file.DirectoryName} -f {file.Filename}";
var command = ssh.CreateCommand(commandText);
command.Execute();
ssh.Disconnect();
if (command.ExitStatus != 0)
throw new PiscalClientException(command.Error);
Console.Write(command.Result);
}
}
public PiscalStatus GetLeafInputFileStatus(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 command = ssh.CreateCommand(commandText);
command.Execute();
ssh.Disconnect();
if (command.ExitStatus != 0)
throw new PiscalClientException(command.Error);
return command.Result
.SplitNewLine()
.Where(s => s.Length > 0)
.Select(s => s.Trim()).ToArray();
}
}
/// <summary>
/// Gets the leaf output from piscal, only run on if result status is success
/// </summary>
public IEnumerable<PiscalLeafOutputFile> RetrieveLeafOutput(PiscalLeafInputFile file)
{
// get output files
var status = GetLeafInputStatusRaw(file);
if (status[0] != "success")
throw new PiscalClientException("output not available, status is " + status[0]);
var filePaths = status.Skip(1);
using (var scp = GetScpClient())
{
scp.Connect();
foreach (var filePath in filePaths)
{
using (var stream = new MemoryStream())
{
scp.Download(filePath, stream);
yield return
new PiscalLeafOutputFile
{
Contents = stream.ToArray(),
Filename = filePath.FilenameFromPath(),
DirectoryName = file.DirectoryName
};
}
}
scp.Disconnect();
}
}
}
}