Skip to content

Commit

Permalink
Block the start of an environment deploy/app if the agent is disconne…
Browse files Browse the repository at this point in the history
…cted (#72)

* Ident code. Add CheckConnection on app handler and environment handler.

* version bump

* little fixes

* apply fix to wait for the async call

* Apply PR suggestion regarding a log position.

* Apply PR suggestion. Update src/Common/ICustomerPortalClient.cs

Co-authored-by: joseloc300 <JoseCunha@criticalmanufacturing.com>

* apply pr suggestions

* Revert "apply pr suggestions"

This reverts commit 1a7581c.

* fix to get the direct boolean value from output request

* Update src/Common/Services/INewEnvironmentUtilities.cs

Co-authored-by: joseloc300 <JoseCunha@criticalmanufacturing.com>

* apply pr suggestions

---------

Co-authored-by: CMF\afsilva <andresilva@criticalmanufacturing.com>
Co-authored-by: joseloc300 <JoseCunha@criticalmanufacturing.com>
  • Loading branch information
3 people authored Feb 1, 2024
1 parent ecacdd8 commit 204493d
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 144 deletions.
13 changes: 12 additions & 1 deletion src/Common/CustomerPortalClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Xml;
using System.Xml.Linq;
using Cmf.CustomerPortal.BusinessObjects;
using Cmf.CustomerPortal.Common.CustomerInfrastructure;
using Cmf.CustomerPortal.Orchestration.CustomerEnvironmentManagement.InputObjects;
using Cmf.CustomerPortal.Orchestration.CustomerEnvironmentManagement.OutputObjects;
using Cmf.Foundation.BusinessObjects;
Expand Down Expand Up @@ -386,7 +387,7 @@ public async Task<bool> CheckCustomerEnvironmentConnectionStatus(long? definitio
{
CheckCustomerEnvironmentConnectionStatusOutput output = await new CheckCustomerEnvironmentConnectionStatusInput() { DefinitionId = definitionId }
.CheckCustomerEnvironmentConnectionStatusAsync();
return output.IsCustomerEnvironmentConnected;
return output.CustomerEnvironmentConnectionStatus == InfrastructureConnectionStatus.Connected;
}

/// <inheritdoc/>
Expand All @@ -401,5 +402,15 @@ public async Task<CustomerEnvironmentApplicationPackage> CreateOrUpdateAppInstal
CustomerLicenseName = customerLicenseName
}.CreateOrUpdateAppInstallationAsync(true)).CustomerEnvironmentApplicationPackage;
}


public async Task<bool> CheckStartDeploymentConnection(CustomerEnvironment customerEnvironment, CustomerInfrastructure customerInfrastructure)
{
return (await new CheckStartDeploymentConnectionInput()
{
CustomerEnvironment = customerEnvironment,
CustomerInfrastructure = customerInfrastructure
}.CheckStartDeploymentConnectionAsync(true)).CanStartDeploymentConnection;
}
}
}
14 changes: 6 additions & 8 deletions src/Common/Handlers/InstallAppHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Cmf.CustomerPortal.BusinessObjects;
using Cmf.CustomerPortal.Sdk.Common.Services;
using Cmf.LightBusinessObjects.Infrastructure.Errors;

namespace Cmf.CustomerPortal.Sdk.Common.Handlers
{
Expand Down Expand Up @@ -32,14 +30,14 @@ public async Task Run(string name, string appVersion, string customerEnvironment
string appParameters = parameters == null ? null : await Utils.ReplaceTokens(Session, File.ReadAllText(parameters.FullName), replaceTokens, true);

// load the customer environment
CustomerEnvironment environment = null;
environment = await _customerPortalClient.GetObjectByName<CustomerEnvironment>(customerEnvironmentName);
CustomerEnvironment environment = await _customerPortalClient.GetObjectByName<CustomerEnvironment>(customerEnvironmentName);

// check environment connection
await _newEnvironmentUtilities.CheckEnvironmentConnection(environment);

// create or update the relationship between the environment and the app
CustomerEnvironmentApplicationPackage customerEnvironmentApplicationPackage = null;
customerEnvironmentApplicationPackage = await _customerPortalClient.CreateOrUpdateAppInstallation(
environment.Id, name, appVersion, appParameters, license
);
CustomerEnvironmentApplicationPackage customerEnvironmentApplicationPackage = await _customerPortalClient.CreateOrUpdateAppInstallation(
environment.Id, name, appVersion, appParameters, license);

// start deployment
await _appInstallationHandler.Handle(name, customerEnvironmentApplicationPackage, environment.DeploymentTarget, output, timeout, timeoutToGetSomeMBMessage);
Expand Down
43 changes: 32 additions & 11 deletions src/Common/Handlers/NewEnvironmentHandler.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using Cmf.CustomerPortal.BusinessObjects;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Cmf.CustomerPortal.BusinessObjects;
using Cmf.CustomerPortal.Orchestration.CustomerEnvironmentManagement.InputObjects;
using Cmf.CustomerPortal.Sdk.Common.Services;
using Cmf.Foundation.BusinessObjects;
using Cmf.Foundation.BusinessOrchestration.EntityTypeManagement.InputObjects;
using Cmf.Foundation.BusinessOrchestration.GenericServiceManagement.InputObjects;
using Cmf.Foundation.Common.Licenses.Enums;
using Cmf.LightBusinessObjects.Infrastructure.Errors;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace Cmf.CustomerPortal.Sdk.Common.Handlers
{
Expand Down Expand Up @@ -96,8 +96,10 @@ bool terminateOtherVersionsRemoveVolumes
environment.Parameters = rawParameters;
environment.ChangeSet = null;

Session.LogInformation($"Creating a new version of the Customer environment {name}...");
// check environment connection
await _newEnvironmentUtilities.CheckEnvironmentConnection(environment);

Session.LogInformation($"Creating a new version of the Customer environment {name}...");
environment = await CreateEnvironment(_customerPortalClient, environment);

// terminate other versions
Expand All @@ -108,7 +110,7 @@ bool terminateOtherVersionsRemoveVolumes
var customerEnvironmentsToTerminate = await _newEnvironmentUtilities.GetOtherVersionToTerminate(environment);
OperationAttributeCollection terminateOperationAttibutes = new OperationAttributeCollection();
EntityType ceET = new GetEntityTypeByNameInput { Name = "CustomerEnvironment" }.GetEntityTypeByNameSync().EntityType;
foreach (var ce in customerEnvironmentsToTerminate)
foreach (var ce in customerEnvironmentsToTerminate)
{
OperationAttribute attributeRemove = new OperationAttribute();
attributeRemove.EntityId = ce.Id;
Expand All @@ -135,7 +137,8 @@ bool terminateOtherVersionsRemoveVolumes
await _environmentDeploymentHandler.WaitForEnvironmentsToBeTerminated(customerEnvironmentsToTerminate);

Session.LogInformation("Other versions terminated!");
} else
}
else
{
Session.LogInformation("There are no versions with an eligible status to be terminated.");
}
Expand All @@ -144,8 +147,6 @@ bool terminateOtherVersionsRemoveVolumes
// if not, check if we are creating a new environment for an infrastructure
else if (!string.IsNullOrWhiteSpace(customerInfrastructureName))
{
Session.LogInformation($"Creating the customer environment {name} for a customer infrastructure...");

ProductSite environmentSite = null;
// If we are creating in an infrastructure, and we are not creating the agent, the user must define the site for the environment
if (!isInfrastructureAgent)
Expand All @@ -157,7 +158,7 @@ bool terminateOtherVersionsRemoveVolumes
}
else
{
throw new ArgumentNullException("Name of the Site is mandatory to create a Customer Environment");
throw new ArgumentNullException(nameof(siteName), "Name of the Site is mandatory to create a Customer Environment");
}
}

Expand All @@ -173,6 +174,11 @@ bool terminateOtherVersionsRemoveVolumes
CustomerLicense = isInfrastructureAgent ? null : await _customerPortalClient.GetObjectByName<CustomerLicense>(licenseName)
};

// check environment connection
await CheckConnectionNewEnvironmentCreation(environment, customerInfrastructureName);

Session.LogInformation($"Creating the customer environment {name} for a customer infrastructure...");

environment = (await new CreateCustomerEnvironmentForCustomerInfrastructureInput
{
CustomerInfrastructureName = customerInfrastructureName,
Expand Down Expand Up @@ -201,10 +207,25 @@ bool terminateOtherVersionsRemoveVolumes

Session.LogInformation($"Customer environment {name} created...");


// handle installation
await _environmentDeploymentHandler.Handle(interactive, environment, target, outputDir, minutesTimeoutMainTask, minutesTimeoutToGetSomeMBMsg);
}

/// <summary>
/// Check the connection for a creation of a new environment in some infrastructure
/// </summary>
/// <param name="newEnvironment">The new environment</param>
/// <param name="infrastructureName">Infrastructure name</param>
private async Task CheckConnectionNewEnvironmentCreation(CustomerEnvironment newEnvironment, string infrastructureName)
{
CustomerInfrastructure infrastructure = new() { Name = infrastructureName };
newEnvironment.CustomerInfrastructure = infrastructure;

// check environment connection
await _newEnvironmentUtilities.CheckEnvironmentConnection(newEnvironment);
}

/// <summary>
/// Creates the environment or a new version if it already exists.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Common/ICustomerPortalClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Cmf.CustomerPortal.BusinessObjects;
using Cmf.CustomerPortal.Orchestration.CustomerEnvironmentManagement.OutputObjects;
using Cmf.Foundation.BusinessObjects;
using Cmf.Foundation.BusinessObjects.QueryObject;
using Cmf.Foundation.Common.Base;
Expand Down Expand Up @@ -70,6 +71,7 @@ public interface ICustomerPortalClient
/// <param name="definitionId">definition id</param>
/// <returns></returns>
Task<bool> CheckCustomerEnvironmentConnectionStatus(long? definitionId);

/// Get's an object by its Id.
/// </summary>
/// <typeparam name="T">The object's Type</typeparam>
Expand All @@ -93,5 +95,13 @@ public interface ICustomerPortalClient
/// <param name="customerLicenseName">Name of a CustomerLicense.</param>
/// <returns>The CustomerEnvironmentApplicationPackage relation.</returns>
Task<CustomerEnvironmentApplicationPackage> CreateOrUpdateAppInstallation(long customerEnvironmentId, string appName, string appVersion, string parameters, string customerLicenseName);

/// <summary>
/// Check the Deployment connection to verify if the deployment of an environment/app can occur.
/// </summary>
/// <param name="customerEnvironment">customer environment</param>
/// <param name="customerInfrastructure">customer infrastructure</param>
/// <returns></returns>
Task<bool> CheckStartDeploymentConnection(CustomerEnvironment customerEnvironment, CustomerInfrastructure customerInfrastructure);
}
}
13 changes: 11 additions & 2 deletions src/Common/Services/INewEnvironmentUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Cmf.CustomerPortal.BusinessObjects;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Cmf.CustomerPortal.BusinessObjects;

namespace Cmf.CustomerPortal.Sdk.Common.Services
{
Expand All @@ -21,5 +21,14 @@ public interface INewEnvironmentUtilities
/// <param name="customerEnvironment">Specific customer environment version</param>
/// <returns></returns>
Task<CustomerEnvironmentCollection> GetOtherVersionToTerminate(CustomerEnvironment customerEnvironment);


/// <summary>
/// In the case of a remote target, checks if the necessary connections are possible (if any) to make a deployment possible
/// If is a remote target and the deployment and the connection to the agent can't be startd, a exception is returned.
/// </summary>
/// <param name="environment">Environment</param>
/// <exception cref="CmfFaultException">Throw an exception if the environment needs to have a connection established with the agent, and that is not possible.</exception>
Task CheckEnvironmentConnection(CustomerEnvironment environment);
}
}
Loading

0 comments on commit 204493d

Please sign in to comment.