|
@@ -28,15 +28,32 @@ if [[ $EUID -eq 0 ]]; then
|
|
|
exit 1
|
|
exit 1
|
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
|
|
+# Acquire sudo up front. Without this the first privileged command fails on a
|
|
|
|
|
+# non-interactive shell and, because install_file runs as an `if` condition
|
|
|
|
|
+# (where bash disables set -e), the failure would be swallowed and the script
|
|
|
|
|
+# would report writes that never happened.
|
|
|
|
|
+if ! sudo -v; then
|
|
|
|
|
+ log_error "Could not obtain sudo credentials."
|
|
|
|
|
+ log_error "Run this from a real terminal -- sudo needs a tty to prompt."
|
|
|
|
|
+ exit 1
|
|
|
|
|
+fi
|
|
|
|
|
+
|
|
|
# Write a file only if its content differs, so re-runs stay quiet and we avoid
|
|
# Write a file only if its content differs, so re-runs stay quiet and we avoid
|
|
|
-# needlessly restarting units.
|
|
|
|
|
|
|
+# needlessly restarting units. Aborts on failure rather than reporting a
|
|
|
|
|
+# phantom success -- see the set -e note above.
|
|
|
install_file() {
|
|
install_file() {
|
|
|
local dest="$1" content="$2"
|
|
local dest="$1" content="$2"
|
|
|
if [[ -f "$dest" ]] && [[ "$(cat "$dest")" == "$content" ]]; then
|
|
if [[ -f "$dest" ]] && [[ "$(cat "$dest")" == "$content" ]]; then
|
|
|
return 1
|
|
return 1
|
|
|
fi
|
|
fi
|
|
|
- sudo mkdir -p "$(dirname "$dest")"
|
|
|
|
|
- printf '%s\n' "$content" | sudo tee "$dest" >/dev/null
|
|
|
|
|
|
|
+ if ! sudo mkdir -p "$(dirname "$dest")"; then
|
|
|
|
|
+ log_error "Failed to create $(dirname "$dest")"
|
|
|
|
|
+ exit 1
|
|
|
|
|
+ fi
|
|
|
|
|
+ if ! printf '%s\n' "$content" | sudo tee "$dest" >/dev/null; then
|
|
|
|
|
+ log_error "Failed to write $dest"
|
|
|
|
|
+ exit 1
|
|
|
|
|
+ fi
|
|
|
log_info "Wrote $dest"
|
|
log_info "Wrote $dest"
|
|
|
return 0
|
|
return 0
|
|
|
}
|
|
}
|
|
@@ -47,7 +64,10 @@ enable_unit() {
|
|
|
return 0
|
|
return 0
|
|
|
fi
|
|
fi
|
|
|
log_info "Enabling $unit"
|
|
log_info "Enabling $unit"
|
|
|
- sudo systemctl enable "$unit"
|
|
|
|
|
|
|
+ if ! sudo systemctl enable "$unit"; then
|
|
|
|
|
+ log_error "Failed to enable $unit"
|
|
|
|
|
+ exit 1
|
|
|
|
|
+ fi
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
enable_user_unit() {
|
|
enable_user_unit() {
|
|
@@ -56,7 +76,10 @@ enable_user_unit() {
|
|
|
return 0
|
|
return 0
|
|
|
fi
|
|
fi
|
|
|
log_info "Enabling (user) $unit"
|
|
log_info "Enabling (user) $unit"
|
|
|
- systemctl --user enable "$unit"
|
|
|
|
|
|
|
+ if ! systemctl --user enable "$unit"; then
|
|
|
|
|
+ log_error "Failed to enable user unit $unit"
|
|
|
|
|
+ exit 1
|
|
|
|
|
+ fi
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
log_info "Bootstrapping system state for $hostname_id"
|
|
log_info "Bootstrapping system state for $hostname_id"
|