Skip to content

Commit e8184d8

Browse files
committed
Remove redirecting echos and cats to stderr
The command that sends the scripts output streams to the loggers merges stdout and stderr anyway, so there is no point in writing to stderr in the script. This is the logging command: ``` exec > >(tee -a /var/log/elastic-stack.log | logger -t user-data -s 2>/dev/console) 2>&1 ``` it is present in each of these scripts.
1 parent 21ea647 commit e8184d8

File tree

3 files changed

+95
-96
lines changed

3 files changed

+95
-96
lines changed

packer/linux/conf/bin/bk-configure-docker.sh

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ on_error() {
1414
trap 'on_error $LINENO' ERR
1515

1616
on_exit() {
17-
echo "${BASH_SOURCE[0]} completed successfully." >&2
17+
echo "${BASH_SOURCE[0]} completed successfully."
1818
}
1919

2020
trap on_exit EXIT
@@ -26,63 +26,63 @@ trap on_exit EXIT
2626
exec > >(tee -a /var/log/elastic-stack.log | logger -t user-data -s 2>/dev/console) 2>&1
2727

2828

29-
echo Reading variables from AMI creation... >&2
29+
echo Reading variables from AMI creation...
3030
# shellcheck disable=SC1091
3131
source /usr/local/lib/bk-configure-docker.sh
3232

3333
if [[ "${DOCKER_USERNS_REMAP:-false}" == "true" ]]; then
34-
echo Configuring user namespace remapping... >&2
34+
echo Configuring user namespace remapping...
3535

3636
cat <<< "$(jq '."userns-remap"="buildkite-agent"' /etc/docker/daemon.json)" > /etc/docker/daemon.json
3737

38-
echo Writing subuid... >&2
38+
echo Writing subuid...
3939
cat <<EOF | tee /etc/subuid
4040
buildkite-agent:$(id -u buildkite-agent):1
4141
buildkite-agent:100000:65536
4242
EOF
4343

44-
echo Writing subgid... >&2
44+
echo Writing subgid...
4545
cat <<EOF | tee /etc/subgid
4646
buildkite-agent:$(getent group docker | awk -F: '{print $3}'):1
4747
buildkite-agent:100000:65536
4848
EOF
4949
else
50-
echo User namespace remapping not configured. >&2
50+
echo User namespace remapping not configured.
5151
fi
5252

5353
if [[ "${DOCKER_EXPERIMENTAL:-false}" == "true" ]]; then
54-
echo Configuring experiment flag for docker daemon... >&2
54+
echo Configuring experiment flag for docker daemon...
5555
cat <<< "$(jq '.experimental=true' /etc/docker/daemon.json)" > /etc/docker/daemon.json
5656
else
57-
echo Experiment flag for docker daemon not configured. >&2
57+
echo Experiment flag for docker daemon not configured.
5858
fi
5959

6060
if [[ "${BUILDKITE_ENABLE_INSTANCE_STORAGE:-false}" == "true" ]]; then
61-
echo Creating docker root directory in instance storage... >&2
61+
echo Creating docker root directory in instance storage...
6262
mkdir -p /mnt/ephemeral/docker
63-
echo Configuring docker root directory to be in instance storage... >&2
63+
echo Configuring docker root directory to be in instance storage...
6464
cat <<< "$(jq '."data-root"="/mnt/ephemeral/docker"' /etc/docker/daemon.json)" > /etc/docker/daemon.json
6565
else
66-
echo Instance storage not configured. >&2
66+
echo Instance storage not configured.
6767
fi
6868

69-
echo Customising docker IP address pools... >&2
69+
echo Customising docker IP address pools...
7070
cat <<<"$(jq \
7171
'."default-address-pools"=[{"base":"172.17.0.0/12","size":20},{"base":"192.168.0.0/16","size":24}]' \
7272
/etc/docker/daemon.json \
7373
)" >/etc/docker/daemon.json
7474

7575
# See https://docs.docker.com/build/building/multi-platform/
76-
echo Installing qemu binfmt for multiarch... >&2
76+
echo Installing qemu binfmt for multiarch...
7777
docker run \
7878
--privileged \
7979
--userns=host \
8080
--rm \
8181
"tonistiigi/binfmt:${QEMU_BINFMT_TAG}" \
8282
--install all
8383

84-
echo Cleaning up docker images... >&2
84+
echo Cleaning up docker images...
8585
systemctl start docker-low-disk-gc.service
8686

87-
echo Restarting docker daemon... >&2
87+
echo Restarting docker daemon...
8888
systemctl restart docker

packer/linux/conf/bin/bk-install-elastic-stack.sh

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ on_error() {
1515
--instance-id "$INSTANCE_ID" \
1616
--health-status Unhealthy
1717
then
18-
echo Failed to set instance health to unhealthy >&2
18+
echo Failed to set instance health to unhealthy.
1919
fi
2020
fi
2121

@@ -32,7 +32,7 @@ on_error() {
3232
trap 'on_error $LINENO' ERR
3333

3434
on_exit() {
35-
echo "${BASH_SOURCE[0]} completed successfully." >&2
35+
echo "${BASH_SOURCE[0]} completed successfully."
3636
}
3737

3838
trap on_exit EXIT
@@ -44,15 +44,15 @@ exec > >(tee -a /var/log/elastic-stack.log | logger -t user-data -s 2>/dev/conso
4444
# This needs to happen first so that the error reporting works
4545
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)
4646
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"
4848

4949
# This script is run on every boot so that we can gracefully recover from hard failures (eg. kernel panics) during
5050
# any previous attempts. If a previous run is detected as started but not complete then we will fail this run and mark
5151
# the instance as unhealthy.
5252
STATUS_FILE=/var/log/elastic-stack-bootstrap-status
5353

5454
check_status() {
55-
echo "Checking status file $STATUS_FILE..." >&2
55+
echo "Checking status file $STATUS_FILE..."
5656

5757
if [[ -f "$STATUS_FILE" ]]; then
5858
if [[ "$(<"$STATUS_FILE")" == "Completed" ]]; then
@@ -74,16 +74,16 @@ case $(uname -m) in
7474
aarch64) ARCH=arm64;;
7575
*) ARCH=unknown;;
7676
esac
77-
echo "Detected ARCH=$ARCH" >&2
77+
echo "Detected ARCH=$ARCH"
7878

7979
DOCKER_VERSION=$(docker --version | cut -f3 -d' ' | sed 's/,//')
80-
echo "Detected DOCKER_VERSION=$DOCKER_VERSION" >&2
80+
echo "Detected DOCKER_VERSION=$DOCKER_VERSION"
8181

8282
PLUGINS_ENABLED=()
8383
[[ $SECRETS_PLUGIN_ENABLED == "true" ]] && PLUGINS_ENABLED+=("secrets")
8484
[[ $ECR_PLUGIN_ENABLED == "true" ]] && PLUGINS_ENABLED+=("ecr")
8585
[[ $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[*]-}"
8787

8888
# cfn-env is sourced by the environment hook in builds
8989
# 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
9595

9696
# Step 1: Helper function. Note that we clobber the target file and DO NOT apply variable
9797
# 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...
9999
cat <<-"EOF" >/var/lib/buildkite-agent/cfn-env
100100
# The Buildkite agent sets a number of variables such as AWS_DEFAULT_REGION to fixed values which
101101
# are determined at AMI-build-time. However, sometimes a user might want to override such variables
@@ -126,7 +126,7 @@ EOF
126126

127127
# Step 2: Populate the default variable values. This time, we append to the file, and allow
128128
# 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...
130130
cat <<EOF >>/var/lib/buildkite-agent/cfn-env
131131
132132
set_always "BUILDKITE_AGENTS_PER_INSTANCE" "$BUILDKITE_AGENTS_PER_INSTANCE"
@@ -143,29 +143,29 @@ set_unless_present "AWS_REGION" "$AWS_REGION"
143143
EOF
144144

145145
# 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
148148
echo
149149

150150
if [[ "${BUILDKITE_AGENT_RELEASE}" == "edge" ]]; then
151-
echo Downloading buildkite-agent edge... >&2
151+
echo Downloading buildkite-agent edge...
152152
curl -Lsf -o /usr/bin/buildkite-agent-edge \
153153
"https://download.buildkite.com/agent/experimental/latest/buildkite-agent-linux-${ARCH}"
154154
chmod +x /usr/bin/buildkite-agent-edge
155155
buildkite-agent-edge --version
156156
else
157-
echo Not using buildkite-agent edge. >&2
157+
echo Not using buildkite-agent edge.
158158
fi
159159

160160
if [[ "${BUILDKITE_ADDITIONAL_SUDO_PERMISSIONS}" != "" ]]; then
161161
echo "buildkite-agent ALL=NOPASSWD: ${BUILDKITE_ADDITIONAL_SUDO_PERMISSIONS}" \
162162
>/etc/sudoers.d/buildkite-agent-additional
163163
chmod 440 /etc/sudoers.d/buildkite-agent-additional
164164

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
167167
else
168-
echo No additional sudo permissions. >&2
168+
echo No additional sudo permissions.
169169
fi
170170

171171
# Choose the right agent binary
@@ -178,65 +178,65 @@ agent_metadata=(
178178
"buildkite-aws-stack=${BUILDKITE_STACK_VERSION}"
179179
)
180180

181-
echo "Initial agent metadata: ${agent_metadata[*]-}" >&2
181+
echo "Initial agent metadata: ${agent_metadata[*]-}"
182182
if [[ -n "${BUILDKITE_AGENT_TAGS:-}" ]]; then
183183
IFS=',' read -r -a extra_agent_metadata <<<"${BUILDKITE_AGENT_TAGS:-}"
184184
agent_metadata=("${agent_metadata[@]}" "${extra_agent_metadata[@]}")
185185
fi
186-
echo "Agent metadata after splitting commas: ${agent_metadata[*]-}" >&2
186+
echo "Agent metadata after splitting commas: ${agent_metadata[*]-}"
187187

188188
# Enable git-mirrors
189189
BUILDKITE_AGENT_GIT_MIRRORS_PATH=""
190190
if [[ "${BUILDKITE_AGENT_ENABLE_GIT_MIRRORS:-false}" == "true" ]]; then
191191
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"
193193
mkdir -p "${BUILDKITE_AGENT_GIT_MIRRORS_PATH}"
194194

195195
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...
197197

198198
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"
200200
mkdir -p "${EPHEMERAL_GIT_MIRRORS_PATH}"
201201

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...
203203
mount -o bind "${EPHEMERAL_GIT_MIRRORS_PATH}" "${BUILDKITE_AGENT_GIT_MIRRORS_PATH}"
204204

205-
echo Writing bind mount to fstab... >&2
205+
echo Writing bind mount to fstab...
206206
echo "${EPHEMERAL_GIT_MIRRORS_PATH} ${BUILDKITE_AGENT_GIT_MIRRORS_PATH} none defaults,bind 0 0" >>/etc/fstab
207207

208-
echo fstab is now: >&2
209-
cat /etc/fstab >&2
208+
echo fstab is now:
209+
cat /etc/fstab
210210
echo
211211
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.
213213
fi
214214

215-
echo Setting ownership of git-mirrors directory to buildkite-agent... >&2
215+
echo Setting ownership of git-mirrors directory to buildkite-agent...
216216
chown buildkite-agent: "$BUILDKITE_AGENT_GIT_MIRRORS_PATH"
217217
else
218-
echo git-mirrors disabled. >&2
218+
echo git-mirrors disabled.
219219
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"
221221

222222
BUILDKITE_AGENT_BUILD_PATH="/var/lib/buildkite-agent/builds"
223223
mkdir -p "${BUILDKITE_AGENT_BUILD_PATH}"
224224
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...
226226

227227
EPHEMERAL_BUILD_PATH="/mnt/ephemeral/builds"
228228
mkdir -p "${EPHEMERAL_BUILD_PATH}"
229229

230230
mount -o bind "${EPHEMERAL_BUILD_PATH}" "${BUILDKITE_AGENT_BUILD_PATH}"
231231
echo "${EPHEMERAL_BUILD_PATH} ${BUILDKITE_AGENT_BUILD_PATH} none defaults,bind 0 0" >>/etc/fstab
232232

233-
echo fstab is now: >&2
234-
cat /etc/fstab >&2
233+
echo fstab is now:
234+
cat /etc/fstab
235235
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.
237237
fi
238238

239-
echo Setting ownership of build path to buildkite-agent. >&2
239+
echo Setting ownership of build path to buildkite-agent.
240240
chown buildkite-agent: "$BUILDKITE_AGENT_BUILD_PATH"
241241

242242
# Either you can have timestamp-lines xor ansi-timestamps.
@@ -249,11 +249,11 @@ else
249249
BUILDKITE_AGENT_TIMESTAMPS_LINES="false"
250250
BUILDKITE_AGENT_NO_ANSI_TIMESTAMPS="false"
251251
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"
255255

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"
257257
BUILDKITE_AGENT_TOKEN="$(
258258
aws ssm get-parameter \
259259
--name "$BUILDKITE_AGENT_TOKEN_PATH" \
@@ -285,62 +285,62 @@ cancel-grace-period=60
285285
EOF
286286

287287
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}..."
289289
/usr/local/bin/bk-fetch.sh "${BUILDKITE_ENV_FILE_URL}" /var/lib/buildkite-agent/env
290290
else
291-
echo No env file to fetch. >&2
291+
echo No env file to fetch.
292292
fi
293293

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...
295295
chown buildkite-agent: /etc/buildkite-agent/buildkite-agent.cfg
296296

297297
if [[ -n "$BUILDKITE_AUTHORIZED_USERS_URL" ]]; then
298-
echo Writing authorized user fetching script... >&2
298+
echo Writing authorized user fetching script...
299299
cat <<-EOF | tee /usr/local/bin/refresh_authorized_keys
300300
/usr/local/bin/bk-fetch.sh "$BUILDKITE_AUTHORIZED_USERS_URL" /tmp/authorized_keys
301301
mv /tmp/authorized_keys /home/ec2-user/.ssh/authorized_keys
302302
chmod 600 /home/ec2-user/.ssh/authorized_keys
303303
chown ec2-user: /home/ec2-user/.ssh/authorized_keys
304304
EOF
305305

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...
307307
chmod +x /usr/local/bin/refresh_authorized_keys
308308

309-
echo Running authorized user fetching script... >&2
309+
echo Running authorized user fetching script...
310310
/usr/local/bin/refresh_authorized_keys
311311

312-
echo Enabling authorized user fetching timer... >&2
312+
echo Enabling authorized user fetching timer...
313313
systemctl enable refresh_authorized_keys.timer
314314
else
315-
echo No authorized users to fetch >&2
315+
echo No authorized users to fetch.
316316
fi
317317

318-
echo Installing git-lfs for buildkite-agent user... >&2
318+
echo Installing git-lfs for buildkite-agent user...
319319
su buildkite-agent -l -c 'git lfs install'
320320

321321
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..."
323323
/usr/local/bin/bk-fetch.sh "$BUILDKITE_ELASTIC_BOOTSTRAP_SCRIPT" /tmp/elastic_bootstrap
324324
bash </tmp/elastic_bootstrap
325325
rm /tmp/elastic_bootstrap
326326
else
327-
echo No bootstrap script to run. >&2
327+
echo No bootstrap script to run.
328328
fi
329329

330-
echo Writing lifecycled configuration... >&2
330+
echo Writing lifecycled configuration...
331331
cat <<EOF | tee /etc/lifecycled
332332
AWS_REGION=$AWS_REGION
333333
LIFECYCLED_HANDLER=/usr/local/bin/stop-agent-gracefully
334334
LIFECYCLED_CLOUDWATCH_GROUP=/buildkite/lifecycled
335335
EOF
336336

337-
echo Starting lifecycled... >&2
337+
echo Starting lifecycled...
338338
systemctl enable --now lifecycled.service
339339

340-
echo Waiting for docker to start... >&2
340+
echo Waiting for docker to start...
341341
check_docker() {
342342
if ! docker ps >/dev/null; then
343-
echo "Failed to contact docker."
343+
echo Failed to contact docker.
344344
return 1
345345
fi
346346
}
@@ -350,14 +350,13 @@ until check_docker || [[ $next_wait_time -eq 5 ]]; do
350350
sleep $((next_wait_time++))
351351
done
352352

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."
355354
check_docker
356355

357-
echo Starting buildkite-agent... >&2
356+
echo Starting buildkite-agent...
358357
systemctl enable --now buildkite-agent
359358

360-
echo Signaling success to CloudFormation... >&2
359+
echo Signaling success to CloudFormation...
361360
# This will fail if the stack has already completed, for instance if there is a min size
362361
# of 1 and this is the 2nd instance. This is ok, so we just ignore the error
363362
cfn-signal \

0 commit comments

Comments
 (0)