-
Notifications
You must be signed in to change notification settings - Fork 0
/
New-ScratchImage.ps1
136 lines (126 loc) · 3.78 KB
/
New-ScratchImage.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
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
Remove-Item -Path "build" -Force -Recurse -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Path "build" | Out-Null
New-Item -ItemType Directory -Path "build\layer" | Out-Null
# Create the files that ProcessBaseLayer on Windows validates when unpackage images.
# These files can be empty, they just need to exist at specific paths.
New-Item -ItemType Directory -Path "build\layer\Files\Windows\System32\config" -Force | Out-Null
foreach ($f in @('DEFAULT', 'SAM', 'SECURITY', 'SOFTWARE', 'SYSTEM')) {
New-Item -ItemType File -Name $f -Path "build\layer\Files\Windows\System32\config" | Out-Null
}
# Create layer.tar
Push-Location build\layer
tar.exe -cf layer.tar Files
Pop-Location
# Get hash of layer.tar
$layerHash = (Get-FileHash -Algorithm SHA256 "build\layer\layer.tar").Hash.ToLower()
Write-Output "layer.tar hash: $layerHash"
# Add json and VERSION files for layer
New-Item -ItemType Directory -Path "build\image\${layerhash}" | Out-Null
"1.0" | Out-File -FilePath "build\image\${layerHash}\VERSION" -Encoding ascii
Copy-Item -Path "build\layer\layer.tar" -Destination "build\image\${layerHash}\layer.tar"
$now = [DateTime]::UtcNow.ToString("o")
@"
{
"id": "${layerHash}",
"created": "${now}",
"container_config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": null,
"Cmd": null,
"Image": "",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": null
},
"config": {
"Hostname": "",
"Domainname": "",
"User": "ContainerUser",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": null,
"Cmd": [
"c:\\windows\\system32\\cmd.exe"
],
"Image": "",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": null
},
"architecture": "amd64",
"os": "windows"
}
"@ | Out-File -FilePath "build\image\${layerHash}\json" -Encoding ascii
# Create the image config and manifest files
@"
{
"architecture": "amd64",
"config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": null,
"Cmd": [
"c:\\windows\\system32\\cmd.exe"
],
"Image": "",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": null
},
"created": "${now}",
"history": [
{
"created": "${now}"
}
],
"os": "windows",
"os.version": "10.0.17763.1",
"rootfs": {
"type": "layers",
"diff_ids": [
"sha256:${layerHash}"
]
}
}
"@ | Out-File -FilePath "build\image\config.json" -Encoding ascii
$configHash = (Get-FileHash -Algorithm SHA256 "build\image\config.json").Hash.ToLower()
Move-Item -Path "build\image\config.json" -Destination "build\image\${configHash}.json"
@"
[
{
"Config": "${configHash}.json",
"Layers": [
"${layerHash}/layer.tar"
]
}
]
"@ | Out-File -FilePath "build\image\manifest.json" -Encoding ascii
# Tar the image
tar.exe -cf "build\host-process-scratch.tar" -C "build\image" .
# Output a file with the image hash so we can import/push the image from CI
"${configHash}" | Out-File -FilePath "build\image-id.txt" -Encoding ascii -NoNewline