Skip to content

Commit

Permalink
Terminate the instance directly rather than mark it unhealthy
Browse files Browse the repository at this point in the history
This will allow us to have more control over the timing and capacity
of the ASG.

Signed-off-by: Tom Duffield <tom@chef.io>
  • Loading branch information
tduffield committed Jan 31, 2019
1 parent 346ff34 commit 724d56c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packer/conf/bin/bk-install-elastic-stack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ EOF

cat << EOF > /etc/systemd/system/buildkite-agent@.service.d/10-power-off-stop.conf
[Service]
ExecStopPost=/usr/local/bin/mark-asg-unhealthy
ExecStopPost=/usr/local/bin/terminate-instance ${BUILDKITE_TERMINATE_INSTANCE_AFTER_JOB_DECREASE_DESIRED_CAPACITY}
ExecStopPost=/bin/sudo poweroff
EOF
fi
Expand Down
8 changes: 0 additions & 8 deletions packer/conf/buildkite-agent/scripts/mark-asg-unhealthy

This file was deleted.

14 changes: 14 additions & 0 deletions packer/conf/buildkite-agent/scripts/terminate-instance
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

set -euo pipefail

if [[ "${1:-false}" == "true" ]] ; then
should_decrement="should"
else
should_decrement="no-should"
fi

instance_id=$(curl -fsSL http://169.254.169.254/latest/meta-data/instance-id)
region=$(curl -fsSL http://169.254.169.254/latest/meta-data/placement/availability-zone | head -c -1)

aws autoscaling terminate-instance-in-auto-scaling-group --region "$region" --instance-id "$instance_id" "--${should_decrement}-decrement-desired-capacity"
2 changes: 1 addition & 1 deletion packer/scripts/install-buildkite-agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ sudo cp /tmp/conf/buildkite-agent/systemd/buildkite-agent@.service /etc/systemd/

echo "Adding termination scripts..."
sudo cp /tmp/conf/buildkite-agent/scripts/stop-agent-gracefully /usr/local/bin/stop-agent-gracefully
sudo cp /tmp/conf/buildkite-agent/scripts/mark-asg-unhealthy /usr/local/bin/mark-asg-unhealthy
sudo cp /tmp/conf/buildkite-agent/scripts/terminate-instance /usr/local/bin/terminate-instance

echo "Copying built-in plugins..."
sudo mkdir -p /usr/local/buildkite-aws-stack/plugins
Expand Down
12 changes: 11 additions & 1 deletion templates/aws-stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Metadata:
- BuildkiteAgentExperiments
- BuildkiteTerminateInstanceAfterJob
- BuildkiteTerminateInstanceAfterJobTimeout
- BuildkiteTerminateInstanceAfterJobShouldDecreaseCapacity

- Label:
default: Network Configuration
Expand Down Expand Up @@ -127,7 +128,7 @@ Parameters:
Default: ""

BuildkiteTerminateInstanceAfterJob:
Description: Whether or not to terminate the instance after the job has completed.
Description: Set to "true" to terminate the instance after a job has completed.
Type: String
AllowedValues:
- "true"
Expand All @@ -140,6 +141,14 @@ Parameters:
Default: 1800
MinValue: 1

BuildkiteTerminateInstanceAfterJobDecreaseDesiredCapacity:
Description: Set to "true" to decrease the desired capacity of the autoscaling group by 1 when terminating an instance after the job completes.
Type: String
AllowedValues:
- "true"
- "false"
Default: "false"

BuildkiteQueue:
Description: Queue name that agents will use, targeted in pipeline steps using "queue={value}"
Type: String
Expand Down Expand Up @@ -715,6 +724,7 @@ Resources:
BUILDKITE_LIFECYCLE_TOPIC=${AgentLifecycleTopic} \
BUILDKITE_TERMINATE_INSTANCE_AFTER_JOB=${BuildkiteTerminateInstanceAfterJob} \
BUILDKITE_TERMINATE_INSTANCE_AFTER_JOB_TIMEOUT=${BuildkiteTerminateInstanceAfterJobTimeout} \
BUILDKITE_TERMINATE_INSTANCE_AFTER_JOB_DECREASE_DESIRED_CAPACITY=${BuildkiteTerminateInstanceAfterJobDecreaseDesiredCapacity} \
AWS_DEFAULT_REGION=${AWS::Region} \
SECRETS_PLUGIN_ENABLED=${EnableSecretsPlugin} \
ECR_PLUGIN_ENABLED=${EnableECRPlugin} \
Expand Down

0 comments on commit 724d56c

Please sign in to comment.