Skip to content
Merged
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
127 changes: 127 additions & 0 deletions Src/StackifyLib/Models/AzureConfig.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
38 changes: 27 additions & 11 deletions Src/StackifyLib/Models/EnvironmentDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -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;
}
}
}

Expand Down
34 changes: 34 additions & 0 deletions Src/StackifyLib/Utils/ServerConfigHelper.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
36 changes: 36 additions & 0 deletions Src/StackifyLib/Utils/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}