forked from redhat-developer/s2i-dotnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-imagestreams.ps1
276 lines (233 loc) · 6.93 KB
/
install-imagestreams.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
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
<#
.SYNOPSIS
OpenShift .NET Core S2I Installer
.DESCRIPTION
Install/Update/Remove .NET Core S2I streams.
Credentials are required for the pulling the RHEL images. If the project does not contain a secret
for pulling the images yet, you can add one by specifying the '-User' and '-Password' options.
For more information see: https://access.redhat.com/articles/3399531.
.PARAMETER OS
Installs image streams based on this distro ('rhel', 'centos', or 'fedora').
.PARAMETER Namespace
Namespace to add imagestreams to. Defaults to current 'oc' project.
Set this to 'openshift' to install globally (requires admin priviledges).
.PARAMETER Remove
Remove the image streams.
.PARAMETER User
Username for pulling images from the registry..
.PARAMETER Password
Password for pulling images from the registry.
.LINK
https://github.com/redhat-developer/s2i-dotnetcore
.EXAMPLE
.\install-imagestreams -OS rhel
Install RHEL based image streams into the current OpenShift 'oc' project.
.EXAMPLE
.\install-imagestreams -Remove
Remove all image streams from the current OpenShift 'oc' project.
#>
[cmdletbinding()]
param(
[string]$OS,
[string]$Namespace=$null,
[switch]$Remove=$false,
[string]$User=$null,
[string]$Password=$null
)
$pull_secret_name="redhat-registry-dotnet-install"
Set-StrictMode -Version Latest
$ErrorActionPreference="Stop"
$ProgressPreference="SilentlyContinue"
function Say($str)
{
Write-Host "dotnet-install: $str"
}
function Execute([string] $filename, [string] $arguments)
{
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = $filename
$psi.Arguments = $arguments
$psi.RedirectStandardError = $true
$psi.RedirectStandardOutput = $true
$psi.UseShellExecute = $false
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $psi
$p.Start() | Out-Null
$stdout = $p.StandardOutput.ReadToEnd().trim()
$stderr = $p.StandardError.ReadToEnd().trim()
return $p.ExitCode, $stdout, $stderr
}
function ExecuteCheckSuccess([string] $filename, [string] $arguments)
{
$exitcode,$stdout,$stderr = Execute $filename $arguments
If ($exitcode -eq 0)
{
Write-Host $stdout
}
Else
{
throw $stderr
}
}
function OcDelete([string] $arguments)
{
$exitcode,$stdout,$stderr = Execute "oc.exe" "delete $arguments"
If (($exitcode -eq 0) -or ($stderr -like '*NotFound*'))
{
return
}
Else
{
throw "Cannot delete ${arguments}: $stderr"
}
}
function GetCurrentNamespace()
{
$exitcode, $stdout, $stderr = Execute "oc.exe" "project -q"
If ($exitcode -eq 0)
{
return $stdout.trim()
}
Else
{
throw "Cannot determine current oc namespace"
}
}
function HasDotnetImagestreams()
{
$exitcode, $stdout, $stderr = Execute "oc.exe" "get -n $Namespace is dotnet"
If ($exitcode -ne 0)
{
If ($stderr -like '*NotFound*')
{
return $false
}
Else
{
throw "Cannot determine if the project already contains dotnet imagestreams."
}
}
return $true
}
function HasSecretForRegistry()
{
$exitcode, $stdout, $stderr = Execute "oc.exe" "get secret -o name -n $namespace"
If ( $exitcode -ne 0 )
{
throw "Cannot retrieve secrets."
}
$secret_names = $stdout.Split("`n")
foreach ($secret_name in $secret_names)
{
$exitcode, $stdout, $stderr = Execute "oc.exe" "get $secret_name -o json -n $namespace"
$secret = ConvertFrom-Json $stdout
$secret_type = $secret.type
$data = $null
switch ( $secret_type )
{
"kubernetes.io/dockercfg"
{
$data = $secret.data.".dockercfg"
}
"kubernetes.io/dockerconfigjson"
{
$data = $secret.data.".dockerconfigjson"
}
}
If ( $data -ne $null )
{
$data = [Text.Encoding]::Utf8.GetString([Convert]::FromBase64String($data))
If ( $data -like "*`"$registry`"*" )
{
return $true
}
}
}
return $false
}
If ( $PSBoundParameters.Values.Count -eq 0 )
{
Get-Help $PSCommandPath
exit
}
# Check prerequisite tools
if ((Get-Command "oc.exe" -ErrorAction SilentlyContinue) -eq $null)
{
throw "Unable to find oc.exe on PATH. Please install oc."
}
# Remove
if($Remove.IsPresent)
{
Say "Removing installed image streams."
OcDelete "is dotnet"
OcDelete "is dotnet-runtime"
OcDelete "secret $pull_secret_name"
exit
}
# The OS parameter is required.
switch -wildcard ( $OS )
{
"centos*"
{
$imagestreams_url = "https://raw.githubusercontent.com/redhat-developer/s2i-dotnetcore/master/dotnet_imagestreams_centos.json"
$registry_requires_auth = $false
}
"rhel8"
{
$imagestreams_url = "https://raw.githubusercontent.com/redhat-developer/s2i-dotnetcore/master/dotnet_imagestreams_rhel8.json"
$registry_requires_auth = $false
break
}
"rhel*"
{
$imagestreams_url = "https://raw.githubusercontent.com/redhat-developer/s2i-dotnetcore/master/dotnet_imagestreams.json"
$registry_requires_auth = $true
$registry="registry.redhat.io"
}
"fedora"
{
$imagestreams_url = "https://raw.githubusercontent.com/redhat-developer/s2i-dotnetcore/master/dotnet_imagestreams_fedora.json"
$registry_requires_auth = $false
}
default
{
throw "Unsupported value for --os: $OS. Valid values are 'centos', 'rhel', and 'fedora'."
}
}
$create_secret=[bool]$User
# Inform user he may need a pull secret
If (( $create_secret -eq $false) -and ($registry_requires_auth -eq $true))
{
Say "note: The image streams for $OS require authentication against $registry. See 'Get-Help .\install-imagestreams.ps1' for more info."
}
# Determine namespace
If (-not ($PSBoundParameters.ContainsKey("Namespace")))
{
$Namespace = GetCurrentNamespace
}
# Create pull secret
If ( $create_secret -eq $true)
{
If (HasSecretForRegistry)
{
Say "A secret for the registry is already present in the namespace."
}
Else
{
Say "Creating a secret for authenticating with $registry :"
ExecuteCheckSuccess "oc.exe" "create secret docker-registry $pull_secret_name -n $namespace --docker-server=$registry --docker-username=$user --docker-password=$password --docker-email=unused"
ExecuteCheckSuccess "oc.exe" "secrets link default $pull_secret_name --for=pull -n $namespace"
ExecuteCheckSuccess "oc.exe" "secrets link builder $pull_secret_name -n $namespace"
}
}
# Install/update imagestreams
If (HasDotnetImagestreams)
{
Say "Updating image streams:"
ExecuteCheckSuccess "oc.exe" "replace -n $namespace -f $imagestreams_url"
}
Else
{
Say "Installing image streams:"
ExecuteCheckSuccess "oc.exe" "create -n $namespace -f $imagestreams_url"
}