Add Quota for Hyper-V replication
This commit is contained in:
parent
bf3ec75c08
commit
054241f84e
13 changed files with 321 additions and 161 deletions
|
@ -0,0 +1,62 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace WebsitePanel.EnterpriseServer.Code.Virtualization2012
|
||||
{
|
||||
public static class QuotaHelper
|
||||
{
|
||||
public static void CheckNumericQuota(PackageContext cntx, List<string> errors, string quotaName, long currentVal, long val, string messageKey)
|
||||
{
|
||||
CheckQuotaValue(cntx, errors, quotaName, currentVal, val, messageKey);
|
||||
}
|
||||
public static void CheckNumericQuota(PackageContext cntx, List<string> errors, string quotaName, int currentVal, int val, string messageKey)
|
||||
{
|
||||
CheckQuotaValue(cntx, errors, quotaName, Convert.ToInt64(currentVal), Convert.ToInt64(val), messageKey);
|
||||
}
|
||||
|
||||
public static void CheckNumericQuota(PackageContext cntx, List<string> errors, string quotaName, int val, string messageKey)
|
||||
{
|
||||
CheckQuotaValue(cntx, errors, quotaName, 0, val, messageKey);
|
||||
}
|
||||
|
||||
public static void CheckBooleanQuota(PackageContext cntx, List<string> errors, string quotaName, bool val, string messageKey)
|
||||
{
|
||||
CheckQuotaValue(cntx, errors, quotaName, 0, val ? 1 : 0, messageKey);
|
||||
}
|
||||
|
||||
public static void CheckListsQuota(PackageContext cntx, List<string> errors, string quotaName, string messageKey)
|
||||
{
|
||||
CheckQuotaValue(cntx, errors, quotaName, 0, -1, messageKey);
|
||||
}
|
||||
|
||||
public static void CheckQuotaValue(PackageContext cntx, List<string> errors, string quotaName, long currentVal, long val, string messageKey)
|
||||
{
|
||||
if (!cntx.Quotas.ContainsKey(quotaName))
|
||||
return;
|
||||
|
||||
QuotaValueInfo quota = cntx.Quotas[quotaName];
|
||||
|
||||
if (val == -1 && quota.QuotaExhausted) // check if quota already reached
|
||||
{
|
||||
errors.Add(messageKey + ":" + quota.QuotaAllocatedValue);
|
||||
}
|
||||
else if (quota.QuotaAllocatedValue == -1)
|
||||
return; // unlimited
|
||||
else if (quota.QuotaTypeId == 1 && val == 1 && quota.QuotaAllocatedValue == 0) // bool quota
|
||||
errors.Add(messageKey);
|
||||
else if (quota.QuotaTypeId == 2)
|
||||
{
|
||||
long maxValue = quota.QuotaAllocatedValue - quota.QuotaUsedValue + currentVal;
|
||||
if (val > maxValue)
|
||||
errors.Add(messageKey + ":" + maxValue);
|
||||
}
|
||||
else if (quota.QuotaTypeId == 3 && val > quota.QuotaAllocatedValue)
|
||||
{
|
||||
int maxValue = quota.QuotaAllocatedValue;
|
||||
errors.Add(messageKey + ":" + maxValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using WebsitePanel.Providers.Common;
|
||||
using WebsitePanel.Providers.Virtualization;
|
||||
using WebsitePanel.Providers.Virtualization2012;
|
||||
|
||||
namespace WebsitePanel.EnterpriseServer.Code.Virtualization2012
|
||||
{
|
||||
public static class ReplicationHelper
|
||||
{
|
||||
|
||||
public static void CleanUpReplicaServer(VirtualMachine originalVm)
|
||||
{
|
||||
try
|
||||
{
|
||||
ResultObject result = new ResultObject();
|
||||
|
||||
// Get replica server
|
||||
var replicaServer = GetReplicaForService(originalVm.ServiceId, ref result);
|
||||
|
||||
// Clean up replica server
|
||||
var replicaVm = replicaServer.GetVirtualMachines().FirstOrDefault(m => m.Name == originalVm.Name);
|
||||
if (replicaVm != null)
|
||||
{
|
||||
replicaServer.DisableVmReplication(replicaVm.VirtualMachineId);
|
||||
replicaServer.ShutDownVirtualMachine(replicaVm.VirtualMachineId, true, "ReplicaDelete");
|
||||
replicaServer.DeleteVirtualMachine(replicaVm.VirtualMachineId);
|
||||
}
|
||||
}
|
||||
catch { /* skip */ }
|
||||
}
|
||||
|
||||
public static ReplicationServerInfo GetReplicaInfoForService(int serviceId, ref ResultObject result)
|
||||
{
|
||||
// Get service id of replica server
|
||||
StringDictionary vsSesstings = ServerController.GetServiceSettings(serviceId);
|
||||
string replicaServiceId = vsSesstings["ReplicaServerId"];
|
||||
|
||||
if (string.IsNullOrEmpty(replicaServiceId))
|
||||
{
|
||||
result.ErrorCodes.Add(VirtualizationErrorCodes.NO_REPLICA_SERVER_ERROR);
|
||||
return null;
|
||||
}
|
||||
|
||||
// get replica server info for replica service id
|
||||
VirtualizationServer2012 vsReplica = VirtualizationHelper.GetVirtualizationProxy(Convert.ToInt32(replicaServiceId));
|
||||
StringDictionary vsReplicaSesstings = ServerController.GetServiceSettings(Convert.ToInt32(replicaServiceId));
|
||||
string computerName = vsReplicaSesstings["ServerName"];
|
||||
var replicaServerInfo = vsReplica.GetReplicaServer(computerName);
|
||||
|
||||
if (!replicaServerInfo.Enabled)
|
||||
{
|
||||
result.ErrorCodes.Add(VirtualizationErrorCodes.NO_REPLICA_SERVER_ERROR);
|
||||
return null;
|
||||
}
|
||||
|
||||
return replicaServerInfo;
|
||||
}
|
||||
|
||||
public static VirtualizationServer2012 GetReplicaForService(int serviceId, ref ResultObject result)
|
||||
{
|
||||
// Get service id of replica server
|
||||
StringDictionary vsSesstings = ServerController.GetServiceSettings(serviceId);
|
||||
string replicaServiceId = vsSesstings["ReplicaServerId"];
|
||||
|
||||
if (string.IsNullOrEmpty(replicaServiceId))
|
||||
{
|
||||
result.ErrorCodes.Add(VirtualizationErrorCodes.NO_REPLICA_SERVER_ERROR);
|
||||
return null;
|
||||
}
|
||||
|
||||
// get replica server for replica service id
|
||||
return VirtualizationHelper.GetVirtualizationProxy(Convert.ToInt32(replicaServiceId));
|
||||
}
|
||||
|
||||
public static void CheckReplicationQuota(int packageId, ref ResultObject result)
|
||||
{
|
||||
List<string> quotaResults = new List<string>();
|
||||
PackageContext cntx = PackageController.GetPackageContext(packageId);
|
||||
|
||||
QuotaHelper.CheckBooleanQuota(cntx, quotaResults, Quotas.VPS2012_REPLICATION_ENABLED, true, VirtualizationErrorCodes.QUOTA_REPLICATION_ENABLED);
|
||||
|
||||
if (quotaResults.Count > 0)
|
||||
result.ErrorCodes.AddRange(quotaResults);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using WebsitePanel.Providers.Virtualization2012;
|
||||
|
||||
namespace WebsitePanel.EnterpriseServer.Code.Virtualization2012
|
||||
{
|
||||
public static class VirtualizationHelper
|
||||
{
|
||||
public static VirtualizationServer2012 GetVirtualizationProxy(int serviceId)
|
||||
{
|
||||
VirtualizationServer2012 ws = new VirtualizationServer2012();
|
||||
ServiceProviderProxy.Init(ws, serviceId);
|
||||
return ws;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue