Add common service script

This commit is contained in:
Jeevitha Kannan K S 2024-10-13 13:09:18 +05:30
parent 0f79e00725
commit 45a880ae9a
No known key found for this signature in database
GPG Key ID: 5904C34A2F7CE333
8 changed files with 115 additions and 35 deletions

View File

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

View File

@ -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'

View File

@ -1,6 +1,7 @@
#!/bin/sh -e #!/bin/sh -e
. ../common-script.sh . ../common-script.sh
. ../common-service-script.sh
# Function to check Bluez is installed # Function to check Bluez is installed
setupBluetooth() { setupBluetooth() {
@ -10,6 +11,9 @@ setupBluetooth() {
pacman) pacman)
"$ESCALATION_TOOL" "$PACKAGER" -S --noconfirm bluez-utils "$ESCALATION_TOOL" "$PACKAGER" -S --noconfirm bluez-utils
;; ;;
apk)
"$ESCALATION_TOOL" "$PACKAGER" add bluez
;;
*) *)
"$ESCALATION_TOOL" "$PACKAGER" install -y bluez "$ESCALATION_TOOL" "$PACKAGER" install -y bluez
;; ;;
@ -18,15 +22,7 @@ setupBluetooth() {
printf "%b\n" "${GREEN}Bluez is already installed.${RC}" printf "%b\n" "${GREEN}Bluez is already installed.${RC}"
fi fi
# Check if bluetooth service is running startService bluetooth
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
} }
# Function to display the main menu # Function to display the main menu

View File

@ -1,6 +1,7 @@
#!/bin/sh -e #!/bin/sh -e
. ../common-script.sh . ../common-script.sh
. ../common-service-script.sh
# setleds can be used in all distros # setleds can be used in all distros
# This method works by calling a script using systemd service # This method works by calling a script using systemd service
@ -40,6 +41,11 @@ EOF
numlockSetup() { numlockSetup() {
# Check if the script and service files exists # 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 if [ ! -f "/usr/local/bin/numlock" ]; then
create_file create_file
fi fi
@ -51,10 +57,10 @@ numlockSetup() {
printf "%b" "Do you want to enable Numlock on boot? (y/N): " printf "%b" "Do you want to enable Numlock on boot? (y/N): "
read -r confirm read -r confirm
if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
"$ESCALATION_TOOL" systemctl enable numlock.service --quiet enableService numlock
printf "%b\n" "Numlock will be enabled on boot" printf "%b\n" "Numlock will be enabled on boot"
else else
"$ESCALATION_TOOL" systemctl disable numlock.service --quiet disableService numlock
printf "%b\n" "Numlock will not be enabled on boot" printf "%b\n" "Numlock will not be enabled on boot"
fi fi
} }

View File

@ -11,7 +11,7 @@ installollama() {
else else
printf "%b\n" "${YELLOW}Installing ollama...${RC}" printf "%b\n" "${YELLOW}Installing ollama...${RC}"
curl -fsSL https://ollama.com/install.sh | sh curl -fsSL https://ollama.com/install.sh | sh
"$ESCALATION_TOOL" systemctl start ollama startService ollama
fi fi
} }

View File

@ -47,17 +47,13 @@ setup_ssh() {
;; ;;
esac esac
# Enable and start the appropriate SSH service startAndEnableService "$SSH_SERVICE"
"$ESCALATION_TOOL" systemctl enable "$SSH_SERVICE"
"$ESCALATION_TOOL" systemctl start "$SSH_SERVICE"
# Get the local IP address
LOCAL_IP=$(ip -4 addr show | awk '/inet / {print $2}' | tail -n 1) 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}" printf "%b\n" "${GREEN}Your local IP address is: $LOCAL_IP${RC}"
# Check if SSH is running if isServiceActive "$SSH_SERVICE"; then
if systemctl is-active --quiet "$SSH_SERVICE"; then
printf "%b\n" "${GREEN}SSH is up and running.${RC}" printf "%b\n" "${GREEN}SSH is up and running.${RC}"
else else
printf "%b\n" "${RED}Failed to start SSH.${RC}" printf "%b\n" "${RED}Failed to start SSH.${RC}"
@ -137,12 +133,11 @@ setup_samba() {
EOL EOL
fi fi
# Enable and start Samba services for service in smb nmb; do
"$ESCALATION_TOOL" systemctl enable smb nmb startAndEnableService "$service"
"$ESCALATION_TOOL" systemctl start smb nmb done
# Check if Samba is running if isServiceActive smb && isServiceActive nmb; then
if systemctl is-active --quiet smb && systemctl is-active --quiet nmb; then
printf "%b\n" "${GREEN}Samba is up and running.${RC}" printf "%b\n" "${GREEN}Samba is up and running.${RC}"
printf "%b\n" "${YELLOW}Samba share available at: $SHARED_DIR${RC}" printf "%b\n" "${YELLOW}Samba share available at: $SHARED_DIR${RC}"
else else

View File

@ -16,7 +16,8 @@ install_timeshift() {
"$ESCALATION_TOOL" "${PACKAGER}" install -y timeshift "$ESCALATION_TOOL" "${PACKAGER}" install -y timeshift
;; ;;
*) *)
printf "%b\n" "${RED}Unsupported pacakge manager.${RC}" printf "%b\n" "${RED}Unsupported package manager.${RC}"
;;
esac esac
else else
printf "%b\n" "${GREEN}Timeshift is already installed.${RC}" printf "%b\n" "${GREEN}Timeshift is already installed.${RC}"

View File

@ -1,6 +1,7 @@
#!/bin/sh -e #!/bin/sh -e
. ../common-script.sh . ../common-script.sh
. ../common-service-script.sh
# Function to check if NetworkManager is installed # Function to check if NetworkManager is installed
setupNetworkManager() { setupNetworkManager() {
@ -25,14 +26,12 @@ setupNetworkManager() {
fi fi
# Check if NetworkManager service is running # 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}" printf "%b\n" "${YELLOW}NetworkManager service is not running. Starting it now...${RC}"
"$ESCALATION_TOOL" systemctl start NetworkManager startService NetworkManager
else
if systemctl is-active --quiet NetworkManager; then
printf "%b\n" "${GREEN}NetworkManager service started successfully.${RC}" printf "%b\n" "${GREEN}NetworkManager service started successfully.${RC}"
fi fi
fi
} }
# Function to display the main menu # Function to display the main menu