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