Skip to content

Commit

Permalink
refactor: Improve setup script for production readiness and best prac…
Browse files Browse the repository at this point in the history
…tices

- Modularized script into functions for better readability and maintainability.
- Added logging functionality to track progress and actions taken during the script execution.
- Ensured the script exits on errors using `set -eo pipefail` for robust error handling.
- Externalized configuration values such as URLs to the top of the script.
- Enhanced comments for better understanding of the script's purpose and functionality.
- Separated setup logic for macOS and Ubuntu into distinct functions (`macos_setup` and `ubuntu_setup`).
- Implemented user confirmation prompt for adding SSH key during Ubuntu setup.
- Included functions for cleaning up old files and backing up existing configurations.
- Maintained all original functionalities, including kubectl plugin installation and repository setup.
- Ensured environment-specific setups (macOS and Ubuntu) are handled correctly.
- Added default shell setup for ZSH on Ubuntu.
- Updated `install` and `main` functions to handle installation flow and reinstallation prompts.

Overall, these changes make the script more maintainable, readable, and ready for production environments.
  • Loading branch information
tankibaj committed Jun 13, 2024
1 parent 45bccc9 commit d1c9165
Showing 1 changed file with 99 additions and 136 deletions.
235 changes: 99 additions & 136 deletions install
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,62 @@

set -eo pipefail

DOTFILES=$HOME/.dotfiles
ZSH=$HOME/.oh-my-zsh


#=========================================================================
# ---------------| Common |---------------
#=========================================================================
kubectxAutoCompletion() {
mkdir -p ~/.oh-my-zsh/completions
chmod -R 755 ~/.oh-my-zsh/completions
wget -O $HOME/.oh-my-zsh/completions/_kubectx.zsh https://raw.githubusercontent.com/ahmetb/kubectx/master/completion/_kubectx.zsh
wget -O $HOME/.oh-my-zsh/completions/_kubens.zsh https://raw.githubusercontent.com/ahmetb/kubectx/master/completion/_kubens.zsh
DOTFILES="$HOME/.dotfiles"
ZSH="$HOME/.oh-my-zsh"
LOG_FILE="$HOME/setup.log"

# URLs
EXA_LINUX_URL="https://api.github.com/repos/ogham/exa/releases/latest"
HOMEBREW_INSTALL_URL="https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"
SPEEDTEST_INSTALL_URL="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
#KUBECTX_COMPLETION_URL="https://raw.githubusercontent.com/ahmetb/kubectx/master/completion"

# Logging function
log() {
echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" | tee -a $LOG_FILE
}

kubectlPlugin() {
kubectl krew install open-svc # -- Open the Kubernetes URL(s) for the specified service in your browser.
kubectl krew install pod-shell # -- Display a list of pods to execute a shell in
kubectl krew install node-shell # -- Spawn a root shell on a node via kubectl
kubectl krew install pod-lens # -- Show pod-related resources
kubectl krew install sick-pods # -- Find and debug Pods that are "Not Ready"
kubectl krew install neat # -- Remove clutter from Kubernetes manifests to make them more readable.
# Common functions
#kubectx_auto_completion() {
# log "Setting up kubectx autocompletion..."
# mkdir -p "$ZSH/completions"
# chmod -R 755 "$ZSH/completions"
# wget -O "$ZSH/completions/_kubectx.zsh" "$KUBECTX_COMPLETION_URL/_kubectx.zsh"
# wget -O "$ZSH/completions/_kubens.zsh" "$KUBECTX_COMPLETION_URL/_kubens.zsh"
#}

kubectl_plugin() {
log "Installing kubectl plugins..."
kubectl krew install open-svc pod-shell node-shell pod-lens sick-pods neat
}

#=========================================================================
# ---------------| MacOS |---------------
#=========================================================================
macOS() {
if test ! $(which brew); then # -- Check for Homebrew and install if don't have it
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >>$HOME/.zprofile
# MacOS specific setup
macos_setup() {
log "Setting up macOS environment..."
if ! command -v brew &> /dev/null; then
/bin/bash -c "$(curl -fsSL $HOMEBREW_INSTALL_URL)"
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> "$HOME/.zprofile"
eval "$(/opt/homebrew/bin/brew shellenv)"
fi

setupRepo
brew update # -- Update Homebrew recipes
brew bundle install --file=$DOTFILES/Brewfile # -- Install all contents from Brewfile
kubectxAutoCompletion
kubectlPlugin
setup_repo
brew update
brew bundle install --file="$DOTFILES/Brewfile"
# kubectx_auto_completion
kubectl_plugin
}

#=========================================================================
# ---------------| Ubuntu |---------------
#=========================================================================
installEXA() {
curl -s https://api.github.com/repos/ogham/exa/releases/latest |
grep browser_download_url |
grep exa-linux-x86_64-v |
cut -d '"' -f 4 |
wget -i -
# Ubuntu specific setup
install_exa() {
log "Installing EXA..."
curl -s $EXA_LINUX_URL | grep browser_download_url | grep exa-linux-x86_64-v | cut -d '"' -f 4 | wget -i -
unzip exa-linux-x86_64-v*.zip
sudo mv bin/exa /usr/local/bin/exa
sudo mv completions/exa.zsh /usr/local/share/zsh/site-functions/exa.zsh
rm -rf bin completions man exa-linux-x86_64-v*.zip
}

setupLocale() {
setup_locale() {
log "Setting up locale..."
sudo apt install -qq -y locales
sudo rm -f /etc/default/locale
sudo locale-gen "en_US.UTF-8"
Expand All @@ -66,135 +66,98 @@ setupLocale() {
sudo timedatectl set-timezone Europe/Berlin
}

ubuntu() {
ubuntu_setup() {
log "Setting up Ubuntu environment..."
echo "$USER ALL=(ALL:ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/$USER

# Ask for user confirmation before adding the SSH key
read -p "Do you want to add the SSH key from GitHub? (y/N): " confirm
confirm=${confirm:-N}

if [[ "$confirm" =~ ^[Yy]$ ]]; then
if [[ ! -d $HOME/.ssh ]]; then
mkdir -p $HOME/.ssh
chmod 700 $HOME/.ssh
fi
KEYS_URL="https://github.com/tankibaj.keys"
AUTHORIZED_KEYS_FILE="$HOME/.ssh/authorized_keys"
wget -qO- $KEYS_URL >> $AUTHORIZED_KEYS_FILE # -- Fetch SSH public key from GitHub profile
wget -qO- $KEYS_URL >> $AUTHORIZED_KEYS_FILE
chmod 600 $AUTHORIZED_KEYS_FILE
echo "SSH key added to $AUTHORIZED_KEYS_FILE"
log "SSH key added to $AUTHORIZED_KEYS_FILE"
else
echo "Skipping adding SSH key."
log "Skipping adding SSH key."
fi

# Install necessary packages
curl -s https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh | sudo bash
curl -s $SPEEDTEST_INSTALL_URL | sudo bash
sudo apt update
sudo apt install -qq -y zsh curl vim git jq zip ntp net-tools speedtest

# Call additional setup functions
installEXA
setupLocale

# Set ZSH as the default shell for the user
sudo usermod -s /usr/bin/zsh $(whoami)
setupRepo
install_exa
setup_locale
sudo usermod -s /usr/bin/zsh "$(whoami)"
setup_repo
}


#=========================================================================
# ---------------| Common |---------------
#=========================================================================
# Common setup functions
backup() {
if [[ -e $HOME/.zshrc ]]; then
cp $HOME/.zshrc $HOME/.zshrc.original
fi

if [[ -e $HOME/.bashrc ]]; then
cp $HOME/.bashrc $HOME/.bashrc.original
fi
log "Backing up existing configurations..."
[[ -e $HOME/.zshrc ]] && cp "$HOME/.zshrc" "$HOME/.zshrc.original"
[[ -e $HOME/.bashrc ]] && cp "$HOME/.bashrc" "$HOME/.bashrc.original"
}

cleanUp() {
if [[ -L $HOME/.p10k.zsh ]]; then
sudo rm -f $HOME/.p10k.zsh
fi

if [[ -L $HOME/.zshrc ]]; then
sudo rm -f $HOME/.zshrc
fi

if [[ -L $HOME/.gitignore ]]; then
sudo rm -f $HOME/.gitignore
fi

if [[ -d $DOTFILES ]]; then
sudo rm -rf $DOTFILES
fi

if [[ -d $ZSH ]]; then
sudo rm -rf $ZSH
fi
clean_up() {
log "Cleaning up old files and directories..."
[[ -L $HOME/.p10k.zsh ]] && sudo rm -f "$HOME/.p10k.zsh"
[[ -L $HOME/.zshrc ]] && sudo rm -f "$HOME/.zshrc"
[[ -L $HOME/.gitignore ]] && sudo rm -f "$HOME/.gitignore"
[[ -d $DOTFILES ]] && sudo rm -rf "$DOTFILES"
[[ -d $ZSH ]] && sudo rm -rf "$ZSH"
}

setupRepo() {
git clone https://github.com/tankibaj/dotfiles.git $DOTFILES # -- Clone Dotfiles
git clone https://github.com/ohmyzsh/ohmyzsh.git $ZSH # -- Clone Oh My Zsh
git clone https://github.com/romkatv/powerlevel10k.git $ZSH/themes/powerlevel10k # -- Clone PowerLeve10K theme

# -- Clone PowerLeve10K plugins for autosuggestion and syntax highlighting:
git clone https://github.com/zsh-users/zsh-autosuggestions.git $ZSH/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH/plugins/zsh-syntax-highlighting

ln -s $DOTFILES/.zshrc $HOME/.zshrc # -- Symlinks the .zshrc file from the .dotfiles/.zshrc
ln -s $DOTFILES/.p10k.zsh $HOME/.p10k.zsh # -- Symlinks the .p10k.zsh file from .dotfiles/.p10k.zsh
setup_repo() {
log "Setting up repositories..."
git clone https://github.com/tankibaj/dotfiles.git "$DOTFILES"
git clone https://github.com/ohmyzsh/ohmyzsh.git "$ZSH"
git clone https://github.com/romkatv/powerlevel10k.git "$ZSH/themes/powerlevel10k"
git clone https://github.com/zsh-users/zsh-autosuggestions.git "$ZSH/plugins/zsh-autosuggestions"
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git "$ZSH/plugins/zsh-syntax-highlighting"

# -- Git config
ln -s $DOTFILES/.gitignore $HOME/.gitignore
ln -s "$DOTFILES/.zshrc" "$HOME/.zshrc"
ln -s "$DOTFILES/.p10k.zsh" "$HOME/.p10k.zsh"
ln -s "$DOTFILES/.gitignore" "$HOME/.gitignore"

if [[ -e $HOME/.gitconfig ]]; then
rm -f $HOME/.gitconfig
fi
ln -s $DOTFILES/.gitconfig $HOME/.gitconfig
# git config --global core.excludesfile $HOME/.dotfiles/.gitignore
# git config --global init.defaultBranch main
# git config --global alias.squash-all '!f() { count=$(git rev-list --count HEAD); git rebase -i HEAD~$((count - 1)); }; f'
[[ -e $HOME/.gitconfig ]] && rm -f "$HOME/.gitconfig"
ln -s "$DOTFILES/.gitconfig" "$HOME/.gitconfig"
}

install() {
echo
echo
echo "Setting up dotfiles....."
echo
echo

cleanUp # -- Remove old files and directories if exist
backup # -- Backup existing .zshrc or .bashrc before install Oh My Zsh
log "Starting setup..."
clean_up
backup

if [[ "$(uname -s)" == "Darwin" ]]; then
macOS
macos_setup
elif [[ "$(uname -s)" == "Linux" ]]; then
if [[ "$(cat /etc/issue | grep Ubuntu | awk '{ print $1}')" = "Ubuntu" ]] || [[ "$(cat /etc/issue | grep Debian | awk '{ print $1}')" = "Debian" ]]; then
ubuntu
if grep -q -E 'Ubuntu|Debian' /etc/issue; then
ubuntu_setup
fi
fi

echo
echo
echo "[INFO] Setup has been completed!!! Please restart your terminal"
echo
log "[INFO] Setup has been completed!!! Please restart your terminal."
}

if [[ ! -d $DOTFILES ]]; then
install
else
read -p "[INFO] Dotfiles already installed. Do you want to reinstall? [y/N]: " confirmation
case $(echo $confirmation | tr '[A-Z]' '[a-z]') in
y | yes)
main() {
if [[ ! -d $DOTFILES ]]; then
install
;;
*)
exit 1
;;
esac
fi
else
read -p "[INFO] Dotfiles already installed. Do you want to reinstall? [y/N]: " confirmation
case $(echo $confirmation | tr '[:upper:]' '[:lower:]') in
y | yes)
install
;;
*)
log "Reinstallation canceled."
exit 1
;;
esac
fi
}

main "$@"

0 comments on commit d1c9165

Please sign in to comment.