-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap
124 lines (105 loc) · 3.57 KB
/
bootstrap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env bash
LOG_FILE="/tmp/bootstrap_log.txt"
exec > >(tee -a "$LOG_FILE") 2>&1
set -e
# Color Definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
SEPARATOR="-----------------------------------"
echo -e "\n${GREEN}Log file created at: ${LOG_FILE}${NC}"
export HISTIGNORE='*sudo -S*'
# Print colored header
echo -e "\n${BLUE}${SEPARATOR}${NC}"
echo -e "${GREEN}Bootstrapping your system${NC}"
echo -e "${BLUE}${SEPARATOR}${NC}\n"
# Detecting the operating system
OSTYPE=$(uname -s | tr '[:upper:]' '[:lower:]')
if [[ "$OSTYPE" == "linux"* ]]; then
DIST=$(awk -F= '$1=="ID" { print $2 ;}' /etc/os-release)
fi
echo -e "${YELLOW}OSTYPE:${NC} ${OSTYPE}"
echo -e "${YELLOW}DIST :${NC} ${DIST}"
echo -e "${BLUE}${SEPARATOR}${NC}"
# Function to handle errors
handle_error() {
echo -e "${RED}An error occurred. Please check the log for details.${NC}"
exit 1
}
trap handle_error ERR
# Validate Sudo Access
echo -e "\n${YELLOW}Please enter your sudo password (for installing base packages and Ansible privileges):${NC}"
read -s -p "Password: " SUDO_PASSWORD
echo -e '\n'
echo "${SUDO_PASSWORD}" | sudo -S -v &>/dev/null
if [[ $? -ne 0 ]]; then
echo -e "${RED}Error: Invalid sudo password or no sudo privileges.${NC}"
exit 1
fi
install_homebrew() {
echo -e "${BLUE}${SEPARATOR}${NC}"
echo -e "${GREEN}Installing Homebrew${NC}"
echo -e "${BLUE}${SEPARATOR}${NC}"
if ! command -v brew &>/dev/null; then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
else
echo -e "${YELLOW}Homebrew is already installed${NC}"
fi
}
install_linux_deps(){
case "$DIST" in
debian|ubuntu)
pkgs="python3-minimal python3-venv locales python3-pip git curl"
echo "${SUDO_PASSWORD}" | sudo -S apt-get update -y
echo "${SUDO_PASSWORD}" | sudo -S apt-get upgrade -y
echo "${SUDO_PASSWORD}" | sudo -S apt-get install --no-install-recommends -y $pkgs
;;
*)
echo -e "${RED}No task defined for $DIST on Linux${NC}"
exit 1
;;
esac
}
# Install dependencies based on OS type
case "$OSTYPE" in
darwin*)
install_homebrew
eval "$(/opt/homebrew/bin/brew shellenv)"
;;
linux*)
install_linux_deps
install_homebrew
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
;;
*)
echo -e "${RED}Unsupported OS${NC}"
exit 1
;;
esac
# Install Python dependencies
echo -e "${BLUE}${SEPARATOR}${NC}"
echo -e "${GREEN}Installing Python3 Dependencies${NC}"
echo -e "${BLUE}${SEPARATOR}${NC}"
pip_pkgs="pip ansible jmespath pynvim uv"
pip3 install --upgrade --no-cache-dir --break-system-packages --no-warn-script-location $pip_pkgs
# Clone or update dotfiles
DOTFILES_DIR="${HOME}/.dotfiles"
if [[ ! -d "$DOTFILES_DIR" ]]; then
echo -e "${GREEN}Cloning psadi/.dotfiles${NC}"
git clone https://github.com/psadi/dotfiles.git "$DOTFILES_DIR"
else
echo -e "${YELLOW}psadi/.dotfiles already exists, pulling latest changes${NC}"
cd "$DOTFILES_DIR" && git pull && cd -
fi
# Export required variables for Ansible
export PATH="${HOME}/.local/bin:$PATH"
export ANSIBLE_CONFIG="${HOME}/.dotfiles/ansible/ansible.cfg"
export ANSIBLE_INVENTORY_WARNING=false
export ANSIBLE_BECOME_PASSWORD="${SUDO_PASSWORD}"
# Dynamic tag support for Ansible playbook
TAGS="${1:-$OSTYPE}" # Use first argument or fallback to OSTYPE
echo -e "${BLUE}Running Ansible Playbook with tags: ${TAGS}${NC}"
ansible-playbook "${DOTFILES_DIR}/ansible/site.yaml" --tags "$TAGS" --user "$USER"
exit $?