Last active
November 27, 2024 00:03
-
-
Save thegreatco/fe2d85e31517f566bb4fb964abffcba3 to your computer and use it in GitHub Desktop.
setup a new machine
This file contains hidden or 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 | |
set -euo pipefail | |
# Global Variables | |
GO_INSTALL_PATH="/usr/local/go" | |
GO_DOWNLOAD_URL="https://go.dev/dl/" | |
ARCH=$(dpkg --print-architecture) | |
BASHRC_FILE="$HOME/.bashrc" | |
GITHUB_USER="thegreatco" # Replace with your GitHub username | |
# Functions | |
setup_bash_rc() { | |
# Ensure .bashrc exists | |
touch ~/.bashrc | |
local temp_bashrc=~/.bashrc.tmp | |
# Copy existing .bashrc to the temporary file | |
cp ~/.bashrc "$temp_bashrc" | |
# Define configurations to add | |
declare -a bashrc_configurations=( | |
"export LC_COLLATE=C" | |
"alias ll='ls -alF'" | |
"alias vl='viam login --disable-browser-open'" | |
"alias start='sudo systemctl start viam-server'" | |
"alias stop='sudo systemctl stop viam-server'" | |
"alias restart='sudo systemctl restart viam-server'" | |
"alias status='sudo systemctl status viam-server'" | |
"alias logs='sudo journalctl -fu viam-server'" | |
'export PATH=$PATH:/usr/local/go/bin' | |
'export PATH="$HOME/go/bin:$PATH"' | |
) | |
# Check and append each configuration only if it doesn't already exist | |
for config in "${bashrc_configurations[@]}"; do | |
if ! grep -Fxq "$config" ~/.bashrc; then | |
echo "$config" >> "$temp_bashrc" | |
fi | |
done | |
# Replace the original .bashrc with the modified one | |
mv "$temp_bashrc" ~/.bashrc | |
} | |
# Ensure required packages are installed | |
install_essential_packages() { | |
echo "Checking if required packages are installed..." | |
local -a packages=( | |
"build-essential" | |
"git" | |
"ssh-import-id" | |
) | |
local missing_packages=() | |
for package in "${packages[@]}"; do | |
if ! dpkg -l | grep -E "^ii\s*$package\s" >/dev/null 2>&1; then | |
missing_packages+=("$package") | |
fi | |
done | |
if [[ ${#missing_packages[@]} -gt 0 ]]; then | |
echo "Installing missing packages: ${missing_packages[*]}" | |
sudo apt update | |
sudo apt install -y "${missing_packages[@]}" | |
else | |
echo "All required packages are already installed." | |
fi | |
} | |
# Import GitHub SSH key | |
import_github_ssh_key() { | |
echo "Importing GitHub SSH key for user: $GITHUB_USER" | |
ssh-import-id-gh "$GITHUB_USER" | |
echo "GitHub SSH key imported." | |
} | |
# Update swap size to 2GB if it's less than 2GB | |
update_swap() { | |
echo "Checking current swap configuration in /etc/dphys-swapfile..." | |
# Define the desired swap size | |
desired_swap_mb=2048 | |
# Check if dphys-swapfile is installed | |
if ! command -v dphys-swapfile >/dev/null 2>&1; then | |
echo "Installing dphys-swapfile..." | |
sudo apt update && sudo apt install -y dphys-swapfile | |
fi | |
# Check current swap size configuration | |
if grep -q "^CONF_SWAPSIZE=" /etc/dphys-swapfile; then | |
current_swap_mb=$(grep "^CONF_SWAPSIZE=" /etc/dphys-swapfile | cut -d= -f2) | |
if [[ "$current_swap_mb" -eq "$desired_swap_mb" ]]; then | |
echo "Swap size is already configured to ${desired_swap_mb}MB. Skipping update." | |
return | |
fi | |
echo "Current swap size configuration is ${current_swap_mb}MB. Updating to ${desired_swap_mb}MB..." | |
else | |
echo "No swap size configuration found. Setting it to ${desired_swap_mb}MB..." | |
fi | |
# Update the swap size configuration | |
sudo sed -i "s/^CONF_SWAPSIZE=.*/CONF_SWAPSIZE=${desired_swap_mb}/" /etc/dphys-swapfile || echo "CONF_SWAPSIZE=${desired_swap_mb}" | sudo tee -a /etc/dphys-swapfile > /dev/null | |
# Restart the swap service to apply the new configuration | |
sudo systemctl restart dphys-swapfile | |
echo "Swap size updated to ${desired_swap_mb}MB." | |
} | |
# Check if the board is a Raspberry Pi | |
check_raspberry_pi() { | |
if command -v sbcidentify >/dev/null 2>&1; then | |
local board_info | |
board_info=$(sbcidentify) | |
if echo "$board_info" | grep -qi "Raspberry Pi"; then | |
update_swap | |
else | |
echo "Board is not a Raspberry Pi. Skipping swap update." | |
fi | |
else | |
echo "sbcidentify is not installed. Skipping board detection." | |
fi | |
} | |
# Install or update Go to the latest version | |
install_go() { | |
local latest_go_version | |
local foo | |
latest_go_version=$(curl -s "$GO_DOWNLOAD_URL" | grep -oP 'go1\.23\.\d+' | head -n 1 | sed 's/go//') | |
if [[ -z "$latest_go_version" ]]; then | |
echo "Failed to fetch the latest Go version. Exiting." | |
exit 1 | |
fi | |
if command -v go >/dev/null 2>&1; then | |
local installed_version | |
installed_version=$(go version | awk '{print $3}' | sed 's/go//') | |
if [[ "$installed_version" == "$latest_go_version" ]]; then | |
echo "Go $installed_version is already installed. Skipping installation." | |
return | |
else | |
echo "Go $installed_version is installed, but version $latest_go_version is required." | |
fi | |
fi | |
echo "Installing Go $latest_go_version..." | |
local go_tarball="go${latest_go_version}.linux-${ARCH}.tar.gz" | |
echo $go_tarball | |
curl -OL "${GO_DOWNLOAD_URL}${go_tarball}" | |
sudo rm -rf "$GO_INSTALL_PATH" | |
sudo tar -C /usr/local -xzf "$go_tarball" | |
rm -f "$go_tarball" | |
echo "Go $latest_go_version installed successfully." | |
} | |
# Add Go paths to .bashrc | |
configure_go_path() { | |
local paths=( | |
'export PATH=$PATH:/usr/local/go/bin' | |
'export PATH="$HOME/go/bin:$PATH"' | |
) | |
for path in "${paths[@]}"; do | |
if ! grep -q "$path" "$BASHRC_FILE"; then | |
echo "$path" >> "$BASHRC_FILE" | |
fi | |
done | |
echo "Reloading .bashrc for the current session..." | |
source "$BASHRC_FILE" | |
echo "Go environment configured." | |
} | |
# Install Viam RDK CLI | |
install_viam_rdk() { | |
if command -v viam >/dev/null 2>&1; then | |
echo "Viam RDK CLI is already installed. Skipping installation." | |
return | |
fi | |
echo "Installing Viam RDK CLI..." | |
sudo curl -o /usr/local/bin/viam https://storage.googleapis.com/packages.viam.com/apps/viam-cli/viam-cli-stable-linux-arm64 | |
sudo chmod a+rx /usr/local/bin/viam | |
echo "Viam RDK CLI installed." | |
} | |
# Install Tailscale | |
install_tailscale() { | |
if command -v tailscale >/dev/null 2>&1; then | |
echo "Tailscale is already installed. Skipping installation." | |
return | |
fi | |
echo "Installing Tailscale..." | |
curl -fsSL https://tailscale.com/install.sh | sh | |
echo "Tailscale installed. Run 'sudo tailscale up' to enable Tailscale." | |
} | |
# Main Script Execution | |
main() { | |
install_essential_packages | |
import_github_ssh_key | |
check_raspberry_pi | |
install_go | |
configure_go_path | |
install_viam_rdk | |
install_tailscale | |
setup_bash_rc | |
echo "Setup complete!" | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment