diff --git a/Src/StackifyLib/Models/AzureConfig.cs b/Src/StackifyLib/Models/AzureConfig.cs new file mode 100644 index 0000000..8dad9a0 --- /dev/null +++ b/Src/StackifyLib/Models/AzureConfig.cs @@ -0,0 +1,127 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Text.RegularExpressions; +using StackifyLib.Utils; +using System.Linq; +#if NETFULL +using Microsoft.Win32; +#endif + +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 readonly DateTime AppStarted = DateTime.UtcNow; + + private static readonly object Locker = new object(); + + + public static string AzureAppWebConfigEnvironment { get; set; } + public static string AzureAppWebConfigApiKey { get; set; } + public static string AzureDriveLetter { get; set; } + + public static string AzureInstanceName + { + get + { + EnsureInAzureRan(); + return _azureInstanceName; + } + } + + public static bool InAzure + { + get + { + if (_inAzure == null) + { + lock (Locker) + { + try + { + _inAzure = false; + + LoadAzureSettings(); + } + catch (Exception ex) + { + StackifyLib.Utils.StackifyAPILogger.Log("Unable to load azure runtime", ex); + } + } + } + + return _inAzure ?? false; + } + } + + public static bool IsWebsite + { + get + { + if (InAzure) + { + return _azureRoleType == AzureRoleType.WebApp; + } + + return false; + } + } + + private static void EnsureInAzureRan() + { + bool ensureTestHasBeenDone = InAzure; + } + + public static void LoadAzureSettings() + { + if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")) == false && string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID")) == false) + { + _inAzure = true; + _azureRoleType = AzureRoleType.WebApp; + + 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)}]"; + + return; + } + } + + public static string GetDeploymentSlotId() + { + try + { + var siteName2 = Environment.GetEnvironmentVariable("WEBSITE_IIS_SITE_NAME") ?? string.Empty; + if (siteName2.Contains("__")) + { + return siteName2.Right(4); + } + } + catch (Exception ex) + { + StackifyLib.Utils.StackifyAPILogger.Log("#Azure #GetDeploymentSlotId failed", ex); + } + + return null; + } + } + + public enum AzureRoleType : short + { + Unknown = 0, + NotAzure = 1, + Web = 2, + Worker = 3, + Cache = 4, + WebApp = 5 + } +} diff --git a/Src/StackifyLib/Models/EnvironmentDetail.cs b/Src/StackifyLib/Models/EnvironmentDetail.cs index 1295a73..69a95d3 100644 --- a/Src/StackifyLib/Models/EnvironmentDetail.cs +++ b/Src/StackifyLib/Models/EnvironmentDetail.cs @@ -110,14 +110,22 @@ private void GetAzureInfo() public static string GetDeviceName() { var deviceName = Environment.MachineName; - var isDefaultDeviceNameEc2 = IsEc2MachineName(deviceName); - if (Config.IsEc2 == null || Config.IsEc2 == true || isDefaultDeviceNameEc2) + if (AzureConfig.InAzure && ((AzureConfig.IsWebsite) || (AzureConfig.InAzure && Environment.MachineName.StartsWith("RD")))) { - var ec2InstanceId = GetEC2InstanceId(); - if (string.IsNullOrWhiteSpace(ec2InstanceId) == false) + deviceName = AzureConfig.AzureInstanceName; + } + else + { + var isDefaultDeviceNameEc2 = IsEc2MachineName(deviceName); + + if (Config.IsEc2 == null || Config.IsEc2 == true || isDefaultDeviceNameEc2) { - deviceName = ec2InstanceId; + var ec2InstanceId = GetEC2InstanceId(); + if (string.IsNullOrWhiteSpace(ec2InstanceId) == false) + { + deviceName = ec2InstanceId; + } } } @@ -187,15 +195,23 @@ public static string GetEC2InstanceId() public static string GetDeviceName() { var deviceName = Environment.MachineName; - var isDefaultDeviceNameEc2 = IsEc2MachineName(deviceName); - if (Config.IsEc2 == null || Config.IsEc2 == true || isDefaultDeviceNameEc2) + if (AzureConfig.InAzure && ((AzureConfig.IsWebsite) || (AzureConfig.InAzure && Environment.MachineName.StartsWith("RD")))) { - var instanceID_task = GetEC2InstanceId(); - instanceID_task.Wait(); - if (string.IsNullOrWhiteSpace(instanceID_task.Result) == false) + deviceName = AzureConfig.AzureInstanceName; + } + else + { + var isDefaultDeviceNameEc2 = IsEc2MachineName(deviceName); + + if (Config.IsEc2 == null || Config.IsEc2 == true || isDefaultDeviceNameEc2) { - deviceName = instanceID_task.Result; + var instanceID_task = GetEC2InstanceId(); + instanceID_task.Wait(); + if (string.IsNullOrWhiteSpace(instanceID_task.Result) == false) + { + deviceName = instanceID_task.Result; + } } } diff --git a/Src/StackifyLib/Utils/ServerConfigHelper.cs b/Src/StackifyLib/Utils/ServerConfigHelper.cs new file mode 100644 index 0000000..67834c6 --- /dev/null +++ b/Src/StackifyLib/Utils/ServerConfigHelper.cs @@ -0,0 +1,34 @@ +using StackifyLib.Models; +using System; +using System.Collections.Generic; +using System.Text; + +namespace StackifyLib.Utils +{ + public class ServerConfigHelper + { + public const string ProductionEnvName = "Production"; + + public static string GetEnvironment() + { + if (AzureConfig.IsWebsite) + { + var key = Environment.GetEnvironmentVariable("Stackify.Environment"); + + if (string.IsNullOrEmpty(key) == false) + { + return key; + } + + if (string.IsNullOrEmpty(AzureConfig.AzureAppWebConfigEnvironment) == false) + { + return AzureConfig.AzureAppWebConfigEnvironment; + } + + return ProductionEnvName; + } + + return string.Empty; + } + } +} diff --git a/Src/StackifyLib/Utils/StringExtensions.cs b/Src/StackifyLib/Utils/StringExtensions.cs index 63178a0..791a0cb 100644 --- a/Src/StackifyLib/Utils/StringExtensions.cs +++ b/Src/StackifyLib/Utils/StringExtensions.cs @@ -30,5 +30,41 @@ public static string ToMD5Hash(this string input) } return sb.ToString(); } + + public static string Right(this string sValue, int iMaxLength) + { + //Check if the value is valid + if (string.IsNullOrEmpty(sValue)) + { + //Set valid empty string as string could be null + sValue = string.Empty; + } + else if (sValue.Length > iMaxLength) + { + //Make the string no longer than the max length + sValue = sValue.Substring(sValue.Length - iMaxLength, iMaxLength); + } + + //Return the string + return sValue; + } + + public static string Left(this string sValue, int iMaxLength) + { + //Check if the value is valid + if (string.IsNullOrEmpty(sValue)) + { + //Set valid empty string as string could be null + sValue = string.Empty; + } + else if (sValue.Length > iMaxLength) + { + //Make the string no longer than the max length + sValue = sValue.Substring(0, iMaxLength); + } + + //Return the string + return sValue; + } } }