Skip to content

Instantly share code, notes, and snippets.

@JoelGotsch
Last active September 26, 2024 11:14
Show Gist options
  • Save JoelGotsch/eb8a888a4f48d5758ce37a57e1bfe15c to your computer and use it in GitHub Desktop.
Save JoelGotsch/eb8a888a4f48d5758ce37a57e1bfe15c to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script installs or updates various development tools (Terraform, Ansible, Kubernetes, kubectl, Helm, etc.)
# for setting up a Kubernetes cluster and other necessary tools on WSL (Windows Subsystem for Linux).
# Save this script in a file (e.g., install_dependencies.sh) and run it in your WSL environment.
# To update non-APT dependencies (e.g., kustomize, ArgoCD, yq), pass the --update flag when executing the script.
#
# Example usage:
# ./install_dependencies.sh # Installs everything
# ./install_dependencies.sh --update # Updates only non-APT dependencies like kustomize, yq, ArgoCD, etc.
# or you execute it via:
# curl -sL https://gist.githubusercontent.com/JoelGotsch/eb8a888a4f48d5758ce37a57e1bfe15c/raw/install_kubernetes.sh | sudo bash
# and for updating:
# curl -sL https://gist.githubusercontent.com/JoelGotsch/eb8a888a4f48d5758ce37a57e1bfe15c/raw/install_kubernetes.sh | sudo bash -s -- --update
# Global variables
UPDATE=false # Flag to determine if we should update non-APT dependencies
# Check if user requested update mode
if [[ "$1" == "--update" ]]; then
UPDATE=true
fi
# Function to check if a command exists
command_exists() {
command -v "$@" >/dev/null 2>&1
}
# Function to install or update Kustomize
install_or_update_kustomize() {
if [[ "$UPDATE" == true ]] || ! command_exists kustomize; then
echo "Installing or updating Kustomize..."
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash
sudo mv kustomize /usr/local/bin/
sudo chmod +x /usr/local/bin/kustomize
echo "Kustomize installed or updated."
else
echo "Kustomize is already installed."
fi
}
# Function to install or update ArgoCD CLI
install_or_update_argocd() {
if [[ "$UPDATE" == true ]] || ! command_exists argocd; then
echo "Installing or updating Argo CD CLI..."
VERSION=$(curl -L -s https://raw.githubusercontent.com/argoproj/argo-cd/stable/VERSION)
curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/download/v$VERSION/argocd-linux-amd64
sudo install -m 555 argocd-linux-amd64 /usr/local/bin/argocd
rm argocd-linux-amd64
echo "Argo CD CLI installed or updated to version $VERSION."
else
echo "Argo CD CLI is already installed."
fi
}
# Function to install or update yq
install_or_update_yq() {
if [[ "$UPDATE" == true ]] || ! command_exists yq; then
echo "Attempting to install yq via snap..."
sudo snap install yq || {
echo "Snap installation failed."
echo "To install yq via snap in WSL2, run the following command to enable snap:"
echo "sudo bash -c \"\$(wget -O- https://gist.githubusercontent.com/JoelGotsch/eb8a888a4f48d5758ce37a57e1bfe15c/raw/setup-wsl2-snap-support.sh)\""
}
else
echo "yq is already installed."
fi
}
# Update and upgrade the system
echo "Updating and upgrading the system packages..."
sudo apt update && sudo apt upgrade -y
echo "System packages updated."
# Install basic dependencies
echo "Installing basic dependencies: curl, wget, git, unzip, jq, etc..."
sudo apt install -y \
curl \
wget \
git \
unzip \
software-properties-common \
apt-transport-https \
ca-certificates \
gnupg-agent \
build-essential \
python3 \
python3-pip \
python3-venv \
jq
echo "Basic dependencies installed."
# Install Docker (Client only, as Docker Desktop handles the daemon)
if ! command_exists docker; then
echo "Installing Docker..."
sudo apt install -y docker.io
echo "Docker installed."
else
echo "Docker is already installed."
fi
# Check if the user is already part of the docker group
if groups $USER | grep -q "\bdocker\b"; then
echo "User is already in the docker group."
else
echo "Adding user to the docker group..."
sudo usermod -aG docker $USER
echo "User added to the docker group. You will need to log out and back in to use Docker without sudo."
LOGOUT_NEEDED=true
fi
# Install Terraform via APT
if ! command_exists terraform; then
echo "Installing Terraform via APT..."
if [ ! -f /usr/share/keyrings/hashicorp-archive-keyring.gpg ]; then
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
fi
if [ ! -f /etc/apt/sources.list.d/hashicorp.list ]; then
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
fi
sudo apt update && sudo apt install -y terraform
echo "Terraform installed."
else
echo "Terraform is already installed."
fi
# Install Ansible via APT
if ! command_exists ansible; then
echo "Installing Ansible via APT..."
sudo apt install -y ansible
echo "Ansible installed."
else
echo "Ansible is already installed."
fi
# Install Helm via APT
if ! command_exists helm; then
echo "Installing Helm via APT..."
if [ ! -f /usr/share/keyrings/helm.gpg ]; then
curl https://baltocdn.com/helm/signing.asc | gpg --dearmor | sudo tee /usr/share/keyrings/helm.gpg > /dev/null
fi
if [ ! -f /etc/apt/sources.list.d/helm-stable-debian.list ]; then
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/helm.gpg] https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
fi
sudo apt-get update
sudo apt-get install -y helm
echo "Helm installed."
else
echo "Helm is already installed."
fi
# Install or update Kustomize
install_or_update_kustomize
# Install or update ArgoCD CLI
install_or_update_argocd
# Install or update yq
install_or_update_yq
# Install Kubernetes components via APT (kubeadm, kubelet, kubectl)
if ! command_exists kubectl; then
echo "Installing Kubernetes components (kubectl, kubeadm, kubelet) via APT..."
# Check if the keyring exists
if [ ! -f /etc/apt/keyrings/kubernetes-apt-keyring.gpg ]; then
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
else
echo "Kubernetes keyring already exists."
fi
# Check if the repository is added
if [ ! -f /etc/apt/sources.list.d/kubernetes.list ]; then
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list
else
echo "Kubernetes repository already exists."
fi
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
echo "Kubernetes components installed."
else
echo "Kubernetes components are already installed."
fi
# Final message to user
echo "All dependencies have been installed successfully."
# Inform the user if they need to log out and back in for Docker group changes to take effect
if [ "$LOGOUT_NEEDED" = true ]; then
echo "You will need to log out and back in to use Docker without sudo."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment