This repository has been archived by the owner on Aug 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RootConfiguration.ps1
91 lines (78 loc) · 3.43 KB
/
RootConfiguration.ps1
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
Import-Module -Name DscBuildHelpers
$Error.Clear()
$buildVersion = $env:BHBuildVersion
if (-not $buildVersion) {
$buildVersion = '0.0.0'
}
$environment = $node.Environment
if (-not $environment ){
$environment = 'NA'
}
configuration "RootConfiguration"
{
Import-DscResource -ModuleName PSDesiredStateConfiguration
Import-DscResource -ModuleName CommonvSphereTasks
$module = Get-Module -Name PSDesiredStateConfiguration
& $module {
param(
[string]$BuildVersion,
[string]$Environment
)
$Script:PSTopConfigurationName = "MOF_$($Environment)_$($BuildVersion)"
} $buildVersion, $environment
node $ConfigurationData.AllNodes.NodeName {
Write-Host "`r`n$('-'*75)`r`n$($Node.Name) : $($Node.NodeName) : $(&$module { $Script:PSTopConfigurationName })" -ForegroundColor Yellow
$configurationNames = Resolve-NodeProperty -PropertyPath 'Configurations'
foreach ($configurationName in $configurationNames) {
Write-Debug "`tLooking up params for $configurationName"
$properties = Resolve-NodeProperty -PropertyPath $configurationName -DefaultValue @{}
$dscError = [System.Collections.ArrayList]::new()
(Get-DscSplattedResource -ResourceName $configurationName -ExecutionName $configurationName -Properties $properties -NoInvoke).Invoke($properties)
if($Error[0] -and $lastError -ne $Error[0]) {
$lastIndex = [Math]::Max(($Error.LastIndexOf($lastError) -1), -1)
if($lastIndex -gt 0) {
$Error[0..$lastIndex].Foreach{
if($message = Get-DscErrorMessage -Exception $_.Exception) {
$null = $dscError.Add($message)
}
}
}
else {
if($message = Get-DscErrorMessage -Exception $Error[0].Exception) {
$null = $dscError.Add($message)
}
}
$lastError = $Error[0]
}
if($dscError.Count -gt 0) {
$warningMessage = " $($Node.Name) : $($Node.Role) ::> $configurationName "
$n = [System.Math]::Max(1, 100 - $warningMessage.Length)
Write-Host "$warningMessage$('.' * $n)FAILED" -ForeGroundColor Yellow
$dscError.Foreach{
Write-Host "`t$message" -ForeGroundColor Yellow
}
}
else {
$okMessage = " $($Node.Name) : $($Node.Role) ::> $configurationName "
$n = [System.Math]::Max(1, 100 - $okMessage.Length)
Write-Host "$okMessage$('.' * $n)OK" -ForeGroundColor Green
}
}
}
}
$cd = @{}
$cd.Datum = $ConfigurationData.Datum
foreach ($n in $configurationData.AllNodes)
{
$cd.AllNodes = @($ConfigurationData.AllNodes | Where-Object NodeName -eq $n.NodeName)
try
{
RootConfiguration -ConfigurationData $cd -OutputPath (Join-Path -Path $BuildOutput -ChildPath MOF)
}
catch
{
Write-Host "Error occured during compilation of node '$($n.NodeName)' : $($_.Exception.Message)" -ForegroundColor Red
$relevantErrors = $Error | Where-Object Exception -isnot [System.Management.Automation.ItemNotFoundException]
Write-Host ($relevantErrors[0..2] | Out-String) -ForegroundColor Red
}
}