forked from Sitecore/docker-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-support.psm1
238 lines (204 loc) · 8.92 KB
/
build-support.psm1
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
# borrowed from https://github.com/RamblingCookieMonster/PSDepend/blob/master/PSDepend/Private/Get-ClonedObject.ps1
Function Get-ClonedObject {
param($DeepCopyObject)
$memStream = new-object IO.MemoryStream
$formatter = new-object Runtime.Serialization.Formatters.Binary.BinaryFormatter
$formatter.Serialize($memStream, $DeepCopyObject)
$memStream.Position = 0
$formatter.Deserialize($memStream)
}
#--------------------------------------------------------------------------------------------------
# borrowed from https://github.com/RamblingCookieMonster/PSDepend/blob/master/PSDepend/Private/Sort-WithCustomList.ps1
Function Sort-ObjectWithCustomList {
Param (
[parameter(ValueFromPipeline = $true)]
[PSObject]
$InputObject,
[parameter(Position = 1)]
[String]
$Property,
[parameter()]
[Object[]]
$CustomList
)
Begin {
# convert customList (array) to hash
$hash = @{}
$rank = 0
$customList | Select-Object -Unique | ForEach-Object {
$key = $_
$hash.Add($key, $rank)
$rank++
}
# create script block for sorting
# items not in custom list will be last in sort order
$sortOrder = {
$key = if ($Property) { $_.$Property } else { $_ }
$rank = $hash[$key]
if ($rank -ne $null) {
$rank
}
else {
[System.Double]::PositiveInfinity
}
}
# create a place to collect objects from pipeline
# (I don't know how to match behavior of Sort's InputObject parameter)
$objects = @()
}
Process {
$objects += $InputObject
}
End {
$objects | Sort-Object -Property $sortOrder
}
}
#--------------------------------------------------------------------------------------------------
# borrowed from https://github.com/RamblingCookieMonster/PSDepend/blob/master/PSDepend/Private/Get-TopologicalSort.ps1
# Input is a hashtable of @{ID = @(Depended,On,IDs);...}
function Get-TopologicalSort {
param(
[Parameter(Mandatory = $true, Position = 0)]
[hashtable] $edgeList
)
# Make sure we can use HashSet
Add-Type -AssemblyName System.Core
# Clone it so as to not alter original
$currentEdgeList = [hashtable] (Get-ClonedObject $edgeList)
# algorithm from http://en.wikipedia.org/wiki/Topological_sorting#Algorithms
$topologicallySortedElements = New-Object System.Collections.ArrayList
$setOfAllNodesWithNoIncomingEdges = New-Object System.Collections.Queue
$fasterEdgeList = @{}
# Keep track of all nodes in case they put it in as an edge destination but not source
$allNodes = New-Object -TypeName System.Collections.Generic.HashSet[object] -ArgumentList (, [object[]] $currentEdgeList.Keys)
foreach ($currentNode in $currentEdgeList.Keys) {
$currentDestinationNodes = [array] $currentEdgeList[$currentNode]
if ($currentDestinationNodes.Length -eq 0) {
$setOfAllNodesWithNoIncomingEdges.Enqueue($currentNode)
}
foreach ($currentDestinationNode in $currentDestinationNodes) {
if (!$allNodes.Contains($currentDestinationNode)) {
[void] $allNodes.Add($currentDestinationNode)
}
}
# Take this time to convert them to a HashSet for faster operation
$currentDestinationNodes = New-Object -TypeName System.Collections.Generic.HashSet[object] -ArgumentList (, [object[]] $currentDestinationNodes )
[void] $fasterEdgeList.Add($currentNode, $currentDestinationNodes)
}
# Now let's reconcile by adding empty dependencies for source nodes they didn't tell us about
foreach ($currentNode in $allNodes) {
if (!$currentEdgeList.ContainsKey($currentNode)) {
[void] $currentEdgeList.Add($currentNode, (New-Object -TypeName System.Collections.Generic.HashSet[object]))
$setOfAllNodesWithNoIncomingEdges.Enqueue($currentNode)
}
}
$currentEdgeList = $fasterEdgeList
while ($setOfAllNodesWithNoIncomingEdges.Count -gt 0) {
$currentNode = $setOfAllNodesWithNoIncomingEdges.Dequeue()
[void] $currentEdgeList.Remove($currentNode)
[void] $topologicallySortedElements.Add($currentNode)
foreach ($currentEdgeSourceNode in $currentEdgeList.Keys) {
$currentNodeDestinations = $currentEdgeList[$currentEdgeSourceNode]
if ($currentNodeDestinations.Contains($currentNode)) {
[void] $currentNodeDestinations.Remove($currentNode)
if ($currentNodeDestinations.Count -eq 0) {
[void] $setOfAllNodesWithNoIncomingEdges.Enqueue($currentEdgeSourceNode)
}
}
}
}
if ($currentEdgeList.Count -gt 0) {
throw "Graph has at least one cycle!"
}
return $topologicallySortedElements
}
#--------------------------------------------------------------------------------------------------
# borrowed from https://github.com/RamblingCookieMonster/PSDepend/blob/master/PSDepend/Private/Sort-PSDependency.ps1
function Sort-Images {
[cmdletbinding()]
param(
[object[]]$Images
)
$Order = @{}
Foreach ($Image in $Images) {
if ($Image.BaseImages) {
if (-not $Order.ContainsKey($Image.FullName)) {
$Order.add($Image.FullName, $Image.BaseImages)
}
}
}
if ($Order.Keys.Count -gt 0) {
$ImageOrder = Get-TopologicalSort $Order
Sort-ObjectWithCustomList -InputObject $Images -Property FullName -CustomList $ImageOrder
}
else {
$Images
}
}
###################################################################################################################################################
function Scan-ImageBuildFolders {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateScript( {Test-Path $_ -PathType 'Container'})]
[string]$Path,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Filter,
[Parameter(Mandatory = $true)]
[ValidateScript( {Test-Path $_ -PathType 'Container'})]
[string]$InstallSourcePath,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Repository # Can include Organization if needed: <Organization>/<Repository> | <Repository>
)
Get-ChildItem -Path $Path -Filter $Filter | Foreach-Object {
$imageFolder = $_
$buildFilePath = Join-Path $imageFolder.FullName "\build.json"
if (Test-Path $buildFilePath -PathType Leaf) {
$data = Get-Content -Path $buildFilePath | ConvertFrom-Json
$sources = $data.sources | ForEach-Object {
Write-Output (Join-Path $InstallSourcePath $_)
}
# Build up tag to use
$imageName = "{0}:{1}" -f $Repository, $data.tag
$baseImage = '<None>'
if ($data.args) {
$args = $data.args
$args.PSobject.Properties | ForEach-Object {
if ($_.value -is [string]) {
$_.value = $_.value.Replace('${Repository}', $Repository)
}
}
$baseImage = $args.PSobject.Properties | ForEach-Object { if ($_.name -match "BASE_IMAGE") {Write-Output $_.value}}
}
$baseImages = Get-ChildItem -Path $imageFolder.FullName -Filter "Dockerfile" | Foreach-Object {
$fromClauses = Get-Content -Path $_.FullName | Where-Object { $_.StartsWith("FROM ") }
$fromClauses | ForEach-Object {
$image = $_
if ($image -like "* as *") {
$image = $image.Substring(0, $image.IndexOf(" as "))
}
if ([string]::IsNullOrEmpty($image)) {
throw ("Invalid dockerfile '{0}', FROM image could not be read." -f $_.FullName)
}
#$image = $image.Replace('${BASE_IMAGE}', $baseImage)
Write-Output $image
}
}
$baseImages = $baseImages | Select-Object -Unique | ForEach-Object { Write-Output $_.Replace("FROM ", "").Trim() } | ForEach-Object { Write-Output $_.Replace('${BASE_IMAGE}', $baseImage) }
Write-Output (New-Object PSObject -Property @{
Tag = $data.tag;
Args = $args;
FullName = $imageName;
Memory = $data.memory;
Path = $imageFolder.FullName;
BaseImages = $baseImages;
Sources = $sources;
})
}
else {
throw ("Invalid version folder '{0}', file not found: '{1}'." -f $imageFolder.Name, $buildFilePath)
}
}
}