Last active
March 1, 2023 23:31
-
-
Save fondesa/65d47bf70966d16769d6a27e04a3f075 to your computer and use it in GitHub Desktop.
Bootstraps a new device (macOS or Linux) preparing the setup for private YADM dotfiles. Disclaimer: highly opinionated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
COLOR_NULL="\033[0m" | |
SSH_DIR="$HOME/.ssh" | |
SSH_ID_FILE="$SSH_DIR/ssh_id" | |
main() { | |
if [[ "$OSTYPE" == "darwin"* ]]; then | |
bootstrap_device_macos | |
else | |
bootstrap_device_linux | |
fi | |
} | |
bootstrap_device_macos() { | |
install_homebrew_macos | |
install_yadm_macos | |
generate_ssh_key_macos | |
start_ssh_agent | |
add_ssh_key_macos | |
change_ssh_files_permissions | |
add_ssh_keychain_config_macos | |
copy_ssh_key_macos | |
remove_git_config | |
} | |
bootstrap_device_linux() { | |
install_git_linux | |
install_yadm_linux | |
generate_ssh_key_linux | |
start_ssh_agent | |
add_ssh_key_linux | |
change_ssh_files_permissions | |
copy_ssh_key_linux | |
remove_git_config | |
} | |
install_homebrew_macos() { | |
print_info "Installing Homebrew" | |
sudo echo # Workaround to require sudo access on older macOS versions. | |
NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || print_error "Can't install Homebrew" | |
print_success "Homebrew installed successfully" | |
} | |
install_yadm_macos() { | |
print_info "Installing YADM" | |
brew install yadm || print_error "Can't install YADM" | |
print_success "YADM installed successfully" | |
} | |
install_git_linux() { | |
print_info "Installing Git" | |
sudo apt-get -y install git || print_error "Can't install Git" | |
print_success "Git installed successfully" | |
} | |
install_yadm_linux() { | |
print_info "Installing YADM" | |
local yadm_repo_dir="$HOME/.yadm_repo" | |
mkdir -p "$yadm_repo_dir" | |
git clone https://github.com/TheLocehiliosan/yadm.git "$yadm_repo_dir" || print_error "Can't clone the YADM Git repo" | |
sudo ln -s "$yadm_repo_dir/yadm" "/usr/local/bin/yadm" || print_error "Can't symlink YADM in /usr/local/bin" | |
print_success "YADM installed successfully" | |
} | |
generate_ssh_key_linux() { | |
print_info "Installing ssh-askpass" | |
sudo apt-get -y install ssh-askpass || print_error "Can't install ssh-askpass" | |
print_success "ssh-askpass successfully installed" | |
generate_ssh_key "$(sudo dmidecode -s system-uuid | awk '{print tolower($0)}')" | |
} | |
generate_ssh_key_macos() { | |
generate_ssh_key "$(ioreg -d2 -c IOPlatformExpertDevice | awk -F\" '/IOPlatformUUID/{print $(NF-1)}' | awk '{print tolower($0)}')" | |
} | |
generate_ssh_key() { | |
print_info "Generating SSH key" | |
local device_id=$1 | |
local uname now | |
uname=$(uname -s | awk '{print tolower($0)}') | |
now=$(date '+%Y%m%d') | |
ssh-keygen -t ed25519 -f "$SSH_ID_FILE" -C "{$uname}--{$device_id}--{$now}" || print_error "Can't generate SSH key" | |
print_success "Generated SSH key" | |
} | |
start_ssh_agent() { | |
print_info "Starting SSH agent" | |
eval "$(ssh-agent -s)" || print_error "Can't start SSH agent" | |
print_success "Started SSH agent" | |
} | |
add_ssh_key_macos() { | |
print_info "Adding SSH key" | |
local macos_version | |
IFS='.' read -ra macos_version <<<"$(sw_vers -productVersion)" | |
local macos_major_version="${macos_version[0]}" | |
if [[ "$macos_major_version" -ge 12 ]]; then | |
ssh-add --apple-use-keychain "$SSH_ID_FILE" || print_error "Can't add SSH key" | |
else | |
ssh-add -K "$SSH_ID_FILE" || print_error "Can't add SSH key" | |
fi | |
print_success "Added SSH key" | |
} | |
add_ssh_key_linux() { | |
print_info "Adding SSH key" | |
ssh-add "$SSH_ID_FILE" || print_error "Can't add SSH key" | |
print_success "Added SSH key" | |
} | |
change_ssh_files_permissions() { | |
print_info "Updating permissions of SSH files" | |
touch "$SSH_DIR/config" | |
touch "$SSH_DIR/known_hosts" | |
chmod 400 "$SSH_DIR/config" "$SSH_ID_FILE" | |
chmod 444 "$SSH_ID_FILE.pub" | |
chmod 600 "$SSH_DIR/known_hosts" | |
chmod 700 "$SSH_DIR" | |
print_success "Updated permissions of SSH files" | |
} | |
add_ssh_keychain_config_macos() { | |
print_info "Updating SSH config to use Keychain on macOS" | |
chmod 600 "$SSH_DIR/config" | |
printf "Host *\n UseKeychain yes\n AddKeysToAgent yes\n IdentityFile ~/.ssh/ssh_id\n" >>"$SSH_DIR/config" | |
chmod 400 "$SSH_DIR/config" | |
print_success "Updated SSH config to use Keychain on macOS" | |
} | |
copy_ssh_key_macos() { | |
print_info "Copying SSH key to clipboard" | |
cat "$SSH_ID_FILE.pub" | pbcopy | |
print_success "SSH key copied to the clipboard. Go to the following link and add it: https://github.com/settings/ssh/new" | |
} | |
copy_ssh_key_linux() { | |
print_info "Installing xclip" | |
sudo apt-get -y install xclip || print_error "Can't install xclip" | |
print_success "xclip installed successfully" | |
print_info "Copying SSH key to clipboard" | |
xclip -selection clipboard <"$SSH_ID_FILE.pub" | |
print_success "SSH key copied to the clipboard. Go to the following link and add it: https://github.com/settings/ssh/new" | |
} | |
remove_git_config() { | |
print_info "Removing .gitconfig" | |
rm -rf "$HOME/.gitconfig" | |
print_success ".gitconfig removed" | |
} | |
print_info() { | |
local color_yellow="\033[0;33m" | |
local msg=$1 | |
echo -e "${color_yellow}$msg${COLOR_NULL}" | |
} | |
print_error() { | |
local color_red="\033[0;31m" | |
local msg=$1 | |
echo -e "${color_red}$msg${COLOR_NULL}" | |
exit 1 | |
} | |
print_success() { | |
local color_green="\033[0;32m" | |
local msg=$1 | |
echo -e "${color_green}$msg${COLOR_NULL}" | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment