Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure unhealthy Windows instances get marked correctly #614

Merged
merged 3 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions packer/windows/conf/bin/bk-install-elastic-stack.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ function on_error {
$errorLine=$_.InvocationInfo.ScriptLineNumber
$errorMessage=$_.Exception

$instance_id=(Invoke-WebRequest -UseBasicParsing http://169.254.169.254/latest/meta-data/instance-id).content

aws autoscaling set-instance-health `
--instance-id "(Invoke-WebRequest -UseBasicParsing http://169.254.169.254/latest/meta-data/instance-id).content" `
--instance-id "$instance_id" `
--health-status Unhealthy

cfn-signal `
Expand Down Expand Up @@ -117,11 +119,32 @@ Set-PSDebug -Trace 0

Write-Output "Creating buildkite-agent user account in Administrators group"

$Count = Get-Random -min 24 -max 32
$Password = -join ((65..90) + (97..122) + (48..57) | Get-Random -Count $Count | ForEach-Object {[char]$_})
$UserName = "buildkite-agent"

New-LocalUser -Name $UserName -PasswordNeverExpires -Password ($Password | ConvertTo-SecureString -AsPlainText -Force) | out-null
$StopLoop = $false
[int]$RetryCount = "0"

# a Try/Catch block is used in a loop to make a few extra attempts at creating the user account before finally giving up and failing
# because sometimes the generated random password does not satisfy the system's password policy
Do {
Try {
$Count = Get-Random -min 24 -max 32
$Password = -join ((65..90) + (97..122) + (48..57) | Get-Random -Count $Count | ForEach-Object {[char]$_})

New-LocalUser -Name $UserName -PasswordNeverExpires -Password ($Password | ConvertTo-SecureString -AsPlainText -Force) | out-null
$StopLoop = $true
}
Catch {
If ($RetryCount -gt 10){
Write-Output "Could not create $UserName user after 10 retries."
exit 1
}
Else {
Write-Output "Could not create $UserName user, retrying..."
$RetryCount = $RetryCount + 1
}
}
} While ($StopLoop -eq $false)

If ($Env:BUILDKITE_WINDOWS_ADMINISTRATOR -eq "true") {
Add-LocalGroupMember -Group "Administrators" -Member $UserName | out-null
Expand Down
2 changes: 1 addition & 1 deletion templates/aws-stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ Resources:
powershell -file C:\buildkite-agent\bin\bk-configure-docker.ps1 >> C:\buildkite-agent\elastic-stack.log

$Env:BUILDKITE_STACK_NAME="${AWS::StackName}"
$Env:BUILDKITE_STACK_VERSION=%v
$Env:BUILDKITE_STACK_VERSION="%v"
$Env:BUILDKITE_SCALE_IN_IDLE_PERIOD="${ScaleInIdlePeriod}"
$Env:BUILDKITE_SECRETS_BUCKET="${LocalSecretsBucket}"
$Env:BUILDKITE_AGENT_TOKEN="${BuildkiteAgentToken}"
Expand Down