improve logic of checking status, opt to restart server instead of exit, add comments, improve checking deps

This commit is contained in:
Aidan 2024-12-19 22:44:33 -05:00
parent 353c0d35fb
commit 08bcb5b826
No known key found for this signature in database
GPG Key ID: 1773A01F0EFE4FC1

59
manage
View File

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
SC_VERSION=1.2.1 SC_VERSION=1.3.0
SC_CODENAME="bionic" SC_CODENAME="bionic"
PORT=5566 PORT=5566
SETUP_FILE=".setup_complete" SETUP_FILE=".setup_complete"
@ -11,11 +11,17 @@ COLOR_YELLOW="\033[1;33m"
COLOR_BLUE="\033[1;34m" COLOR_BLUE="\033[1;34m"
COLOR_CYAN="\033[1;36m" COLOR_CYAN="\033[1;36m"
function command_exists() {
command -v "$1" &> /dev/null
}
function install_tools() { function install_tools() {
if ! command -v node &> /dev/null 2>&1; then # Check for NodeJS, and install if not found
if ! command_exists node; then
echo -e "${COLOR_RED}NodeJS is not installed. Installing now...${COLOR_RESET}" echo -e "${COLOR_RED}NodeJS is not installed. Installing now...${COLOR_RESET}"
echo -e "${COLOR_YELLOW}You may need to enter your sudo password.${COLOR_RESET}\n" echo -e "${COLOR_YELLOW}You may need to enter your sudo password.${COLOR_RESET}\n"
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
# shellcheck disable=SC2155
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm install 20.18.0 nvm install 20.18.0
@ -24,7 +30,8 @@ function install_tools() {
echo -e "${COLOR_GREEN}NodeJS is installed. Version: $(node -v)${COLOR_RESET}" echo -e "${COLOR_GREEN}NodeJS is installed. Version: $(node -v)${COLOR_RESET}"
fi fi
if ! command -v nc &> /dev/null 2>&1; then # Check for Netcat, and install if not found
if ! command_exists nc; then
echo -e "${COLOR_RED}Netcat is not installed. Installing now...${COLOR_RESET}" echo -e "${COLOR_RED}Netcat is not installed. Installing now...${COLOR_RESET}"
echo -e "${COLOR_YELLOW}You may need to enter your sudo password.${COLOR_RESET}\n" echo -e "${COLOR_YELLOW}You may need to enter your sudo password.${COLOR_RESET}\n"
sudo apt-get install netcat-traditional sudo apt-get install netcat-traditional
@ -33,7 +40,8 @@ function install_tools() {
echo -e "${COLOR_GREEN}Netcat is installed.${COLOR_RESET}" echo -e "${COLOR_GREEN}Netcat is installed.${COLOR_RESET}"
fi fi
if ! command -v npm &> /dev/null 2>&1; then # Check for NPM, and install if not found
if ! command_exists npm; then
echo -e "${COLOR_RED}NPM is not installed. Installing now...${COLOR_RESET}" echo -e "${COLOR_RED}NPM is not installed. Installing now...${COLOR_RESET}"
echo -e "${COLOR_YELLOW}You may need to enter your sudo password.${COLOR_RESET}\n" echo -e "${COLOR_YELLOW}You may need to enter your sudo password.${COLOR_RESET}\n"
sudo apt-get update sudo apt-get update
@ -58,6 +66,7 @@ function install_tools() {
fi fi
} }
# Show help message
function show_help() { function show_help() {
echo -e "${COLOR_GREEN}manage version:${COLOR_RESET} ${COLOR_BLUE}${SC_VERSION} ${SC_CODENAME}${COLOR_RESET}" echo -e "${COLOR_GREEN}manage version:${COLOR_RESET} ${COLOR_BLUE}${SC_VERSION} ${SC_CODENAME}${COLOR_RESET}"
echo -e "${COLOR_BLUE}Usage:${COLOR_RESET} ./manage [command]\n" echo -e "${COLOR_BLUE}Usage:${COLOR_RESET} ./manage [command]\n"
@ -69,6 +78,7 @@ function show_help() {
echo -e " ${COLOR_CYAN}status, -s, --status${COLOR_RESET} Checks if the server is running.\n" echo -e " ${COLOR_CYAN}status, -s, --status${COLOR_RESET} Checks if the server is running.\n"
} }
# Check if essential directories exist (mainly for server startup/shutdown), and create them if they don't already exist
function check_dirs() { function check_dirs() {
local action=$1 local action=$1
local created_count=0 local created_count=0
@ -105,6 +115,7 @@ function check_dirs() {
fi fi
} }
# Handles restarting of server
function restart_server() { function restart_server() {
check_setup check_setup
@ -157,29 +168,38 @@ function restart_server() {
echo -e "${COLOR_BLUE}Starting server...${COLOR_RESET}" echo -e "${COLOR_BLUE}Starting server...${COLOR_RESET}"
npm start > node.log 2>&1 & npm start > node.log 2>&1 &
for i in {1..10}; do MAX_RETRIES=10
RETRY_INTERVAL=1
for ((i=1; i<=MAX_RETRIES; i++)); do
PIDS=$(lsof -t -i:$PORT) PIDS=$(lsof -t -i:$PORT)
if [ -n "$PIDS" ]; then if [ -n "$PIDS" ]; then
echo -e "${COLOR_GREEN}\nDone. Server up and running on port $PORT!\n${COLOR_RESET}" echo -e "${COLOR_GREEN}\nDone. Server up and running on port $PORT!\n${COLOR_RESET}"
break break
else else
echo -e "${COLOR_YELLOW}Waiting for server to start on port $PORT...${COLOR_RESET}" echo -e "${COLOR_YELLOW}Waiting for server to start on port $PORT... Attempt $i/${MAX_RETRIES}${COLOR_RESET}"
sleep 1 sleep $RETRY_INTERVAL
fi fi
done done
if [ -z "$PIDS" ]; then
echo -e "${COLOR_RED}Failed to start the server on port $PORT after $MAX_RETRIES attempts.${COLOR_RESET}"
fi
if [ -z "$PIDS" ]; then if [ -z "$PIDS" ]; then
echo -e "${COLOR_RED}Something went wrong... Check node.log for more details${COLOR_RESET} (no process on port)" echo -e "${COLOR_RED}Something went wrong... Check node.log for more details${COLOR_RESET} (no process on port)"
fi fi
} }
# Handles starting of server
function start_server() { function start_server() {
check_setup check_setup
PID=$(lsof -t -i:$PORT) PID=$(lsof -t -i:$PORT)
if [ -n "$PID" ]; then if [ -n "$PID" ]; then
echo -e "${COLOR_YELLOW}Server already running on port $PORT. Skipping server startup.${COLOR_RESET}" echo -e "${COLOR_GREEN}Server already running on port $PORT, opting to restart.\n${COLOR_RESET}"
restart_server
return return
fi fi
@ -215,22 +235,30 @@ function start_server() {
echo -e "${COLOR_BLUE}Starting server...${COLOR_RESET}" echo -e "${COLOR_BLUE}Starting server...${COLOR_RESET}"
npm start > node.log 2>&1 & npm start > node.log 2>&1 &
for i in {1..10}; do MAX_RETRIES=10
RETRY_INTERVAL=1
for ((i=1; i<=MAX_RETRIES; i++)); do
PIDS=$(lsof -t -i:$PORT) PIDS=$(lsof -t -i:$PORT)
if [ -n "$PIDS" ]; then if [ -n "$PIDS" ]; then
echo -e "${COLOR_GREEN}\nDone. Server up and running on port $PORT!\n${COLOR_RESET}" echo -e "${COLOR_GREEN}\nDone. Server up and running on port $PORT!\n${COLOR_RESET}"
break break
else else
echo -e "${COLOR_YELLOW}Waiting for server to start on port $PORT...${COLOR_RESET}" echo -e "${COLOR_YELLOW}Waiting for server to start on port $PORT... Attempt $i/${MAX_RETRIES}${COLOR_RESET}"
sleep 1 sleep $RETRY_INTERVAL
fi fi
done done
if [ -z "$PIDS" ]; then
echo -e "${COLOR_RED}Failed to start the server on port $PORT after $MAX_RETRIES attempts.${COLOR_RESET}"
fi
if [ -z "$PIDS" ]; then if [ -z "$PIDS" ]; then
echo -e "${COLOR_RED}Something went wrong... Check node.log for more details${COLOR_RESET} (no process on port)" echo -e "${COLOR_RED}Something went wrong... Check node.log for more details${COLOR_RESET} (no process on port)"
fi fi
} }
# Handles stopping of server
function stop_server() { function stop_server() {
check_setup check_setup
PID=$(lsof -t -i:$PORT) PID=$(lsof -t -i:$PORT)
@ -265,6 +293,7 @@ function stop_server() {
fi fi
} }
# Checks if server is running
function check_status() { function check_status() {
check_setup check_setup
PID=$(lsof -t -i:$PORT) PID=$(lsof -t -i:$PORT)
@ -276,6 +305,7 @@ function check_status() {
fi fi
} }
# Master setup function/process
function setup() { function setup() {
clear clear
echo -e "${COLOR_BLUE}Welcome to the aidxnFUN 'manage' script setup!\n${COLOR_RESET}${COLOR_CYAN}You are running manage ${SC_VERSION} ${SC_CODENAME}${COLOR_RESET}\n" echo -e "${COLOR_BLUE}Welcome to the aidxnFUN 'manage' script setup!\n${COLOR_RESET}${COLOR_CYAN}You are running manage ${SC_VERSION} ${SC_CODENAME}${COLOR_RESET}\n"
@ -291,15 +321,18 @@ function setup() {
read -n 1 -s -r -p "" read -n 1 -s -r -p ""
clear clear
# Execute install tool script
install_tools install_tools
# Install NPM dependencies
npm install npm install
echo -e "\n${COLOR_GREEN}Dependancy installation complete!${COLOR_RESET}" echo -e "\n${COLOR_GREEN}Dependency installation complete!${COLOR_RESET}"
echo -e "\n${COLOR_BLUE}If you would like to view the install logs, please do so, or click [ENTER] to continue to the next step.${COLOR_RESET}\n" echo -e "\n${COLOR_BLUE}If you would like to view the install logs, please do so, or click [ENTER] to continue to the next step.${COLOR_RESET}\n"
read -n 1 -s -r -p "" read -n 1 -s -r -p ""
clear clear
echo -e "${COLOR_GREEN}Now, take a look at the commands you can use with this script:${COLOR_RESET}\n" echo -e "${COLOR_GREEN}Now, take a look at the commands you can use with this script:${COLOR_RESET}\n"
# Show the user help message
show_help show_help
echo -e "\nPress any key to continue to the final step..." echo -e "\nPress any key to continue to the final step..."
read -n 1 -s -r -p "" read -n 1 -s -r -p ""
@ -317,11 +350,13 @@ function setup() {
} }
function check_setup() { function check_setup() {
# Check if setup file exists
if [ ! -f "$SETUP_FILE" ]; then if [ ! -f "$SETUP_FILE" ]; then
setup setup
fi fi
} }
# Handles user commands
case $1 in case $1 in
up) up)
check_setup check_setup