revert: "Overhauled `startup.sh` Script (#1454)" (#1561)

This reverts commit 071898c96b.
This commit is contained in:
Callum Tait 2022-06-23 12:39:32 +01:00 committed by GitHub
parent 071898c96b
commit 84d16c1c12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 57 additions and 62 deletions

View File

@ -1,75 +1,70 @@
#!/usr/bin/env bash #!/bin/bash
set -Eeuo pipefail source logger.bash
# This path can only be customized through arguments passed to the dockerd cli function wait_for_process () {
# at startup. Parsing the supervisor docker.conf file is cumbersome, and there local max_time_wait=30
# is little reason to allow users to customize this. Hence, we hardcode the path local process_name="$1"
# here. local waited_sec=0
# while ! pgrep "$process_name" >/dev/null && ((waited_sec < max_time_wait)); do
# See: https://docs.docker.com/engine/reference/commandline/dockerd/ log.debug "Process $process_name is not running yet. Retrying in 1 seconds"
readonly dockerd_config_path=/etc/docker/daemon.json log.debug "Waited $waited_sec seconds of $max_time_wait seconds"
readonly dockerd_supervisor_config_path=/etc/supervisor/conf.d/dockerd.conf sleep 1
((waited_sec=waited_sec+1))
if ((waited_sec >= max_time_wait)); then
return 1
fi
done
return 0
}
# Extend existing config... sudo /bin/bash <<SCRIPT
dockerd_config=$(! cat $dockerd_config_path 2>&-) mkdir -p /etc/docker
# ...and fallback to an empty object if there is no existing configuration, or echo "{}" > /etc/docker/daemon.json
# otherwise the following `jq` merge commands would not produce anything.
if [[ $dockerd_config == '' ]]; then if [ -n "${MTU}" ]; then
dockerd_config='{}' jq ".\"mtu\" = ${MTU}" /etc/docker/daemon.json > /tmp/.daemon.json && mv /tmp/.daemon.json /etc/docker/daemon.json
# See https://docs.docker.com/engine/security/rootless/
echo "environment=DOCKERD_ROOTLESS_ROOTLESSKIT_MTU=${MTU}" >> /etc/supervisor/conf.d/dockerd.conf
fi fi
readonly MTU=${MTU:-} if [ -n "${DOCKER_REGISTRY_MIRROR}" ]; then
if [[ $MTU ]]; then jq ".\"registry-mirrors\"[0] = \"${DOCKER_REGISTRY_MIRROR}\"" /etc/docker/daemon.json > /tmp/.daemon.json && mv /tmp/.daemon.json /etc/docker/daemon.json
# We have to verify that this value is a valid integer because we are going to
# start docker in the background and invalid values would never be surfaced.
# Well, except that startup fails and our users are sent on a nice debugging
# session.
if [[ $MTU =~ ^[1-9][0-9]*$ ]]; then
# See https://docs.docker.com/engine/security/rootless/
sudo tee -a $dockerd_supervisor_config_path 1>&- <<<"environment=DOCKERD_ROOTLESS_ROOTLESSKIT_MTU=$MTU"
dockerd_config=$(jq -S ".mtu = $MTU" <<<"$dockerd_config")
else
# shellcheck source=runner/logger.bash
source logger.bash
log.error "Docker MTU must be an integer, continuing bootstrapping without it, got: $MTU"
MTU=
fi
fi fi
SCRIPT
if [[ ${DOCKER_REGISTRY_MIRROR:-} != '' ]]; then dump() {
dockerd_config=$(jq -S --arg url "$DOCKER_REGISTRY_MIRROR" '."registry-mirrors" += ["$url"]' <<<"$dockerd_config") local path=${1:?missing required <path> argument}
fi shift
printf -- "%s\n---\n" "${*//\{path\}/"$path"}" 1>&2
cat "$path" 1>&2
printf -- '---\n' 1>&2
}
sudo mkdir -p ${dockerd_config_path%/*} for config in /etc/docker/daemon.json /etc/supervisor/conf.d/dockerd.conf; do
sudo tee $dockerd_config_path 1>&- <<<"$dockerd_config" dump "$config" 'Using {path} with the following content:'
for config in $dockerd_config_path $dockerd_supervisor_config_path; do
(
echo "Using '$config' with the following content <<CONFIG"
cat $config
echo 'CONFIG'
) 1>&2
done done
sudo /usr/bin/supervisord -c /etc/supervicor/supervisord.conf log.debug 'Starting supervisor daemon'
sudo /usr/bin/supervisord -n >> /dev/null 2>&1 &
if [[ $MTU ]]; then log.debug 'Waiting for processes to be running...'
# It might take a few ticks for the `docker0` device to be up after starting processes=(dockerd)
# the docker daemon with supervisord, hence, we retry for a little while.
# Notice how we use `nice` to execute the command with a least favorable for process in "${processes[@]}"; do
# scheduling priority and are using a noop (`:`) instruction instead of wait_for_process "$process"
# `sleep`. We do this to ensure that we keep the startup as fast as possible. if [ $? -ne 0 ]; then
if ! nice -n 19 timeout -k 70 60 -- bash -c "(until sudo ifconfig docker0 mtu '$MTU' up; do :; done) 1>&- 2>&-"; then log.error "$process is not running after max time"
# shellcheck source=runner/logger.bash dump /var/log/dockerd.err.log 'Dumping {path} to aid investigation'
source logger.bash exit 1
log.error 'Failed to set docker interface mtu within 1 minute, continuing bootstrapping without it...' else
fi log.debug "$process is running"
fi
done
if [ -n "${MTU}" ]; then
sudo ifconfig docker0 mtu ${MTU} up
fi fi
# We leave the wait for docker to come up to our entrypoint.sh script, since its # Wait processes to be running
# check properly handles the case where the docker service is in a crash loop. entrypoint.sh
# Users still have the ability to disable the wait.
export DOCKER_ENABLED=true
export DISABLE_WAIT_FOR_DOCKER=${DISABLE_WAIT_FOR_DOCKER:-false}
exec entrypoint.sh