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
. ../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() {

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
. ../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

View File

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

View File

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

View File

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

View File

@ -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}"

View File

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