Skip to content

Commit 45a880a

Browse files
Add common service script
1 parent 0f79e00 commit 45a880a

File tree

8 files changed

+115
-35
lines changed

8 files changed

+115
-35
lines changed

core/tabs/applications-setup/docker-setup.sh

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/bin/sh -e
22

33
. ../common-script.sh
4+
. ../common-service-script.sh
45

56
# Function to prompt the user for installation choice
67
choose_installation() {
7-
clear
88
printf "%b\n" "${YELLOW}Choose what to install:${RC}"
99
printf "%b\n" "1. ${YELLOW}Docker${RC}"
1010
printf "%b\n" "2. ${YELLOW}Docker Compose${RC}"
@@ -28,13 +28,9 @@ install_docker() {
2828
;;
2929
zypper)
3030
"$ESCALATION_TOOL" "$PACKAGER" --non-interactive install docker
31-
"$ESCALATION_TOOL" systemctl enable docker
32-
"$ESCALATION_TOOL" systemctl start docker
3331
;;
3432
pacman)
3533
"$ESCALATION_TOOL" "$PACKAGER" -S --noconfirm docker
36-
"$ESCALATION_TOOL" systemctl enable docker
37-
"$ESCALATION_TOOL" systemctl start docker
3834
;;
3935
apk)
4036
"$ESCALATION_TOOL" "$PACKAGER" add docker
@@ -44,6 +40,8 @@ install_docker() {
4440
exit 1
4541
;;
4642
esac
43+
44+
startAndEnableService docker
4745
}
4846

4947
install_docker_compose() {

core/tabs/common-service-script.sh

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/bin/sh -e
2+
3+
checkInitManager() {
4+
for manager in $1; do
5+
if command_exists "$manager"; then
6+
INIT_MANAGER="$manager"
7+
printf "%b\n" "${CYAN}Using ${manager} to interact with init system${RC}"
8+
break
9+
fi
10+
done
11+
12+
if [ -z "$INIT_MANAGER" ]; then
13+
printf "%b\n" "${RED}Can't find a supported init system${RC}"
14+
exit 1
15+
fi
16+
}
17+
18+
startService() {
19+
case "$INIT_MANAGER" in
20+
systemctl)
21+
"$ESCALATION_TOOL" "$INIT_MANAGER" start "$1"
22+
;;
23+
rc-service)
24+
"$ESCALATION_TOOL" "$INIT_MANAGER" "$1" start
25+
;;
26+
esac
27+
}
28+
29+
stopService() {
30+
case "$INIT_MANAGER" in
31+
systemctl)
32+
"$ESCALATION_TOOL" "$INIT_MANAGER" stop "$1"
33+
;;
34+
rc-service)
35+
"$ESCALATION_TOOL" "$INIT_MANAGER" "$1" stop
36+
;;
37+
esac
38+
}
39+
40+
enableService() {
41+
case "$INIT_MANAGER" in
42+
systemctl)
43+
"$ESCALATION_TOOL" "$INIT_MANAGER" enable "$1"
44+
;;
45+
rc-service)
46+
"$ESCALATION_TOOL" rc-update add "$1"
47+
;;
48+
esac
49+
}
50+
51+
disableService() {
52+
case "$INIT_MANAGER" in
53+
systemctl)
54+
"$ESCALATION_TOOL" "$INIT_MANAGER" disable "$1"
55+
;;
56+
rc-service)
57+
"$ESCALATION_TOOL" rc-update del "$1"
58+
;;
59+
esac
60+
}
61+
62+
startAndEnableService() {
63+
case "$INIT_MANAGER" in
64+
systemctl)
65+
"$ESCALATION_TOOL" "$INIT_MANAGER" enable --now "$1"
66+
;;
67+
rc-service)
68+
enableService "$1"
69+
startService "$1"
70+
;;
71+
esac
72+
}
73+
74+
isServiceActive() {
75+
case "$INIT_MANAGER" in
76+
systemctl)
77+
"$ESCALATION_TOOL" "$INIT_MANAGER" is-active --quiet "$1"
78+
;;
79+
rc-service)
80+
"$ESCALATION_TOOL" "$INIT_MANAGER" "$1" status --quiet
81+
;;
82+
esac
83+
}
84+
85+
checkInitManager 'systemctl rc-service'

core/tabs/utils/bluetooth-control.sh

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/sh -e
22

33
. ../common-script.sh
4+
. ../common-service-script.sh
45

56
# Function to check Bluez is installed
67
setupBluetooth() {
@@ -10,6 +11,9 @@ setupBluetooth() {
1011
pacman)
1112
"$ESCALATION_TOOL" "$PACKAGER" -S --noconfirm bluez-utils
1213
;;
14+
apk)
15+
"$ESCALATION_TOOL" "$PACKAGER" add bluez
16+
;;
1317
*)
1418
"$ESCALATION_TOOL" "$PACKAGER" install -y bluez
1519
;;
@@ -18,15 +22,7 @@ setupBluetooth() {
1822
printf "%b\n" "${GREEN}Bluez is already installed.${RC}"
1923
fi
2024

21-
# Check if bluetooth service is running
22-
if ! systemctl is-active --quiet bluetooth; then
23-
printf "%b\n" "${YELLOW}Bluetooth service is not running. Starting it now...${RC}"
24-
"$ESCALATION_TOOL" systemctl start bluetooth
25-
26-
if systemctl is-active --quiet bluetooth; then
27-
printf "%b\n" "${GREEN}Bluetooth service started successfully.${RC}"
28-
fi
29-
fi
25+
startService bluetooth
3026
}
3127

3228
# Function to display the main menu

core/tabs/utils/numlock.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/sh -e
22

33
. ../common-script.sh
4+
. ../common-service-script.sh
45

56
# setleds can be used in all distros
67
# This method works by calling a script using systemd service
@@ -40,6 +41,11 @@ EOF
4041

4142
numlockSetup() {
4243
# Check if the script and service files exists
44+
if [ "$PACKAGER" = "apk" ]; then
45+
printf "%b\n" "${RED}Unsupported package manager.${RC}"
46+
exit 1
47+
fi
48+
4349
if [ ! -f "/usr/local/bin/numlock" ]; then
4450
create_file
4551
fi
@@ -51,10 +57,10 @@ numlockSetup() {
5157
printf "%b" "Do you want to enable Numlock on boot? (y/N): "
5258
read -r confirm
5359
if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
54-
"$ESCALATION_TOOL" systemctl enable numlock.service --quiet
60+
enableService numlock
5561
printf "%b\n" "Numlock will be enabled on boot"
5662
else
57-
"$ESCALATION_TOOL" systemctl disable numlock.service --quiet
63+
disableService numlock
5864
printf "%b\n" "Numlock will not be enabled on boot"
5965
fi
6066
}

core/tabs/utils/ollama.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ installollama() {
1111
else
1212
printf "%b\n" "${YELLOW}Installing ollama...${RC}"
1313
curl -fsSL https://ollama.com/install.sh | sh
14-
"$ESCALATION_TOOL" systemctl start ollama
14+
startService ollama
1515
fi
1616
}
1717

core/tabs/utils/samba-ssh-setup.sh

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,13 @@ setup_ssh() {
4747
;;
4848
esac
4949

50-
# Enable and start the appropriate SSH service
51-
"$ESCALATION_TOOL" systemctl enable "$SSH_SERVICE"
52-
"$ESCALATION_TOOL" systemctl start "$SSH_SERVICE"
50+
startAndEnableService "$SSH_SERVICE"
5351

54-
# Get the local IP address
5552
LOCAL_IP=$(ip -4 addr show | awk '/inet / {print $2}' | tail -n 1)
5653

5754
printf "%b\n" "${GREEN}Your local IP address is: $LOCAL_IP${RC}"
5855

59-
# Check if SSH is running
60-
if systemctl is-active --quiet "$SSH_SERVICE"; then
56+
if isServiceActive "$SSH_SERVICE"; then
6157
printf "%b\n" "${GREEN}SSH is up and running.${RC}"
6258
else
6359
printf "%b\n" "${RED}Failed to start SSH.${RC}"
@@ -137,12 +133,11 @@ setup_samba() {
137133
EOL
138134
fi
139135

140-
# Enable and start Samba services
141-
"$ESCALATION_TOOL" systemctl enable smb nmb
142-
"$ESCALATION_TOOL" systemctl start smb nmb
136+
for service in smb nmb; do
137+
startAndEnableService "$service"
138+
done
143139

144-
# Check if Samba is running
145-
if systemctl is-active --quiet smb && systemctl is-active --quiet nmb; then
140+
if isServiceActive smb && isServiceActive nmb; then
146141
printf "%b\n" "${GREEN}Samba is up and running.${RC}"
147142
printf "%b\n" "${YELLOW}Samba share available at: $SHARED_DIR${RC}"
148143
else

core/tabs/utils/timeshift.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ install_timeshift() {
1616
"$ESCALATION_TOOL" "${PACKAGER}" install -y timeshift
1717
;;
1818
*)
19-
printf "%b\n" "${RED}Unsupported pacakge manager.${RC}"
19+
printf "%b\n" "${RED}Unsupported package manager.${RC}"
20+
;;
2021
esac
2122
else
2223
printf "%b\n" "${GREEN}Timeshift is already installed.${RC}"

core/tabs/utils/wifi-control.sh

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/sh -e
22

33
. ../common-script.sh
4+
. ../common-service-script.sh
45

56
# Function to check if NetworkManager is installed
67
setupNetworkManager() {
@@ -25,13 +26,11 @@ setupNetworkManager() {
2526
fi
2627

2728
# Check if NetworkManager service is running
28-
if ! systemctl is-active --quiet NetworkManager; then
29+
if ! isServiceActive NetworkManager; then
2930
printf "%b\n" "${YELLOW}NetworkManager service is not running. Starting it now...${RC}"
30-
"$ESCALATION_TOOL" systemctl start NetworkManager
31-
32-
if systemctl is-active --quiet NetworkManager; then
33-
printf "%b\n" "${GREEN}NetworkManager service started successfully.${RC}"
34-
fi
31+
startService NetworkManager
32+
else
33+
printf "%b\n" "${GREEN}NetworkManager service started successfully.${RC}"
3534
fi
3635
}
3736

0 commit comments

Comments
 (0)