feat: record infrastructure POC baseline
This commit is contained in:
1 parent
de384796c4
commit
885db5319f
3 files changed
+131
No files matched your search
Executable
+81
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
if [[ "${EUID}" -eq 0 ]]; then
|
||||
sudo_command=()
|
||||
else
|
||||
sudo_command=(sudo)
|
||||
fi
|
||||
|
||||
config_path="${DOCKER_DAEMON_CONFIG:-/etc/docker/daemon.json}"
|
||||
registry_mirror="${DOCKER_REGISTRY_MIRROR:-https://docker.m.daocloud.io}"
|
||||
|
||||
for required_command in cmp jq dockerd systemctl; do
|
||||
if ! command -v "$required_command" >/dev/null 2>&1; then
|
||||
printf 'Required command is unavailable: %s\n' "$required_command" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
case "$registry_mirror" in
|
||||
https://*) ;;
|
||||
*)
|
||||
printf 'DOCKER_REGISTRY_MIRROR must use HTTPS: %s\n' "$registry_mirror" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
current_config="$(mktemp)"
|
||||
next_config="$(mktemp)"
|
||||
trap 'rm -f "$current_config" "$next_config"' EXIT
|
||||
|
||||
had_config=0
|
||||
if "${sudo_command[@]}" test -e "$config_path"; then
|
||||
had_config=1
|
||||
"${sudo_command[@]}" cat "$config_path" > "$current_config"
|
||||
else
|
||||
printf '{}\n' > "$current_config"
|
||||
fi
|
||||
|
||||
jq --arg mirror "$registry_mirror" '
|
||||
if type != "object" then
|
||||
error("Docker daemon configuration must be a JSON object")
|
||||
else
|
||||
.["registry-mirrors"] = ((.["registry-mirrors"] // []) + [$mirror] | unique)
|
||||
end
|
||||
' "$current_config" > "$next_config"
|
||||
|
||||
"${sudo_command[@]}" dockerd --validate --config-file="$next_config"
|
||||
|
||||
if cmp -s <(jq --sort-keys . "$current_config") <(jq --sort-keys . "$next_config"); then
|
||||
printf 'Docker registry mirror is already configured: %s\n' "$registry_mirror"
|
||||
"${sudo_command[@]}" systemctl is-active docker
|
||||
"${sudo_command[@]}" docker info | sed -n '/Registry Mirrors/,+3p'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
backup_path=""
|
||||
if [[ "$had_config" == "1" ]]; then
|
||||
backup_path="${config_path}.bak-$(date +%Y%m%d-%H%M%S)"
|
||||
"${sudo_command[@]}" cp --preserve=mode,ownership,timestamps "$config_path" "$backup_path"
|
||||
fi
|
||||
|
||||
"${sudo_command[@]}" install -d -m 0755 "$(dirname -- "$config_path")"
|
||||
"${sudo_command[@]}" install -m 0644 "$next_config" "$config_path"
|
||||
|
||||
if ! "${sudo_command[@]}" systemctl restart docker; then
|
||||
if [[ -n "$backup_path" ]]; then
|
||||
"${sudo_command[@]}" install -m 0644 "$backup_path" "$config_path"
|
||||
else
|
||||
"${sudo_command[@]}" rm -f "$config_path"
|
||||
fi
|
||||
"${sudo_command[@]}" systemctl restart docker || true
|
||||
printf 'Docker restart failed; the previous daemon configuration was restored.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
"${sudo_command[@]}" systemctl is-active docker
|
||||
"${sudo_command[@]}" docker info | sed -n '/Registry Mirrors/,+3p'
|
||||
if [[ -n "$backup_path" ]]; then
|
||||
printf 'Previous configuration backup: %s\n' "$backup_path"
|
||||
fi
|
||||
Reference in new issue
Block a user