Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Microsoft.Extensions.Hosting;

namespace Microsoft.Omex.Extensions.Abstractions.ExecutionContext
Expand All @@ -25,9 +29,11 @@ public class BaseExecutionContext : IExecutionContext

// defined by Service Fabric https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-environment-variables-reference
internal const string ServiceNameVariableName = "Fabric_ServiceName";
internal const string ServicePackageVariableName = "Fabric_ServicePackageName";
internal const string ApplicationNameVariableName = "Fabric_ApplicationName";
internal const string NodeNameVariableName = "Fabric_NodeName";
internal const string NodeIPOrFQDNVariableName = "Fabric_NodeIPOrFQDN";
internal const string FarbicFolderApplication = "Fabric_Folder_Application";

/// <summary>
/// Create instance of execution context
Expand All @@ -36,10 +42,11 @@ public class BaseExecutionContext : IExecutionContext
public BaseExecutionContext(IHostEnvironment? hostEnvironment = null)
{
MachineName = GetMachineName();
BuildVersion = GetBuildVersion();
ServicePackageName = GetVariable(ServicePackageVariableName) ?? DefaultEmptyValue;
BuildVersion = GetBuildVersionFromServiceManifest() ?? DefaultEmptyValue;

ClusterIpAddress = GetIpAddress(MachineName);

RegionName = GetVariable(RegionNameVariableName) ?? DefaultEmptyValue;
DeploymentSlice = GetVariable(SliceNameVariableName) ?? DefaultEmptyValue;

Expand Down Expand Up @@ -109,6 +116,9 @@ public BaseExecutionContext(IHostEnvironment? hostEnvironment = null)
/// <inheritdoc/>
public bool IsPrivateDeployment { get; protected set; }

/// <inheritdoc/>
public string ServicePackageName { get; protected set; }

/// <summary>
/// Get environment variable value
/// </summary>
Expand Down Expand Up @@ -137,6 +147,37 @@ protected static string GetBuildVersion()
: DefaultEmptyValue;
}

/// <summary>
/// Get build version from the current running service's manifest file
/// </summary>
/// <returns> Build version if found, otherwise null </returns>
protected string? GetBuildVersionFromServiceManifest()
{
string? serviceManifestPath = GetServiceManifestPath();
return serviceManifestPath == null ? null :
XElement.Load(serviceManifestPath).Attribute("Version")?.Value;
}


private string? GetServiceManifestPath()
{
string? applicationDir = GetVariable(FarbicFolderApplication);
string? serviceManifestName = ServicePackageName;

if (applicationDir == null || serviceManifestName == null)
{
return null;
}
string serviceProperName = serviceManifestName.Replace(@"\", @"\\").Replace(".", @"\.");
string regexExp = string.Format(@"(.*{0}.*\.Manifest\..*\.xml)$", serviceProperName);
Regex regex = new(regexExp);
string[] manifests = Directory.GetFiles(applicationDir).Where(
path => regex.IsMatch(path)
).ToArray();

return manifests.Length != 1 ? null : manifests.Single();
}

/// <summary>
/// Default empty value
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,10 @@ public interface IExecutionContext
/// The service name
/// </summary>
string ServiceName { get; }

/// <summary>
/// The service package name
/// </summary>
string ServicePackageName { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<Content Include="BuildTransitive\*" Pack="true" PackagePath="buildTransitive" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="$(Microsoft_Extensions_Hosting_Version)" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="$(Microsoft_Extensions_Hosting_Abstractions_Version)" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(Microsoft_Extensions_Logging_Abstractions_Version)" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="$(System_Diagnostics_DiagnosticSource_Version)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ public void Constructor_InitializesPropertiesProperly(string enviroment, bool is
string applicationName = "SomeApplicationName";
string serviceName = "SomeServiceName";
string nodeName = "SomeNodeName";
string packageName = "SomePackageName";

IPAddress nodeIPOrFQDN = IPAddress.Parse("192.0.0.1");

Environment.SetEnvironmentVariable(BaseExecutionContext.ClusterNameVariableName, clusterName);
Environment.SetEnvironmentVariable(BaseExecutionContext.ServicePackageVariableName, packageName);
Environment.SetEnvironmentVariable(BaseExecutionContext.RegionNameVariableName, regionName);
Environment.SetEnvironmentVariable(BaseExecutionContext.SliceNameVariableName, sliceName);
Environment.SetEnvironmentVariable(BaseExecutionContext.ApplicationNameVariableName, applicationName);
Expand All @@ -42,6 +45,7 @@ public void Constructor_InitializesPropertiesProperly(string enviroment, bool is
Assert.AreEqual(clusterName, info.Cluster);
Assert.AreEqual(regionName, info.RegionName);
Assert.AreEqual(sliceName, info.DeploymentSlice);
Assert.AreEqual(packageName, info.ServicePackageName);

Assert.AreEqual(enviroment, info.EnvironmentName);
Assert.AreEqual(isPrivate, info.IsPrivateDeployment);
Expand Down