Added functionality. Test Connection + Fix other things

This commit is contained in:
Nicola Carminati
2019-08-21 12:02:31 +02:00
parent 5b4a44dfae
commit 5eb6ad7c7b
12 changed files with 117 additions and 24 deletions
@@ -6,12 +6,12 @@ using System.Threading.Tasks;
namespace Step.CmsConnectGateway.Events
{
public class RebootEventHandlerArgs : EventArgs
public class GatewayRebootEventHandlerArgs : EventArgs
{
public Boolean errorStatus { get; internal set; }
public String errorMessage { get; internal set; }
public RebootEventHandlerArgs()
public GatewayRebootEventHandlerArgs()
{
errorStatus = false;
errorMessage = "";
@@ -14,7 +14,7 @@ using System.Threading.Tasks;
namespace Step.CmsConnectGateway
{
public class CmsConnectAdapter : IDisposable
public class GatewayAdapter : IDisposable
{
//Internal Constant
private const string SSH_SET_PROXY_COMMAND = "sudo ./setProxy.sh ";
@@ -23,6 +23,7 @@ namespace Step.CmsConnectGateway
private const string SSH_SET_NETWORK_COMMAND = "sudo ./setNetwork.sh ";
private const string SSH_GET_NETWORK_COMMAND = "sudo ./getNetworkConfiguration.sh ";
private const string SSH_GET_PROXY_COMMAND = "sudo ./getProxyConfiguration.sh ";
private const string SSH_TEST_CONNECTION_COMMAND = "sudo ./testConnection.sh ";
private const string SSH_GW_REBOOT_COMMAND = "sudo ./gatewayReboot.sh ";
const string IP_ADDR_LABEL = "IP_ADDRESS=";
@@ -32,8 +33,12 @@ namespace Step.CmsConnectGateway
const string PROXY_ADDR_LABEL = "PROXY=";
const string NO_PROXY_LABEL = "NO_PROXY=";
const string UNDEF_VALUE = "none";
const string CONNECTION_OK_VALUE = "OK";
const string CONNECTION_NOWEB_VALUE = "NO_WEB";
const string CONNECTION_NOPORT_VALUE = "NO_PORTS";
const int REBOOT_MINUTES_MAX = 2;
const int REBOOT_MSWAIT_BETWEEN_OP = 500;
//Internal Private Variable
@@ -48,7 +53,7 @@ namespace Step.CmsConnectGateway
/// Simple constructor.
/// Create an instance with this configuration: hostname: localhost, username: root, password: root.
/// </summary>
public CmsConnectAdapter()
public GatewayAdapter()
{
this.host = "localhost";
this.username = "root";
@@ -64,7 +69,7 @@ namespace Step.CmsConnectGateway
/// <param name="username">Username for Gateway Login</param>
/// <param name="password">Password for Gateway Login</param>
/// <exception cref="System.NullReferenceException">Thrown when one parameter is null</exception>
public CmsConnectAdapter(string hostname, string username, string password)
public GatewayAdapter(string hostname, string username, string password)
{
setup(hostname, username, password);
}
@@ -138,19 +143,36 @@ namespace Step.CmsConnectGateway
}
/// <summary>
/// Test connection to SCM/CMS Server, on Gateway.
/// </summary>
/// <param name="timeout">Seconds timeout for the request</param>
/// <exception cref="GatewayException"></exception>
public GatewayConnectionStatus TestGatewayConnection(int timeout)
{
if (timeout < 0)
throw new ArgumentOutOfRangeException("Timeout must be > 0");
return ElaborateTestConnectionCommand(SendSSHCommand(SSH_TEST_CONNECTION_COMMAND + timeout));
}
/// <summary>
/// Reboot asynchronously the Gateway.
/// </summary>
/// <param name="delay">Serconds to delay before reboot</param>
/// <param name="handler">Handler Callback when the operation is finished or an error occours.
/// See <see cref="RebootEventHandlerArgs"/>
/// See <see cref="GatewayRebootEventHandlerArgs"/>
/// </param>
public void RebootGatewayAsync(int delay, EventHandler<RebootEventHandlerArgs> handler)
public void RebootGatewayAsync(int delay, EventHandler<GatewayRebootEventHandlerArgs> handler)
{
if (delay < 0)
throw new ArgumentOutOfRangeException("Delay must be > 0");
//Create the Action
Action<int, EventHandler<RebootEventHandlerArgs>> action = (int del, EventHandler<RebootEventHandlerArgs> hand) =>
Action<int, EventHandler<GatewayRebootEventHandlerArgs>> action = (int del, EventHandler<GatewayRebootEventHandlerArgs> hand) =>
{
RebootEventHandlerArgs ev = new RebootEventHandlerArgs();
GatewayRebootEventHandlerArgs ev = new GatewayRebootEventHandlerArgs();
try
{
this.SendSSHReboot(del);
@@ -176,6 +198,9 @@ namespace Step.CmsConnectGateway
/// <exception cref="GatewayException"></exception>
public void RebootGateway(int delay)
{
if (delay < 0)
throw new ArgumentOutOfRangeException("Delay must be > 0");
//Send Command
this.SendSSHReboot(delay);
}
@@ -314,6 +339,17 @@ namespace Step.CmsConnectGateway
return configuration;
}
private GatewayConnectionStatus ElaborateTestConnectionCommand(List<string> lines)
{
GatewayConnectionStatus status = new GatewayConnectionStatus();
foreach (string line in lines)
{
if (!String.IsNullOrWhiteSpace(line))
DecodeConnectionStatus(line.Trim(), ref status);
}
return status;
}
#endregion
//---------------------------------------------------------------------------------------------------
@@ -512,6 +548,20 @@ namespace Step.CmsConnectGateway
}
}
private void DecodeConnectionStatus(string stringValue, ref GatewayConnectionStatus status)
{
if (stringValue == CONNECTION_OK_VALUE)
status = GatewayConnectionStatus.OK;
else if (stringValue == CONNECTION_NOWEB_VALUE)
status = GatewayConnectionStatus.WEB_ERROR;
else if (stringValue == CONNECTION_NOPORT_VALUE)
status = GatewayConnectionStatus.PORT_ERROR;
else
throw new GatewayException("Internal Gateway Error during Connection-Test (bad format): " + stringValue);
}
private bool EvaluateIPV4(string stringValue,out IPAddress address)
{
Regex rgx = new Regex(@"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
@@ -525,8 +575,6 @@ namespace Step.CmsConnectGateway
address = new IPAddress(0);
return false;
}
#endregion
//---------------------------------------------------------------------------------------------------
@@ -617,16 +665,59 @@ namespace Step.CmsConnectGateway
//Create the Instance
SshClient _sshGateway = new SshClient(this.host, this.username, this.password);
bool connected = false;
//Try Connection Cycle
//Phase 1 Wait Disconnection Cycle
bool disconnected = false;
do
{
try
{
//If TimeSpan > TOT MIN -> Exception
if ((DateTime.Now - nowAfterReboot) > TimeSpan.FromMinutes(REBOOT_MINUTES_MAX))
throw new GatewayException("Timeout Error during reboot - Gateway has never been rebooted");
//Try Connection
_sshGateway.Connect();
Thread.Sleep(REBOOT_MSWAIT_BETWEEN_OP);
_sshGateway.Disconnect();
}
catch (SocketException e)
{
//Not Connected
disconnected = true;
}
catch (SshConnectionException e)
{
//Not Connected
disconnected = true;
}
catch (SshException e)
{
//Not Connected
disconnected = true;
}
catch (GatewayException e)
{
//Error
throw new GatewayException(e.Message);
}
catch (Exception e)
{
//Error
throw new GatewayException("Unknown Error during reboot: " + e);
}
} while (!disconnected);
//Phase 2 Wait Re-connection Cycle
bool connected = false;
do
{
try
{
//If TimeSpan > TOT MIN -> Exception
if((DateTime.Now - nowAfterReboot) > TimeSpan.FromMinutes(REBOOT_MINUTES_MAX))
throw new GatewayException("Timeout Error during reboot");
throw new GatewayException("Timeout Error during reboot - Gateway not found during reboot");
//Try Connection
_sshGateway.Connect();
@@ -635,17 +726,17 @@ namespace Step.CmsConnectGateway
catch (SocketException e)
{
//Not Connected
Thread.Sleep(500);
Thread.Sleep(REBOOT_MSWAIT_BETWEEN_OP);
}
catch (SshConnectionException e)
{
//Not Connected
Thread.Sleep(500);
Thread.Sleep(REBOOT_MSWAIT_BETWEEN_OP);
}
catch (SshException e)
{
//Not Connected
Thread.Sleep(500);
Thread.Sleep(REBOOT_MSWAIT_BETWEEN_OP);
}
catch (GatewayException e)
{
@@ -7,7 +7,9 @@ using System.Net;
namespace Step.CmsConnectGateway
{
public enum GatewayConnectionStatus { OK = 0, WEB_ERROR = 1, PORT_ERROR = 2 };
public class GatewayNetworkConfiguration
{
@@ -7,8 +7,8 @@
<ProjectGuid>{49B04D99-0ECD-4900-86D3-7098D61314D7}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Step.CmsConnect</RootNamespace>
<AssemblyName>Step.CmsConnect</AssemblyName>
<RootNamespace>Step.CmsConnectGateway</RootNamespace>
<AssemblyName>Step.CmsConnectGateway</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
@@ -47,8 +47,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Builders\iBuilder.cs" />
<Compile Include="CmsConnectAdapter.cs" />
<Compile Include="Events\RebootEventHandlerArgs.cs" />
<Compile Include="GatewayAdapter.cs" />
<Compile Include="Events\GatewayRebootEventHandlerArgs.cs" />
<Compile Include="Exceptions\GatewayException.cs" />
<Compile Include="Models\GatewayConfiguration.cs" />
<Compile Include="Builders\GatewayProxyConfigurationBuilder.cs" />
+1 -1
View File
@@ -37,7 +37,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step.NC", "Step.NC\Step.NC.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client.Chromium", "Client.Chromium\Client.Chromium.csproj", "{F9F17F23-660E-488C-A7FA-6A5B35D64313}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step.CmsConnectGateway", "Step.CmsConnect\Step.CmsConnectGateway.csproj", "{49B04D99-0ECD-4900-86D3-7098D61314D7}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step.CmsConnectGateway", "Step.CmsConnectGateway\Step.CmsConnectGateway.csproj", "{49B04D99-0ECD-4900-86D3-7098D61314D7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
+1 -1
View File
@@ -16122,7 +16122,7 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Step.CmsConnect\Step.CmsConnectGateway.csproj">
<ProjectReference Include="..\Step.CmsConnectGateway\Step.CmsConnectGateway.csproj">
<Project>{49b04d99-0ecd-4900-86d3-7098d61314d7}</Project>
<Name>Step.CmsConnectGateway</Name>
</ProjectReference>