2017-01-16 20:59:50 +01:00
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2015-07-10 14:36:26 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms,
|
|
|
|
* with or without modification, are permitted provided
|
|
|
|
* that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
* following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
* following disclaimer in the documentation and/or other
|
|
|
|
* materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2015-07-01 04:35:59 +02:00
|
|
|
using XenAPI;
|
|
|
|
using XenAdmin.Core;
|
|
|
|
using System;
|
|
|
|
using System.IO.Pipes;
|
|
|
|
using System.Text;
|
2015-07-14 07:59:35 +02:00
|
|
|
using System.ServiceProcess;
|
2015-08-21 16:18:18 +02:00
|
|
|
using XenAdmin.Model;
|
2017-06-26 23:06:45 +02:00
|
|
|
using XenAdmin.Network;
|
2015-07-01 04:35:59 +02:00
|
|
|
|
|
|
|
namespace XenAdmin.Actions
|
|
|
|
{
|
2017-06-26 23:06:45 +02:00
|
|
|
public abstract class TransferDataToHealthCheckAction : PureAsyncAction
|
2015-07-01 04:35:59 +02:00
|
|
|
{
|
2017-06-26 23:06:45 +02:00
|
|
|
protected const char SEPARATOR = '\x202f'; // narrow non-breaking space.
|
|
|
|
private const string HEALTHCHECKSERVICENAME = "XenServerHealthCheck";
|
2015-07-01 04:35:59 +02:00
|
|
|
|
2017-06-26 23:06:45 +02:00
|
|
|
public TransferDataToHealthCheckAction(IXenConnection connection, string title, string description, bool suppressHistory)
|
|
|
|
: base(connection, title, description, suppressHistory)
|
2015-07-01 04:35:59 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Run()
|
|
|
|
{
|
2017-06-26 23:06:45 +02:00
|
|
|
var message = GetMessageToBeSent();
|
|
|
|
SendMessageToHealthCheck(message);
|
|
|
|
}
|
2017-05-26 17:54:38 +02:00
|
|
|
|
2017-06-26 23:06:45 +02:00
|
|
|
protected abstract string GetMessageToBeSent();
|
|
|
|
|
|
|
|
private void SendMessageToHealthCheck(string message)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(message))
|
|
|
|
return;
|
2017-05-26 17:54:38 +02:00
|
|
|
|
2015-07-29 09:44:41 +02:00
|
|
|
ServiceController sc = new ServiceController(HEALTHCHECKSERVICENAME);
|
2016-01-20 15:47:39 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (sc.Status != ServiceControllerStatus.Running)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2015-07-14 07:59:35 +02:00
|
|
|
return;
|
2016-01-20 15:47:39 +01:00
|
|
|
}
|
2015-07-14 07:59:35 +02:00
|
|
|
|
2015-07-29 09:44:41 +02:00
|
|
|
NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", HealthCheckSettings.HEALTH_CHECK_PIPE, PipeDirection.Out);
|
2015-07-01 04:35:59 +02:00
|
|
|
int retryCount = 120;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-07-01 12:19:44 +02:00
|
|
|
pipeClient.Connect(0);
|
|
|
|
}
|
|
|
|
catch (System.TimeoutException exp)
|
|
|
|
{
|
|
|
|
throw exp;
|
2015-07-01 04:35:59 +02:00
|
|
|
}
|
2017-06-26 23:06:45 +02:00
|
|
|
catch
|
2015-07-01 04:35:59 +02:00
|
|
|
{
|
|
|
|
System.Threading.Thread.Sleep(1000);
|
2015-07-29 09:44:41 +02:00
|
|
|
pipeClient = new NamedPipeClientStream(".", HealthCheckSettings.HEALTH_CHECK_PIPE, PipeDirection.Out);
|
2015-07-01 04:35:59 +02:00
|
|
|
}
|
2015-07-01 12:19:44 +02:00
|
|
|
} while (!pipeClient.IsConnected && retryCount-- != 0);
|
2015-07-01 04:35:59 +02:00
|
|
|
|
2017-06-26 23:06:45 +02:00
|
|
|
var bytes = Encoding.UTF8.GetBytes(message);
|
|
|
|
pipeClient.Write(bytes, 0, bytes.Length);
|
2015-07-01 04:35:59 +02:00
|
|
|
|
2015-07-29 09:44:41 +02:00
|
|
|
pipeClient.Write(Encoding.UTF8.GetBytes(HealthCheckSettings.HEALTH_CHECK_PIPE_END_MESSAGE), 0, HealthCheckSettings.HEALTH_CHECK_PIPE_END_MESSAGE.Length);
|
2015-07-01 04:35:59 +02:00
|
|
|
pipeClient.Close();
|
|
|
|
}
|
|
|
|
}
|
2017-06-26 23:06:45 +02:00
|
|
|
|
|
|
|
public class TransferHealthCheckSettingsAction : TransferDataToHealthCheckAction
|
|
|
|
{
|
|
|
|
private readonly Pool pool;
|
|
|
|
HealthCheckSettings healthCheckSettings;
|
|
|
|
string username;
|
|
|
|
string password;
|
|
|
|
|
|
|
|
public TransferHealthCheckSettingsAction(Pool pool, HealthCheckSettings healthCheckSettings, string username, string password, bool suppressHistory)
|
2017-09-03 04:33:29 +02:00
|
|
|
: base(pool.Connection, Messages.ACTION_TRANSFER_HEALTHCHECK_SETTINGS, string.Format(Messages.ACTION_TRANSFER_HEALTHCHECK_SETTINGS, pool.Name()), suppressHistory)
|
2017-06-26 23:06:45 +02:00
|
|
|
{
|
|
|
|
this.pool = pool;
|
|
|
|
this.healthCheckSettings = healthCheckSettings;
|
|
|
|
this.username = username;
|
|
|
|
this.password = password;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override string GetMessageToBeSent()
|
|
|
|
{
|
|
|
|
var host = Helpers.GetMaster(pool.Connection);
|
|
|
|
if (host == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
if (healthCheckSettings.Status == HealthCheckStatus.Enabled && (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)))
|
|
|
|
return null; // do not send empty/null username or password (when the Health Check is enabled), as they will be ignored
|
|
|
|
|
|
|
|
var credential = healthCheckSettings.Status == HealthCheckStatus.Enabled
|
|
|
|
? String.Join(SEPARATOR.ToString(), host.address, username, password)
|
|
|
|
: String.Join(SEPARATOR.ToString(), host.address);
|
|
|
|
return EncryptionUtils.ProtectForLocalMachine(credential);
|
|
|
|
}
|
|
|
|
}
|
2015-07-01 04:35:59 +02:00
|
|
|
}
|