-
Notifications
You must be signed in to change notification settings - Fork 3
/
Config.cs
748 lines (676 loc) · 25.2 KB
/
Config.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
using log4net;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace HDInsightManagementCLI
{
public class AzureStorageConfig
{
public string Name { get; set; }
public string Key { get; set; }
public string Container { get; set; }
}
public class SqlAzureConfig
{
public string Server { get; set; }
public string Database { get; set; }
public string User { get; set; }
public string Password { get; set; }
public string Type { get; set; }
}
public class Config
{
private static readonly ILog Logger = LogManager.GetLogger(typeof(HDInsightManagementCLI));
public enum ConfigName
{
AzureManagementUri,
AzureResourceManagementUri,
AzureResourceProviderNamespace,
AzureActiveDirectoryUri,
AzureActiveDirectoryRedirectUri,
AzureActiveDirectoryTenantId,
AzureActiveDirectoryClientId,
SubscriptionId,
ResourceGroupName,
ClusterLocation,
ClusterDnsName,
ClusterSize,
ClusterUsername,
ClusterPassword,
SshUsername,
SshPassword,
SshPublicKeyFilePath,
HDInsightVersion,
DefaultStorageAccountName,
DefaultStorageAccountKey,
DefaultStorageAccountContainer,
AdditionalStorageAccountNames,
AdditionalStorageAccountKeys,
SqlAzureAsMetastore,
SqlHiveMetastoreServer,
SqlHiveMetastoreDatabase,
SqlHiveMetastoreUser,
SqlHiveMetastorePassword,
SqlOozieMetastoreDatabase,
SqlOozieMetastoreServer,
SqlOozieMetastoreUser,
SqlOozieMetastorePassword,
OperationPollIntervalInSeconds,
TimeoutPeriodInMinutes,
DeleteCutoffPeriodInHours,
CleanupOnError,
SilentMode,
AutoEnableRdp,
RdpUsername,
RdpPassword,
RdpExpirationInDays,
ClusterType,
OperatingSystemType,
HeadNodeSize,
WorkerNodeSize,
ZookeeperSize,
VirtualNetworkId,
SubnetName,
}
private readonly Dictionary<ConfigName, string> _overrides =
new Dictionary<ConfigName, string>();
public Config(string[] args)
{
var argsList = new List<string>(args);
foreach (ConfigName config in System.Enum.GetValues(typeof(ConfigName)))
{
if (argsList.Count > 0)
{
string result;
if (ApplicationUtilities.TryGetArgumentValue(argsList, string.Format("/{0}:", config), out result))
{
_overrides.Add(config, result);
}
}
else
{
break;
}
}
//Ignore the first valid command
if (argsList.Count - 1 != _overrides.Count)
{
Logger.InfoFormat("WARNING! Override parse count mismatch. Overrides Passed: {0}, Overrides Parsed: {1}", argsList.Count - 1, _overrides.Count);
}
}
public string AzureManagementUri
{
get { return GetConfigurationValue(ConfigName.AzureManagementUri); }
}
public string AzureResourceManagementUri
{
get { return GetConfigurationValue(ConfigName.AzureResourceManagementUri); }
}
public string AzureResourceProviderNamespace
{
get { return GetConfigurationValue(ConfigName.AzureResourceProviderNamespace); }
}
public string AzureActiveDirectoryUri
{
get { return GetConfigurationValue(ConfigName.AzureActiveDirectoryUri); }
}
public string AzureActiveDirectoryRedirectUri
{
get { return GetConfigurationValue(ConfigName.AzureActiveDirectoryRedirectUri); }
}
public string AzureActiveDirectoryTenantId
{
get { return GetConfigurationValue(ConfigName.AzureActiveDirectoryTenantId); }
}
public string AzureActiveDirectoryClientId
{
get { return GetConfigurationValue(ConfigName.AzureActiveDirectoryClientId); }
}
public string ClusterType
{
get
{
return GetConfigurationValue(ConfigName.ClusterType);
}
}
public string OSType
{
get
{
return GetConfigurationValue(ConfigName.OperatingSystemType);
}
}
private string _subscriptionId = null;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")]
public string SubscriptionId
{
get
{
if (_subscriptionId == null)
{
_subscriptionId = GetConfigurationValue(ConfigName.SubscriptionId);
Guid guid;
var result = Guid.TryParse(_subscriptionId, out guid);
if (!result)
{
throw new ApplicationException(
String.Format(
"SubscriptionId: '{0}' is not a valid value, please use a valid subscription id or a well formed guid",
_subscriptionId));
}
}
return _subscriptionId;
}
}
public string ClusterLocation
{
get { return GetConfigurationValue(ConfigName.ClusterLocation); }
}
private string _clusterDnsName = null;
public string ClusterDnsName
{
get
{
if (_clusterDnsName == null)
{
_clusterDnsName = GetConfigurationValue(ConfigName.ClusterDnsName);
}
return _clusterDnsName;
}
set
{
_clusterDnsName = value;
}
}
private string _resourceGroupName = null;
public string ResourceGroupName
{
get
{
if (_resourceGroupName == null)
{
var result = TryGetConfigurationValue(ConfigName.ResourceGroupName, out _resourceGroupName);
if(!result || String.IsNullOrWhiteSpace(_resourceGroupName))
{
_resourceGroupName = HDInsightManagementCLIHelpers.GetResourceGroupName(SubscriptionId, ClusterLocation);
}
}
return _resourceGroupName;
}
set
{
_resourceGroupName = value;
}
}
public string ClusterUsername
{
get
{
string returnValue = null;
bool result = TryGetConfigurationValue(ConfigName.ClusterUsername, out returnValue);
if (!result)
{
returnValue = "admin";
}
return returnValue;
}
}
private string _clusterPassword = null;
public string ClusterPassword
{
get
{
if (string.IsNullOrWhiteSpace(_clusterPassword))
{
bool result = TryGetConfigurationValue(ConfigName.ClusterPassword, out _clusterPassword);
if (!result)
{
Logger.InfoFormat("Generating random cluster password of length 24 using System.Web.Security.Membership.GeneratePassword");
do
{
_clusterPassword = System.Web.Security.Membership.GeneratePassword(24, 2);
}
while (!Regex.IsMatch(_clusterPassword, HDInsightManagementCLIHelpers.HDInsightPasswordValidationRegex));
Logger.InfoFormat("PLEASE NOTE: New cluster password: {0}", _clusterPassword);
Logger.InfoFormat("If the cluster was created previously you should use the previously generated password instead.");
SaveConfigurationValue(ConfigName.ClusterPassword, _clusterPassword);
}
}
return _clusterPassword;
}
set
{
_clusterPassword = value;
}
}
private string _sshUsername;
public string SshUsername
{
get
{
if (_sshUsername == null)
{
bool result = TryGetConfigurationValue(ConfigName.SshUsername, out _sshUsername);
if (!result)
{
_sshUsername = "ssh" + ClusterUsername;
}
}
return _sshUsername;
}
}
private string _sshPassword;
public string SshPassword
{
get
{
if (_sshPassword == null)
{
bool result = TryGetConfigurationValue(ConfigName.SshPassword, out _sshPassword);
}
return _sshPassword;
}
}
public string SshPublicKeyFilePath
{
get
{
string returnValue = null;
bool result = TryGetConfigurationValue(ConfigName.SshPublicKeyFilePath, out returnValue);
if(!result)
{
var keyPair = HDInsightManagementCLIHelpers.GenerateSshKeyPair(ClusterDnsName, ClusterPassword);
Logger.InfoFormat("PLEASE NOTE: A new SSH key pair was generated. Public Key Path: {0}, Private Key Path: {1}, Passphrase: {2}", keyPair.Key, keyPair.Value, ClusterPassword);
Logger.InfoFormat("This new key path will saved in your application configuration, the passphrase is same as your cluster password.");
returnValue = keyPair.Key;
SaveConfigurationValue(ConfigName.SshPublicKeyFilePath, returnValue);
}
return returnValue;
}
}
public int ClusterSize
{
get
{
int defaultValue = 3;
int returnValue = defaultValue;
string outValue = null;
bool result = TryGetConfigurationValue(ConfigName.ClusterSize, out outValue);
if (result)
{
bool flag = int.TryParse(outValue, out returnValue);
if (!flag)
{
returnValue = defaultValue;
}
}
return returnValue;
}
}
public string HDInsightVersion
{
get
{
string returnValue = null;
bool result = TryGetConfigurationValue(ConfigName.HDInsightVersion, out returnValue);
if (!result)
{
returnValue = "default";
}
return returnValue;
}
}
private AzureStorageConfig _defaultStorageAccount;
public AzureStorageConfig DefaultStorageAccount
{
get
{
if(_defaultStorageAccount == null)
{
_defaultStorageAccount = new AzureStorageConfig()
{
Name = GetConfigurationValue(ConfigName.DefaultStorageAccountName),
Key = GetConfigurationValue(ConfigName.DefaultStorageAccountKey)
};
string container = null;
var result = TryGetConfigurationValue(ConfigName.DefaultStorageAccountContainer, out container);
if(result && !String.IsNullOrEmpty(container))
{
_defaultStorageAccount.Container = container;
}
}
return _defaultStorageAccount;
}
set
{
_defaultStorageAccount = value;
}
}
private List<AzureStorageConfig> _additionalStorageAccounts = null;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")]
public List<AzureStorageConfig> AdditionalStorageAccounts
{
get
{
if (_additionalStorageAccounts == null)
{
_additionalStorageAccounts = new List<AzureStorageConfig>();
string additionalStorageAccountNamesValue;
string additionalStorageAccountKeysValue;
if (TryGetConfigurationValue(ConfigName.AdditionalStorageAccountNames, out additionalStorageAccountNamesValue) &&
TryGetConfigurationValue(ConfigName.AdditionalStorageAccountKeys, out additionalStorageAccountKeysValue))
{
var additionalStorageAccountNames = additionalStorageAccountNamesValue.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
var additionalStorageAccountKeys = additionalStorageAccountKeysValue.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
if (additionalStorageAccountNames.Count() != additionalStorageAccountKeys.Count())
{
throw new ApplicationException(
String.Format("AdditionalStorageAccountNames.Count: {0} is not equal to AdditionalStorageAccountKeys.Count: {1}",
additionalStorageAccountNames.Count(), additionalStorageAccountKeys.Count()));
}
for (int i = 0; i < additionalStorageAccountNames.Count(); i++)
{
var additionalStorageAccount = new AzureStorageConfig()
{
Name = additionalStorageAccountNames[i],
Key = additionalStorageAccountKeys[i]
};
_additionalStorageAccounts.Add(additionalStorageAccount);
}
}
}
return _additionalStorageAccounts;
}
set
{
_additionalStorageAccounts = value;
}
}
private List<SqlAzureConfig> _sqlAzureMetastores = null;
public List<SqlAzureConfig> SqlAzureMetastores
{
get
{
if (_sqlAzureMetastores == null && SqlAzureAsMetastore)
{
_sqlAzureMetastores = new List<SqlAzureConfig>();
var sqlHive = new SqlAzureConfig()
{
Server = GetConfigurationValue(ConfigName.SqlHiveMetastoreServer),
Database = GetConfigurationValue(ConfigName.SqlHiveMetastoreDatabase),
User = GetConfigurationValue(ConfigName.SqlHiveMetastoreUser),
Password = GetConfigurationValue(ConfigName.SqlHiveMetastorePassword),
Type = "HiveMetastore"
};
_sqlAzureMetastores.Add(sqlHive);
var sqlOozie = new SqlAzureConfig()
{
Server = GetConfigurationValue(ConfigName.SqlOozieMetastoreServer),
Database = GetConfigurationValue(ConfigName.SqlOozieMetastoreDatabase),
User = GetConfigurationValue(ConfigName.SqlOozieMetastoreUser),
Password = GetConfigurationValue(ConfigName.SqlOozieMetastorePassword),
Type = "OozieMetastore"
};
_sqlAzureMetastores.Add(sqlOozie);
}
return _sqlAzureMetastores;
}
set
{
_sqlAzureMetastores = value;
}
}
public string HeadNodeSize
{
get { return GetConfigurationValue(ConfigName.HeadNodeSize); }
}
public string WorkerNodeSize
{
get { return GetConfigurationValue(ConfigName.WorkerNodeSize); }
}
public string ZookeeperSize
{
get { return GetConfigurationValue(ConfigName.ZookeeperSize); }
}
private string _virtualNetworkId = null;
public string VirtualNetworkId
{
get
{
if (_virtualNetworkId == null)
{
bool result = TryGetConfigurationValue(ConfigName.VirtualNetworkId, out _virtualNetworkId);
}
return _virtualNetworkId;
}
}
private string _subnetName = null;
public string SubnetName
{
get
{
if (_subnetName == null)
{
bool result = TryGetConfigurationValue(ConfigName.SubnetName, out _subnetName);
}
return _subnetName;
}
}
public int OperationPollIntervalInSeconds
{
get
{
int defaultValue = 30;
int returnValue = defaultValue;
string outValue = null;
bool result = TryGetConfigurationValue(ConfigName.OperationPollIntervalInSeconds, out outValue);
if (result)
{
bool flag = int.TryParse(outValue, out returnValue);
if (!flag)
{
returnValue = defaultValue;
}
}
return returnValue;
}
}
public int TimeoutPeriodInMinutes
{
get
{
int defaultValue = 60;
int returnValue = defaultValue;
string outValue = null;
bool result = TryGetConfigurationValue(ConfigName.TimeoutPeriodInMinutes, out outValue);
if (result)
{
bool flag = int.TryParse(outValue, out returnValue);
if (!flag)
returnValue = defaultValue;
}
return returnValue;
}
}
int _deleteCutoffPeriodInHours = 24;
public int DeleteCutoffPeriodInHours
{
get
{
string outValue;
bool result = TryGetConfigurationValue(ConfigName.DeleteCutoffPeriodInHours, out outValue);
if (result)
{
int returnValue;
bool flag = int.TryParse(outValue, out returnValue);
if (flag)
_deleteCutoffPeriodInHours = returnValue;
}
return _deleteCutoffPeriodInHours;
}
set
{
_deleteCutoffPeriodInHours = value;
}
}
public bool SqlAzureAsMetastore
{
get
{
string returnValue;
bool result = TryGetConfigurationValue(ConfigName.SqlAzureAsMetastore, out returnValue);
if (!result)
{
returnValue = "false";
}
return IsValueYesOrTrue(returnValue);
}
}
public bool CleanupOnError
{
get
{
string returnValue;
bool result = TryGetConfigurationValue(ConfigName.CleanupOnError, out returnValue);
if (!result)
{
returnValue = "false";
}
return IsValueYesOrTrue(returnValue);
}
set
{
SetConfigurationValue(ConfigName.CleanupOnError, value.ToString());
}
}
public bool SilentMode
{
get
{
string returnValue;
bool result = TryGetConfigurationValue(ConfigName.SilentMode, out returnValue);
if (!result)
returnValue = "false";
return IsValueYesOrTrue(returnValue);
}
}
public string RdpUsername
{
get
{
string returnValue = null;
bool result = TryGetConfigurationValue(ConfigName.RdpUsername, out returnValue);
if (!result)
{
returnValue = "rdp" + ClusterUsername;
}
return returnValue;
}
}
public string RdpPassword
{
get
{
string returnValue = null;
bool result = TryGetConfigurationValue(ConfigName.RdpPassword, out returnValue);
if (!result)
{
returnValue = ClusterPassword;
SaveConfigurationValue(ConfigName.RdpPassword, returnValue);
}
return returnValue;
}
}
public string RdpExpirationInDays
{
get { return GetConfigurationValue(ConfigName.RdpExpirationInDays); }
}
public bool AutoEnableRdp
{
get
{
string returnValue = null;
bool result = TryGetConfigurationValue(ConfigName.AutoEnableRdp, out returnValue);
if (!result)
{
returnValue = "false";
}
return IsValueYesOrTrue(returnValue);
}
}
private bool IsValueYesOrTrue(string value)
{
if (String.Compare(value, "y", StringComparison.OrdinalIgnoreCase) == 0 || String.Compare(value, "yes", StringComparison.OrdinalIgnoreCase) == 0 ||
String.Compare(value, "true", StringComparison.OrdinalIgnoreCase) == 0)
{
return true;
}
return false;
}
internal string GetConfigurationValue(ConfigName configName)
{
string returnValue = null;
if (_overrides.ContainsKey(configName))
{
returnValue = _overrides[configName];
}
else
{
returnValue = ConfigurationManager.AppSettings[configName.ToString()];
}
if (String.IsNullOrEmpty(returnValue))
{
throw new ArgumentNullException(configName.ToString());
}
return returnValue;
}
internal bool TryGetConfigurationValue(ConfigName configName, out string configValue)
{
configValue = _overrides.ContainsKey(configName) ? _overrides[configName] : ConfigurationManager.AppSettings[configName.ToString()];
return !String.IsNullOrEmpty(configValue);
}
private void SetConfigurationValue(ConfigName configName, string value)
{
if (_overrides.ContainsKey(configName))
{
_overrides[configName] = value;
}
else
{
_overrides.Add(configName, value);
}
}
private void SaveConfigurationValue(ConfigName configName, string value)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection settings = config.AppSettings.Settings;
settings[configName.ToString()].Value = value;
Logger.InfoFormat("Saving application configuration - Key: {0}, Value: {1}", configName.ToString(), value);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
}
public void PrintRunConfiguration()
{
if (this.SilentMode)
{
return;
}
var sb = new StringBuilder();
sb.AppendLine(Environment.NewLine + "====================Current Run Configuration====================");
foreach (ConfigName configPropertyName in Enum.GetValues(typeof(ConfigName)))
{
string configValue;
this.TryGetConfigurationValue(configPropertyName, out configValue);
if (!String.IsNullOrEmpty(configValue))
{
sb.AppendLine(String.Format("{0} : {1}", configPropertyName, configValue));
}
}
Logger.Info(sb.ToString());
}
}
}