2016-01-20 15:47:39 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
2015-07-01 04:35:59 +02:00
|
|
|
using XenAPI;
|
|
|
|
using XenAdmin.Core;
|
|
|
|
using System;
|
|
|
|
using System.IO.Pipes;
|
|
|
|
using System.IO;
|
|
|
|
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;
|
2015-07-01 04:35:59 +02:00
|
|
|
|
|
|
|
namespace XenAdmin.Actions
|
|
|
|
{
|
2015-07-29 09:44:41 +02:00
|
|
|
public class TransferHealthCheckSettingsAction : PureAsyncAction
|
2015-07-01 04:35:59 +02:00
|
|
|
{
|
|
|
|
private readonly Pool pool;
|
2015-07-29 09:44:41 +02:00
|
|
|
HealthCheckSettings healthCheckSettings;
|
2015-07-01 04:35:59 +02:00
|
|
|
string username;
|
|
|
|
string password;
|
|
|
|
|
2015-07-29 09:44:41 +02:00
|
|
|
public TransferHealthCheckSettingsAction(Pool pool, HealthCheckSettings healthCheckSettings, string username, string password, bool suppressHistory)
|
|
|
|
: base(pool.Connection, Messages.ACTION_TRANSFER_HEALTHCHECK_SETTINGS, string.Format(Messages.ACTION_TRANSFER_HEALTHCHECK_SETTINGS, pool.Name), suppressHistory)
|
2015-07-01 04:35:59 +02:00
|
|
|
{
|
|
|
|
this.pool = pool;
|
2015-07-29 09:44:41 +02:00
|
|
|
this.healthCheckSettings = healthCheckSettings;
|
2015-07-01 04:35:59 +02:00
|
|
|
this.username = username;
|
|
|
|
this.password = password;
|
|
|
|
}
|
|
|
|
|
|
|
|
private const char SEPARATOR = '\x202f'; // narrow non-breaking space.
|
|
|
|
private string ProtectCredential(string Host, string username, string passwordSecret)
|
|
|
|
{
|
|
|
|
if (username == string.Empty || password == string.Empty)
|
|
|
|
return EncryptionUtils.ProtectForLocalMachine(String.Join(SEPARATOR.ToString(), new[] { Host }));
|
|
|
|
else
|
|
|
|
return EncryptionUtils.ProtectForLocalMachine(String.Join(SEPARATOR.ToString(), new[] { Host, username, passwordSecret }));
|
|
|
|
}
|
|
|
|
|
2015-07-29 09:44:41 +02:00
|
|
|
private const string HEALTHCHECKSERVICENAME = "XenServerHealthCheck";
|
2015-07-14 07:59:35 +02:00
|
|
|
|
2015-07-01 04:35:59 +02:00
|
|
|
protected override void Run()
|
|
|
|
{
|
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
|
|
|
}
|
2015-07-01 12:19:44 +02:00
|
|
|
catch
|
2015-07-01 04:35:59 +02:00
|
|
|
{
|
|
|
|
System.Threading.Thread.Sleep(1000);
|
2015-07-01 12:19:44 +02:00
|
|
|
pipeClient = null;
|
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
|
|
|
|
|
|
|
foreach (Host host in pool.Connection.Cache.Hosts)
|
|
|
|
{
|
|
|
|
if (host.IsMaster())
|
|
|
|
{
|
|
|
|
string credential;
|
2015-07-29 09:44:41 +02:00
|
|
|
if (healthCheckSettings.Status == HealthCheckStatus.Enabled)
|
2015-07-01 04:35:59 +02:00
|
|
|
credential = ProtectCredential(host.address, username, password);
|
|
|
|
else
|
|
|
|
credential = ProtectCredential(host.address, string.Empty, string.Empty);
|
2015-08-24 13:51:46 +02:00
|
|
|
pipeClient.Write(Encoding.UTF8.GetBytes(credential), 0, (Encoding.UTF8.GetBytes(credential)).Length);
|
2015-07-01 04:35:59 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|