forked from novuhq/novu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev-environment-setup.sh
executable file
·515 lines (429 loc) · 14.2 KB
/
dev-environment-setup.sh
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#!/bin/sh
exec </dev/tty
APPLE_CHIP='Apple'
#INTEL_CHIP='Intel'
NEGATIVE_RESPONSE="No"
POSITIVE_RESPONSE="Yes"
#ZSHRC="$HOME/.zshrc"
ZPROFILE="$HOME/.zprofile"
error_message () {
echo " "
echo "❌ $1 has not been installed correctly"
echo " "
}
skip_message () {
echo " "
echo "⏩ $1 installation has been skipped"
echo " "
}
success_message () {
echo " "
echo "✅ $1 has been installed"
echo " "
}
start_success_message () {
echo " "
echo "✅ $1 has been started"
echo " "
}
already_installed_message () {
echo " "
echo "✅ $1 is already installed"
echo " "
}
installing_dependency () {
echo " "
echo "🛠 $1 is installing"
echo " "
}
updating_dependency () {
echo " "
echo "🛠 $1 is updating"
echo " "
}
execute_command_without_error_print () {
$1 2> /dev/null
}
get_cpu () {
SYSTEM_CPU_BRAND='machdep.cpu.brand.string'
sysctl -a | grep $SYSTEM_CPU_BRAND | cut -f2 -d":"
}
refresh_shell() {
# Refresh shell to apply changes in current session
source "$ZPROFILE"
exec $SHELL
}
get_user_groups() {
# Get user groups
read -r -a USER_GROUP <<< "$(groups $USER)"
}
set_user_dir_ownership() {
USER_GROUP="$(get_user_groups)"
sudo chown -R "$USER":"${USER_GROUP[0]}" "$1"
}
set_user_ownership() {
USER_GROUP="$(get_user_groups)"
sudo chown "$USER":"${USER_GROUP[0]}" "$1"
}
set_user_permissions() {
sudo chmod 644 "$1"
set_user_ownership "$1"
}
install_apple_chip_dependencies () {
CPU=$(get_cpu)
echo "Your CPU is: $CPU"
if [[ "$CPU" == *"$APPLE_CHIP"* ]]; then
ROSETTA_BOM_FILE="/Library/Apple/System/Library/Receipts/com.apple.pkg.RosettaUpdateAuto.bom"
if [[ ! -f $ROSETTA_BOM_FILE ]]; then
installing_dependency "Rosetta for Apple CPU"
softwareupdate --install-rosetta
success_message "Rosetta"
else
already_installed_message "Rosetta"
fi
fi
}
install_xcode () {
echo ""
echo "❓ Do you want to install Xcode? ($POSITIVE_RESPONSE / $NEGATIVE_RESPONSE)"
read -p " > " RESPONSE
echo ""
if [[ "$RESPONSE" == "$POSITIVE_RESPONSE" ]]; then
installing_dependency "Xcode"
xcode-select --install &
PID=$!
wait $PID
sudo xcode-select --switch /Library/Developer/CommandLineTools
sudo xcodebuild -license accept
xcodebuild -runFirstLaunch
success_message "Xcode"
fi
if [[ "$RESPONSE" == "$NEGATIVE_RESPONSE" ]]; then
echo ""
echo "❓ Do you want to update Xcode? ($POSITIVE_RESPONSE / $NEGATIVE_RESPONSE)"
read -p " > " RESPONSE
echo ""
if [[ "$RESPONSE" == "$POSITIVE_RESPONSE" ]]; then
updating_dependency "Xcode"
softwareupdate --install --verbose Xcode &
PID=$!
wait $PID
success_message "Xcode"
fi
fi
}
set_macosx_generics () {
echo "Set MacOSx system configurations"
defaults write com.apple.finder AppleShowAllFiles YES
}
install_macosx_dependencies () {
install_xcode
install_apple_chip_dependencies
set_macosx_generics
}
install_os_dependencies () {
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
installing_dependency "Linux dependencies"
echo "//TODO"
install_novu_tools
elif [[ "$OSTYPE" == "darwin"* ]]; then
installing_dependency "MacOsx dependencies"
install_macosx_dependencies
install_novu_tools
else
echo "OS not supported"
fi
}
check_homebrew () {
TEST_BREW_CMD=$(execute_command_without_error_print "brew --version")
if [[ -z "$TEST_BREW_CMD" ]] || [[ "$TEST_BREW_CMD" == "zsh: command not found: brew" ]]; then
error_message "Homebrew"
echo "⛔️ Homebrew is a hard dependency for this tool"
fi
}
install_homebrew () {
TEST_BREW_CMD=$(execute_command_without_error_print "brew --version")
if [[ -z "$TEST_BREW_CMD" ]] || [[ "$TEST_BREW_CMD" == "zsh: command not found: brew" ]]; then
installing_dependency "Homebrew"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
APPLE_CHIP_BREW_BIN="/opt/homebrew/bin"
BREW_BIN="/usr/local/bin"
ENTRY="export PATH=$BREW_BIN:$APPLE_CHIP_BREW_BIN:\$PATH"
PARAM_TO_CMD="grep -R $ENTRY $ZPROFILE"
CMD=$(execute_command_without_error_print "$PARAM_TO_CMD")
if [[ -z $CMD ]]; then
# Add the Brew paths to the shell profile
echo "$ENTRY" | sudo tee -a "$ZPROFILE"
# As executing `tee` as sudo changes ownership and permissions we roll them back appropriately
set_user_permissions "$ZPROFILE"
source "$ZPROFILE"
fi
AFTER_INSTALL_TEST_CMD=$(execute_command_without_error_print "brew --version")
if [[ -z "$AFTER_INSTALL_TEST_CMD" ]] || [[ "$AFTER_INSTALL_TEST_CMD" == "zsh: command not found: brew" ]]; then
error_message "Homebrew"
exit 1
else
success_message "Homebrew"
fi
else
already_installed_message "Homebrew"
fi
}
install_homebrew_recipes () {
SKIP="$(check_homebrew)"
if [[ -z "$SKIP" ]]; then
# Update Homebrew recipes
echo "Update and Upgrade Homebrew"
brew update
brew upgrade
else
skip_message "Homebrew tap"
echo "$SKIP"
fi
}
make_zsh_default_shell () {
if [[ ! "$SHELL" == "/bin/zsh" ]]; then
echo "Let's make ZSH the default shell"
chsh -s "$(which zsh)"
echo "✅ ZSH made as default shell"
fi
}
# Depends on Git so depends on having Xcode installed
install_ohmyzsh () {
echo ""
echo "❓ Do you want to install Oh My Zsh! ? ($POSITIVE_RESPONSE / $NEGATIVE_RESPONSE)"
read -p " > " RESPONSE
echo ""
if [[ "$RESPONSE" == "$POSITIVE_RESPONSE" ]]; then
OHMYZSH_DIR="$HOME/.oh-my-zsh"
if [[ ! -d $OHMYZSH_DIR ]]; then
installing_dependency "Oh My Zsh!"
curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh | $SHELL
if [[ ! -d $OHMYZSH_DIR ]]; then
error_message "Oh My Zsh!"
else
set_user_dir_ownership "$OHMYZSH_DIR"
success_message "Oh My Zsh!"
fi
else
already_installed_message "Oh My Zsh!"
fi
fi
}
check_nvm () {
TEST_NVM_CMD=$(execute_command_without_error_print "nvm --version")
if [[ -z "$TEST_NVM_CMD" ]] || [[ "$TEST_NVM_CMD" == "zsh: command not found: nvm" ]]; then
error_message "NVM"
echo "⛔️ NVM is a hard dependency for this tool"
fi
}
install_node () {
NODE_JS_VERSION="v20.8.1"
SKIP="$(check_nvm)"
if [[ -z "$SKIP" ]]; then
TEST_CMD=$(execute_command_without_error_print "node --version")
if [[ -z "$TEST_CMD" ]] || [[ "$TEST_CMD" == "zsh: command not found: node" ]]; then
installing_dependency "Node.js $NODE_JS_VERSION"
nvm install $NODE_JS_VERSION
TEST_NODE_CMD=$(execute_command_without_error_print "node --version")
if [[ -z "$TEST_NODE_CMD" ]] || [[ "$TEST_NODE_CMD" == "zsh: command not found: node" ]]; then
error_message "Node.js"
else
success_message "Node.js $NODE_JS_VERSION"
fi
else
already_installed_message "Node.js $NODE_JS_VERSION"
fi
else
skip_message "Node.js $NODE_JS_VERSION"
echo "$SKIP"
fi
}
# NVM is a Node.js version manager
# Make sure that you have installed ZSH previously, so NVM can automatically inject the executable to PATH in the .zshrc config file.
install_nvm () {
NVM_DIR="$HOME/.nvm"
LATEST_NVM_VERSION="v0.39.2"
TEST_CMD=$(execute_command_without_error_print "nvm --version")
if [[ -z "$TEST_CMD" ]] || [[ "$TEST_CMD" == "zsh: command not found: nvm" ]]; then
installing_dependency "NVM"
URL="https://raw.githubusercontent.com/nvm-sh/nvm/$LATEST_NVM_VERSION/install.sh"
echo "Downloading NVM from $URL"
/bin/bash -c "$(curl -fsSL $URL)"
# Loads NVM
source "$NVM_DIR/nvm.sh"
#source $ZSHRC
AFTER_INSTALL_TEST_CMD=$(execute_command_without_error_print "nvm --version")
if [[ -z "$AFTER_INSTALL_TEST_CMD" ]] || [[ "$AFTER_INSTALL_TEST_CMD" == "zsh: command not found: nvm" ]]; then
error_message "NVM"
else
success_message "NVM"
fi
else
already_installed_message "NVM"
fi
}
# PNPM is the package manager used in Novu's monorepo
install_pnpm () {
PNPM_VERSION="8.9.0"
TEST_PNPM_CMD=$(execute_command_without_error_print "pnpm --version")
if [[ -z "$TEST_PNPM_CMD" ]] || [[ "$TEST_PNPM_CMD" == "zsh: command not found: pnpm" ]]; then
installing_dependency "PNPM $PNPM_VERSION"
npm install -g pnpm@$PNPM_VERSION
AFTER_INSTALL_TEST_CMD=$(execute_command_without_error_print "pnpm --version")
if [[ -z "$AFTER_INSTALL_TEST_CMD" ]] || [[ "$AFTER_INSTALL_TEST_CMD" == "zsh: command not found: pnpm" ]]; then
error_message "PNPM"
else
success_message "PNPM $PNPM_VERSION"
fi
else
already_installed_message "PNPM $PNPM_VERSION"
fi
}
install_docker () {
SKIP="$(check_homebrew)"
if [[ -z "$SKIP" ]]; then
TEST_DOCKER_CMD=$(execute_command_without_error_print "docker --version")
if [[ -z "$TEST_DOCKER_CMD" ]] || [[ "$TEST_DOCKER_CMD" == "zsh: command not found: docker" ]]; then
installing_dependency "Docker"
brew install docker
AFTER_INSTALL_TEST_CMD=$(execute_command_without_error_print "docker --version")
if [[ -z "$AFTER_INSTALL_TEST_CMD" ]] || [[ "$AFTER_INSTALL_TEST_CMD" == "zsh: command not found: docker" ]]; then
error_message "Docker"
else
success_message "Docker"
fi
else
already_installed_message "Docker"
fi
else
skip_message "Docker"
echo "$SKIP"
fi
}
install_aws_cli () {
FILE_DESTINATION="$HOME/AWSCLIV2.pkg"
TEST_AWS_CMD=$(execute_command_without_error_print "aws --version")
if [[ -z "$TEST_AWS_CMD" ]] || [[ "$TEST_AWS_CMD" == "zsh: command not found: aws" ]]; then
installing_dependency "AWS CLI"
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "$FILE_DESTINATION"
sudo installer -pkg "$FILE_DESTINATION" -target /
AFTER_INSTALL_TEST_CMD=$(execute_command_without_error_print "aws --version")
if [[ -z "$AFTER_INSTALL_TEST_CMD" ]] || [[ "$AFTER_INSTALL_TEST_CMD" == "zsh: command not found: aws" ]]; then
error_message "AWS CLI"
else
success_message "AWS CLI"
fi
else
already_installed_message "AWS CLI"
fi
if [[ -f $FILE_DESTINATION ]]; then
rm "$FILE_DESTINATION"
fi
}
start_database() {
# Check if brew is installed
command -v brew > /dev/null 2>&1
# Initialize flag
already_installed=0
if [ $? -eq 0 ]; then
# Check if mongodb is installed
brew ls --versions mongodb > /dev/null
if [ $? -eq 0 ]; then
echo "Warning: MongoDB is already installed via brew. Please uninstall it first."
already_installed=1
fi
# Check if redis is installed
brew ls --versions redis > /dev/null
if [ $? -eq 0 ]; then
echo "Warning: Redis is already installed via brew. Please uninstall it first."
already_installed=1
fi
else
echo "brew is not installed, checking default ports for MongoDB and Redis"
# Check MongoDB (port 27017) and Redis (port 6379)
if lsof -Pi :27017 -sTCP:LISTEN -t >/dev/null ; then
echo "Warning: MongoDB is running on port 27017. Please stop it first."
already_installed=1
fi
if lsof -Pi :6379 -sTCP:LISTEN -t >/dev/null ; then
echo "Warning: Redis is running on port 6379. Please stop it first."
already_installed=1
fi
fi
# Only copy the example env file and start Docker Compose if both MongoDB and Redis are not already installed
if [ $already_installed -ne 1 ]; then
# Copy the example env file
cp ./docker/.env.example ./docker/local/development/.env
# Start Docker Compose detached
docker-compose -f ./docker/local/development/docker-compose.yml up -d
start_success_message "Docker Infrastructure"
else
echo "We recommend removing mongodb and redis databases from brew with 'brew remove <package_name>'."
echo "To manually start the containerized databases by going to /docker in the novu project"
fi
}
check_git () {
TEST_GIT_CMD=$(execute_command_without_error_print "git --version")
if [[ -z "$TEST_GIT_CMD" ]] || [[ "$TEST_GIT_CMD" == *"Failed to locate 'git'"* ]]; then
error_message "Git"
echo "⛔️ Git is a hard dependency to clone the monorepo"
exit 1
fi
already_installed_message "git"
}
clone_monorepo () {
SKIP="$(check_git)"
if [[ -z "$SKIP" ]]; then
echo ""
echo "❓ Do you want to clone Novu's monorepo? ($POSITIVE_RESPONSE / $NEGATIVE_RESPONSE)"
read -p " > " RESPONSE
echo ""
if [[ "$RESPONSE" == "$POSITIVE_RESPONSE" ]]; then
REPOSITORY="[email protected]:novuhq/novu.git"
DESTINATION_FOLDER="$HOME/Dev"
NOVU_FOLDER="$DESTINATION_FOLDER/novu"
[[ ! -d "$DESTINATION_FOLDER" ]] && mkdir "$DESTINATION_FOLDER"
if [[ ! -d "$NOVU_FOLDER" ]]; then
git clone "$REPOSITORY $NOVU_FOLDER"
success_message "Novu monorepo"
else
already_installed_message "Novu monorepo"
fi
fi
else
skip_message "Novu monorepo"
echo "$SKIP"
fi
}
# Novu set of tools chosen
install_novu_tools () {
check_git
make_zsh_default_shell
install_ohmyzsh
install_homebrew
install_homebrew_recipes
install_nvm
install_node
install_pnpm
install_docker
install_aws_cli
start_database
}
install_os_dependencies () {
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
echo "Install 🐧 Linux dependencies"
echo "//TODO"
install_novu_tools
elif [[ "$OSTYPE" == "darwin"* ]]; then
echo "Install 👿 MacOSx dependencies"
install_macosx_dependencies
install_novu_tools
else
echo "OS not supported"
fi
}
# Entry point
install_os_dependencies
clone_monorepo
refresh_shell