@@ -15,7 +15,7 @@ on_error() {
15
15
--instance-id " $INSTANCE_ID " \
16
16
--health-status Unhealthy
17
17
then
18
- echo Failed to set instance health to unhealthy >&2
18
+ echo Failed to set instance health to unhealthy.
19
19
fi
20
20
fi
21
21
@@ -32,7 +32,7 @@ on_error() {
32
32
trap ' on_error $LINENO' ERR
33
33
34
34
on_exit () {
35
- echo " ${BASH_SOURCE[0]} completed successfully." >&2
35
+ echo " ${BASH_SOURCE[0]} completed successfully."
36
36
}
37
37
38
38
trap on_exit EXIT
@@ -44,15 +44,15 @@ exec > >(tee -a /var/log/elastic-stack.log | logger -t user-data -s 2>/dev/conso
44
44
# This needs to happen first so that the error reporting works
45
45
token=$( curl -X PUT -H " X-aws-ec2-metadata-token-ttl-seconds: 60" --fail --silent --show-error --location http://169.254.169.254/latest/api/token)
46
46
INSTANCE_ID=$( curl -H " X-aws-ec2-metadata-token: $token " --fail --silent --show-error --location http://169.254.169.254/latest/meta-data/instance-id)
47
- echo " Detected INSTANCE_ID=$INSTANCE_ID " >&2
47
+ echo " Detected INSTANCE_ID=$INSTANCE_ID "
48
48
49
49
# This script is run on every boot so that we can gracefully recover from hard failures (eg. kernel panics) during
50
50
# any previous attempts. If a previous run is detected as started but not complete then we will fail this run and mark
51
51
# the instance as unhealthy.
52
52
STATUS_FILE=/var/log/elastic-stack-bootstrap-status
53
53
54
54
check_status () {
55
- echo " Checking status file $STATUS_FILE ..." >&2
55
+ echo " Checking status file $STATUS_FILE ..."
56
56
57
57
if [[ -f " $STATUS_FILE " ]]; then
58
58
if [[ " $( < " $STATUS_FILE " ) " == " Completed" ]]; then
@@ -74,16 +74,16 @@ case $(uname -m) in
74
74
aarch64) ARCH=arm64;;
75
75
* ) ARCH=unknown;;
76
76
esac
77
- echo " Detected ARCH=$ARCH " >&2
77
+ echo " Detected ARCH=$ARCH "
78
78
79
79
DOCKER_VERSION=$( docker --version | cut -f3 -d' ' | sed ' s/,//' )
80
- echo " Detected DOCKER_VERSION=$DOCKER_VERSION " >&2
80
+ echo " Detected DOCKER_VERSION=$DOCKER_VERSION "
81
81
82
82
PLUGINS_ENABLED=()
83
83
[[ $SECRETS_PLUGIN_ENABLED == " true" ]] && PLUGINS_ENABLED+=(" secrets" )
84
84
[[ $ECR_PLUGIN_ENABLED == " true" ]] && PLUGINS_ENABLED+=(" ecr" )
85
85
[[ $DOCKER_LOGIN_PLUGIN_ENABLED == " true" ]] && PLUGINS_ENABLED+=(" docker-login" )
86
- echo " The following plugins will be enabled: ${PLUGINS_ENABLED[*]-} " >&2
86
+ echo " The following plugins will be enabled: ${PLUGINS_ENABLED[*]-} "
87
87
88
88
# cfn-env is sourced by the environment hook in builds
89
89
# DO NOT PUT SECRETES IN HERE, they will appear in both the cloudwatch and
@@ -95,7 +95,7 @@ echo "The following plugins will be enabled: ${PLUGINS_ENABLED[*]-}" >&2
95
95
96
96
# Step 1: Helper function. Note that we clobber the target file and DO NOT apply variable
97
97
# substitution, this is controlled by the double-quoted "EOF".
98
- echo Writing Phase 1/2 for /var/lib/buildkite-agent/cfn-env helper function... >&2
98
+ echo Writing Phase 1/2 for /var/lib/buildkite-agent/cfn-env helper function...
99
99
cat << -"EOF " >/var/lib/buildkite-agent/cfn-env
100
100
# The Buildkite agent sets a number of variables such as AWS_DEFAULT_REGION to fixed values which
101
101
# are determined at AMI-build-time. However, sometimes a user might want to override such variables
126
126
127
127
# Step 2: Populate the default variable values. This time, we append to the file, and allow
128
128
# variable substitution.
129
- echo Writing Phase 2/2 for /var/lib/buildkite-agent/cfn-env helper function... >&2
129
+ echo Writing Phase 2/2 for /var/lib/buildkite-agent/cfn-env helper function...
130
130
cat << EOF >>/var/lib/buildkite-agent/cfn-env
131
131
132
132
set_always "BUILDKITE_AGENTS_PER_INSTANCE" "$BUILDKITE_AGENTS_PER_INSTANCE "
@@ -143,29 +143,29 @@ set_unless_present "AWS_REGION" "$AWS_REGION"
143
143
EOF
144
144
145
145
# We warned about not putting secrets in this file
146
- echo Wrote to /var/lib/buildkite-agent/cfn-env: >&2
147
- cat /var/lib/buildkite-agent/cfn-env >&2
146
+ echo Wrote to /var/lib/buildkite-agent/cfn-env:
147
+ cat /var/lib/buildkite-agent/cfn-env
148
148
echo
149
149
150
150
if [[ " ${BUILDKITE_AGENT_RELEASE} " == " edge" ]]; then
151
- echo Downloading buildkite-agent edge... >&2
151
+ echo Downloading buildkite-agent edge...
152
152
curl -Lsf -o /usr/bin/buildkite-agent-edge \
153
153
" https://download.buildkite.com/agent/experimental/latest/buildkite-agent-linux-${ARCH} "
154
154
chmod +x /usr/bin/buildkite-agent-edge
155
155
buildkite-agent-edge --version
156
156
else
157
- echo Not using buildkite-agent edge. >&2
157
+ echo Not using buildkite-agent edge.
158
158
fi
159
159
160
160
if [[ " ${BUILDKITE_ADDITIONAL_SUDO_PERMISSIONS} " != " " ]]; then
161
161
echo " buildkite-agent ALL=NOPASSWD: ${BUILDKITE_ADDITIONAL_SUDO_PERMISSIONS} " \
162
162
> /etc/sudoers.d/buildkite-agent-additional
163
163
chmod 440 /etc/sudoers.d/buildkite-agent-additional
164
164
165
- echo Wrote to /etc/sudoers.d/buildkite-agent-additional... >&2
166
- cat /etc/sudoers.d/buildkite-agent-additional >&2
165
+ echo Wrote to /etc/sudoers.d/buildkite-agent-additional...
166
+ cat /etc/sudoers.d/buildkite-agent-additional
167
167
else
168
- echo No additional sudo permissions. >&2
168
+ echo No additional sudo permissions.
169
169
fi
170
170
171
171
# Choose the right agent binary
@@ -178,65 +178,65 @@ agent_metadata=(
178
178
" buildkite-aws-stack=${BUILDKITE_STACK_VERSION} "
179
179
)
180
180
181
- echo " Initial agent metadata: ${agent_metadata[*]-} " >&2
181
+ echo " Initial agent metadata: ${agent_metadata[*]-} "
182
182
if [[ -n " ${BUILDKITE_AGENT_TAGS:- } " ]]; then
183
183
IFS=' ,' read -r -a extra_agent_metadata <<< " ${BUILDKITE_AGENT_TAGS:-}"
184
184
agent_metadata=(" ${agent_metadata[@]} " " ${extra_agent_metadata[@]} " )
185
185
fi
186
- echo " Agent metadata after splitting commas: ${agent_metadata[*]-} " >&2
186
+ echo " Agent metadata after splitting commas: ${agent_metadata[*]-} "
187
187
188
188
# Enable git-mirrors
189
189
BUILDKITE_AGENT_GIT_MIRRORS_PATH=" "
190
190
if [[ " ${BUILDKITE_AGENT_ENABLE_GIT_MIRRORS:- false} " == " true" ]]; then
191
191
BUILDKITE_AGENT_GIT_MIRRORS_PATH=/var/lib/buildkite-agent/git-mirrors
192
- echo " git-mirrors enabled at $BUILDKITE_AGENT_GIT_MIRRORS_PATH " >&2
192
+ echo " git-mirrors enabled at $BUILDKITE_AGENT_GIT_MIRRORS_PATH "
193
193
mkdir -p " ${BUILDKITE_AGENT_GIT_MIRRORS_PATH} "
194
194
195
195
if [[ " ${BUILDKITE_ENABLE_INSTANCE_STORAGE:- false} " == " true" ]]; then
196
- echo Mounting git-mirrors to instance storage... >&2
196
+ echo Mounting git-mirrors to instance storage...
197
197
198
198
EPHEMERAL_GIT_MIRRORS_PATH=" /mnt/ephemeral/git-mirrors"
199
- echo " Creating ephemeral git-mirrors direcotry at $EPHEMERAL_GIT_MIRRORS_PATH " >&2
199
+ echo " Creating ephemeral git-mirrors direcotry at $EPHEMERAL_GIT_MIRRORS_PATH "
200
200
mkdir -p " ${EPHEMERAL_GIT_MIRRORS_PATH} "
201
201
202
- echo Bind mounting ephemeral git-mirror directory to git-mirrors path... >&2
202
+ echo Bind mounting ephemeral git-mirror directory to git-mirrors path...
203
203
mount -o bind " ${EPHEMERAL_GIT_MIRRORS_PATH} " " ${BUILDKITE_AGENT_GIT_MIRRORS_PATH} "
204
204
205
- echo Writing bind mount to fstab... >&2
205
+ echo Writing bind mount to fstab...
206
206
echo " ${EPHEMERAL_GIT_MIRRORS_PATH} ${BUILDKITE_AGENT_GIT_MIRRORS_PATH} none defaults,bind 0 0" >> /etc/fstab
207
207
208
- echo fstab is now: >&2
209
- cat /etc/fstab >&2
208
+ echo fstab is now:
209
+ cat /etc/fstab
210
210
echo
211
211
else
212
- echo Not mounting git-mirrors to instance storage as instance storage is disabled. >&2
212
+ echo Not mounting git-mirrors to instance storage as instance storage is disabled.
213
213
fi
214
214
215
- echo Setting ownership of git-mirrors directory to buildkite-agent... >&2
215
+ echo Setting ownership of git-mirrors directory to buildkite-agent...
216
216
chown buildkite-agent: " $BUILDKITE_AGENT_GIT_MIRRORS_PATH "
217
217
else
218
- echo git-mirrors disabled. >&2
218
+ echo git-mirrors disabled.
219
219
fi
220
- echo " BUILDKITE_AGENT_GIT_MIRRORS_PATH is $BUILDKITE_AGENT_GIT_MIRRORS_PATH " >&2
220
+ echo " BUILDKITE_AGENT_GIT_MIRRORS_PATH is $BUILDKITE_AGENT_GIT_MIRRORS_PATH "
221
221
222
222
BUILDKITE_AGENT_BUILD_PATH=" /var/lib/buildkite-agent/builds"
223
223
mkdir -p " ${BUILDKITE_AGENT_BUILD_PATH} "
224
224
if [[ " ${BUILDKITE_ENABLE_INSTANCE_STORAGE:- false} " == " true" ]]; then
225
- echo Bind mounting build path to instance storage... >&2
225
+ echo Bind mounting build path to instance storage...
226
226
227
227
EPHEMERAL_BUILD_PATH=" /mnt/ephemeral/builds"
228
228
mkdir -p " ${EPHEMERAL_BUILD_PATH} "
229
229
230
230
mount -o bind " ${EPHEMERAL_BUILD_PATH} " " ${BUILDKITE_AGENT_BUILD_PATH} "
231
231
echo " ${EPHEMERAL_BUILD_PATH} ${BUILDKITE_AGENT_BUILD_PATH} none defaults,bind 0 0" >> /etc/fstab
232
232
233
- echo fstab is now: >&2
234
- cat /etc/fstab >&2
233
+ echo fstab is now:
234
+ cat /etc/fstab
235
235
else
236
- echo Not mounting build path to instance storage as instance storage is disabled. >&2
236
+ echo Not mounting build path to instance storage as instance storage is disabled.
237
237
fi
238
238
239
- echo Setting ownership of build path to buildkite-agent. >&2
239
+ echo Setting ownership of build path to buildkite-agent.
240
240
chown buildkite-agent: " $BUILDKITE_AGENT_BUILD_PATH "
241
241
242
242
# Either you can have timestamp-lines xor ansi-timestamps.
@@ -249,11 +249,11 @@ else
249
249
BUILDKITE_AGENT_TIMESTAMPS_LINES=" false"
250
250
BUILDKITE_AGENT_NO_ANSI_TIMESTAMPS=" false"
251
251
fi
252
- echo Set \$ BUILDKITE_AGENT_NO_ANSI_TIMESTAMPS to \$ BUILDKITE_AGENT_TIMESTAMP_LINES >&2
253
- echo " BUILDKITE_AGENT_TIMESTAMP_LINES is $BUILDKITE_AGENT_TIMESTAMPS_LINES " >&2
254
- echo " BUILDKITE_AGENT_NO_ANSI_TIMESTAMPS is $BUILDKITE_AGENT_NO_ANSI_TIMESTAMPS " >&2
252
+ echo Setting \$ BUILDKITE_AGENT_NO_ANSI_TIMESTAMPS to \$ BUILDKITE_AGENT_TIMESTAMP_LINES
253
+ echo " BUILDKITE_AGENT_TIMESTAMP_LINES is $BUILDKITE_AGENT_TIMESTAMPS_LINES "
254
+ echo " BUILDKITE_AGENT_NO_ANSI_TIMESTAMPS is $BUILDKITE_AGENT_NO_ANSI_TIMESTAMPS "
255
255
256
- echo " Setting \$ BUILDKITE_AGENT_TOKEN from SSM Parameter $BUILDKITE_AGENT_TOKEN_PATH " >&2
256
+ echo " Setting \$ BUILDKITE_AGENT_TOKEN from SSM Parameter $BUILDKITE_AGENT_TOKEN_PATH "
257
257
BUILDKITE_AGENT_TOKEN=" $(
258
258
aws ssm get-parameter \
259
259
--name " $BUILDKITE_AGENT_TOKEN_PATH " \
@@ -285,62 +285,62 @@ cancel-grace-period=60
285
285
EOF
286
286
287
287
if [[ " ${BUILDKITE_ENV_FILE_URL} " != " " ]]; then
288
- echo " Fetching env file from ${BUILDKITE_ENV_FILE_URL} ..." >&2
288
+ echo " Fetching env file from ${BUILDKITE_ENV_FILE_URL} ..."
289
289
/usr/local/bin/bk-fetch.sh " ${BUILDKITE_ENV_FILE_URL} " /var/lib/buildkite-agent/env
290
290
else
291
- echo No env file to fetch. >&2
291
+ echo No env file to fetch.
292
292
fi
293
293
294
- echo Setting ownership of /etc/buildkite-agent/buildkite-agent.cfg to buildkite-agent... >&2
294
+ echo Setting ownership of /etc/buildkite-agent/buildkite-agent.cfg to buildkite-agent...
295
295
chown buildkite-agent: /etc/buildkite-agent/buildkite-agent.cfg
296
296
297
297
if [[ -n " $BUILDKITE_AUTHORIZED_USERS_URL " ]]; then
298
- echo Writing authorized user fetching script... >&2
298
+ echo Writing authorized user fetching script...
299
299
cat << -EOF | tee /usr/local/bin/refresh_authorized_keys
300
300
/usr/local/bin/bk-fetch.sh "$BUILDKITE_AUTHORIZED_USERS_URL " /tmp/authorized_keys
301
301
mv /tmp/authorized_keys /home/ec2-user/.ssh/authorized_keys
302
302
chmod 600 /home/ec2-user/.ssh/authorized_keys
303
303
chown ec2-user: /home/ec2-user/.ssh/authorized_keys
304
304
EOF
305
305
306
- echo Setting ownership of /usr/local/bin/refresh_authorized_keys to root... >&2
306
+ echo Setting ownership of /usr/local/bin/refresh_authorized_keys to root...
307
307
chmod +x /usr/local/bin/refresh_authorized_keys
308
308
309
- echo Running authorized user fetching script... >&2
309
+ echo Running authorized user fetching script...
310
310
/usr/local/bin/refresh_authorized_keys
311
311
312
- echo Enabling authorized user fetching timer... >&2
312
+ echo Enabling authorized user fetching timer...
313
313
systemctl enable refresh_authorized_keys.timer
314
314
else
315
- echo No authorized users to fetch >&2
315
+ echo No authorized users to fetch.
316
316
fi
317
317
318
- echo Installing git-lfs for buildkite-agent user... >&2
318
+ echo Installing git-lfs for buildkite-agent user...
319
319
su buildkite-agent -l -c ' git lfs install'
320
320
321
321
if [[ -n " $BUILDKITE_ELASTIC_BOOTSTRAP_SCRIPT " ]]; then
322
- echo " Running bootstrap script from $BUILDKITE_ELASTIC_BOOTSTRAP_SCRIPT ..." >&2
322
+ echo " Running bootstrap script from $BUILDKITE_ELASTIC_BOOTSTRAP_SCRIPT ..."
323
323
/usr/local/bin/bk-fetch.sh " $BUILDKITE_ELASTIC_BOOTSTRAP_SCRIPT " /tmp/elastic_bootstrap
324
324
bash < /tmp/elastic_bootstrap
325
325
rm /tmp/elastic_bootstrap
326
326
else
327
- echo No bootstrap script to run. >&2
327
+ echo No bootstrap script to run.
328
328
fi
329
329
330
- echo Writing lifecycled configuration... >&2
330
+ echo Writing lifecycled configuration...
331
331
cat << EOF | tee /etc/lifecycled
332
332
AWS_REGION=$AWS_REGION
333
333
LIFECYCLED_HANDLER=/usr/local/bin/stop-agent-gracefully
334
334
LIFECYCLED_CLOUDWATCH_GROUP=/buildkite/lifecycled
335
335
EOF
336
336
337
- echo Starting lifecycled... >&2
337
+ echo Starting lifecycled...
338
338
systemctl enable --now lifecycled.service
339
339
340
- echo Waiting for docker to start... >&2
340
+ echo Waiting for docker to start...
341
341
check_docker () {
342
342
if ! docker ps > /dev/null; then
343
- echo " Failed to contact docker."
343
+ echo Failed to contact docker.
344
344
return 1
345
345
fi
346
346
}
@@ -350,14 +350,13 @@ until check_docker || [[ $next_wait_time -eq 5 ]]; do
350
350
sleep $(( next_wait_time++ ))
351
351
done
352
352
353
- echo " Waited $next_wait_time times for docker to start." >&2
354
- echo We will exit if it still has not started. >&2
353
+ echo " Waited $next_wait_time times for docker to start. We will exit if it still has not started."
355
354
check_docker
356
355
357
- echo Starting buildkite-agent... >&2
356
+ echo Starting buildkite-agent...
358
357
systemctl enable --now buildkite-agent
359
358
360
- echo Signaling success to CloudFormation... >&2
359
+ echo Signaling success to CloudFormation...
361
360
# This will fail if the stack has already completed, for instance if there is a min size
362
361
# of 1 and this is the 2nd instance. This is ok, so we just ignore the error
363
362
cfn-signal \
0 commit comments