-
Notifications
You must be signed in to change notification settings - Fork 0
/
lara-stacker.sh
executable file
·335 lines (271 loc) · 9.29 KB
/
lara-stacker.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
#!/bin/bash
clear
# * ===========================
# * Display a status indicator
# * =========================
current_version="???"
is_updateable=false
script_dir="$(pwd)"
if command -v git &> /dev/null && [ -d ".git" ]; then
is_updateable=true
# ? Add the current script directory to the Git safe list
git config --global --add safe.directory "$script_dir"
fi
if [[ "$is_updateable" == true ]]; then
# ? Fetch the latest commit message that starts with a version tag
current_version=$(git log --pretty=format:'%s' | grep -o '\[v[0-9]*\.[0-9]*\.[0-9]*\]' | head -1 | tr -d '[]')
fi
echo -e "-=|[ LARA-STACKER $current_version ]|=-"
# * ===========
# * Validation
# * =========
# ? ================================================
# ? Source prompt script or abort if it isn't found
# ? ==============================================
prompt_function_dir="./scripts/functions/helpers/prompt.sh"
if [[ ! -f $prompt_function_dir ]]; then
echo -e "\nError: Working directory isn't the script's main.\n"
echo -e "Tip: Maybe [cd ~/Downloads/lara-stacker/ && sudo ./lara-stacker.sh] instead.\n"
echo -n "Press any key to exit..."
read whatever
clear
exit 1
fi
chmod +x $prompt_function_dir
source $prompt_function_dir
# ? Abort if the script isn't run with sudo
if [ "$EUID" -ne 0 ]; then
prompt "Aborted for missing super-user (sudo) permission." "Run the script using [sudo ./lara-stacker.sh] command."
fi
# ? Ensure that the environment file exists
if [ ! -f "./.env" ]; then
prompt "Aborted for missing [.env] file." "Copy one using [cp .env.example .env] command then fill its values."
fi
# ? =================================================
# ? Ensure that there is no placeholders in the file
# ? ===============================================
placeholders=("<your-username>" "<your-password>")
while IFS= read -r line; do
# ? Skip comments and empty lines
[[ "$line" =~ ^#.*$ || "$line" == "" ]] && continue
for placeholder in "${placeholders[@]}"; do
if [[ "$line" == *"$placeholder"* ]]; then
prompt "Aborted because [.env] file contains a placeholder: '$placeholder'." "Please replace placeholders with values."
fi
done
done < "./.env"
# ? ===================================================
# ? Double check for environment variables consistency
# ? =================================================
env_example_vars=$(grep -oE '^[A-Z_]+=' .env.example | sort)
env_vars=$(grep -oE '^[A-Z_]+=' .env | sort)
diff <(echo "$env_example_vars") <(echo "$env_vars") &>/dev/null
if [ $? -ne 0 ]; then
prompt "Aborted for different environment variables." "Ensure that [.env.example] variables match [.env] ones."
fi
# ? Ensure all side scripts are executable
find ./scripts -type f -not -path "*/functions/*" ! -perm -111 -exec chmod +x {} +
# * ============
# * Preparation
# * ==========
# ? Get environment variables and defaults
lara_stacker_dir=$PWD
source $lara_stacker_dir/.env
# * ========
# * Process
# * ======
# ? =====================
# ? Checking for updates
# ? ===================
if [[ -f "/tmp/updated-lara-stacker.flag" ]]; then
rm /tmp/updated-lara-stacker.flag
fi
echo -en "\nChecking for updates"
sleep 1
echo -en "."
sleep 1
echo -en "."
update_available=false
latest_version=""
# ? Check for updates if it's possible
if [[ "$is_updateable" == true ]]; then
git fetch origin
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse "origin/main")
if [ "$LOCAL" != "$REMOTE" ]; then
update_available=true
latest_version=$(git ls-remote --tags origin | grep -o 'v[0-9]*\.[0-9]*\.[0-9]*$' | sort -V | tail -1)
fi
fi
echo -en "."
clear
# ? Display the update version, if any
if [ "$update_available" == true ]; then
echo -e "New version ($latest_version) is available!\n"
fi
# ? Loop the menu until user chooses to exit
counter=0
while true; do
counter=$((counter + 1))
echo -e "-=|[ LARA-STACKER $current_version ]|=-\n"
echo -e "Available Operations:\n"
options=("1. Manage TALL Projects" "2. Manage MySQL Databases" "3. Manage Apache Sites" "4. Create A Raw Laravel Project" "5. Exit")
# ? Conditional options
include_zero=false
if [[ -f "/tmp/updated-lara-stacker.flag" ]]; then
rm /tmp/updated-lara-stacker.flag
update_available=false
fi
if [ "$update_available" == true ]; then
options+=("6. Download Updates")
fi
if [[ ! -f "$lara_stacker_dir/done-setup.flag" ]]; then
options+=("0. Initial Setup")
include_zero=true
fi
for opt in "${options[@]}"; do
echo "$opt "
done
echo ""
if [[ $counter -eq 1 && "$1" ]]; then
choice="$1"
else
if [ "$include_zero" == true ]; then
options_count=$((${#options[@]} - 1))
read -p "Choose an operation (0-$options_count): " choice
else
options_count=$((${#options[@]}))
read -p "Choose an operation (1-$options_count): " choice
fi
fi
clear
# ? Options logic
case $choice in
0)
if [[ -f "$lara_stacker_dir/done-setup.flag" ]]; then
prompt "-=|[ LARA-STACKER [$current_version] ]|=-" "Invalid option! Please type one the of digits in the list..." false
else
sudo RAN_MAIN_SCRIPT="true" ./scripts/setup.sh
fi
;;
1)
while true; do
clear
echo -e "-=|[ Lara-Stacker |> TALL PROJECTS MANAGEMENT ]|=-\n"
echo -e "TALL Stack Frameworks:\n"
echo "- TailwindCSS"
echo "- AlpineJS"
echo "- Livewire"
echo -e "- Laravel\n"
echo -e "Available Operations:\n"
echo "1. List All Projects"
echo "2. Create A Project"
echo "3. Delete A Project"
echo "4. Import A Project"
echo -e "5. Go Back To Main Menu\n"
read -p "Choose an operation (1-5): " stack_choice
case $stack_choice in
1)
RAN_MAIN_SCRIPT="true" ./scripts/TALL/list.sh
;;
2)
sudo RAN_MAIN_SCRIPT="true" ./scripts/TALL/create.sh
;;
3)
sudo RAN_MAIN_SCRIPT="true" ./scripts/TALL/delete.sh
;;
4)
sudo RAN_MAIN_SCRIPT="true" ./scripts/TALL/import.sh
;;
5)
clear
break
;;
*)
clear
prompt "-=|[ Lara-Stacker |> TALL PROJECTS MANAGEMENT ]|=-" "Invalid option! Please type one the of digits in the list..." false true
;;
esac
done
;;
2)
while true; do
clear
echo -e "-=|[ Lara-Stacker |> MYSQL DATABASE MANAGEMENT ]|=-\n"
echo -e "Available Operations:\n"
echo "1. List All Databases"
echo "2. Create A Database"
echo "3. Delete A Database"
echo -e "4. Go Back To Main Menu\n"
read -p "Choose an operation (1-4): " db_choice
case $db_choice in
1)
RAN_MAIN_SCRIPT="true" ./scripts/mysql/list.sh
;;
2)
RAN_MAIN_SCRIPT="true" ./scripts/mysql/create.sh
;;
3)
RAN_MAIN_SCRIPT="true" ./scripts/mysql/delete.sh
;;
4)
clear
break
;;
*)
clear
prompt "-=|[ Lara-Stacker |> MYSQL DATABASE MANAGEMENT ]|=-" "Invalid option! Please type one the of digits in the list..." false true
;;
esac
done
;;
3)
while true; do
clear
echo -e "-=|[ Lara-Stacker |> APACHE SITE MANAGEMENT ]|=-\n"
echo -e "Available Operations:\n"
echo "1. List All Sites"
echo "2. Enable A Site"
echo "3. Disable A Site"
echo -e "4. Go Back To Main Menu\n"
read -p "Choose an operation (1-4): " site_choice
case $site_choice in
1)
RAN_MAIN_SCRIPT="true" ./scripts/apache/list.sh
;;
2)
RAN_MAIN_SCRIPT="true" ./scripts/apache/enable.sh
;;
3)
RAN_MAIN_SCRIPT="true" ./scripts/apache/disable.sh
;;
4)
clear
break
;;
*)
clear
prompt "-=|[ Lara-Stacker |> APACHE SITE MANAGEMENT ]|=-" "Invalid option! Please type one the of digits in the list..." false true
;;
esac
done
;;
4)
sudo RAN_MAIN_SCRIPT="true" ./scripts/create_raw.sh
;;
5)
echo -e "\nExiting Lara-Stacker...\n"
exit 0
;;
6)
if [ "$update_available" == false ]; then
prompt "-=|[ LARA-STACKER [$current_version] ]|=-" "Invalid option! Please type one the of digits in the list..." false
else
sudo RAN_MAIN_SCRIPT="true" ./scripts/update.sh
fi
;;
*)
prompt "-=|[ LARA-STACKER [$current_version] ]|=-" "Invalid option! Please type one the of digits in the list..." false true
;;
esac
done