/
ACS::ECS::InstallDockerCE.yml
533 lines (469 loc) · 19.4 KB
/
ACS::ECS::InstallDockerCE.yml
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
FormatVersion: OOS-2019-06-01
Description:
name-en: ACS::ECS::InstallDockerCE
name-zh-cn: ECS实例安装Docker社区版
en: Install docker CE on ECS instance
zh-cn: ECS实例安装Docker社区版
Parameters:
regionId:
Description:
en: The ID of region
zh-cn: 地域ID
Type: String
Default: '{{ ACS::RegionId }}'
action:
Type: String
Label:
en: Action
zh-cn: 操作类型
AllowedValues:
- install
- uninstall
Default: install
instanceId:
Description:
en: The ID of ECS instance
zh-cn: ECS实例ID
Type: String
Tasks:
- Name: queryInstanceOSType
Action: 'ACS::ExecuteApi'
Description:
en: Get ECS instance OS type
zh-cn: 获取实例操作系统类型
Properties:
Service: ECS
API: DescribeInstances
Parameters:
RegionId: '{{ regionId }}'
InstanceIds:
- '{{ instanceId }}'
Outputs:
OSType:
Type: String
ValueSelector: 'Instances.Instance[].OSType'
- Name: installDockerCEOnLinuxInstance
When:
'Fn::Equals':
- '{{ queryInstanceOSType.OSType }}'
- linux
Action: 'ACS::ECS::RunCommand'
Description:
en: Install docker CE on linux instance
zh-cn: 在linux类型实例上安装dockerCE
Properties:
instanceId: '{{ instanceId }}'
regionId: '{{ regionId }}'
commandType: RunShellScript
commandContent: |-
#!/bin/bash
function check_distro() {
for i in issue os-release issue.net centos-release redhat-release; do
if [[ -f /etc/$i ]]; then
if grep -q "$1" /etc/$i; then
return 0
fi
fi
done
return 1
}
function install_docker_CE() {
if check_distro 'CentOS'; then
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's+https://mirrors.aliyun.com+http://mirrors.cloud.aliyuncs.com+' /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce
elif check_distro 'Ubuntu'; then
apt-get update
apt-get -y install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL http://mirrors.cloud.aliyuncs.com/docker-ce/linux/ubuntu/gpg | apt-key add -
add-apt-repository -y "deb [arch=amd64] http://mirrors.cloud.aliyuncs.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
apt-get -y install docker-ce
elif check_distro 'Debian'; then
apt-get update
apt-get -y install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL http://mirrors.cloud.aliyuncs.com/docker-ce/linux/debian/gpg | apt-key add -
add-apt-repository -y "deb [arch=amd64] http://mirrors.cloud.aliyuncs.com/docker-ce/linux/debian $(lsb_release -cs) stable"
apt-get update
apt-get -y install docker-ce
elif check_distro 'Fedora'; then
dnf config-manager --add-repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/fedora/docker-ce.repo
sed -i 's+https://mirrors.aliyun.com+http://mirrors.cloud.aliyuncs.com+' /etc/yum.repos.d/docker-ce.repo
dnf -y install docker-ce
elif check_distro 'Alibaba Cloud Linux (Aliyun Linux) 2'; then
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's+https://mirrors.aliyun.com+http://mirrors.cloud.aliyuncs.com+' /etc/yum.repos.d/docker-ce.repo
yum install yum-plugin-releasever-adapter --disablerepo=* --enablerepo=plus
yum -y install docker-ce
elif check_distro 'Alibaba Cloud Linux 3'; then
dnf config-manager --add-repo=http://mirrors.cloud.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's+https://mirrors.aliyun.com+http://mirrors.cloud.aliyuncs.com+' /etc/yum.repos.d/docker-ce.repo
dnf -y install dnf-plugin-releasever-adapter --repo alinux3-plus
dnf -y install docker-ce
else
echo 'Linux release version not support.'
exit 1;
fi
}
function uninstall_docker_CE() {
if check_distro 'CentOS'; then
yum -y remove docker-ce
elif check_distro 'Ubuntu'; then
apt-get -y remove docker-ce
elif check_distro 'Debian'; then
apt-get -y remove docker-ce
elif check_distro 'Fedora'; then
dnf -y remove docker-ce
elif check_distro 'Alibaba Cloud Linux (Aliyun Linux) 2'; then
yum -y remove docker-ce
elif check_distro 'Alibaba Cloud Linux 3'; then
dnf -y remove docker-ce
else
echo 'Linux release version not support.'
exit 1;
fi
}
if [ "{{ action }}" = "install" ]; then
install_docker_CE
docker -v
else
uninstall_docker_CE
fi
Outputs:
commandOutput:
Type: String
ValueSelector: invocationOutput
- Name: installDockerCEOnWindowsInstance
When:
'Fn::Equals':
- '{{ queryInstanceOSType.OSType }}'
- windows
Action: 'ACS::ECS::RunCommand'
Description:
en: Install docker CE on windows instance
zh-cn: 在windows类型实例上安装dockerCE
Properties:
instanceId: '{{ instanceId }}'
regionId: '{{ regionId }}'
commandType: RunPowerShellScript
commandContent: |-
[CmdletBinding(DefaultParameterSetName="Standard")]
param(
[string]
[ValidateNotNullOrEmpty()]
$DockerPath = "default",
[string]
[ValidateNotNullOrEmpty()]
$DockerDPath = "default",
[string]
[ValidateNotNullOrEmpty()]
$DockerVersion = "latest"
)
$global:RebootRequired = $false
$global:ErrorFile = "$pwd\Install-ContainerHost.err"
$global:BootstrapTask = "ContainerBootstrap"
$global:HyperVImage = "NanoServer"
$global:AdminPriviledges = $false
$global:DefaultDockerLocation = "http://mirrors.cloud.aliyuncs.com/docker-ce/win/static/stable/x86_64/"
$global:DockerDataPath = "$($env:ProgramData)\docker"
$global:DockerServiceName = "docker"
function
Install-Feature
{
[CmdletBinding()]
param(
[ValidateNotNullOrEmpty()]
[string]
$FeatureName
)
Write-Output "Querying status of Windows feature: $FeatureName..."
if (Get-Command Get-WindowsFeature -ErrorAction SilentlyContinue)
{
if ((Get-WindowsFeature $FeatureName).Installed)
{
Write-Output "Feature $FeatureName is already enabled."
}
else
{
Write-Output "Enabling feature $FeatureName..."
}
$featureInstall = Add-WindowsFeature $FeatureName
if ($featureInstall.RestartNeeded -eq "Yes")
{
$global:RebootRequired = $true;
}
}
else
{
if ((Get-WindowsOptionalFeature -Online -FeatureName $FeatureName).State -eq "Disabled")
{
if (Test-Nano)
{
throw "This NanoServer deployment does not include $FeatureName. Please add the appropriate package"
}
Write-Output "Enabling feature $FeatureName..."
$feature = Enable-WindowsOptionalFeature -Online -FeatureName $FeatureName -All -NoRestart
if ($feature.RestartNeeded -eq "True")
{
$global:RebootRequired = $true;
}
}
else
{
Write-Output "Feature $FeatureName is already enabled."
if (Test-Nano)
{
#
# Get-WindowsEdition is not present on Nano. On Nano, we assume reboot is not needed
#
}
elseif ((Get-WindowsEdition -Online).RestartNeeded)
{
$global:RebootRequired = $true;
}
}
}
}
function
Install-ContainerHost
{
"If this file exists when Install-ContainerHost.ps1 exits, the script failed!" | Out-File -FilePath $global:ErrorFile
if (Test-Client)
{
if (-not $HyperV)
{
Write-Output "Enabling Hyper-V containers by default for Client SKU"
$HyperV = $true
}
}
#
# Validate required Windows features
#
Install-Feature -FeatureName Containers
if ($HyperV)
{
Install-Feature -FeatureName Hyper-V
}
#
# Install and register Docker
#
if (Test-Docker)
{
Write-Output "Docker is already installed."
}
else
{
Install-Docker -DockerPath $DockerPath -DockerDPath $DockerDPath -ContainerBaseImage $ContainerBaseImage
}
if ($TarPath)
{
cmd /c "docker load -i `"$TarPath`""
}
Remove-Item $global:ErrorFile
Write-Output "Script complete! Docker is already installed. You must restart the machine to start docker!"
docker -v
}
function
Copy-File
{
[CmdletBinding()]
param(
[string]
$SourcePath,
[string]
$DestinationPath
)
if ($SourcePath -eq $DestinationPath)
{
return
}
if (Test-Path $SourcePath)
{
Copy-Item -Path $SourcePath -Destination $DestinationPath
}
elseif (($SourcePath -as [System.URI]).AbsoluteURI -ne $null)
{
if (Test-Nano)
{
$handler = New-Object System.Net.Http.HttpClientHandler
$client = New-Object System.Net.Http.HttpClient($handler)
$client.Timeout = New-Object System.TimeSpan(0, 30, 0)
$cancelTokenSource = [System.Threading.CancellationTokenSource]::new()
$responseMsg = $client.GetAsync([System.Uri]::new($SourcePath), $cancelTokenSource.Token)
$responseMsg.Wait()
if (!$responseMsg.IsCanceled)
{
$response = $responseMsg.Result
if ($response.IsSuccessStatusCode)
{
$downloadedFileStream = [System.IO.FileStream]::new($DestinationPath, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write)
$copyStreamOp = $response.Content.CopyToAsync($downloadedFileStream)
$copyStreamOp.Wait()
$downloadedFileStream.Close()
if ($copyStreamOp.Exception -ne $null)
{
throw $copyStreamOp.Exception
}
}
}
}
elseif ($PSVersionTable.PSVersion.Major -ge 5)
{
#
# We disable progress display because it kills performance for large downloads (at least on 64-bit PowerShell)
#
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $SourcePath -OutFile $DestinationPath -UseBasicParsing
$ProgressPreference = 'Continue'
}
else
{
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile($SourcePath, $DestinationPath)
}
}
else
{
throw "Cannot copy from $SourcePath"
}
}
function
Test-Client()
{
return (-not ((Get-Command Get-WindowsFeature -ErrorAction SilentlyContinue) -or (Test-Nano)))
}
function
Test-Nano()
{
$EditionId = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name 'EditionID').EditionId
return (($EditionId -eq "ServerStandardNano") -or
($EditionId -eq "ServerDataCenterNano") -or
($EditionId -eq "NanoServer") -or
($EditionId -eq "ServerTuva"))
}
function
Install-Docker()
{
[CmdletBinding()]
param(
[string]
[ValidateNotNullOrEmpty()]
$DockerPath = "default",
[string]
[ValidateNotNullOrEmpty()]
$DockerDPath = "default",
[string]
[ValidateNotNullOrEmpty()]
$NATSubnet,
[switch]
$SkipDefaultHost,
[string]
$ContainerBaseImage
)
#If one of these are set to default then the whole .zip needs to be downloaded anyways.
Write-Output "DOCKER $DockerPath"
if ($DockerPath -eq "default" -or $DockerDPath -eq "default") {
Write-Output "Checking Docker versions"
#Get the list of .zip packages available from docker.
$availableVersions = ((Invoke-WebRequest -Uri $DefaultDockerLocation -UseBasicParsing).Links | Where-Object {$_.href -like "docker*"}).href | Sort-Object -Descending
#Parse the versions from the file names
$availableVersions = ($availableVersions | Select-String -Pattern "docker-(\d+\.\d+\.\d+).+" -AllMatches | Select-Object -Expand Matches | %{ $_.Groups[1].Value })
$version = $availableVersions[0]
if($DockerVersion -ne "latest") {
$version = $DockerVersion
if(!($availableVersions | Select-String $DockerVersion)) {
Write-Error "Docker version supplied $DockerVersion was invalid, please choose from the list of available versions: $availableVersions"
throw "Invalid docker version supplied."
}
}
$zipUrl = $global:DefaultDockerLocation + "docker-$version.zip"
$destinationFolder = "$env:UserProfile\DockerDownloads"
if(!(Test-Path "$destinationFolder")) {
md -Path $destinationFolder | Out-Null
} elseif(Test-Path "$destinationFolder\docker-$version") {
Remove-Item -Recurse -Force "$destinationFolder\docker-$version"
}
Write-Output "Downloading $zipUrl to $destinationFolder\docker-$version.zip"
Copy-File -SourcePath $zipUrl -DestinationPath "$destinationFolder\docker-$version.zip"
Expand-Archive -Path "$destinationFolder\docker-$version.zip" -DestinationPath "$destinationFolder\docker-$version"
if($DockerPath -eq "default") {
$DockerPath = "$destinationFolder\docker-$version\docker\docker.exe"
}
if($DockerDPath -eq "default") {
$DockerDPath = "$destinationFolder\docker-$version\docker\dockerd.exe"
}
}
Write-Output "Installing Docker... $DockerPath"
Copy-File -SourcePath $DockerPath -DestinationPath $env:windir\System32\docker.exe
Write-Output "Installing Docker daemon... $DockerDPath"
Copy-File -SourcePath $DockerDPath -DestinationPath $env:windir\System32\dockerd.exe
$dockerConfigPath = Join-Path $global:DockerDataPath "config"
if (!(Test-Path $dockerConfigPath))
{
md -Path $dockerConfigPath | Out-Null
}
#
# Register the docker service.
# Configuration options should be placed at %programdata%\docker\config\daemon.json
#
Write-Output "Configuring the docker service..."
$daemonSettings = New-Object PSObject
$certsPath = Join-Path $global:DockerDataPath "certs.d"
if (Test-Path $certsPath)
{
$daemonSettings | Add-Member NoteProperty hosts @("npipe://", "0.0.0.0:2376")
$daemonSettings | Add-Member NoteProperty tlsverify true
$daemonSettings | Add-Member NoteProperty tlscacert (Join-Path $certsPath "ca.pem")
$daemonSettings | Add-Member NoteProperty tlscert (Join-Path $certsPath "server-cert.pem")
$daemonSettings | Add-Member NoteProperty tlskey (Join-Path $certsPath "server-key.pem")
}
elseif (!$SkipDefaultHost.IsPresent)
{
# Default local host
$daemonSettings | Add-Member NoteProperty hosts @("npipe://")
}
if ($NATSubnet -ne "")
{
$daemonSettings | Add-Member NoteProperty fixed-cidr $NATSubnet
}
$daemonSettingsFile = Join-Path $dockerConfigPath "daemon.json"
$daemonSettings | ConvertTo-Json | Out-File -FilePath $daemonSettingsFile -Encoding ASCII
& dockerd --register-service --service-name $global:DockerServiceName
}
function
Test-Docker()
{
$service = Get-Service -Name $global:DockerServiceName -ErrorAction SilentlyContinue
return ($service -ne $null)
}
try
{
switch($action)
{
"install" {
Install-ContainerHost
}
"uninstall" {
Write-Error "Not support uninstall DockerCE on Windows"
}
}
}
catch
{
Write-Error $_
}
Outputs:
commandOutput:
Type: String
ValueSelector: invocationOutput
Outputs:
commandOutput:
Type: String
Value:
'Fn::If':
- 'Fn::Equals':
- linux
- '{{ queryInstanceOSType.OSType }}'
- '{{ installDockerCEOnLinuxInstance.commandOutput }}'
- '{{ installDockerCEOnWindowsInstance.commandOutput }}'