From 45a880ae9a7ddb8e8ec6fe110663de349565b7b8 Mon Sep 17 00:00:00 2001 From: Jeevitha Kannan K S Date: Sun, 13 Oct 2024 13:09:18 +0530 Subject: [PATCH] Add common service script --- core/tabs/applications-setup/docker-setup.sh | 8 +- core/tabs/common-service-script.sh | 85 ++++++++++++++++++++ core/tabs/utils/bluetooth-control.sh | 14 ++-- core/tabs/utils/numlock.sh | 10 ++- core/tabs/utils/ollama.sh | 2 +- core/tabs/utils/samba-ssh-setup.sh | 17 ++-- core/tabs/utils/timeshift.sh | 3 +- core/tabs/utils/wifi-control.sh | 11 ++- 8 files changed, 115 insertions(+), 35 deletions(-) create mode 100644 core/tabs/common-service-script.sh diff --git a/core/tabs/applications-setup/docker-setup.sh b/core/tabs/applications-setup/docker-setup.sh index 5ddc2257..74aa4983 100755 --- a/core/tabs/applications-setup/docker-setup.sh +++ b/core/tabs/applications-setup/docker-setup.sh @@ -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}" @@ -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 @@ -44,6 +40,8 @@ install_docker() { exit 1 ;; esac + + startAndEnableService docker } install_docker_compose() { diff --git a/core/tabs/common-service-script.sh b/core/tabs/common-service-script.sh new file mode 100644 index 00000000..c3a2053d --- /dev/null +++ b/core/tabs/common-service-script.sh @@ -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' \ No newline at end of file diff --git a/core/tabs/utils/bluetooth-control.sh b/core/tabs/utils/bluetooth-control.sh index da7ee23d..6bfe266b 100644 --- a/core/tabs/utils/bluetooth-control.sh +++ b/core/tabs/utils/bluetooth-control.sh @@ -1,6 +1,7 @@ #!/bin/sh -e . ../common-script.sh +. ../common-service-script.sh # Function to check Bluez is installed setupBluetooth() { @@ -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 ;; @@ -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 diff --git a/core/tabs/utils/numlock.sh b/core/tabs/utils/numlock.sh index 742ceeb7..6500a327 100755 --- a/core/tabs/utils/numlock.sh +++ b/core/tabs/utils/numlock.sh @@ -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 @@ -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 @@ -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 } diff --git a/core/tabs/utils/ollama.sh b/core/tabs/utils/ollama.sh index 35453ba3..6c18374a 100644 --- a/core/tabs/utils/ollama.sh +++ b/core/tabs/utils/ollama.sh @@ -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 } diff --git a/core/tabs/utils/samba-ssh-setup.sh b/core/tabs/utils/samba-ssh-setup.sh index 07128105..76b5f48e 100755 --- a/core/tabs/utils/samba-ssh-setup.sh +++ b/core/tabs/utils/samba-ssh-setup.sh @@ -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}" @@ -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 diff --git a/core/tabs/utils/timeshift.sh b/core/tabs/utils/timeshift.sh index d78b1075..b38483e5 100644 --- a/core/tabs/utils/timeshift.sh +++ b/core/tabs/utils/timeshift.sh @@ -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}" diff --git a/core/tabs/utils/wifi-control.sh b/core/tabs/utils/wifi-control.sh index 856c8a0f..d4ed2d10 100755 --- a/core/tabs/utils/wifi-control.sh +++ b/core/tabs/utils/wifi-control.sh @@ -1,6 +1,7 @@ #!/bin/sh -e . ../common-script.sh +. ../common-service-script.sh # Function to check if NetworkManager is installed setupNetworkManager() { @@ -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 }