Skip to content

Commit

Permalink
Add common service script
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevithakannan2 committed Oct 13, 2024
1 parent 0f79e00 commit 45a880a
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 35 deletions.
8 changes: 3 additions & 5 deletions core/tabs/applications-setup/docker-setup.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/sh -e

. ../common-script.sh
. ../common-service-script.sh

# Function to prompt the user for installation choice
choose_installation() {
clear
printf "%b\n" "${YELLOW}Choose what to install:${RC}"
printf "%b\n" "1. ${YELLOW}Docker${RC}"
printf "%b\n" "2. ${YELLOW}Docker Compose${RC}"
Expand All @@ -28,13 +28,9 @@ install_docker() {
;;
zypper)
"$ESCALATION_TOOL" "$PACKAGER" --non-interactive install docker
"$ESCALATION_TOOL" systemctl enable docker
"$ESCALATION_TOOL" systemctl start docker
;;
pacman)
"$ESCALATION_TOOL" "$PACKAGER" -S --noconfirm docker
"$ESCALATION_TOOL" systemctl enable docker
"$ESCALATION_TOOL" systemctl start docker
;;
apk)
"$ESCALATION_TOOL" "$PACKAGER" add docker
Expand All @@ -44,6 +40,8 @@ install_docker() {
exit 1
;;
esac

startAndEnableService docker
}

install_docker_compose() {
Expand Down
85 changes: 85 additions & 0 deletions core/tabs/common-service-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/bin/sh -e

checkInitManager() {
for manager in $1; do
if command_exists "$manager"; then
INIT_MANAGER="$manager"
printf "%b\n" "${CYAN}Using ${manager} to interact with init system${RC}"
break
fi
done

if [ -z "$INIT_MANAGER" ]; then
printf "%b\n" "${RED}Can't find a supported init system${RC}"
exit 1
fi
}

startService() {
case "$INIT_MANAGER" in
systemctl)
"$ESCALATION_TOOL" "$INIT_MANAGER" start "$1"
;;
rc-service)
"$ESCALATION_TOOL" "$INIT_MANAGER" "$1" start
;;
esac
}

stopService() {
case "$INIT_MANAGER" in
systemctl)
"$ESCALATION_TOOL" "$INIT_MANAGER" stop "$1"
;;
rc-service)
"$ESCALATION_TOOL" "$INIT_MANAGER" "$1" stop
;;
esac
}

enableService() {
case "$INIT_MANAGER" in
systemctl)
"$ESCALATION_TOOL" "$INIT_MANAGER" enable "$1"
;;
rc-service)
"$ESCALATION_TOOL" rc-update add "$1"
;;
esac
}

disableService() {
case "$INIT_MANAGER" in
systemctl)
"$ESCALATION_TOOL" "$INIT_MANAGER" disable "$1"
;;
rc-service)
"$ESCALATION_TOOL" rc-update del "$1"
;;
esac
}

startAndEnableService() {
case "$INIT_MANAGER" in
systemctl)
"$ESCALATION_TOOL" "$INIT_MANAGER" enable --now "$1"
;;
rc-service)
enableService "$1"
startService "$1"
;;
esac
}

isServiceActive() {
case "$INIT_MANAGER" in
systemctl)
"$ESCALATION_TOOL" "$INIT_MANAGER" is-active --quiet "$1"
;;
rc-service)
"$ESCALATION_TOOL" "$INIT_MANAGER" "$1" status --quiet
;;
esac
}

checkInitManager 'systemctl rc-service'
14 changes: 5 additions & 9 deletions core/tabs/utils/bluetooth-control.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh -e

. ../common-script.sh
. ../common-service-script.sh

# Function to check Bluez is installed
setupBluetooth() {
Expand All @@ -10,6 +11,9 @@ setupBluetooth() {
pacman)
"$ESCALATION_TOOL" "$PACKAGER" -S --noconfirm bluez-utils
;;
apk)
"$ESCALATION_TOOL" "$PACKAGER" add bluez
;;
*)
"$ESCALATION_TOOL" "$PACKAGER" install -y bluez
;;
Expand All @@ -18,15 +22,7 @@ setupBluetooth() {
printf "%b\n" "${GREEN}Bluez is already installed.${RC}"
fi

# Check if bluetooth service is running
if ! systemctl is-active --quiet bluetooth; then
printf "%b\n" "${YELLOW}Bluetooth service is not running. Starting it now...${RC}"
"$ESCALATION_TOOL" systemctl start bluetooth

if systemctl is-active --quiet bluetooth; then
printf "%b\n" "${GREEN}Bluetooth service started successfully.${RC}"
fi
fi
startService bluetooth
}

# Function to display the main menu
Expand Down
10 changes: 8 additions & 2 deletions core/tabs/utils/numlock.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh -e

. ../common-script.sh
. ../common-service-script.sh

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

numlockSetup() {
# Check if the script and service files exists
if [ "$PACKAGER" = "apk" ]; then
printf "%b\n" "${RED}Unsupported package manager.${RC}"
exit 1
fi

if [ ! -f "/usr/local/bin/numlock" ]; then
create_file
fi
Expand All @@ -51,10 +57,10 @@ numlockSetup() {
printf "%b" "Do you want to enable Numlock on boot? (y/N): "
read -r confirm
if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
"$ESCALATION_TOOL" systemctl enable numlock.service --quiet
enableService numlock
printf "%b\n" "Numlock will be enabled on boot"
else
"$ESCALATION_TOOL" systemctl disable numlock.service --quiet
disableService numlock
printf "%b\n" "Numlock will not be enabled on boot"
fi
}
Expand Down
2 changes: 1 addition & 1 deletion core/tabs/utils/ollama.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ installollama() {
else
printf "%b\n" "${YELLOW}Installing ollama...${RC}"
curl -fsSL https://ollama.com/install.sh | sh
"$ESCALATION_TOOL" systemctl start ollama
startService ollama
fi
}

Expand Down
17 changes: 6 additions & 11 deletions core/tabs/utils/samba-ssh-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,13 @@ setup_ssh() {
;;
esac

# Enable and start the appropriate SSH service
"$ESCALATION_TOOL" systemctl enable "$SSH_SERVICE"
"$ESCALATION_TOOL" systemctl start "$SSH_SERVICE"
startAndEnableService "$SSH_SERVICE"

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

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

# Check if SSH is running
if systemctl is-active --quiet "$SSH_SERVICE"; then
if isServiceActive "$SSH_SERVICE"; then
printf "%b\n" "${GREEN}SSH is up and running.${RC}"
else
printf "%b\n" "${RED}Failed to start SSH.${RC}"
Expand Down Expand Up @@ -137,12 +133,11 @@ setup_samba() {
EOL
fi

# Enable and start Samba services
"$ESCALATION_TOOL" systemctl enable smb nmb
"$ESCALATION_TOOL" systemctl start smb nmb
for service in smb nmb; do
startAndEnableService "$service"
done

# Check if Samba is running
if systemctl is-active --quiet smb && systemctl is-active --quiet nmb; then
if isServiceActive smb && isServiceActive nmb; then
printf "%b\n" "${GREEN}Samba is up and running.${RC}"
printf "%b\n" "${YELLOW}Samba share available at: $SHARED_DIR${RC}"
else
Expand Down
3 changes: 2 additions & 1 deletion core/tabs/utils/timeshift.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ install_timeshift() {
"$ESCALATION_TOOL" "${PACKAGER}" install -y timeshift
;;
*)
printf "%b\n" "${RED}Unsupported pacakge manager.${RC}"
printf "%b\n" "${RED}Unsupported package manager.${RC}"
;;
esac
else
printf "%b\n" "${GREEN}Timeshift is already installed.${RC}"
Expand Down
11 changes: 5 additions & 6 deletions core/tabs/utils/wifi-control.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh -e

. ../common-script.sh
. ../common-service-script.sh

# Function to check if NetworkManager is installed
setupNetworkManager() {
Expand All @@ -25,13 +26,11 @@ setupNetworkManager() {
fi

# Check if NetworkManager service is running
if ! systemctl is-active --quiet NetworkManager; then
if ! isServiceActive NetworkManager; then
printf "%b\n" "${YELLOW}NetworkManager service is not running. Starting it now...${RC}"
"$ESCALATION_TOOL" systemctl start NetworkManager

if systemctl is-active --quiet NetworkManager; then
printf "%b\n" "${GREEN}NetworkManager service started successfully.${RC}"
fi
startService NetworkManager
else
printf "%b\n" "${GREEN}NetworkManager service started successfully.${RC}"
fi
}

Expand Down

0 comments on commit 45a880a

Please sign in to comment.