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

Improve logging for startup scripts on linux #1230

Merged
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
56 changes: 46 additions & 10 deletions packer/linux/conf/bin/bk-configure-docker.sh
Original file line number Diff line number Diff line change
@@ -1,43 +1,76 @@
#!/bin/bash
#!/usr/bin/env bash
# shellcheck disable=SC2094
set -euxo pipefail

## Configures docker before system starts
set -Eeuo pipefail

on_error() {
local exit_code="$?"
local error_line="$1"

echo "${BASH_SOURCE[0]} errored with exit code ${exit_code} on line ${error_line}."
exit "$exit_code"
}

trap 'on_error $LINENO' ERR

on_exit() {
echo "${BASH_SOURCE[0]} completed successfully."
}

trap '[[ $? = 0 ]] && on_exit' EXIT

## Configure docker before system starts

# Write to system console and to our log file
# See https://alestic.com/2010/12/ec2-user-data-output/
exec > >(tee -a /var/log/elastic-stack.log | logger -t user-data -s 2>/dev/console) 2>&1


echo Reading variables from AMI creation...
# shellcheck disable=SC1091
source /usr/local/lib/bk-configure-docker.sh

# Set user namespace remapping in config
if [[ "${DOCKER_USERNS_REMAP:-false}" == "true" ]]; then
echo Configuring user namespace remapping...

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

echo Writing subuid...
cat <<EOF | tee /etc/subuid
buildkite-agent:$(id -u buildkite-agent):1
buildkite-agent:100000:65536
EOF
cat <<EOF > /etc/subgid

echo Writing subgid...
cat <<EOF | tee /etc/subgid
buildkite-agent:$(getent group docker | awk -F: '{print $3}'):1
buildkite-agent:100000:65536
EOF
else
echo User namespace remapping not configured.
fi

# Set experimental in config
if [[ "${DOCKER_EXPERIMENTAL:-false}" == "true" ]]; then
echo Configuring experiment flag for docker daemon...
cat <<< "$(jq '.experimental=true' /etc/docker/daemon.json)" > /etc/docker/daemon.json
else
echo Experiment flag for docker daemon not configured.
fi

# Move docker root to the ephemeral device
if [[ "${BUILDKITE_ENABLE_INSTANCE_STORAGE:-false}" == "true" ]]; then
echo Creating docker root directory in instance storage...
mkdir -p /mnt/ephemeral/docker
echo Configuring docker root directory to be in instance storage...
cat <<< "$(jq '."data-root"="/mnt/ephemeral/docker"' /etc/docker/daemon.json)" > /etc/docker/daemon.json
else
echo Instance storage not configured.
fi

# Customise address pools
cat <<<"$(jq '."default-address-pools"=[{"base":"172.17.0.0/12","size":20},{"base":"192.168.0.0/16","size":24}]' /etc/docker/daemon.json)" >/etc/docker/daemon.json
echo Customising docker IP address pools...
cat <<<"$(jq \
'."default-address-pools"=[{"base":"172.17.0.0/12","size":20},{"base":"192.168.0.0/16","size":24}]' \
/etc/docker/daemon.json \
)" >/etc/docker/daemon.json

# See https://docs.docker.com/build/building/multi-platform/
echo Installing qemu binfmt for multiarch...
Expand All @@ -48,5 +81,8 @@ docker run \
"tonistiigi/binfmt:${QEMU_BINFMT_TAG}" \
--install all

echo Cleaning up docker images...
systemctl start docker-low-disk-gc.service

echo Restarting docker daemon...
systemctl restart docker
Loading