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 ;
using System.Linq ;
using System.Collections.Generic ;
using XenAPI ;
using XenAdmin.Alerts ;
using System.ComponentModel ;
using System.Threading ;
namespace XenAdmin.Actions
{
/// <summary>
/// This Action is for v6 (Midnight Ride) licensing. It sets the license server details and sets the edition to advanced, enterprise, enterprise-xd, platinum or free.
/// </summary>
public class ApplyLicenseEditionAction : AsyncAction
{
private readonly IEnumerable < IXenObject > xos ;
private readonly Host . Edition _edition ;
private readonly string _licenseServerAddress ;
private readonly string _licenseServerPort ;
2021-10-12 03:16:46 +02:00
public List < LicenseFailure > LicenseFailures { get ; }
2014-08-14 15:40:24 +02:00
2021-10-12 03:16:46 +02:00
private readonly Action < List < LicenseFailure > , string > _doOnLicensingFailure ;
2013-06-24 13:41:48 +02:00
/// <summary>
/// Initializes a new instance of the <see cref="ApplyLicenseEditionAction"/> class.
/// </summary>
/// <param name="xos">The hosts for which the license should be set.</param>
/// <param name="edition">The edition the hosts should be set to.</param>
/// <param name="licenseServerAddress">The license server address.</param>
/// <param name="licenseServerPort">The license server port.</param>
2021-10-12 03:16:46 +02:00
/// <param name="doOnLicensingFailure">The method to call when the licensing fails. This method will show failure details in a dialog</param>
public ApplyLicenseEditionAction ( IEnumerable < IXenObject > xos , Host . Edition edition , string licenseServerAddress , string licenseServerPort , Action < List < LicenseFailure > , string > doOnLicensingFailure = null )
2013-06-24 13:41:48 +02:00
: base ( null , Messages . LICENSE_UPDATING_LICENSES )
{
2021-09-15 16:08:45 +02:00
if ( xos = = null | | ! xos . Any ( ) )
throw new ArgumentException ( @"Parameter cannot be null or empty" , nameof ( xos ) ) ;
2013-06-24 13:41:48 +02:00
2021-09-15 16:08:45 +02:00
LicenseFailures = new List < LicenseFailure > ( ) ;
2013-06-24 13:41:48 +02:00
_edition = edition ;
this . xos = xos ;
_licenseServerAddress = licenseServerAddress ;
_licenseServerPort = licenseServerPort ;
2021-10-12 03:16:46 +02:00
_doOnLicensingFailure = doOnLicensingFailure ;
2016-01-15 14:45:54 +01:00
if ( xos ! = null & & Host = = null & & Pool = = null & & xos . Count ( ) = = 1 )
{
var xo = xos . FirstOrDefault ( ) ;
if ( xo is Host )
Host = xo as Host ;
else if ( xo is Pool )
Pool = xo as Pool ;
}
2013-06-24 13:41:48 +02:00
}
private static void SetLicenseServer ( Host host , string licenseServerAddress , string licenseServerPort )
{
Dictionary < string , string > d = new Dictionary < string , string > ( ) ;
if ( ! string . IsNullOrEmpty ( licenseServerAddress ) & & ! string . IsNullOrEmpty ( licenseServerPort ) )
{
d . Add ( "address" , licenseServerAddress ) ;
d . Add ( "port" , licenseServerPort ) ;
}
2020-01-17 13:44:37 +01:00
Host . set_license_server ( host . Connection . Session , host . opaque_ref , d ) ;
2013-06-24 13:41:48 +02:00
}
protected override void Run ( )
{
2020-01-17 13:44:37 +01:00
Description = Messages . LICENSE_UPDATING_LICENSES ;
2013-06-24 13:41:48 +02:00
foreach ( IXenObject xo in xos )
{
Connection = xo . Connection ;
if ( ! Connection . IsConnected )
{
continue ;
}
Host host = null ;
Pool pool = null ;
2020-01-17 13:44:37 +01:00
if ( xo is Host )
2013-06-24 13:41:48 +02:00
host = xo as Host ;
2020-01-17 13:44:37 +01:00
if ( xo is Pool )
2013-06-24 13:41:48 +02:00
{
pool = xo as Pool ;
host = xo . Connection . Resolve ( pool . master ) ;
}
string previousLicenseServerAddress = null ;
string previousLicenseServerPort = null ;
CollectionChangeEventHandler alertsChangeHandler = null ;
string alertText = null ;
object lck = new object ( ) ;
if ( host ! = null & & host . license_server . ContainsKey ( "address" ) )
{
previousLicenseServerAddress = host . license_server [ "address" ] ;
}
if ( host ! = null & & host . license_server . ContainsKey ( "port" ) )
{
previousLicenseServerPort = host . license_server [ "port" ] ;
}
try
{
2020-01-17 13:44:37 +01:00
if ( pool ! = null )
2013-06-24 13:41:48 +02:00
pool . Connection . Cache . Hosts . ToList ( ) . ForEach ( h = > SetLicenseServer ( h , _licenseServerAddress , _licenseServerPort ) ) ;
else
SetLicenseServer ( host , _licenseServerAddress , _licenseServerPort ) ;
IXenObject xoClosure = xo ;
alertsChangeHandler = delegate ( object sender , CollectionChangeEventArgs e )
{
if ( e . Action = = CollectionChangeAction . Add )
{
lock ( lck )
{
Alert alert = ( Alert ) e . Element ;
2017-03-16 11:40:41 +01:00
Message . MessageType messageType ;
// if this is a message alert, its Name property will contain the MessageType
if ( host ! = null & & host . uuid = = alert . HostUuid & & Enum . TryParse ( alert . Name , out messageType ) )
2013-06-24 13:41:48 +02:00
{
2017-03-16 11:40:41 +01:00
switch ( messageType )
2013-06-24 13:41:48 +02:00
{
2017-03-16 11:40:41 +01:00
case Message . MessageType . LICENSE_NOT_AVAILABLE :
case Message . MessageType . LICENSE_SERVER_UNREACHABLE :
case Message . MessageType . LICENSE_SERVER_VERSION_OBSOLETE :
2017-09-03 04:33:29 +02:00
alertText = string . Format ( Message . FriendlyBody ( alert . Name ) , xoClosure . Name ( ) ) ;
2017-03-16 11:40:41 +01:00
break ;
case Message . MessageType . GRACE_LICENSE :
alertText = string . Empty ;
break ;
2013-06-24 13:41:48 +02:00
}
}
}
}
} ;
2013-11-21 12:57:57 +01:00
Alert . RegisterAlertCollectionChanged ( alertsChangeHandler ) ;
2013-06-24 13:41:48 +02:00
2020-01-17 13:44:37 +01:00
if ( xo is Host & & host ! = null )
2013-06-24 13:41:48 +02:00
{
2018-09-04 11:31:41 +02:00
Host . apply_edition ( host . Connection . Session , host . opaque_ref , host . GetEditionText ( _edition ) , false ) ;
2013-06-24 13:41:48 +02:00
}
if ( xo is Pool )
{
2020-06-20 15:35:25 +02:00
var firstHost = xo . Connection . Cache . Hosts . FirstOrDefault ( ) ;
2021-12-21 16:53:46 +01:00
if ( firstHost ! = null & & pool ! = null )
2020-06-20 15:35:25 +02:00
Pool . apply_edition ( xo . Connection . Session , pool . opaque_ref , firstHost . GetEditionText ( _edition ) ) ;
2013-06-24 13:41:48 +02:00
}
Description = Messages . APPLYLICENSE_UPDATED ;
}
catch ( Failure e )
{
for ( int i = 0 ; i < 50 ; i + + )
{
Thread . Sleep ( 100 ) ;
lock ( lck )
{
if ( alertText ! = null )
break ;
}
}
2014-08-14 15:40:24 +02:00
LicenseFailures . Add ( new LicenseFailure ( host , alertText ? ? e . Message ) ) ;
2013-06-24 13:41:48 +02:00
if ( pool ! = null )
pool . Connection . Cache . Hosts . ToList ( ) . ForEach ( h = > SetLicenseServer ( h , previousLicenseServerAddress , previousLicenseServerPort ) ) ;
else
SetLicenseServer ( host , previousLicenseServerAddress , previousLicenseServerPort ) ;
}
finally
{
2013-11-21 12:57:57 +01:00
Alert . DeregisterAlertCollectionChanged ( alertsChangeHandler ) ;
2013-06-24 13:41:48 +02:00
}
}
2014-08-14 15:40:24 +02:00
if ( LicenseFailures . Count > 0 )
2013-06-24 13:41:48 +02:00
{
2017-09-03 04:33:29 +02:00
string exceptionText = LicenseFailures . Count = = 1 ? string . Format ( Messages . LICENSE_ERROR_1 , LicenseFailures [ 0 ] . Host . Name ( ) ) : string . Format ( Messages . LICENSE_ERROR_MANY , LicenseFailures . Count , new List < IXenObject > ( xos ) . Count ) ;
2013-06-24 13:41:48 +02:00
2021-10-12 03:16:46 +02:00
_doOnLicensingFailure ? . Invoke ( LicenseFailures , exceptionText ) ;
2013-06-24 13:41:48 +02:00
throw new InvalidOperationException ( exceptionText ) ;
}
}
}
public class LicenseFailure
{
public Host Host ;
public string AlertText ;
public LicenseFailure ( Host host , string alertText )
{
Host = host ;
AlertText = alertText ;
}
}
}