2023-12-24 16:00:36 -08:00
#!/usr/bin/env bash
# @file Homebrew / Xcode Tools Installation
# @brief Ensures Xcode tools are installed on macOS and ensures Homebrew is installed on either Linux or macOS
# @description
# This script ensures macOS has tools like `git` by installing the Xcode command-line developer tools if they are
# not already installed. Then, on both Linux and macOS, it ensures Homebrew is installed.
#
# It also tries to perform a system update and there may be some manual work required since it is not possible
# to completely automate the system update process for macOS.
#
# ## Environment Variables
#
# * `NO_RESTART` - Set this variable to skip restarts triggered by system updates on macOS
#
# ## Homebrew Requirement
#
# Install Doctor relies on Homebrew for many tools that are currently only available via Homebrew. Removing the dependency
# would be a nice-to-have but there are currently no plans for supporting systems without Homebrew installed.
{{ includeTemplate "universal/profile-before" }}
{{ includeTemplate "universal/logg-before" }}
# @description This script ensures all the system utilities necessary to install Homebrew are present on the system.
ensureHomebrewDeps() {
if ! command -v curl > /dev/null || ! command -v git > /dev/null || ! command -v expect > /dev/null || ! command -v rsync > /dev/null; then
if command -v apt-get > /dev/null; then
# @description Ensure `build-essential`, `curl`, `expect`, `git`, `rsync`, `procps`, and `file` are installed on Debian / Ubuntu
sudo apt-get update
sudo apt-get install -y build-essential curl expect git rsync procps file
elif command -v dnf > /dev/null; then
# @description Ensure `curl`, `expect`, `git`, `rsync`, `procps-ng`, and `file` are installed on Fedora (as well as the Development Tools package)
sudo dnf groupinstall -y 'Development Tools'
sudo dnf install -y curl expect git rsync procps-ng file
elif command -v yum > /dev/null; then
# @description Ensure `curl`, `expect`, `git`, `rsync`, `procps-ng`, and `file` are installed on CentOS (as well as the Development Tools package)
sudo yum groupinstall -y 'Development Tools'
sudo yum install -y curl expect git rsync procps-ng file
elif command -v pacman > /dev/null; then
# @description Ensure `base-devel`, `curl`, `expect`, `git`, `rsync`, `procps-ng`, and `file` are installed on Archlinux
sudo pacman update
sudo pacman -Sy base-devel curl expect git rsync procps-ng file
elif command -v zypper > /dev/null; then
# @description Ensure `curl`, `expect`, `git`, `rsync`, `procps`, and `file` are installed on OpenSUSE (as well as the devel_basis pattern)
sudo zypper install -yt pattern devel_basis
sudo zypper install -y curl expect git rsync procps file
elif command -v apk > /dev/null; then
# @description Ensure `curl`, `expect`, `git`, `rsync`, `procps`, and `file` are installed on Alpine
apk add build-base curl expect git rsync procps file
elif [ -d /Applications ] && [ -d /Library ]; then
# @description Ensure CLI developer tools are available on macOS (via `xcode-select`)
sudo xcode-select -p >/dev/null 2>&1 || xcode-select --install
elif [[ " $ OSTYPE " == 'cygwin' ]] || [[ " $ OSTYPE " == 'msys' ]] || [[ " $ OSTYPE " == 'win32' ]]; then
# @description Ensure `curl`, `expect`, `git`, and `rsync` are installed on Windows
choco install -y curl expect git rsync
fi
fi
}
# @description This script ensures Homebrew is installed.
ensurePackageManagerHomebrew() {
if ! command -v brew > /dev/null; then
if command -v sudo > /dev/null && sudo -n true; then
echo | bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
else
logg info 'Homebrew is not installed. Password may be required.'
bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || BREW_EXIT_CODE="$?"
if [ -n " $ BREW_EXIT_CODE " ]; then
logg warn 'Homebrew was installed but part of the installation failed to complete successfully.'
if command -v brew > /dev/null; then
logg info 'Applying proper permissions on Homebrew folders'
sudo chmod -R go-w "$(brew --prefix)/share"
BREW_DIRS="share etc/bash_completion.d"
for BREW_DIR in $ BREW_DIRS ; do
if [ -d "$(brew --prefix)/ $ BREW_DIR " ]; then
sudo chown -Rf "$(whoami)" "$(brew --prefix)/ $ BREW_DIR "
fi
done
logg info 'Running brew update --force --quiet' && brew update --force --quiet
fi
fi
fi
fi
}
# @description Helper function utilized by [[upgradeDarwin]] to ensure the `expect` command is available on macOS
installExpect() {
if ! command -v expect > /dev/null; then
logg info 'Installing expect via Homebrew' && brew install expect
fi
}
# @description Helper function utilized by [[upgradeDarwin]] to ensure the `gsed` command is available on macOS
installGsed() {
if ! command -v gsed > /dev/null; then
logg info 'Installing gnu-sed via Homebrew' && brew install gnu-sed
fi
}
# @description Helper function utilized by [[upgradeDarwin]] to ensure the `gtimeout` command is available on macOS
installGtimeout() {
if ! command -v gtimeout > /dev/null; then
logg info 'Installing coreutils via Homebrew' && brew install coreutils
fi
}
# @description This script ensures `expect` and `gsed` are available. It then either extracts the escalation password by decrypting the
# `SUDO_PASSWORD` secret or prompts the user for the sudo password. After that, it automates the process of downloading
# system updates, rebooting, and re-starting the provisioning process until the system is fully updated.
#
# It may be important to note that although this script attempts to make the process fully automated, there may be
# certain circumstances where additional user input is necessary during the process. This might be true in cases
# where the system settings are controlled by MDM profiles for corporate laptops.
upgradeDarwin() {
if [ -d /Applications ] && [ -d /Library ] && [ -z " $ NO_RESTART " ]; then
### Ensure dependencies are installed
installExpect
installGsed
installGtimeout
### Attempt to populate SUDO_PASSWORD from secrets
if [ -z " $ SUDO_PASSWORD " ]; then
SUDO_PASSWORD="{{- if and (stat (joinPath .host.home ".config" "age" "chezmoi.txt")) (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "SUDO_PASSWORD")) -}}{{- includeTemplate "secrets/SUDO_PASSWORD" | decrypt | trim -}}{{- end -}}"
export SUDO_PASSWORD
fi
### Prompt for SUDO_PASSWORD
if [ " $ SUDO_PASSWORD " = "" ]; then
logg prompt "Enter the current user's login / admin password. Press ENTER to bypass and skip enabling auto-login. If you would like to bypass this prompt next time then pass in the password as an environment variable named SUDO_PASSWORD before running the kickstart script."
SUDO_PASSWORD="$(gum input --password --placeholder="Enter password..")"
export SUDO_PASSWORD
fi
### Run upgrade process
logg info 'Applying OS upgrades (if available)'
expect -c "set timeout -1
spawn sudo softwareupdate -i -a --agree-to-license
expect \"Password:\"
send \" ${ SUDO_PASSWORD } \r\"
expect eof" &> /dev/null || EXIT_CODE=$?
if [ -n " $ EXIT_CODE " ]; then
logg warn 'Error running softwareupdate'
unset EXIT_CODE
fi
# sudo sh -c "sudo softwareupdate -i -a --agree-to-license" || logg error 'Failed to trigger a system update via sudo softwareupdate -i -a --agree-to-license'
### Reboot if necessary
# Source: https://community.jamf.com/t5/jamf-pro/determine-if-update-requires-restart/m-p/11682
2023-12-24 16:17:19 -08:00
logg info 'Checking if softwareupdate requires a reboot'
2023-12-24 16:00:36 -08:00
if softwareupdate -l | grep restart > /dev/null; then
### Add kickstart script to .zshrc so it triggers automatically
if [ ! -f " $ HOME /.zshrc" ] || ! cat " $ HOME /.zshrc" | grep ' # TEMPORARY FOR INSTALL DOCTOR MACOS' > /dev/null; then
2023-12-24 16:18:49 -08:00
logg info 'Adding kickstart script to ~/.zshrc so script continues automatically if reboot is necessary'
2023-12-24 16:00:36 -08:00
echo 'bash <(curl -sSL --compressed https://install.doctor/start) # TEMPORARY FOR INSTALL DOCTOR MACOS' >> " $ HOME /.zshrc"
fi
if [ -n " $ SUDO_PASSWORD " ] && [ " $ SUDO_PASSWORD " != "" ]; then
### Install kcpassword
if ! command -v enable_autologin > /dev/null; then
2023-12-24 16:18:49 -08:00
logg info 'enable_autologin is not installed and it is a requirement for auto-logging in after reboot' && brew install xfreebird/utils/kcpassword
2023-12-24 16:00:36 -08:00
fi
### Enable auto-login via kcpassword
2023-12-24 16:17:19 -08:00
logg info "Enabling autologin for $ USER with acquired sudo password" && sudo enable_autologin " $ USER " " $ SUDO_PASSWORD "
2023-12-24 16:00:36 -08:00
fi
### Reboot
2023-12-24 17:02:53 -08:00
logg info 'Reboot required' && exit 140
2023-12-24 16:59:44 -08:00
# gtimeout 60 osascript -e 'tell application "Finder" to shut down' || OSASCRIPT_EXIT_CODE=$?
# if [ -n " $ OSASCRIPT_EXIT_CODE " ]; then
# logg info 'Reboot required and failed to gracefully shutdown within 1 minute, so forcing reboot' && sudo sh -c 'shutdown -r now'
# fi
2023-12-24 16:17:19 -08:00
else
logg info 'No reboot required for softwareupdate'
2023-12-24 16:00:36 -08:00
fi
if [ -f " $ HOME /.zshrc" ]; then
2023-12-24 16:17:19 -08:00
logg info 'Ensuring provision kickstart script is removed from ~/.zshrc'
2023-12-24 16:00:36 -08:00
if command -v gsed > /dev/null; then
2023-12-24 15:35:24 -08:00
sudo gsed -i '/ # TEMPORARY FOR INSTALL DOCTOR MACOS/d' " $ HOME /.zshrc" || logg warn "Failed to remove kickstart script from .zshrc"
else
sudo sed -i '/ # TEMPORARY FOR INSTALL DOCTOR MACOS/d' " $ HOME /.zshrc" || logg warn "Failed to remove kickstart script from .zshrc"
fi
2023-07-17 00:28:38 -07:00
fi
2023-12-24 15:35:24 -08:00
fi
}
Update dotfiles/.local/share/chezmoi/home/.chezmoiscripts/_universal/run_onchange_before_8-install-zx.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoiscripts/_universal/run_onchange_before_5-install-homebrew.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoiscripts/darwin/run_onchange_before_10_install-darwin-dependencies.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoitemplates/universal/profile, dotfiles/.local/share/chezmoi/home/private_dot_config/shell/exports.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoiscripts/_universal/run_onchange_after_99_bootstrap-z4h.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoiscripts/_universal/run_onchange_after_80-bash-completions.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoiscripts/_universal/run_onchange_after_60-cleanup.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoiscripts/_universal/run_onchange_after_50-crontab.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoiscripts/_universal/run_onchange_after_15_install-asdf-packages.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoiscripts/_universal/run_onchange_after_10_install-aqua-packages.sh.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoiscripts/archlinux/run_onchange_before_10_install-archlinux-dependencies.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoiscripts/centos/run_onchange_before_10-install-centos-dependencies.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoiscripts/darwin/run_onchange_after_10_configure-macos.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoiscripts/darwin/run_onchange_after_20-ensure-zsh-macos.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoiscripts/debian/run_onchange_before_10-install-debian-dependencies.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoiscripts/fedora/run_onchange_before_10-install-fedora-dependencies.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoiscripts/freebsd/run_onchange_before_11-install-freebsd-packages.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoiscripts/opensuse/run_onchange_before_11-install-opensuse-software.tmpl, dotfiles/.local/share/chezmoi/home/.chezmoiscripts/ubuntu/run_onchange_before_10_install-ubuntu-dependencies.tmpl, dotfiles/.local/share/chezmoi/home/private_dot_config/tabby/config.yaml.tmpl
Deleted dotfiles/.local/share/chezmoi/home/.chezmoiscripts/_universal/run_onchange_before_1-logging-deps
2022-11-26 19:55:43 -08:00
2023-12-24 15:35:24 -08:00
# @description This script attempts to load Homebrew by checking in typical locations.
2023-12-08 16:39:21 -08:00
loadHomebrew() {
2023-07-17 00:28:38 -07:00
if ! command -v brew > /dev/null; then
2023-12-08 16:39:21 -08:00
if [ -f /usr/local/bin/brew ]; then
eval "$(/usr/local/bin/brew shellenv)"
2023-12-09 04:44:09 -08:00
elif [ -f " ${ HOMEBREW_PREFIX : - / opt / homebrew } /bin/brew" ]; then
2023-12-08 16:39:21 -08:00
eval "$(" ${ HOMEBREW_PREFIX : - / opt / homebrew } /bin/brew" shellenv)"
2023-12-09 04:44:09 -08:00
elif [ -d " $ HOME /.linuxbrew" ]; then
2023-07-17 00:28:38 -07:00
eval "$(" $ HOME /.linuxbrew/bin/brew" shellenv)"
elif [ -d "/home/linuxbrew/.linuxbrew" ]; then
eval "(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
fi
fi
}
2023-12-24 15:35:24 -08:00
### Logic sequence
ensureHomebrewDeps
2023-12-08 16:39:21 -08:00
loadHomebrew
2022-11-26 19:27:29 -08:00
ensurePackageManagerHomebrew
2023-12-08 16:39:21 -08:00
loadHomebrew
2023-12-24 15:35:24 -08:00
upgradeDarwin