-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.sh
executable file
·145 lines (121 loc) · 3.59 KB
/
init.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
#!/bin/bash
# set -euo pipefail
function rnd_pwd() {
LENGTH=$1
tr -dc 'A-Za-z0-9\-_' </dev/urandom | head -c "${LENGTH:=42}"
echo
}
usage() {
echo "Usage: $0 -u username"
echo "Options:"
echo " -u <username> default admin user"
echo " -p <password> default admin password"
echo " -t <discord_token>"
echo " -c <discord_channel>"
echo " -i to force install docker"
echo " -f to force .env overwrite"
echo " -d for dev environment"
}
while getopts u:p:t:c:ifdh flag; do
case "${flag}" in
u) USERNAME=${OPTARG} ;;
p) PASSWORD=${OPTARG} ;;
t) DISCORD_TOKEN=${OPTARG} ;;
c) DISCORD_CHANNEL=${OPTARG} ;;
i) FORCE_INSTALL=True ;;
f) FORCE_ENV_OVERWRITE=True ;;
d) DEV=True ;;
h)
usage
exit 0
;;
*)
echo 'Error in command line parsing' >&2
exit 1
;;
esac
done
[[ -n "$DEV" ]] && PROFILE="dev" || PROFILE="prod"
COLOR_RESET="\e[0;0m"
COLOR_GREEN="\e[0;32m"
COLOR_CYAN="\e[0;36m"
# check docker installation
if ! type "docker" >/dev/null || [ -n "$FORCE_INSTALL" ]; then
echo 'Docker not found proceding to its installation'
curl -fsSL https://test.docker.com -o install-docker.sh
chmod +x install-docker.sh
./install-docker.sh
else
echo 'Docker is already present'
fi
# check existence of .env file
if ! test -f ".env" || [ -n "$FORCE_ENV_OVERWRITE" ]; then
MONGO_DATABASE=sthack-scoreboard
MONGO_USERNAME=sthack-admin
MONGO_PASSWORD=$(rnd_pwd)
SALT=$(rnd_pwd 64)
cat >.env <<EOF
MONGO_DATABASE=${MONGO_DATABASE}
MONGO_USERNAME=${MONGO_USERNAME}
MONGO_PASSWORD=${MONGO_PASSWORD}
APP_SALT=${SALT}
APP_DISCORD_TOKEN=${DISCORD_TOKEN}
APP_DISCORD_CHANNEL=${DISCORD_CHANNEL}
EOF
echo ".env file has been generated"
if [ -n "$DEV" ]; then
ME_USERNAME=sthack-me-admin
ME_PASSWORD=sthack-me-password
cat >>.env <<EOF
ME_USERNAME=${ME_USERNAME}
ME_PASSWORD=${ME_PASSWORD}
EOF
echo ".env file has been update for dev"
echo -e "${COLOR_GREEN}Mongo express${COLOR_RESET} is available at ${COLOR_GREEN}http://localhost:8081${COLOR_RESET} with:"
echo -e "username: ${COLOR_GREEN}${ME_USERNAME}${COLOR_RESET}"
echo -e "password: ${COLOR_GREEN}${ME_PASSWORD}${COLOR_RESET}"
echo -e "${COLOR_CYAN}Redisinsight${COLOR_RESET} is available at ${COLOR_CYAN}http://localhost:8001${COLOR_RESET}"
if ! test -f "server/.env" || [ -n "$FORCE_ENV_OVERWRITE" ]; then
cat >server/.env <<EOF
NODE_ENV=development
NODE_PATH=./
APP_MONGO=mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@localhost:27017
APP_MONGO_DB=${MONGO_DATABASE}
APP_REDIS=redis://localhost:6379
APP_SALT=${SALT}
EOF
if [ -n "$DISCORD_TOKEN" ]; then
cat >>.env <<EOF
APP_DISCORD_TOKEN=${DISCORD_TOKEN}
APP_DISCORD_CHANNEL=${DISCORD_CHANNEL}
EOF
fi
echo "server/.env file has been generated"
fi
fi
else
echo ".env already exist, we keep its config"
fi
# prepare docker env
echo "Building docker for $PROFILE"
sudo docker compose --profile "$PROFILE" build
echo "Launching docker for $PROFILE"
sudo docker compose --profile "$PROFILE" up -d --wait
# create admin user
if [ -n "$USERNAME" ]; then
PASSWORD="${PASSWORD:=$(rnd_pwd)}"
echo "Creating the admin on $PROFILE"
if [ -n "$DEV" ]; then
if [ ! -d "./node_modules" ]; then
echo "Installing npm packages"
npm install
fi
npm run -w server create-admin -- --user "$USERNAME" --password "$PASSWORD"
else
sudo docker compose --profile "$PROFILE" exec website \
npm run -w server create-admin -- --user "$USERNAME" --password "$PASSWORD"
fi
fi
if [ -n "$DEV" ]; then
echo -e "Now, you can use ${COLOR_CYAN}npm run start${COLOR_RESET} and start coding"
fi