2023-01-24 15:29:31 +01:00
|
|
|
|
/* Copyright (c) Cloud Software Group, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
*
|
|
|
|
|
* 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;
|
|
|
|
|
using XenAPI;
|
|
|
|
|
using XenAdmin.Network;
|
|
|
|
|
using XenAdmin.Actions.Wlb;
|
2020-03-19 03:29:08 +01:00
|
|
|
|
using XenAdmin.Core;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenAdmin.Wlb;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Actions
|
|
|
|
|
{
|
2022-12-22 00:02:07 +01:00
|
|
|
|
public class SavePowerOnSettingsAction : AsyncAction
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-03-19 03:29:08 +01:00
|
|
|
|
private readonly List<KeyValuePair<Host, Host.PowerOnMode>> hostModes;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-03-19 03:29:08 +01:00
|
|
|
|
public SavePowerOnSettingsAction(IXenConnection connection, List<KeyValuePair<Host, Host.PowerOnMode>> hostModes, bool suppressHistory)
|
2014-07-21 12:19:04 +02:00
|
|
|
|
: base(connection, Messages.ACTION_CHANGE_POWER_ON, Messages.ACTION_CHANGING_POWER_ON, suppressHistory)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-03-19 03:29:08 +01:00
|
|
|
|
this.hostModes = hostModes;
|
2022-12-22 00:02:07 +01:00
|
|
|
|
|
|
|
|
|
foreach (var kvp in hostModes)
|
|
|
|
|
{
|
|
|
|
|
var mode = kvp.Value.ToString();
|
|
|
|
|
var config = kvp.Value.Config;
|
|
|
|
|
|
|
|
|
|
if (mode == "iLO" && Helpers.StockholmOrGreater(Connection))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if ((mode == "DRAC" || mode == "iLO" || mode != "wake-on-lan" && mode != "") &&
|
|
|
|
|
config.ContainsKey("power_on_password_secret"))
|
|
|
|
|
{
|
|
|
|
|
ApiMethodsToRoleCheck.AddRange(
|
|
|
|
|
"Secret.create",
|
|
|
|
|
"Secret.get_uuid",
|
|
|
|
|
"Secret.get_by_uuid",
|
|
|
|
|
"Secret.destroy");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ApiMethodsToRoleCheck.Add("host.set_power_on_mode");
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Run()
|
|
|
|
|
{
|
2020-03-19 03:29:08 +01:00
|
|
|
|
foreach (var kvp in hostModes)
|
|
|
|
|
SaveConfig(kvp.Key, kvp.Value);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-19 03:29:08 +01:00
|
|
|
|
private void SaveConfig(Host host, Host.PowerOnMode mode)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-03-19 03:29:08 +01:00
|
|
|
|
var newMode = mode.ToString();
|
|
|
|
|
var config = mode.Config;
|
|
|
|
|
|
2020-03-23 05:27:36 +01:00
|
|
|
|
if (newMode == "iLO" && Helpers.StockholmOrGreater(Connection))
|
|
|
|
|
{
|
|
|
|
|
//the UI should have already prevented us from getting here, but be defensive
|
|
|
|
|
newMode = "";
|
|
|
|
|
config = new Dictionary<string, string>();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-22 00:02:07 +01:00
|
|
|
|
string secretuuid = "";
|
2013-06-24 13:41:48 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-03-19 03:29:08 +01:00
|
|
|
|
if (newMode == "DRAC" || newMode == "iLO" || newMode != "wake-on-lan" && newMode != "")
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-03-19 03:29:08 +01:00
|
|
|
|
if (config.ContainsKey("power_on_password_secret"))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-03-19 03:29:08 +01:00
|
|
|
|
secretuuid = Secret.CreateSecret(Session, config["power_on_password_secret"]);
|
|
|
|
|
config["power_on_password_secret"] = secretuuid;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (string.IsNullOrEmpty(newMode))
|
|
|
|
|
{
|
2020-03-19 03:29:08 +01:00
|
|
|
|
//if WLB is on, we need to exclude the host from WLB power management,
|
|
|
|
|
//since we cannot turn it back on if it is powered down automatically
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-03-19 03:29:08 +01:00
|
|
|
|
Pool pool = Helpers.GetPool(Connection);
|
|
|
|
|
if (pool != null && WlbServerState.GetState(pool) == WlbServerState.ServerState.Enabled)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-03-19 03:29:08 +01:00
|
|
|
|
var hostConfig = new WlbHostConfiguration(host.uuid) {ParticipatesInPowerManagement = false};
|
|
|
|
|
|
|
|
|
|
var wlbAction = new SendWlbConfigurationAction(pool, hostConfig.ToDictionary(),
|
|
|
|
|
SendWlbConfigurationKind.SetHostConfiguration);
|
2022-04-05 12:52:32 +02:00
|
|
|
|
wlbAction.RunSync(Session);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-19 03:29:08 +01:00
|
|
|
|
catch
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
//Do nothing on failure.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-19 03:29:08 +01:00
|
|
|
|
Host.set_power_on_mode(Session, host.opaque_ref, newMode, config);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2020-03-19 03:29:08 +01:00
|
|
|
|
catch
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(secretuuid))
|
|
|
|
|
{
|
|
|
|
|
string secretRef = Secret.get_by_uuid(Session, secretuuid);
|
|
|
|
|
Secret.destroy(Session, secretRef);
|
|
|
|
|
}
|
2020-03-19 03:29:08 +01:00
|
|
|
|
|
|
|
|
|
throw;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|