-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap-linux.sh
More file actions
executable file
·266 lines (228 loc) · 7.54 KB
/
bootstrap-linux.sh
File metadata and controls
executable file
·266 lines (228 loc) · 7.54 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env bash
set -euo pipefail
# Linux-specific bootstrap script
# This script sets up the dotfiles environment on Linux
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Ensure we protect files before commit
if [ -d ".git" ]; then
cp repo_config/pre-commit .git/hooks/pre-commit
fi
# Function to check if a command exists
command_exists() {
command -v "$1" &> /dev/null
}
# Function to check if running as root
check_root() {
if [ "$EUID" -eq 0 ]; then
echo "Error: Do not run this script as root"
exit 1
fi
}
# Function to install Linuxbrew if not present
install_linuxbrew() {
if ! command_exists brew; then
echo "Installing Linuxbrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add Linuxbrew to PATH
if [ -d "/home/linuxbrew/.linuxbrew" ]; then
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
elif [ -d "$HOME/.linuxbrew" ]; then
eval "$($HOME/.linuxbrew/bin/brew shellenv)"
fi
else
echo "Linuxbrew already installed"
fi
}
# Function to install build essentials (required for Linuxbrew)
install_build_essentials() {
if command_exists apt-get; then
echo "Installing build essentials..."
sudo apt-get update
sudo apt-get install -y build-essential curl file git
elif command_exists yum; then
echo "Installing build essentials..."
sudo yum groupinstall -y "Development Tools"
sudo yum install -y curl file git
elif command_exists dnf; then
echo "Installing build essentials..."
sudo dnf groupinstall -y "Development Tools"
sudo dnf install -y curl file git
else
echo "Warning: Could not detect package manager. Please install build-essential manually."
fi
}
# Function to install Stow
install_stow() {
if ! command_exists stow; then
echo "Installing Stow..."
if command_exists brew; then
brew install stow
elif command_exists apt-get; then
sudo apt-get install -y stow
elif command_exists yum; then
sudo yum install -y stow
elif command_exists dnf; then
sudo dnf install -y stow
else
echo "Error: Could not install Stow. Please install it manually."
return 1
fi
else
echo "Stow already installed"
fi
}
# Function to install pipx and tools
setup_pipx_tools() {
if ! command_exists pipx; then
echo "Installing pipx..."
if command_exists brew; then
brew install pipx
elif command_exists apt-get; then
sudo apt-get update
sudo apt-get install -y pipx
elif command_exists yum; then
sudo yum install -y pipx
elif command_exists dnf; then
sudo dnf install -y pipx
else
# Fallback: install via pip
python3 -m pip install --user pipx
fi
pipx ensurepath
# Add pipx bin to PATH for current session
export PATH="$HOME/.local/bin:$PATH"
else
echo "pipx already installed"
# Ensure pipx bin is in PATH for current session
export PATH="$HOME/.local/bin:$PATH"
fi
echo "Installing project dependencies with pipx..."
# Install tools if not already installed
if ! command_exists ansible; then
pipx install ansible
# Re-export PATH after installation to ensure it's available
export PATH="$HOME/.local/bin:$PATH"
fi
if ! command_exists ansible-lint; then
pipx install ansible-lint
fi
if ! command_exists yamllint; then
pipx install yamllint
fi
# Verify ansible-playbook is accessible
if ! command_exists ansible-playbook; then
echo "Warning: ansible-playbook not found in PATH"
echo "Trying to locate it..."
ANSIBLE_PATH=$(find ~ -name ansible-playbook 2>/dev/null | head -1)
if [ -n "$ANSIBLE_PATH" ]; then
echo "Found ansible-playbook at: $ANSIBLE_PATH"
export PATH="$(dirname "$ANSIBLE_PATH"):$PATH"
else
echo "Error: Could not find ansible-playbook after installation"
echo "Please check pipx installation: pipx list"
return 1
fi
fi
echo "✓ Ansible tools installed and available"
}
# Function to set zsh as default shell
setup_zsh_shell() {
if command_exists zsh; then
if command_exists brew; then
ZSH_PATH="$(brew --prefix)/bin/zsh"
else
ZSH_PATH="$(command -v zsh)"
fi
if [ -n "$ZSH_PATH" ] && [ -f "$ZSH_PATH" ]; then
echo "Setting zsh as default shell..."
echo "$ZSH_PATH" | sudo tee -a /etc/shells > /dev/null
chsh -s "$ZSH_PATH"
fi
else
echo "Warning: zsh not found. Please install zsh first."
fi
}
# Function to run Ansible playbook for Linux
run_ansible_playbook() {
# Ensure pipx bin is in PATH
export PATH="$HOME/.local/bin:$PATH"
# Try to find ansible-playbook if not in PATH
if ! command_exists ansible-playbook; then
ANSIBLE_PATH=$(find ~ -name ansible-playbook 2>/dev/null | head -1)
if [ -n "$ANSIBLE_PATH" ]; then
echo "Found ansible-playbook at: $ANSIBLE_PATH"
export PATH="$(dirname "$ANSIBLE_PATH"):$PATH"
else
echo "Error: ansible-playbook not found."
echo "Please ensure pipx is installed and ansible is installed via: pipx install ansible"
return 1
fi
fi
if command_exists ansible-playbook; then
echo "Running Ansible playbook for Linux..."
ansible-playbook dotfiles.yml \
-i hosts \
--tags linux \
--become \
--become-method sudo \
--extra-vars="ansible_python_interpreter=$(which python3)"
else
echo "Error: ansible-playbook not found. It should have been installed via pipx."
return 1
fi
}
# Function to setup dotfiles with Stow
setup_dotfiles() {
if ! command_exists stow; then
echo "Error: Stow is required but not installed"
return 1
fi
echo "Setting up dotfiles with Stow..."
pushd dotfile_templates > /dev/null
# Zsh configuration
ln -sf "$(pwd)/zsh/.zshrc_linux" "${HOME}/.zshrc"
pushd zsh > /dev/null
mkdir -p ~/.config/zsh
stow --adopt -R --target ~/.config/zsh zsh
popd > /dev/null
# Starship prompt
mkdir -p ~/.config/starship
stow --adopt -R --target ~/.config/starship starship
# Alacritty terminal
mkdir -p ~/.config/alacritty
stow --adopt -R --target ~/.config/alacritty alacritty
# Tmux
mkdir -p ~/.config/tmux
stow --adopt -R --target ~/.config/tmux tmux
# K9s
mkdir -p ~/.config/k9s/skins
stow --adopt -R --target ~/.config/k9s k9s
pushd k9s > /dev/null
stow --adopt -R --target ~/.config/k9s/skins skins
popd > /dev/null
# Kitty
mkdir -p ~/.config/kitty
stow --adopt -R --target ~/.config/kitty kitty
popd > /dev/null
}
# Main execution
main() {
check_root
echo "Starting Linux bootstrap..."
install_build_essentials
install_linuxbrew
install_stow
setup_pipx_tools
setup_zsh_shell
# Optionally run Ansible playbook (commented out by default)
# run_ansible_playbook
setup_dotfiles
echo "Bootstrap complete!"
echo ""
echo "To apply changes in your current shell, run:"
echo " source ~/.zshrc"
echo ""
echo "Or simply open a new terminal window."
}
main "$@"