Skip to content

(IMPROVEMENT)(DOTNET-199)(WIP) - Fallback to Config Stackify.Environment for azure instance hostname #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 63 additions & 23 deletions Src/StackifyLib/Models/AzureConfig.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
// Copyright (c) 2024-2025 BMC Software, Inc.
// Copyright (c) 2021-2024 Netreo
// Copyright (c) 2019 Stackify
using System;
using StackifyLib.Utils;
using System.Linq;
#if NETFULL
using Microsoft.Win32;
#endif
Expand All @@ -13,22 +11,26 @@ namespace StackifyLib.Models
{
public class AzureConfig
{
private static bool? _inAzure;
private static AzureRoleType _azureRoleType = AzureRoleType.Unknown;
private static string _azureRoleName;
private static string _azureInstanceName;
private static string _entryPoint;
private static AzureConfig _instance = new AzureConfig();
public static AzureConfig Instance => _instance;

private static readonly DateTime AppStarted = DateTime.UtcNow;
private bool? _inAzure;
private AzureRoleType _azureRoleType = AzureRoleType.Unknown;
private string _azureRoleName;
private string _azureInstanceName;
private string _entryPoint;

private static readonly object Locker = new object();
private readonly DateTime AppStarted = DateTime.UtcNow;

private readonly object Locker = new object();
public const string ProductionEnvName = "Production";

public static string AzureAppWebConfigEnvironment { get; set; }
public static string AzureAppWebConfigApiKey { get; set; }
public static string AzureDriveLetter { get; set; }

public static string AzureInstanceName
public string AzureAppWebConfigEnvironment { get; set; }
public string AzureAppWebConfigApiKey { get; set; }
public string AzureDriveLetter { get; set; }

public string AzureInstanceName
{
get
{
Expand All @@ -37,7 +39,7 @@ public static string AzureInstanceName
}
}

public static bool InAzure
public bool InAzure
{
get
{
Expand All @@ -62,7 +64,7 @@ public static bool InAzure
}
}

public static bool IsWebsite
public bool IsWebsite
{
get
{
Expand All @@ -75,12 +77,24 @@ public static bool IsWebsite
}
}

private static void EnsureInAzureRan()
private EnvironmentDetail _environmentDetail = null;

public AzureConfig()
{

}

public AzureConfig(EnvironmentDetail environmentDetail)
{
_environmentDetail = environmentDetail;
}

private void EnsureInAzureRan()
{
bool ensureTestHasBeenDone = InAzure;
}

public static void LoadAzureSettings()
public void LoadAzureSettings()
{
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")) == false && string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID")) == false)
{
Expand All @@ -90,13 +104,13 @@ public static void LoadAzureSettings()
var slotId = GetDeploymentSlotId() ?? "0000";

_azureRoleName = Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME");
_azureInstanceName = $"{Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")} {ServerConfigHelper.GetEnvironment()} [{slotId}-{Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID").Left(6)}]";
_azureInstanceName = $"{Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")} {GetEnvironment()} [{slotId}-{Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID").Left(6)}]";

return;
}
}

public static string GetDeploymentSlotId()
public string GetDeploymentSlotId()
{
try
{
Expand All @@ -113,6 +127,32 @@ public static string GetDeploymentSlotId()

return null;
}

public string GetEnvironment()
{
if (IsWebsite)
{
if (_environmentDetail == null)
{
_environmentDetail = EnvironmentDetail.Get();
}

var key = Environment.GetEnvironmentVariable("Stackify.Environment");
if (string.IsNullOrEmpty(key))
{
key = _environmentDetail.ConfiguredEnvironmentName;
}

if (string.IsNullOrEmpty(key) == false)
{
return key;
}

return ProductionEnvName;
}

return string.Empty;
}
}

public enum AzureRoleType : short
Expand Down
6 changes: 3 additions & 3 deletions Src/StackifyLib/Models/EnvironmentDetail.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024 BMC Software, Inc.
// Copyright (c) 2024-2025 BMC Software, Inc.
// Copyright (c) 2021-2024 Netreo
// Copyright (c) 2019 Stackify

Expand Down Expand Up @@ -41,9 +41,9 @@ private void GetAzureInfo()
{
//IsAzureWorkerRole is set when instantiating EnvironmentDetail
//Useful in other parts directly referencing AzureInstanceName
if(!IsAzureWorkerRole && AzureConfig.InAzure && AzureConfig.IsWebsite)
if(!IsAzureWorkerRole && AzureConfig.Instance.InAzure && AzureConfig.Instance.IsWebsite)
{
AzureInstanceName = AzureConfig.AzureInstanceName;
AzureInstanceName = AzureConfig.Instance.AzureInstanceName;

}

Expand Down
10 changes: 5 additions & 5 deletions Src/StackifyLib/StackifyLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<AssemblyTitle>Stackify API</AssemblyTitle>
<VersionPrefix>2.2.16</VersionPrefix>
<VersionSuffix></VersionSuffix>
<VersionPrefix>2.2.17</VersionPrefix>
<VersionSuffix>beta1</VersionSuffix>
<TargetFrameworks>netstandard2.0;net40;net45;net451;net452;net46;net461;net462</TargetFrameworks>
<AssemblyName>StackifyLib</AssemblyName>
<PackageId>StackifyLib</PackageId>
Expand All @@ -14,15 +14,15 @@
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<Version>2.2.16</Version>
<Version>2.2.17-beta1</Version>
<Authors>StackifyLib</Authors>
<PackageProjectUrl>https://github.com/stackify/stackify-api-dotnet</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/stackify/stackify-api-dotnet/blob/master/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/stackify/stackify-api-dotnet</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageIconUrl>https://stackify.com/wp-content/uploads/2017/02/stk.png</PackageIconUrl>
<AssemblyVersion>2.2.16.0</AssemblyVersion>
<FileVersion>2.2.16.0</FileVersion>
<AssemblyVersion>2.2.17.0</AssemblyVersion>
<FileVersion>2.2.17.0</FileVersion>
<PackageReleaseNotes>Remove default internal file logger</PackageReleaseNotes>
<SignAssembly>false</SignAssembly>
</PropertyGroup>
Expand Down
9 changes: 6 additions & 3 deletions Src/StackifyLib/Utils/HttpClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
// Copyright (c) 2024-2025 BMC Software, Inc.
// Copyright (c) 2021-2024 Netreo
// Copyright (c) 2019 Stackify
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -313,9 +316,9 @@ public bool IdentifyApp()
}

//Applicable only for Azure AppService
if(AzureConfig.InAzure && AzureConfig.IsWebsite)
if(AzureConfig.Instance.InAzure && AzureConfig.Instance.IsWebsite)
{
env.DeviceName = AzureConfig.AzureInstanceName;
env.DeviceName = AzureConfig.Instance.AzureInstanceName;
}

string jsonData = JsonConvert.SerializeObject(env, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
Expand Down
34 changes: 0 additions & 34 deletions Src/StackifyLib/Utils/ServerConfigHelper.cs

This file was deleted.

Loading
Loading