From e2ef02e334b8bc20ce55cc8e1c83d116f545f56d Mon Sep 17 00:00:00 2001 From: Brian Zalewski <59970525+ProfessorManhattan@users.noreply.github.com> Date: Mon, 25 Dec 2023 01:56:03 +0000 Subject: [PATCH] Latest --- home/.chezmoitemplates/darwin/Brewfile | 1 + .../universal/common-dependencies | 1 + scripts/cloudflared-ssh.sh | 14 +-- scripts/homebrew.sh | 14 +-- scripts/partials/basic-deps | 14 +-- scripts/provision.sh | 105 ++++++++++-------- scripts/src/provision.sh.tmpl | 91 ++++++++------- 7 files changed, 130 insertions(+), 110 deletions(-) diff --git a/home/.chezmoitemplates/darwin/Brewfile b/home/.chezmoitemplates/darwin/Brewfile index f7a93fb5..3273d2ff 100644 --- a/home/.chezmoitemplates/darwin/Brewfile +++ b/home/.chezmoitemplates/darwin/Brewfile @@ -24,6 +24,7 @@ brew "gsed" brew "gum" brew "htmlq" brew "jq" +brew "moreutils" brew "node" brew "progress" brew "python@3.11" diff --git a/home/.chezmoitemplates/universal/common-dependencies b/home/.chezmoitemplates/universal/common-dependencies index ad8d7102..f20d7da6 100644 --- a/home/.chezmoitemplates/universal/common-dependencies +++ b/home/.chezmoitemplates/universal/common-dependencies @@ -11,6 +11,7 @@ "gnupg2" "jq" "make" + "moreutils" "re2c" "util-linux" "wget" diff --git a/scripts/cloudflared-ssh.sh b/scripts/cloudflared-ssh.sh index 5df7b217..d7fdfa3e 100644 --- a/scripts/cloudflared-ssh.sh +++ b/scripts/cloudflared-ssh.sh @@ -129,26 +129,26 @@ ensureBasicDeps() { if command -v apt-get > /dev/null; then ### Debian / Ubuntu logg info 'Running sudo apt-get update' && sudo apt-get update - logg info 'Running sudo apt-get install -y build-essential curl expect git rsync procps file' && sudo apt-get install -y build-essential curl expect git rsync procps file + logg info 'Running sudo apt-get install -y build-essential curl expect git moreutils rsync procps file' && sudo apt-get install -y build-essential curl expect git moreutils rsync procps file elif command -v dnf > /dev/null; then ### Fedora logg info 'Running sudo dnf groupinstall -y "Development Tools"' && sudo dnf groupinstall -y 'Development Tools' - logg info 'Running sudo dnf install -y curl expect git rsync procps-ng file' && sudo dnf install -y curl expect git rsync procps-ng file + logg info 'Running sudo dnf install -y curl expect git moreutils rsync procps-ng file' && sudo dnf install -y curl expect git moreutils rsync procps-ng file elif command -v yum > /dev/null; then ### CentOS logg info 'Running sudo yum groupinstall -y "Development Tools"' && sudo yum groupinstall -y 'Development Tools' - logg info 'Running sudo yum install -y curl expect git rsync procps-ng file' && sudo yum install -y curl expect git rsync procps-ng file + logg info 'Running sudo yum install -y curl expect git moreutils rsync procps-ng file' && sudo yum install -y curl expect git moreutils rsync procps-ng file elif command -v pacman > /dev/null; then ### Archlinux logg info 'Running sudo pacman update' && sudo pacman update - logg info 'Running sudo pacman -Syu base-devel curl expect git rsync procps-ng file' && sudo pacman -Syu base-devel curl expect git rsync procps-ng file + logg info 'Running sudo pacman -Syu base-devel curl expect git moreutils rsync procps-ng file' && sudo pacman -Syu base-devel curl expect git moreutils rsync procps-ng file elif command -v zypper > /dev/null; then ### OpenSUSE logg info 'Running sudo zypper install -yt pattern devel_basis' && sudo zypper install -yt pattern devel_basis - logg info 'Running sudo zypper install -y curl expect git rsync procps file' && sudo zypper install -y curl expect git rsync procps file + logg info 'Running sudo zypper install -y curl expect git moreutils rsync procps file' && sudo zypper install -y curl expect git moreutils rsync procps file elif command -v apk > /dev/null; then ### Alpine - logg info 'Running sudo apk add build-base curl expect git rsync ruby procps file' && sudo apk add build-base curl expect git rsync ruby procps file + logg info 'Running sudo apk add build-base curl expect git moreutils rsync ruby procps file' && sudo apk add build-base curl expect git moreutils rsync ruby procps file elif [ -d /Applications ] && [ -d /Library ]; then ### macOS logg info "Ensuring Xcode Command Line Tools are installed.." @@ -161,7 +161,7 @@ ensureBasicDeps() { fi elif [[ "$OSTYPE" == 'cygwin' ]] || [[ "$OSTYPE" == 'msys' ]] || [[ "$OSTYPE" == 'win32' ]]; then ### Windows - logg info 'Running choco install -y curl expect git rsync' && choco install -y curl expect git rsync + logg info 'Running choco install -y curl expect git moreutils rsync' && choco install -y curl expect git moreutils rsync elif command -v nix-env > /dev/null; then ### NixOS logg warn "TODO - Add support for NixOS" diff --git a/scripts/homebrew.sh b/scripts/homebrew.sh index 7e8dcae8..417efc80 100644 --- a/scripts/homebrew.sh +++ b/scripts/homebrew.sh @@ -98,26 +98,26 @@ ensureBasicDeps() { if command -v apt-get > /dev/null; then ### Debian / Ubuntu logg info 'Running sudo apt-get update' && sudo apt-get update - logg info 'Running sudo apt-get install -y build-essential curl expect git rsync procps file' && sudo apt-get install -y build-essential curl expect git rsync procps file + logg info 'Running sudo apt-get install -y build-essential curl expect git moreutils rsync procps file' && sudo apt-get install -y build-essential curl expect git moreutils rsync procps file elif command -v dnf > /dev/null; then ### Fedora logg info 'Running sudo dnf groupinstall -y "Development Tools"' && sudo dnf groupinstall -y 'Development Tools' - logg info 'Running sudo dnf install -y curl expect git rsync procps-ng file' && sudo dnf install -y curl expect git rsync procps-ng file + logg info 'Running sudo dnf install -y curl expect git moreutils rsync procps-ng file' && sudo dnf install -y curl expect git moreutils rsync procps-ng file elif command -v yum > /dev/null; then ### CentOS logg info 'Running sudo yum groupinstall -y "Development Tools"' && sudo yum groupinstall -y 'Development Tools' - logg info 'Running sudo yum install -y curl expect git rsync procps-ng file' && sudo yum install -y curl expect git rsync procps-ng file + logg info 'Running sudo yum install -y curl expect git moreutils rsync procps-ng file' && sudo yum install -y curl expect git moreutils rsync procps-ng file elif command -v pacman > /dev/null; then ### Archlinux logg info 'Running sudo pacman update' && sudo pacman update - logg info 'Running sudo pacman -Syu base-devel curl expect git rsync procps-ng file' && sudo pacman -Syu base-devel curl expect git rsync procps-ng file + logg info 'Running sudo pacman -Syu base-devel curl expect git moreutils rsync procps-ng file' && sudo pacman -Syu base-devel curl expect git moreutils rsync procps-ng file elif command -v zypper > /dev/null; then ### OpenSUSE logg info 'Running sudo zypper install -yt pattern devel_basis' && sudo zypper install -yt pattern devel_basis - logg info 'Running sudo zypper install -y curl expect git rsync procps file' && sudo zypper install -y curl expect git rsync procps file + logg info 'Running sudo zypper install -y curl expect git moreutils rsync procps file' && sudo zypper install -y curl expect git moreutils rsync procps file elif command -v apk > /dev/null; then ### Alpine - logg info 'Running sudo apk add build-base curl expect git rsync ruby procps file' && sudo apk add build-base curl expect git rsync ruby procps file + logg info 'Running sudo apk add build-base curl expect git moreutils rsync ruby procps file' && sudo apk add build-base curl expect git moreutils rsync ruby procps file elif [ -d /Applications ] && [ -d /Library ]; then ### macOS logg info "Ensuring Xcode Command Line Tools are installed.." @@ -130,7 +130,7 @@ ensureBasicDeps() { fi elif [[ "$OSTYPE" == 'cygwin' ]] || [[ "$OSTYPE" == 'msys' ]] || [[ "$OSTYPE" == 'win32' ]]; then ### Windows - logg info 'Running choco install -y curl expect git rsync' && choco install -y curl expect git rsync + logg info 'Running choco install -y curl expect git moreutils rsync' && choco install -y curl expect git moreutils rsync elif command -v nix-env > /dev/null; then ### NixOS logg warn "TODO - Add support for NixOS" diff --git a/scripts/partials/basic-deps b/scripts/partials/basic-deps index 70481406..42cd29f6 100644 --- a/scripts/partials/basic-deps +++ b/scripts/partials/basic-deps @@ -8,26 +8,26 @@ ensureBasicDeps() { if command -v apt-get > /dev/null; then ### Debian / Ubuntu logg info 'Running sudo apt-get update' && sudo apt-get update - logg info 'Running sudo apt-get install -y build-essential curl expect git rsync procps file' && sudo apt-get install -y build-essential curl expect git rsync procps file + logg info 'Running sudo apt-get install -y build-essential curl expect git moreutils rsync procps file' && sudo apt-get install -y build-essential curl expect git moreutils rsync procps file elif command -v dnf > /dev/null; then ### Fedora logg info 'Running sudo dnf groupinstall -y "Development Tools"' && sudo dnf groupinstall -y 'Development Tools' - logg info 'Running sudo dnf install -y curl expect git rsync procps-ng file' && sudo dnf install -y curl expect git rsync procps-ng file + logg info 'Running sudo dnf install -y curl expect git moreutils rsync procps-ng file' && sudo dnf install -y curl expect git moreutils rsync procps-ng file elif command -v yum > /dev/null; then ### CentOS logg info 'Running sudo yum groupinstall -y "Development Tools"' && sudo yum groupinstall -y 'Development Tools' - logg info 'Running sudo yum install -y curl expect git rsync procps-ng file' && sudo yum install -y curl expect git rsync procps-ng file + logg info 'Running sudo yum install -y curl expect git moreutils rsync procps-ng file' && sudo yum install -y curl expect git moreutils rsync procps-ng file elif command -v pacman > /dev/null; then ### Archlinux logg info 'Running sudo pacman update' && sudo pacman update - logg info 'Running sudo pacman -Syu base-devel curl expect git rsync procps-ng file' && sudo pacman -Syu base-devel curl expect git rsync procps-ng file + logg info 'Running sudo pacman -Syu base-devel curl expect git moreutils rsync procps-ng file' && sudo pacman -Syu base-devel curl expect git moreutils rsync procps-ng file elif command -v zypper > /dev/null; then ### OpenSUSE logg info 'Running sudo zypper install -yt pattern devel_basis' && sudo zypper install -yt pattern devel_basis - logg info 'Running sudo zypper install -y curl expect git rsync procps file' && sudo zypper install -y curl expect git rsync procps file + logg info 'Running sudo zypper install -y curl expect git moreutils rsync procps file' && sudo zypper install -y curl expect git moreutils rsync procps file elif command -v apk > /dev/null; then ### Alpine - logg info 'Running sudo apk add build-base curl expect git rsync ruby procps file' && sudo apk add build-base curl expect git rsync ruby procps file + logg info 'Running sudo apk add build-base curl expect git moreutils rsync ruby procps file' && sudo apk add build-base curl expect git moreutils rsync ruby procps file elif [ -d /Applications ] && [ -d /Library ]; then ### macOS logg info "Ensuring Xcode Command Line Tools are installed.." @@ -40,7 +40,7 @@ ensureBasicDeps() { fi elif [[ "$OSTYPE" == 'cygwin' ]] || [[ "$OSTYPE" == 'msys' ]] || [[ "$OSTYPE" == 'win32' ]]; then ### Windows - logg info 'Running choco install -y curl expect git rsync' && choco install -y curl expect git rsync + logg info 'Running choco install -y curl expect git moreutils rsync' && choco install -y curl expect git moreutils rsync elif command -v nix-env > /dev/null; then ### NixOS logg warn "TODO - Add support for NixOS" diff --git a/scripts/provision.sh b/scripts/provision.sh index a89e4997..12b1bc3d 100644 --- a/scripts/provision.sh +++ b/scripts/provision.sh @@ -163,26 +163,26 @@ ensureBasicDeps() { if command -v apt-get > /dev/null; then ### Debian / Ubuntu logg info 'Running sudo apt-get update' && sudo apt-get update - logg info 'Running sudo apt-get install -y build-essential curl expect git rsync procps file' && sudo apt-get install -y build-essential curl expect git rsync procps file + logg info 'Running sudo apt-get install -y build-essential curl expect git moreutils rsync procps file' && sudo apt-get install -y build-essential curl expect git moreutils rsync procps file elif command -v dnf > /dev/null; then ### Fedora logg info 'Running sudo dnf groupinstall -y "Development Tools"' && sudo dnf groupinstall -y 'Development Tools' - logg info 'Running sudo dnf install -y curl expect git rsync procps-ng file' && sudo dnf install -y curl expect git rsync procps-ng file + logg info 'Running sudo dnf install -y curl expect git moreutils rsync procps-ng file' && sudo dnf install -y curl expect git moreutils rsync procps-ng file elif command -v yum > /dev/null; then ### CentOS logg info 'Running sudo yum groupinstall -y "Development Tools"' && sudo yum groupinstall -y 'Development Tools' - logg info 'Running sudo yum install -y curl expect git rsync procps-ng file' && sudo yum install -y curl expect git rsync procps-ng file + logg info 'Running sudo yum install -y curl expect git moreutils rsync procps-ng file' && sudo yum install -y curl expect git moreutils rsync procps-ng file elif command -v pacman > /dev/null; then ### Archlinux logg info 'Running sudo pacman update' && sudo pacman update - logg info 'Running sudo pacman -Syu base-devel curl expect git rsync procps-ng file' && sudo pacman -Syu base-devel curl expect git rsync procps-ng file + logg info 'Running sudo pacman -Syu base-devel curl expect git moreutils rsync procps-ng file' && sudo pacman -Syu base-devel curl expect git moreutils rsync procps-ng file elif command -v zypper > /dev/null; then ### OpenSUSE logg info 'Running sudo zypper install -yt pattern devel_basis' && sudo zypper install -yt pattern devel_basis - logg info 'Running sudo zypper install -y curl expect git rsync procps file' && sudo zypper install -y curl expect git rsync procps file + logg info 'Running sudo zypper install -y curl expect git moreutils rsync procps file' && sudo zypper install -y curl expect git moreutils rsync procps file elif command -v apk > /dev/null; then ### Alpine - logg info 'Running sudo apk add build-base curl expect git rsync ruby procps file' && sudo apk add build-base curl expect git rsync ruby procps file + logg info 'Running sudo apk add build-base curl expect git moreutils rsync ruby procps file' && sudo apk add build-base curl expect git moreutils rsync ruby procps file elif [ -d /Applications ] && [ -d /Library ]; then ### macOS logg info "Ensuring Xcode Command Line Tools are installed.." @@ -195,7 +195,7 @@ ensureBasicDeps() { fi elif [[ "$OSTYPE" == 'cygwin' ]] || [[ "$OSTYPE" == 'msys' ]] || [[ "$OSTYPE" == 'win32' ]]; then ### Windows - logg info 'Running choco install -y curl expect git rsync' && choco install -y curl expect git rsync + logg info 'Running choco install -y curl expect git moreutils rsync' && choco install -y curl expect git moreutils rsync elif command -v nix-env > /dev/null; then ### NixOS logg warn "TODO - Add support for NixOS" @@ -496,6 +496,9 @@ ensureHomebrewDeps() { if ! command -v gtimeout > /dev/null; then brew install --quiet coreutils fi + if ! command -v ts > /dev/null; then + brew install --quiet moreutils + fi fi } @@ -546,58 +549,65 @@ initChezmoiAndPrompt() { fi } -# @description Run `chezmoi apply` and enable verbose mode if the `DEBUG_MODE` or `DEBUG` environment variable is set to true -configureDebugMode() { +# @description Save the log of the provision process to `$HOME/.local/var/log/install.doctor/install.doctor.$(date +%s).log` and add the Chezmoi +# `--force` flag if the `HEADLESS_INSTALL` variable is set to `true`. +runChezmoi() { + ### Set up logging + mkdir -p "$HOME/.local/var/log/install.doctor" + LOG_FILE="$HOME/.local/var/log/install.doctor/chezmoi-apply-$(date +%s).log" + + ### Apply command flags + FORCE_MODIFIER="" + if [ -n "$HEADLESS_INSTALL" ]; then + logg info 'Running chezmoi apply forcefully because HEADLESS_INSTALL is set' + FORCE_MODIFIER="--force" + fi + KEEP_GOING_MODIFIER="" + if [ -n "$KEEP_GOING" ]; then + logg info 'Instructing chezmoi to keep going in the case of errors because KEEP_GOING is set' + KEEP_GOING_MODIFIER="-k" + fi + DEBUG_MODIFIER="" if [ -n "$DEBUG_MODE" ] || [ -n "$DEBUG" ]; then logg info "Either DEBUG_MODE or DEBUG environment variables were set so Chezmoi will be run in debug mode" export DEBUG_MODIFIER="-vvvvv" fi -} -# @description Save the log of the provision process to `$HOME/.local/var/log/install.doctor/install.doctor.$(date +%s).log` and add the Chezmoi -# `--force` flag if the `HEADLESS_INSTALL` variable is set to `true`. -runChezmoi() { - mkdir -p "$HOME/.local/var/log/install.doctor" - LOG_FILE="$HOME/.local/var/log/install.doctor/install.doctor.$(date +%s).log" - if [ "$HEADLESS_INSTALL" = 'true' ]; then - logg info 'Running chezmoi apply forcefully' - if command -v unbuffer > /dev/null; then - if command -v caffeinate > /dev/null; then - caffeinate unbuffer -p chezmoi apply $DEBUG_MODIFIER -k --force 2>&1 | tee "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? - else - unbuffer -p chezmoi apply $DEBUG_MODIFIER -k --force 2>&1 | tee "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? - fi + ### Run chezmoi apply + if command -v unbuffer > /dev/null; then + if command -v caffeinate > /dev/null; then + logg info "Running: caffeinate unbuffer -p chezmoi apply $DEBUG_MODIFIER $KEEP_GOING_MODIFIER $FORCE_MODIFIER" + caffeinate unbuffer -p chezmoi apply $DEBUG_MODIFIER $KEEP_GOING_MODIFIER $FORCE_MODIFIER 2>&1 | tee /dev/tty | ts '[%Y-%m-%d %H:%M:%S]' > "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? else - if command -v caffeinate > /dev/null; then - caffeinate chezmoi apply $DEBUG_MODIFIER -k --force 2>&1 | tee "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? - else - chezmoi apply $DEBUG_MODIFIER -k --force 2>&1 | tee "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? - fi + logg info "Running: unbuffer -p chezmoi apply $DEBUG_MODIFIER $KEEP_GOING_MODIFIER $FORCE_MODIFIER" + unbuffer -p chezmoi apply $DEBUG_MODIFIER $KEEP_GOING_MODIFIER $FORCE_MODIFIER 2>&1 | tee /dev/tty | ts '[%Y-%m-%d %H:%M:%S]' > "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? fi + logg info "Unbuffering log file $LOG_FILE" + UNBUFFER_TMP="$(mktemp)" + unbuffer cat "$LOG_FILE" > "$UNBUFFER_TMP" + mv -f "$UNBUFFER_TMP" "$LOG_FILE" else - logg info 'Running chezmoi apply' - if command -v unbuffer > /dev/null; then - if command -v caffeinate > /dev/null; then - caffeinate unbuffer -p chezmoi apply $DEBUG_MODIFIER -k 2>&1 | tee "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? - else - unbuffer -p chezmoi apply $DEBUG_MODIFIER -k 2>&1 | tee "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? - fi + if command -v caffeinate > /dev/null; then + logg info "Running: caffeinate chezmoi apply $DEBUG_MODIFIER $KEEP_GOING_MODIFIER $FORCE_MODIFIER" + caffeinate chezmoi apply $DEBUG_MODIFIER $KEEP_GOING_MODIFIER $FORCE_MODIFIER 2>&1 | tee /dev/tty | ts '[%Y-%m-%d %H:%M:%S]' > "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? else - if command -v caffeinate > /dev/null; then - caffeinate chezmoi apply $DEBUG_MODIFIER -k 2>&1 | tee "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? - else - chezmoi apply $DEBUG_MODIFIER -k 2>&1 | tee "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? - fi + logg info "Running: chezmoi apply $DEBUG_MODIFIER $KEEP_GOING_MODIFIER $FORCE_MODIFIER" + chezmoi apply $DEBUG_MODIFIER $KEEP_GOING_MODIFIER $FORCE_MODIFIER 2>&1 | tee /dev/tty | ts '[%Y-%m-%d %H:%M:%S]' > "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? fi fi + + ### Handle exit codes in log + if cat "$LOG_FILE" | grep 'chezmoi: exit status 140' > /dev/null; then + logg info "Chezmoi signalled that a reboot is necessary to apply a system update" + logg info "Running softwareupdate with the reboot flag" + sudo softwareupdate -i -a -R --agree-to-license && exit + fi + + ### Handle actual process exit code if [ -n "$CHEZMOI_EXIT_CODE" ]; then - if [ "$CHEZMOI_EXIT_CODE" = "140" ]; then - logg info "Chezmoi signalled that a reboot is necessary to apply a system update" - logg info "Running softwareupdate with the reboot flag" - sudo softwareupdate -i -a -R --agree-to-license && exit - else - logg error "Chezmoi encountered an error and exitted with an exit code of $CHEZMOI_EXIT_CODE" - fi + logg error "Chezmoi encountered an error and exitted with an exit code of $CHEZMOI_EXIT_CODE" + else + logg success 'Finished provisioning the system' fi } @@ -634,7 +644,6 @@ provisionLogic() { logg info "Handling Qubes dom0 logic (if applicable)" && handleQubesDom0 logg info "Cloning / updating source repository" && cloneChezmoiSourceRepo logg info "Handling pre-provision logic" && initChezmoiAndPrompt - logg info "Handling debug mode if DEBUG or DEBUG_MODE are defined" && configureDebugMode logg info "Running the Chezmoi provisioning" && runChezmoi logg info "Ensuring temporary passwordless sudo is removed" && removePasswordlessSudo logg info "Handling post-provision logic" && postProvision diff --git a/scripts/src/provision.sh.tmpl b/scripts/src/provision.sh.tmpl index 6c221f5d..76ea6820 100644 --- a/scripts/src/provision.sh.tmpl +++ b/scripts/src/provision.sh.tmpl @@ -259,6 +259,9 @@ ensureHomebrewDeps() { if ! command -v gtimeout > /dev/null; then brew install --quiet coreutils fi + if ! command -v ts > /dev/null; then + brew install --quiet moreutils + fi fi } @@ -309,58 +312,65 @@ initChezmoiAndPrompt() { fi } -# @description Run `chezmoi apply` and enable verbose mode if the `DEBUG_MODE` or `DEBUG` environment variable is set to true -configureDebugMode() { +# @description Save the log of the provision process to `$HOME/.local/var/log/install.doctor/install.doctor.$(date +%s).log` and add the Chezmoi +# `--force` flag if the `HEADLESS_INSTALL` variable is set to `true`. +runChezmoi() { + ### Set up logging + mkdir -p "$HOME/.local/var/log/install.doctor" + LOG_FILE="$HOME/.local/var/log/install.doctor/chezmoi-apply-$(date +%s).log" + + ### Apply command flags + FORCE_MODIFIER="" + if [ -n "$HEADLESS_INSTALL" ]; then + logg info 'Running chezmoi apply forcefully because HEADLESS_INSTALL is set' + FORCE_MODIFIER="--force" + fi + KEEP_GOING_MODIFIER="" + if [ -n "$KEEP_GOING" ]; then + logg info 'Instructing chezmoi to keep going in the case of errors because KEEP_GOING is set' + KEEP_GOING_MODIFIER="-k" + fi + DEBUG_MODIFIER="" if [ -n "$DEBUG_MODE" ] || [ -n "$DEBUG" ]; then logg info "Either DEBUG_MODE or DEBUG environment variables were set so Chezmoi will be run in debug mode" export DEBUG_MODIFIER="-vvvvv" fi -} -# @description Save the log of the provision process to `$HOME/.local/var/log/install.doctor/install.doctor.$(date +%s).log` and add the Chezmoi -# `--force` flag if the `HEADLESS_INSTALL` variable is set to `true`. -runChezmoi() { - mkdir -p "$HOME/.local/var/log/install.doctor" - LOG_FILE="$HOME/.local/var/log/install.doctor/install.doctor.$(date +%s).log" - if [ "$HEADLESS_INSTALL" = 'true' ]; then - logg info 'Running chezmoi apply forcefully' - if command -v unbuffer > /dev/null; then - if command -v caffeinate > /dev/null; then - caffeinate unbuffer -p chezmoi apply $DEBUG_MODIFIER -k --force 2>&1 | tee "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? - else - unbuffer -p chezmoi apply $DEBUG_MODIFIER -k --force 2>&1 | tee "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? - fi + ### Run chezmoi apply + if command -v unbuffer > /dev/null; then + if command -v caffeinate > /dev/null; then + logg info "Running: caffeinate unbuffer -p chezmoi apply $DEBUG_MODIFIER $KEEP_GOING_MODIFIER $FORCE_MODIFIER" + caffeinate unbuffer -p chezmoi apply $DEBUG_MODIFIER $KEEP_GOING_MODIFIER $FORCE_MODIFIER 2>&1 | tee /dev/tty | ts '[%Y-%m-%d %H:%M:%S]' > "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? else - if command -v caffeinate > /dev/null; then - caffeinate chezmoi apply $DEBUG_MODIFIER -k --force 2>&1 | tee "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? - else - chezmoi apply $DEBUG_MODIFIER -k --force 2>&1 | tee "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? - fi + logg info "Running: unbuffer -p chezmoi apply $DEBUG_MODIFIER $KEEP_GOING_MODIFIER $FORCE_MODIFIER" + unbuffer -p chezmoi apply $DEBUG_MODIFIER $KEEP_GOING_MODIFIER $FORCE_MODIFIER 2>&1 | tee /dev/tty | ts '[%Y-%m-%d %H:%M:%S]' > "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? fi + logg info "Unbuffering log file $LOG_FILE" + UNBUFFER_TMP="$(mktemp)" + unbuffer cat "$LOG_FILE" > "$UNBUFFER_TMP" + mv -f "$UNBUFFER_TMP" "$LOG_FILE" else - logg info 'Running chezmoi apply' - if command -v unbuffer > /dev/null; then - if command -v caffeinate > /dev/null; then - caffeinate unbuffer -p chezmoi apply $DEBUG_MODIFIER -k 2>&1 | tee "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? - else - unbuffer -p chezmoi apply $DEBUG_MODIFIER -k 2>&1 | tee "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? - fi + if command -v caffeinate > /dev/null; then + logg info "Running: caffeinate chezmoi apply $DEBUG_MODIFIER $KEEP_GOING_MODIFIER $FORCE_MODIFIER" + caffeinate chezmoi apply $DEBUG_MODIFIER $KEEP_GOING_MODIFIER $FORCE_MODIFIER 2>&1 | tee /dev/tty | ts '[%Y-%m-%d %H:%M:%S]' > "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? else - if command -v caffeinate > /dev/null; then - caffeinate chezmoi apply $DEBUG_MODIFIER -k 2>&1 | tee "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? - else - chezmoi apply $DEBUG_MODIFIER -k 2>&1 | tee "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? - fi + logg info "Running: chezmoi apply $DEBUG_MODIFIER $KEEP_GOING_MODIFIER $FORCE_MODIFIER" + chezmoi apply $DEBUG_MODIFIER $KEEP_GOING_MODIFIER $FORCE_MODIFIER 2>&1 | tee /dev/tty | ts '[%Y-%m-%d %H:%M:%S]' > "$LOG_FILE" || CHEZMOI_EXIT_CODE=$? fi fi + + ### Handle exit codes in log + if cat "$LOG_FILE" | grep 'chezmoi: exit status 140' > /dev/null; then + logg info "Chezmoi signalled that a reboot is necessary to apply a system update" + logg info "Running softwareupdate with the reboot flag" + sudo softwareupdate -i -a -R --agree-to-license && exit + fi + + ### Handle actual process exit code if [ -n "$CHEZMOI_EXIT_CODE" ]; then - if [ "$CHEZMOI_EXIT_CODE" = "140" ]; then - logg info "Chezmoi signalled that a reboot is necessary to apply a system update" - logg info "Running softwareupdate with the reboot flag" - sudo softwareupdate -i -a -R --agree-to-license && exit - else - logg error "Chezmoi encountered an error and exitted with an exit code of $CHEZMOI_EXIT_CODE" - fi + logg error "Chezmoi encountered an error and exitted with an exit code of $CHEZMOI_EXIT_CODE" + else + logg success 'Finished provisioning the system' fi } @@ -397,7 +407,6 @@ provisionLogic() { logg info "Handling Qubes dom0 logic (if applicable)" && handleQubesDom0 logg info "Cloning / updating source repository" && cloneChezmoiSourceRepo logg info "Handling pre-provision logic" && initChezmoiAndPrompt - logg info "Handling debug mode if DEBUG or DEBUG_MODE are defined" && configureDebugMode logg info "Running the Chezmoi provisioning" && runChezmoi logg info "Ensuring temporary passwordless sudo is removed" && removePasswordlessSudo logg info "Handling post-provision logic" && postProvision