|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Text; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | +using StackifyLib.Utils; |
| 7 | +using System.Linq; |
| 8 | +#if NETFULL |
| 9 | +using Microsoft.Win32; |
| 10 | +#endif |
| 11 | + |
| 12 | +namespace StackifyLib.Models |
| 13 | +{ |
| 14 | + public class AzureConfig |
| 15 | + { |
| 16 | + private static bool? _inAzure; |
| 17 | + private static AzureRoleType _azureRoleType = AzureRoleType.Unknown; |
| 18 | + private static string _azureRoleName; |
| 19 | + private static string _azureInstanceName; |
| 20 | + private static string _entryPoint; |
| 21 | + |
| 22 | + private static readonly DateTime AppStarted = DateTime.UtcNow; |
| 23 | + |
| 24 | + private static readonly object Locker = new object(); |
| 25 | + |
| 26 | + |
| 27 | + public static string AzureAppWebConfigEnvironment { get; set; } |
| 28 | + public static string AzureAppWebConfigApiKey { get; set; } |
| 29 | + public static string AzureDriveLetter { get; set; } |
| 30 | + |
| 31 | + public static string AzureInstanceName |
| 32 | + { |
| 33 | + get |
| 34 | + { |
| 35 | + EnsureInAzureRan(); |
| 36 | + return _azureInstanceName; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public static bool InAzure |
| 41 | + { |
| 42 | + get |
| 43 | + { |
| 44 | + if (_inAzure == null) |
| 45 | + { |
| 46 | + lock (Locker) |
| 47 | + { |
| 48 | + try |
| 49 | + { |
| 50 | + _inAzure = false; |
| 51 | + |
| 52 | + LoadAzureSettings(); |
| 53 | + } |
| 54 | + catch (Exception ex) |
| 55 | + { |
| 56 | + StackifyLib.Utils.StackifyAPILogger.Log("Unable to load azure runtime", ex); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return _inAzure ?? false; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static bool IsWebsite |
| 66 | + { |
| 67 | + get |
| 68 | + { |
| 69 | + if (InAzure) |
| 70 | + { |
| 71 | + return _azureRoleType == AzureRoleType.WebApp; |
| 72 | + } |
| 73 | + |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static void EnsureInAzureRan() |
| 79 | + { |
| 80 | + bool ensureTestHasBeenDone = InAzure; |
| 81 | + } |
| 82 | + |
| 83 | + public static void LoadAzureSettings() |
| 84 | + { |
| 85 | + if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")) == false && string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID")) == false) |
| 86 | + { |
| 87 | + _inAzure = true; |
| 88 | + _azureRoleType = AzureRoleType.WebApp; |
| 89 | + |
| 90 | + var slotId = GetDeploymentSlotId() ?? "0000"; |
| 91 | + |
| 92 | + _azureRoleName = Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME"); |
| 93 | + _azureInstanceName = $"{Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")} {ServerConfigHelper.GetEnvironment()} [{slotId}-{Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID").Left(6)}]"; |
| 94 | + |
| 95 | + return; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public static string GetDeploymentSlotId() |
| 100 | + { |
| 101 | + try |
| 102 | + { |
| 103 | + var siteName2 = Environment.GetEnvironmentVariable("WEBSITE_IIS_SITE_NAME") ?? string.Empty; |
| 104 | + if (siteName2.Contains("__")) |
| 105 | + { |
| 106 | + return siteName2.Right(4); |
| 107 | + } |
| 108 | + } |
| 109 | + catch (Exception ex) |
| 110 | + { |
| 111 | + StackifyLib.Utils.StackifyAPILogger.Log("#Azure #GetDeploymentSlotId failed", ex); |
| 112 | + } |
| 113 | + |
| 114 | + return null; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public enum AzureRoleType : short |
| 119 | + { |
| 120 | + Unknown = 0, |
| 121 | + NotAzure = 1, |
| 122 | + Web = 2, |
| 123 | + Worker = 3, |
| 124 | + Cache = 4, |
| 125 | + WebApp = 5 |
| 126 | + } |
| 127 | +} |
0 commit comments