@@ -116,34 +116,32 @@ spawn=${BUILDKITE_AGENTS_PER_INSTANCE}
116
116
no-color=true
117
117
EOF
118
118
119
+ terminate_instance_on_agent_stop () {
120
+ mkdir -p /etc/systemd/system/buildkite-agent.service.d/
121
+ cat << EOF > /etc/systemd/system/buildkite-agent.service.d/10-power-off-stop.conf
122
+ [Service]
123
+ ExecStopPost=/usr/local/bin/terminate-instance
124
+ ExecStopPost=/bin/sudo poweroff
125
+ EOF
126
+
127
+ # If we modify the systemd, we need to rebuild the dependency tree
128
+ systemctl daemon-reload
129
+ }
130
+
119
131
# Add conditional config to the agent config
120
132
if [[ " ${BUILDKITE_TERMINATE_INSTANCE_AFTER_JOB:- false} " == " true" ]] ; then
121
133
cat << EOF >> /etc/buildkite-agent/buildkite-agent.cfg
122
134
disconnect-after-job=true
123
135
disconnect-after-job-timeout=${BUILDKITE_TERMINATE_INSTANCE_AFTER_JOB_TIMEOUT}
124
136
EOF
125
- elif [[ " ${BUILDKITE_LAMBDA_AUTOSCALING} " == " true" ]] ; then
126
- cat << EOF >> /etc/buildkite-agent/buildkite-agent.cfg
127
- disconnect-after-idle-timeout=${BUILDKITE_SCALE_DOWN_PERIOD}
128
- EOF
137
+ terminate_instance_on_agent_stop
129
138
fi
130
139
131
- # If we are using the lambda for autoscaling, always decrease capacity
132
140
if [[ " ${BUILDKITE_LAMBDA_AUTOSCALING} " == " true" ]] ; then
133
- BUILDKITE_TERMINATE_INSTANCE_AFTER_JOB_DECREASE_DESIRED_CAPACITY=true
134
- fi
135
-
136
- if [[ " ${BUILDKITE_TERMINATE_INSTANCE_AFTER_JOB} " == " true" || " ${BUILDKITE_LAMBDA_AUTOSCALING} " == " true" ]] ; then
137
- mkdir -p /etc/systemd/system/buildkite-agent.service.d/
138
-
139
- cat << EOF > /etc/systemd/system/buildkite-agent.service.d/10-power-off-stop.conf
140
- [Service]
141
- ExecStopPost=/usr/local/bin/terminate-instance ${BUILDKITE_TERMINATE_INSTANCE_AFTER_JOB_DECREASE_DESIRED_CAPACITY}
142
- ExecStopPost=/bin/sudo poweroff
141
+ cat << EOF >> /etc/buildkite-agent/buildkite-agent.cfg
142
+ disconnect-after-idle-timeout=${BUILDKITE_SCALE_IN_IDLE_PERIOD}
143
143
EOF
144
-
145
- # If we modify the systemd, we need to rebuild the dependency tree
146
- systemctl daemon-reload
144
+ terminate_instance_on_agent_stop
147
145
fi
148
146
149
147
chown buildkite-agent: /etc/buildkite-agent/buildkite-agent.cfg
168
166
169
167
cat << EOF > /etc/lifecycled
170
168
AWS_REGION=${AWS_REGION}
171
- LIFECYCLED_SNS_TOPIC=${BUILDKITE_LIFECYCLE_TOPIC}
172
169
LIFECYCLED_HANDLER=/usr/local/bin/stop-agent-gracefully
173
170
LIFECYCLED_CLOUDWATCH_GROUP=/buildkite/lifecycled
174
171
EOF
0 commit comments