forked from picodotdev/alis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alis-packages.sh
executable file
·469 lines (405 loc) · 12.7 KB
/
alis-packages.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
#!/usr/bin/env bash
set -e
# Arch Linux Install Script Packages (alis-packages) installs software
# packages.
# Copyright (C) 2021 picodotdev
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This script is hosted at https://github.com/picodotdev/alis. For new features,
# improvements and bugs fill an issue in GitHub or make a pull request.
# Pull Request are welcome!
#
# If you test it in real hardware please send me an email to [email protected] with
# the machine description and tell me if somethig goes wrong or all works fine.
#
# Please, don't ask for support for this script in Arch Linux forums, first read
# the Arch Linux wiki [1], the Installation Guide [2] and the General
# Recomendations [3], later compare the commands with those of this script.
#
# [1] https://wiki.archlinux.org
# [2] https://wiki.archlinux.org/index.php/Installation_guide
# [3] https://wiki.archlinux.org/index.php/General_recommendations
# Reference:
# * [Change root](https://wiki.archlinux.org/index.php/Change_root)
# * [Deactivate volume group](https://wiki.archlinux.org/index.php/LVM#Deactivate_volume_group)
# Usage:
# # loadkeys es
# # curl https://raw.githubusercontent.com/picodotdev/alis/master/download.sh | bash, or with URL shortener curl -sL https://bit.ly/2F3CATp | bash
# # vim alis-packages.conf
# # sudo ./alis-packages.sh
# enviroment variables
#USER_NAME=""
#USER_PASSWORD=""
# global variables (no configuration, don't edit)
SYSTEM_INSTALLATION="false"
CONF_FILE="alis-packages.conf"
LOG_FILE="alis-packages.log"
RED='\033[0;31m'
GREEN='\033[0;32m'
LIGHT_BLUE='\033[1;34m'
NC='\033[0m'
function configuration_install() {
source "$CONF_FILE"
}
function sanitize_variables() {
PACKAGES_PACMAN=$(sanitize_variable "$PACKAGES_PACMAN")
PACKAGES_FLATPAK=$(sanitize_variable "$PACKAGES_FLATPAK")
PACKAGES_SDKMAN=$(sanitize_variable "$PACKAGES_SDKMAN")
PACKAGES_AUR_COMMAND=$(sanitize_variable "$PACKAGES_AUR_COMMAND")
PACKAGES_AUR=$(sanitize_variable "$PACKAGES_AUR")
SYSTEMD_UNITS=$(sanitize_variable "$SYSTEMD_UNITS")
}
function sanitize_variable() {
VARIABLE=$1
VARIABLE=$(echo $VARIABLE | sed "s/![^ ]*//g") # remove disabled
VARIABLE=$(echo $VARIABLE | sed "s/ {2,}/ /g") # remove unnecessary white spaces
VARIABLE=$(echo $VARIABLE | sed 's/^[[:space:]]*//') # trim leading
VARIABLE=$(echo $VARIABLE | sed 's/[[:space:]]*$//') # trim trailing
echo "$VARIABLE"
}
function check_variables() {
check_variables_boolean "PACKAGES_PACMAN_INSTALL" "$PACKAGES_PACMAN_INSTALL"
check_variables_boolean "PACKAGES_FLATPAK_INSTALL" "$PACKAGES_FLATPAK_INSTALL"
check_variables_boolean "PACKAGES_SDKMAN_INSTALL" "$PACKAGES_SDKMAN_INSTALL"
check_variables_boolean "PACKAGES_AUR_INSTALL" "$PACKAGES_AUR_INSTALL"
check_variables_list "PACKAGES_AUR_COMMAND" "$PACKAGES_AUR_COMMAND" "paru yay aurman" "true"
}
function check_variables_value() {
NAME=$1
VALUE=$2
if [ -z "$VALUE" ]; then
echo "$NAME environment variable must have a value."
exit
fi
}
function check_variables_boolean() {
NAME=$1
VALUE=$2
check_variables_list "$NAME" "$VALUE" "true false"
}
function check_variables_list() {
NAME=$1
VALUE=$2
VALUES=$3
REQUIRED=$4
if [ "$REQUIRED" == "" -o "$REQUIRED" == "true" ]; then
check_variables_value "$NAME" "$VALUE"
fi
if [ "$VALUE" != "" -a -z "$(echo "$VALUES" | grep -F -w "$VALUE")" ]; then
echo "$NAME environment variable value [$VALUE] must be in [$VALUES]."
exit
fi
}
function check_variables_equals() {
NAME1=$1
NAME2=$2
VALUE1=$3
VALUE2=$4
if [ "$VALUE1" != "$VALUE2" ]; then
echo "$NAME1 and $NAME2 must be equal [$VALUE1, $VALUE2]."
exit
fi
}
function check_variables_size() {
NAME=$1
SIZE_EXPECT=$2
SIZE=$3
if [ "$SIZE_EXPECT" != "$SIZE" ]; then
echo "$NAME array size [$SIZE] must be [$SIZE_EXPECT]."
exit
fi
}
function init() {
init_log
}
function init_log() {
if [ "$LOG" == "true" ]; then
exec > >(tee -a $LOG_FILE)
exec 2> >(tee -a $LOG_FILE >&2)
fi
set -o xtrace
}
function facts() {
print_step "facts()"
if [ $(whoami) == "root" ]; then
SYSTEM_INSTALLATION="true"
else
SYSTEM_INSTALLATION="false"
USER_NAME="$(whoami)"
fi
}
function checks() {
print_step "checks()"
check_variables_value "USER_NAME" "$USER_NAME"
if [ -n "$PACKAGES_PACMAN" ]; then
pacman -Si $PACKAGES_PACMAN
fi
if [ "$SYSTEM_INSTALLATION" == "false" ]; then
ask_sudo
fi
}
function ask_sudo() {
sudo pwd >> /dev/null
}
function prepare() {
print_step "prepare()"
}
function packages() {
print_step "packages()"
packages_pacman
packages_flatpak
packages_sdkman
packages_aur
}
function packages_pacman() {
print_step "packages_pacman()"
if [ "$PACKAGES_PACMAN_INSTALL" == "true" ]; then
if [ -n "$PACKAGES_PACMAN" ]; then
pacman_install "$PACKAGES_PACMAN"
fi
fi
}
function packages_flatpak() {
print_step "packages_flatpak()"
if [ "$PACKAGES_FLATPAK_INSTALL" == "true" ]; then
pacman_install "flatpak"
if [ -n "$PACKAGES_FLATPAK" ]; then
execute_flatpak "flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo"
flatpak_install "$PACKAGES_FLATPAK"
fi
fi
}
function packages_sdkman() {
print_step "packages_sdkman()"
if [ "$PACKAGES_SDKMAN_INSTALL" == "true" ]; then
pacman_install "zip unzip"
execute_user "curl -s https://get.sdkman.io | bash"
if [ -n "$PACKAGES_SDKMAN" ]; then
execute_user "sed -i 's/sdkman_auto_answer=.*/sdkman_auto_answer=true/g' /home/$USER_NAME/.sdkman/etc/config"
sdkman_install "$PACKAGES_SDKMAN"
execute_user "sed -i 's/sdkman_auto_answer=.*/sdkman_auto_answer=false/g' /home/$USER_NAME/.sdkman/etc/config"
fi
fi
}
function packages_aur() {
print_step "packages_aur()"
if [ "$PACKAGES_AUR_INSTALL" == "true" ]; then
pacman_install "git"
case "$PACKAGES_AUR_COMMAND" in
"aurman" )
execute_aur "rm -rf /home/$USER_NAME/.alis/aur/$PACKAGES_AUR_COMMAND && mkdir -p /home/$USER_NAME/.alis/aur && cd /home/$USER_NAME/.alis/aur && git clone https://aur.archlinux.org/$PACKAGES_AUR_COMMAND.git && (cd $PACKAGES_AUR_COMMAND && makepkg -si --noconfirm) && rm -rf /home/$USER_NAME/.alis/aur/$PACKAGES_AUR_COMMAND"
;;
"yay" )
execute_aur "rm -rf /home/$USER_NAME/.alis/aur/$PACKAGES_AUR_COMMAND && mkdir -p /home/$USER_NAME/.alis/aur && cd /home/$USER_NAME/.alis/aur && git clone https://aur.archlinux.org/$PACKAGES_AUR_COMMAND.git && (cd $PACKAGES_AUR_COMMAND && makepkg -si --noconfirm) && rm -rf /home/$USER_NAME/.alis/aur/$PACKAGES_AUR_COMMAND"
;;
"paru" | *)
execute_aur "rm -rf /home/$USER_NAME/.alis/aur/$PACKAGES_AUR_COMMAND && mkdir -p /home/$USER_NAME/.alis/aur && cd /home/$USER_NAME/.alis/aur && git clone https://aur.archlinux.org/$PACKAGES_AUR_COMMAND.git && (cd $PACKAGES_AUR_COMMAND && makepkg -si --noconfirm) && rm -rf /home/$USER_NAME/.alis/aur/$PACKAGES_AUR_COMMAND"
;;
esac
if [ -n "$PACKAGES_AUR" ]; then
aur_install "$PACKAGES_AUR"
fi
fi
}
function pacman_install() {
ERROR="true"
set +e
IFS=' ' PACKAGES=($1)
for VARIABLE in {1..5}
do
COMMAND="pacman -Syu --noconfirm --needed ${PACKAGES[@]}"
execute_sudo "$COMMAND"
if [ $? == 0 ]; then
ERROR="false"
break
else
sleep 10
fi
done
set -e
if [ "$ERROR" == "true" ]; then
exit
fi
}
function flatpak_install() {
OPTIONS=""
if [ "$SYSTEM_INSTALLATION" == "true" ]; then
OPTIONS="--system"
fi
ERROR="true"
set +e
IFS=' ' PACKAGES=($1)
for VARIABLE in {1..5}
do
COMMAND="flatpak install $OPTIONS -y flathub ${PACKAGES[@]}"
execute_flatpak "$COMMAND"
if [ $? == 0 ]; then
ERROR="false"
break
else
sleep 10
fi
done
set -e
if [ "$ERROR" == "true" ]; then
exit
fi
}
function sdkman_install() {
ERROR="true"
set +e
IFS=' ' PACKAGES=($1)
for PACKAGE in "${PACKAGES[@]}"
do
IFS=':' PACKAGE=($PACKAGE)
for VARIABLE in {1..5}
do
COMMAND="source /home/$USER_NAME/.sdkman/bin/sdkman-init.sh && sdk install ${PACKAGE[@]}"
execute_user "$COMMAND"
if [ $? == 0 ]; then
ERROR="false"
break
else
sleep 10
fi
done
done
set -e
if [ "$ERROR" == "true" ]; then
exit
fi
}
function aur_install() {
ERROR="true"
set +e
IFS=' ' PACKAGES=($1)
for VARIABLE in {1..5}
do
COMMAND="$PACKAGES_AUR_COMMAND -Syu --noconfirm --needed ${PACKAGES[@]}"
execute_aur "$COMMAND"
if [ $? == 0 ]; then
ERROR="false"
break
else
sleep 10
fi
done
set -e
if [ "$ERROR" == "true" ]; then
exit
fi
}
function systemd_units() {
IFS=' ' UNITS=($SYSTEMD_UNITS)
for U in ${UNITS[@]}; do
ACTION=""
UNIT=${U}
if [[ $UNIT == -* ]]; then
ACTION="disable"
UNIT=$(echo $UNIT | sed "s/^-//g")
elif [[ $UNIT == +* ]]; then
ACTION="enable"
UNIT=$(echo $UNIT | sed "s/^+//g")
elif [[ $UNIT =~ ^[a-zA-Z0-9]+ ]]; then
ACTION="enable"
UNIT=$UNIT
fi
if [ -n "$ACTION" ]; then
execute_sudo "systemctl $ACTION $UNIT"
fi
done
}
function execute_flatpak() {
COMMAND="$1"
if [ "$SYSTEM_INSTALLATION" == "true" ]; then
arch-chroot /mnt bash -c "$COMMAND"
else
bash -c "$COMMAND"
fi
}
function execute_aur() {
COMMAND="$1"
if [ "$SYSTEM_INSTALLATION" == "true" ]; then
arch-chroot /mnt sed -i 's/^%wheel ALL=(ALL) ALL$/%wheel ALL=(ALL) NOPASSWD: ALL/' /etc/sudoers
arch-chroot /mnt bash -c "echo -e \"$USER_PASSWORD\n$USER_PASSWORD\n$USER_PASSWORD\n$USER_PASSWORD\n\" | su $USER_NAME -s /usr/bin/bash -c \"$COMMAND\""
arch-chroot /mnt sed -i 's/^%wheel ALL=(ALL) NOPASSWD: ALL$/%wheel ALL=(ALL) ALL/' /etc/sudoers
else
bash -c "$COMMAND"
fi
}
function execute_sudo() {
COMMAND="$1"
if [ "$SYSTEM_INSTALLATION" == "true" ]; then
arch-chroot /mnt bash -c "$COMMAND"
else
bash -c "sudo $COMMAND"
fi
}
function execute_user() {
COMMAND="$1"
if [ "$SYSTEM_INSTALLATION" == "true" ]; then
arch-chroot /mnt bash -c "su $USER_NAME -s /usr/bin/bash -c \"$COMMAND\""
else
bash -c "$COMMAND"
fi
}
function end() {
echo ""
echo -e "${GREEN}Arch Linux packages installed successfully"'!'"${NC}"
echo ""
}
function print_step() {
STEP="$1"
echo ""
echo -e "${LIGHT_BLUE}# ${STEP} step${NC}"
echo ""
}
function execute_step() {
STEP="$1"
STEPS="$2"
if [[ " $STEPS " =~ " $STEP " ]]; then
eval $STEP
else
echo "Skipping $STEP"
fi
}
function main() {
ALL_STEPS=("configuration_install" "sanitize_variables" "check_variables" "init" "facts" "checks" "prepare" "packages" "systemd_units" "end")
STEP="configuration_install"
if [ -n "$1" ]; then
STEP="$1"
fi
if [ "$STEP" == "steps" ]; then
echo "Steps: $ALL_STEPS"
return 0
fi
# get step execute from
FOUND="false"
STEPS=""
for S in ${ALL_STEPS[@]}; do
if [ $FOUND = "true" -o "${STEP}" = "${S}" ]; then
FOUND="true"
STEPS="$STEPS $S"
fi
done
execute_step "configuration_install" "${STEPS}"
execute_step "sanitize_variables" "${STEPS}"
execute_step "check_variables" "${STEPS}"
execute_step "init" "${STEPS}"
execute_step "facts" "${STEPS}"
execute_step "prepare" "${STEPS}"
execute_step "packages" "${STEPS}"
execute_step "systemd_units" "${STEPS}"
execute_step "end" "${STEPS}"
}
main $@