/
DeploymentEnvironment.cs
134 lines (108 loc) · 5.2 KB
/
DeploymentEnvironment.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.WindowsAzure.Storage;
using NLog;
namespace NuGetGallery.Operations
{
public class DeploymentEnvironment
{
public static readonly Logger Log = LogManager.GetLogger("DeploymentEnvironment");
public IDictionary<string, string> Settings { get; private set; }
public string EnvironmentName { get; private set; }
public string SubscriptionId { get; private set; }
public string SubscriptionName { get; private set; }
public SqlConnectionStringBuilder MainDatabase { get; private set; }
public SqlConnectionStringBuilder WarehouseDatabase { get; private set; }
public CloudStorageAccount MainStorage { get; private set; }
public CloudStorageAccount BackupStorage { get; private set; }
public CloudStorageAccount DiagnosticsStorage { get; private set; }
public Uri SqlDacEndpoint { get; private set; }
public Uri LicenseReportService { get; private set; }
public NetworkCredential LicenseReportServiceCredentials { get; private set; }
public DeploymentEnvironment(string environmentName, string subscriptionId, string subscriptionName, IDictionary<string, string> deploymentSettings)
{
Settings = deploymentSettings;
EnvironmentName = environmentName;
SubscriptionId = subscriptionId;
SubscriptionName = subscriptionName;
MainDatabase = GetSqlConnectionStringBuilder("Operations.Sql.Primary");
WarehouseDatabase = GetSqlConnectionStringBuilder("Operations.Sql.Warehouse");
MainStorage = GetCloudStorageAccount("Operations.Storage.Primary");
BackupStorage = GetCloudStorageAccount("Operations.Storage.Backup") ?? MainStorage;
DiagnosticsStorage = GetCloudStorageAccount("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString") ?? MainStorage;
SqlDacEndpoint = Get("Operations.SqlDac", str => new Uri(str, UriKind.Absolute));
LicenseReportService = Get("Operations.LicenseReport.Service", str => new Uri(str, UriKind.Absolute));
string licenseReportUser = Get("Operations.LicenseReport.User");
string licenseReportPassword = Get("Operations.LicenseReport.Password");
if (!String.IsNullOrEmpty(licenseReportUser) && !String.IsNullOrEmpty(licenseReportPassword))
{
LicenseReportServiceCredentials = new NetworkCredential(licenseReportUser, licenseReportPassword);
}
}
private string Get(string key)
{
string value;
if (!Settings.TryGetValue(key, out value))
{
return null;
}
return value;
}
private T Get<T>(string key, Func<string, T> thunk)
{
string val = Get(key);
return String.IsNullOrEmpty(val) ? default(T) : thunk(val);
}
private CloudStorageAccount GetCloudStorageAccount(string key)
{
return Get(key, str => CloudStorageAccount.Parse(str));
}
private SqlConnectionStringBuilder GetSqlConnectionStringBuilder(string key)
{
return Get(key, str => new SqlConnectionStringBuilder(str));
}
private static IDictionary<string, string> BuildSettingsDictionary(XDocument doc)
{
XNamespace ns = XNamespace.Get("http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration");
return (from s in doc.Element(ns + "ServiceConfiguration")
.Element(ns + "Role")
.Element(ns + "ConfigurationSettings")
.Elements(ns + "Setting")
select new KeyValuePair<string, string>(
s.Attribute("name").Value,
s.Attribute("value").Value))
.ToDictionary(p => p.Key, p => p.Value);
}
public static DeploymentEnvironment FromEnvironment()
{
string serviceConfig = Environment.GetEnvironmentVariable("NUGET_SERVICE_CONFIG");
IDictionary<string, string> settings = null;
if (!String.IsNullOrEmpty(serviceConfig) && File.Exists(serviceConfig))
{
try
{
// Load the file
var doc = XDocument.Load(serviceConfig);
// Build a dictionary of settings
settings = BuildSettingsDictionary(doc);
}
catch(Exception ex)
{
Log.ErrorException("Unable to load service config: " + serviceConfig, ex);
}
}
return new DeploymentEnvironment(
Environment.GetEnvironmentVariable("NUCMD_ENVIRONMENT_NAME"),
Environment.GetEnvironmentVariable("NUCMD_SUBSCRIPTION_ID"),
Environment.GetEnvironmentVariable("NUCMD_SUBSCRIPTION_NAME"),
settings ?? new Dictionary<string, string>());
}
}
}