-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·161 lines (126 loc) · 6.37 KB
/
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
#!/usr/bin/bash
bold=$(tput bold)
normal=$(tput sgr0)
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
git submodule update --init --recursive
if [ "$?" -ne 0 ]; then
echo "Git submodule update failed. Exiting script"
exit 1;
fi
generate_password() {
tr -dc '[:alnum:]' < /dev/urandom | head -c 15
}
calc_sha256() {
echo -n "$1" | sha256sum | cut -d " " -f1
}
copy_file_if_not_exist() {
folder=$1
original_file=$2
copied_file=$3
if ! [ -f "$folder/$copied_file" ]; then
if ! [ -w "$folder" ]; then
echo "No write access to the $folder folder. Please change the permissions to that folder."
exit 1
fi
echo -n "$copied_file not found. Copying $folder/$original_file"
cp "$folder/$original_file" "$folder/$copied_file"
echo -e "... ${GREEN}done ${NC}"
echo
fi
}
replace_line_in_file() {
file=$1
original_line=$2
new_line=$3
type=$4
if ! grep -Ei "$original_line" < "$file" 2>&1 > /dev/null; then
echo "Cannot copy $type into $file. Please do so manually."
exit 1
fi
echo -n "Copying $type into $file"
sed -i -e "s/$original_line/$new_line/" $file
echo -e "... ${GREEN}done${NC}"
echo
}
echo -e "${RED}========== Lots of passwords will be generated by this script randomly. Make sure you note them down! ==========${NC}"
echo
# =============== Set up API env ===============
echo -e "${CYAN}Setting up API's .env file${NC}"
echo ======================================================
copy_file_if_not_exist "api" ".env.template" ".env"
copy_file_if_not_exist "mysql" ".env.template" ".env"
db_pass=$(generate_password)
echo "Generated password for MySQL access as MOCBOT_API: ${bold}$db_pass${normal}"
replace_line_in_file "api/.env" "DB_PASS.*$" "DB_PASS = $db_pass" "MySQL MOCBOT_API password"
replace_line_in_file "mysql/.env" "^MYSQL_PASSWORD.*$" "MYSQL_PASSWORD=$db_pass" "MySQL MOCBOT_API password"
# =============== Set up MOCBOT config ===============
echo -e "${CYAN}Setting up MOCBOT's config.yml${NC}"
echo ======================================================
copy_file_if_not_exist "bot" "config.template.yml" "config.yml"
mocbot_api_key=$(generate_password)
echo "Generated key for API access from MOCBOT: ${bold}$mocbot_api_key${normal}"
hash_result=$(calc_sha256 "$mocbot_api_key")
replace_line_in_file "bot/config.yml" "API_KEY:.*$" "API_KEY: $mocbot_api_key" "API Key for API access from MOCBOT"
# =============== Script to insert API key hash ===============
if [ -w "mysql/data" ]; then
echo -n "Creating an SQL script to add API key hash into the database"
echo "INSERT INTO APIKeys VALUES ('MOCBOT_API', \"$hash_result\");" > ./mysql/data/sql-db-add-info.sql
echo -e "S... ${GREEN}done${NC}"
echo
else
echo "No write access to mysql/data folder. Please change the permissions to that folder."
exit 1
fi
# =============== Set up Lavalink config ===============
echo -e "${CYAN}Setting up Lavalink's application.yml${NC}"
echo ======================================================
copy_file_if_not_exist "lavalink" "application.template.yml" "application.yml"
lavalink_pass=$(generate_password)
echo "Generated password for Lavalink access: ${bold}$lavalink_pass${normal}"
replace_line_in_file "lavalink/application.yml" "password:.*$" "password: $lavalink_pass" "Lavalink's password"
replace_line_in_file "bot/config.yml" "PASS:.*$" "PASS: $lavalink_pass" "Lavalink's password"
# =============== Set up MySql env ===============
echo -e "${CYAN}Setting up MySql env file${NC}"
echo ======================================================
db_root_pass=$(generate_password)
echo "Generated password for MySQL root user: ${bold}$db_root_pass${normal}"
replace_line_in_file "mysql/.env" "^MYSQL_ROOT_PASSWORD.*$" "MYSQL_ROOT_PASSWORD=$db_root_pass" "MySQL root password"
# =============== Manual setup ===============
# Copy configs
echo
echo -e "${CYAN}Discord Authentication${NC}"
echo ======================================================
printf 'See the \e]8;;https://discord.com/developers/applications\e\\Discord Developer Portal\e]8;;\e\\ to generate a bot token.\nMOCBOT utilises two tokens for a production and development environment.\n\n'
read -p "Paste your generated production bot token here: " bot_token_prod
replace_line_in_file "bot/config.yml" "PRODUCTION:.*$" "PRODUCTION: \"$bot_token_prod\"" "Discord token"
echo "Paste your development bot token here. If you don't want to utilise a dev environment, paste your production token here instead"
echo
read -p "Paste your generated development/production bot token here: " bot_token_dev
replace_line_in_file "bot/config.yml" "DEVELOPMENT:.*$" "DEVELOPMENT: \"$bot_token_dev\"" "Discord token"
echo -e "${CYAN}Spotify Authentication${NC}"
echo ======================================================
printf "See the \e]8;;https://developer.spotify.com/dashboard\e\\Spotify Developer Portal\e]8;;\e\\ to generate spotify developer application.\nMOCBOT utilises this for music playing capabilities.\n\n"
read -p "Paste Spotify Client ID: " spotify_client_id
replace_line_in_file "bot/config.yml" "CLIENT_ID:.*$" "CLIENT_ID: \"$spotify_client_id\"" "Spotify client ID"
replace_line_in_file "lavalink/application.yml" "clientId:.*$" "clientId: \"$spotify_client_id\"" "Spotify client ID"
read -p "Paste Spotify Client Secret: " spotify_client_secret
replace_line_in_file "bot/config.yml" "CLIENT_SECRET:.*$" "CLIENT_SECRET: \"$spotify_client_secret\"" "Spotify client secret"
replace_line_in_file "lavalink/application.yml" "clientSecret:.*$" "clientSecret: \"$spotify_client_secret\"" "Spotify client secret"
read -p "To gain access to specific developer features, please enter the Discord user IDs of the users that you wish to be labelled as developers, space separated. (E.g.: 123 234 345): " developer_ids
echo -n "Creating an SQL script to add developer user IDs into the database"
while IFS=' ' read -ra items; do
for item in "${items[@]}"; do
echo "INSERT INTO Developers VALUES ($item);" >> mysql/data/sql-db-add-info.sql
done
done <<< "$developer_ids"
echo -e "... ${GREEN}done${NC}"
echo
read -p "All done! Would you like to start the MOCBOT system? (y/n): " start_input
if [ $start_input == 'y' ]; then
docker compose up --build -d
else
echo -e "You can start the MOCBOT service manually by running ${bold}docker compose up --build -d${normal}"
fi