This commit is contained in:
Marius Oprin 2018-10-26 11:33:36 +03:00
Родитель 32b0cc2c69
Коммит ac3e195450
4 изменённых файлов: 255 добавлений и 403 удалений

1
.gitignore поставляемый
Просмотреть файл

@ -7,6 +7,7 @@ bin/
.env
pkg/operations/junit.xml
pkg/operations/dcosupgrade/windows_upgrade_scripts.go
pkg/acsengine/templates.go
pkg/i18n/translations.go

Просмотреть файл

@ -1,36 +1,16 @@
filter Timestamp {"[$(Get-Date -Format o)] $_"}
$ErrorActionPreference = "Stop"
function Write-Log($message)
{
$msg = $message | Timestamp
Write-Output $msg
}
$global:DockerDir = Join-Path $env:SystemDrive "docker"
$global:upgradeDir = Join-Path $env:SystemDrive "AzureData\upgrade\NEW_VERSION"
$global:genconfDir = Join-Path $global:upgradeDir "genconf"
$global:upgradeUrlPath = Join-Path $global:upgradeDir "upgrade_url"
$global:volume = "$(${global:genconfDir} -replace '\\', '/')/serve/:c:/nginx/html:ro"
$global:networkName = "customnat"
function RetryCurl($url, $path)
{
for($i = 1; $i -le 10; $i++) {
try {
& curl.exe --keepalive-time 2 -fLsS --retry 20 -o $path $url
if ($LASTEXITCODE -eq 0) {
Write-Log "Downloaded $url in $i attempts"
return
}
} catch {
}
Sleep(2)
}
throw "Failed to download $url"
}
function CreateDockerStart($fileName, $log, $volume)
{
$content = "Start-Transcript -path $log -append"
Set-Content -Path $fileName -Value $content
$content = 'Write-Output ("[{0}] {1}" -f (Get-Date -Format o), "Starting docker container")'
Add-Content -Path $fileName -Value $content
$content = "& docker.exe run --rm -d --network customnat -p 8086:80 -v $volume nginx:1803"
Add-Content -Path $fileName -Value $content
$content = '
$global:DockerStartScript = @"
Start-Transcript -path "${global:DockerDir}\StartDocker.log" -append
Write-Output ("[{0}] {1}" -f (Get-Date -Format o), "Starting docker container")
& docker.exe run --rm -d --network $global:networkName -p 8086:80 -v $global:volume nginx:1803
if ($LASTEXITCODE -ne 0) {
Write-Output ("[{0}] {1}" -f (Get-Date -Format o), "Failed to run docker image")
Stop-Transcript
@ -39,96 +19,177 @@ if ($LASTEXITCODE -ne 0) {
Write-Output ("[{0}] {1}" -f (Get-Date -Format o), "Successfully started docker container")
Stop-Transcript
Exit 0
'
Add-Content -Path $fileName -Value $content
"@
filter Timestamp {"[$(Get-Date -Format o)] $_"}
function Write-Log {
Param(
[string]$Message
)
$msg = $Message | Timestamp
Write-Output $msg
}
function Start-ExecuteWithRetry {
Param(
[Parameter(Mandatory=$true)]
[ScriptBlock]$ScriptBlock,
[int]$MaxRetryCount=10,
[int]$RetryInterval=3,
[string]$RetryMessage,
[array]$ArgumentList=@()
)
$currentErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Continue"
$retryCount = 0
while ($true) {
Write-Log "Start-ExecuteWithRetry attempt $retryCount"
try {
$res = Invoke-Command -ScriptBlock $ScriptBlock `
-ArgumentList $ArgumentList
$ErrorActionPreference = $currentErrorActionPreference
Write-Log "Start-ExecuteWithRetry terminated"
return $res
} catch [System.Exception] {
$retryCount++
if ($retryCount -gt $MaxRetryCount) {
$ErrorActionPreference = $currentErrorActionPreference
Write-Log "Start-ExecuteWithRetry exception thrown"
throw
} else {
if($RetryMessage) {
Write-Log "Start-ExecuteWithRetry RetryMessage: $RetryMessage"
} elseif($_) {
Write-Log "Start-ExecuteWithRetry Retry: $_.ToString()"
}
Start-Sleep $RetryInterval
}
}
}
}
function Start-FileDownload {
Param(
[Parameter(Mandatory=$true)]
[string]$URL,
[Parameter(Mandatory=$true)]
[string]$Destination,
[Parameter(Mandatory=$false)]
[int]$RetryCount=10
)
$params = @('-fLsS', '-o', "`"${Destination}`"", "`"${URL}`"")
Start-ExecuteWithRetry -ScriptBlock {
$p = Start-Process -FilePath 'curl.exe' -NoNewWindow -ArgumentList $params -Wait -PassThru
if($p.ExitCode -ne 0) {
Throw "Fail to download $URL"
}
} -MaxRetryCount $RetryCount -RetryInterval 3 -RetryMessage "Failed to download ${URL}. Retrying"
}
function New-NginxDockerContainer {
# Stop docker container
$process = docker ps -q
if ($LASTEXITCODE -ne 0) {
throw "Failed to run docker ps command"
}
if ($process) {
Write-Log "Stopping nginx service $process"
& docker.exe kill $process
if ($LASTEXITCODE -ne 0) {
throw "Failed to run docker kill $process"
}
}
Write-Log "Starting nginx service"
# Run docker container with nginx
$network = $(docker.exe network ls --quiet --filter name=$global:networkName)
if($LASTEXITCODE -ne 0) {
Throw "Failed to list Docker networks"
}
# only create customnat if it does not exist
if(!$network) {
docker.exe network create --driver="nat" --opt "com.docker.network.windowsshim.disable_gatewaydns=true" $global:networkName
if ($LASTEXITCODE -ne 0) {
Throw "Failed to create $global:networkName Docker network"
}
}
Push-Location $global:DockerDir
& docker.exe build --network $global:networkName -t nginx:1803 $global:DockerDir
if ($LASTEXITCODE -ne 0) {
throw "Failed to build docker image"
}
& docker.exe run --rm -d --network $global:networkName -p 8086:80 -v $global:volume nginx:1803
if ($LASTEXITCODE -ne 0) {
throw "Failed to run docker image"
}
$script = Join-Path $global:DockerDir "StartDocker.ps1"
Set-Content -Path $script -Value $global:DockerStartScript
}
function New-ConfigFiles {
Write-Log "Starting upgrade configuration"
$BootstrapURL = "WIN_BOOTSTRAP_URL"
$logPath = Join-Path $global:upgradeDir "dcos_generate_config.log"
Write-Log "Setting up Windows bootstrap node for upgrade"
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue $global:upgradeDir
New-Item -ItemType Directory -Force -Path $global:genconfDir
$path = Join-Path $global:genconfDir "config.yaml"
Copy-Item "C:\AzureData\config-win.NEW_VERSION.yaml" $path
Copy-Item "c:\temp\genconf\ip-detect.ps1" $global:genconfDir
Push-Location $global:upgradeDir
$path = Join-Path $global:upgradeDir "dcos_generate_config.windows.tar.xz"
Start-FileDownload -URL $BootstrapURL -Destination $path
$7z_exe = Join-Path $env:ProgramFiles "7-Zip\7z.exe"
$7z_cmd = "`"$7z_exe`" e .\dcos_generate_config.windows.tar.xz -so | `"$7z_exe`" x -si -ttar"
cmd.exe /c "$7z_cmd"
if ($LASTEXITCODE -ne 0) {
throw "Failed to untar dcos_generate_config.windows.tar.xz"
}
& .\dcos_generate_config.ps1 --generate-node-upgrade-script CURR_VERSION > $logPath
if ($LASTEXITCODE -ne 0) {
throw "Failed to run dcos_generate_config.ps1"
}
# Fetch upgrade script URL
$match = Select-String -Path $logPath -Pattern "Node upgrade script URL:" -CaseSensitive
if (-Not $match) {
throw "Missing Node upgrade script URL in $logPath"
}
$upgradeUrl = ($match.Line -replace 'Node upgrade script URL:','').Trim()
if (-Not $upgradeUrl) {
throw "Bad Node upgrade script URL in $logPath"
}
Set-Content -Path $global:upgradeUrlPath -Value $upgradeUrl -Encoding Ascii
$upgradeUrl = Get-Content -Path $global:upgradeUrlPath -Encoding Ascii
if (-Not $upgradeUrl) {
Remove-Item $global:upgradeUrlPath -Force
throw "Failed to set up bootstrap node. Please try again"
} else {
# keep Write-Output - used in parsing
Write-Output "Setting up bootstrap node completed. Node upgrade script URL $upgradeUrl"
}
}
try {
Write-Log "Starting upgrade configuration"
$BootstrapURL = "WIN_BOOTSTRAP_URL"
$upgradeDir = "C:\AzureData\upgrade\NEW_VERSION"
$genconfDir = Join-Path $upgradeDir "genconf"
$logPath = Join-Path $upgradeDir "dcos_generate_config.log"
$upgradeUrlPath = Join-Path $upgradeDir "upgrade_url"
Write-Log "Setting up Windows bootstrap node for upgrade"
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue $upgradeDir
New-Item -ItemType Directory -Force -Path $genconfDir
$path = Join-Path $genconfDir "config.yaml"
cp "C:\AzureData\config-win.NEW_VERSION.yaml" $path
cp "c:\temp\genconf\ip-detect.ps1" $genconfDir
cd $upgradeDir
$path = Join-Path $upgradeDir "dcos_generate_config.windows.tar.xz"
RetryCurl $BootstrapURL $path
cmd.exe /c '"C:\Program Files\7-Zip\7z.exe" e .\dcos_generate_config.windows.tar.xz -so | "C:\Program Files\7-Zip\7z.exe" x -si -ttar'
if ($LASTEXITCODE -ne 0) {
throw "Failed to untar dcos_generate_config.windows.tar.xz"
}
& .\dcos_generate_config.ps1 --generate-node-upgrade-script CURR_VERSION > $logPath
if ($LASTEXITCODE -ne 0) {
throw "Failed to run dcos_generate_config.ps1"
}
# Fetch upgrade script URL
$match = Select-String -Path $logPath -Pattern "Node upgrade script URL:" -CaseSensitive
if (-Not $match) {
throw "Missing Node upgrade script URL in $logPath"
}
$url = ($match.Line -replace 'Node upgrade script URL:','').Trim()
if (-Not $url) {
throw "Bad Node upgrade script URL in $logPath"
}
# Stop docker container
$process = docker ps -q
if ($process) {
Write-Log "Stopping nginx service $process"
& docker.exe kill $process
}
Write-Log "Starting nginx service"
# Run docker container with nginx
cd c:\docker
# only create customnat if it does not exist
$a = docker network ls | select-string -pattern "customnat"
if ($a.count -eq 0)
{
& docker.exe network create --driver="nat" --opt "com.docker.network.windowsshim.disable_gatewaydns=true" "customnat"
if ($LASTEXITCODE -ne 0) {
throw "Failed to create customnat docker network"
}
}
& docker.exe build --network customnat -t nginx:1803 c:\docker
if ($LASTEXITCODE -ne 0) {
throw "Failed to build docker image"
}
$volume = ($genconfDir+"/serve/:c:/nginx/html:ro")
& docker.exe run --rm -d --network customnat -p 8086:80 -v $volume nginx:1803
if ($LASTEXITCODE -ne 0) {
throw "Failed to run docker image"
}
CreateDockerStart "c:\docker\StartDocker.ps1" "c:\docker\StartDocker.log" $volume
Set-Content -Path $upgradeUrlPath -Value $url -Encoding Ascii
$url = Get-Content -Path $upgradeUrlPath -Encoding Ascii
if (-Not $url) {
Remove-Item $upgradeUrlPath -Force
throw "Failed to set up bootstrap node. Please try again"
} else {
# keep Write-Output - used in parsing
Write-Output "Setting up bootstrap node completed. Node upgrade script URL $url"
}
New-ConfigFiles
New-NginxDockerContainer
Write-Log "Setting up bootstrap node completed"
exit 0
} catch {
Write-Log "Failed to upgrade Windows bootstrap node: $_"
exit 1
}
Write-Log "Setting up bootstrap node completed"
exit 0

Просмотреть файл

@ -1,51 +1,99 @@
filter Timestamp {"[$(Get-Date -Format o)] $_"}
function Write-Log($message)
{
$msg = $message | Timestamp
function Write-Log {
Param(
[string]$Message
)
$msg = $Message | Timestamp
Write-Output $msg
}
function RetryCurl($url, $path)
{
for($i = 1; $i -le 10; $i++) {
function Start-ExecuteWithRetry {
Param(
[Parameter(Mandatory=$true)]
[ScriptBlock]$ScriptBlock,
[int]$MaxRetryCount=10,
[int]$RetryInterval=3,
[string]$RetryMessage,
[array]$ArgumentList=@()
)
$currentErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Continue"
$retryCount = 0
while ($true) {
Write-Log "Start-ExecuteWithRetry attempt $retryCount"
try {
& curl.exe --keepalive-time 2 -fLsS --retry 20 -o $path $url
if ($LASTEXITCODE -eq 0) {
Write-Log "Downloaded $url in $i attempts"
return
$res = Invoke-Command -ScriptBlock $ScriptBlock `
-ArgumentList $ArgumentList
$ErrorActionPreference = $currentErrorActionPreference
Write-Log "Start-ExecuteWithRetry terminated"
return $res
} catch [System.Exception] {
$retryCount++
if ($retryCount -gt $MaxRetryCount) {
$ErrorActionPreference = $currentErrorActionPreference
Write-Log "Start-ExecuteWithRetry exception thrown"
throw
} else {
if($RetryMessage) {
Write-Log "Start-ExecuteWithRetry RetryMessage: $RetryMessage"
} elseif($_) {
Write-Log "Start-ExecuteWithRetry Retry: $_.ToString()"
}
Start-Sleep $RetryInterval
}
} catch {
}
Sleep(2)
}
throw "Failed to download $url"
}
function Start-FileDownload {
Param(
[Parameter(Mandatory=$true)]
[string]$URL,
[Parameter(Mandatory=$true)]
[string]$Destination,
[Parameter(Mandatory=$false)]
[int]$RetryCount=10
)
$params = @('-fLsS', '-o', "`"${Destination}`"", "`"${URL}`"")
Start-ExecuteWithRetry -ScriptBlock {
$p = Start-Process -FilePath 'curl.exe' -NoNewWindow -ArgumentList $params -Wait -PassThru
if($p.ExitCode -ne 0) {
Throw "Fail to download $URL"
}
} -MaxRetryCount $RetryCount -RetryInterval 3 -RetryMessage "Failed to download ${URL}. Retrying"
}
$upgradeScriptURL = "WIN_UPGRADE_SCRIPT_URL"
$upgradeDir = "C:\AzureData\upgrade\NEW_VERSION"
$log = "C:\AzureData\upgrade_NEW_VERSION.log"
$upgradeDir = Join-Path $env:SystemDrive "AzureData\upgrade\NEW_VERSION"
$log = Join-Path $env:SystemDrive "AzureData\upgrade_NEW_VERSION.log"
$adminUser = "ADMIN_USER"
$password = "ADMIN_PASSWORD"
try {
Start-Transcript -Path $log -append
Write-Log "Starting node upgrade to DCOS NEW_VERSION"
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue $upgradeDir
New-Item -ItemType Directory -Force -Path $upgradeDir
cd $upgradeDir
Start-Transcript -Path $log -append
Write-Log "Starting node upgrade to DCOS NEW_VERSION"
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue $upgradeDir
New-Item -ItemType Directory -Force -Path $upgradeDir
Push-Location $upgradeDir
[Environment]::SetEnvironmentVariable("SYSTEMD_SERVICE_USERNAME", "$env:computername\\$adminUser", "Machine")
[Environment]::SetEnvironmentVariable("SYSTEMD_SERVICE_PASSWORD", $password, "Machine")
[Environment]::SetEnvironmentVariable("SYSTEMD_SERVICE_USERNAME", "$env:computername\\$adminUser", "Machine")
[Environment]::SetEnvironmentVariable("SYSTEMD_SERVICE_PASSWORD", $password, "Machine")
[Environment]::SetEnvironmentVariable("SYSTEMD_SERVICE_USERNAME", "$env:computername\\$adminUser", "Process")
[Environment]::SetEnvironmentVariable("SYSTEMD_SERVICE_PASSWORD", $password, "Process")
[Environment]::SetEnvironmentVariable("SYSTEMD_SERVICE_USERNAME", "$env:computername\\$adminUser", "Process")
[Environment]::SetEnvironmentVariable("SYSTEMD_SERVICE_PASSWORD", $password, "Process")
RetryCurl $upgradeScriptURL "dcos_node_upgrade.ps1"
Start-FileDownload -URL $upgradeScriptURL -Destination "dcos_node_upgrade.ps1"
.\dcos_node_upgrade.ps1
.\dcos_node_upgrade.ps1
if ($LASTEXITCODE -ne 0) {
throw "Failed to run dcos_node_upgrade.ps1"
}
}catch {
Write-Log "Failed to upgrade Windows agent node: $_"
Stop-Transcript
Write-Log "Failed to upgrade Windows agent node: $_"
Stop-Transcript
exit 1
}
Write-Log "Successfully upgraded Windows agent node"

Различия файлов скрыты, потому что одна или несколько строк слишком длинны