Files
nodejs-website/run.sh

149 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
# 2022-09-14 Hyperling
# Ensure dependencies are met and start the webserver.
## Setup ##
DIR="$(dirname -- "${BASH_SOURCE[0]}")"
PROG="$(basename -- "${BASH_SOURCE[0]}")"
# Ensure we are executing from this file's directory.
cd $DIR
DIR="`pwd`"
NAME="'$DIR/$PROG'"
echo $NAME
## Functions ##
function usage {
cat <<- EOF
$PROG calls the main Node.js program after ensuring the project can run.
(PORTS)
-p : Pass the port numbers that the API/website should listen at.
Example: $PROG -p 80 -p 443 -p 8080
(HELP)
-h : Show this usage output and exit successfully.
EOF
exit $1
}
function log {
message="$1"
echo -e "`date` - $message"
}
log "Local process information:"
ps $$
function check_main {
if [[ -z "$1" ]]; then
echo "ERROR: Subprocess name was not provided. $1"
exit 0
fi
log "Subprocess '$1' checking if main process is still running..."
ps $$ >/dev/null
status=$?
if [[ $status != 0 ]]; then
log "Process '$$' not found, '$1' from '$DIR/$PROG' exiting."
exit 0
fi
}
## Parameters ##
while getopts ':p:h' opt; do
case "$opt" in
p) (( OPTARG < 1024 )) && [[ $LOGNAME != "root" ]] && {
echo "WARNING: Port $OPTARG is privileged. Will need to be root."
exit 1
}
ports="$ports $OPTARG" ;;
h) usage 0 ;;
*) echo "ERROR: Option $OPTARG not recognized." >&2
usage 1 ;;
esac
done
if [[ -z $ports ]]; then
ports=8080
fi
## Build Environment ##
sudo=""
if [[ -z $LOGNAME ]]; then
LOGNAME="`whoami`"
fi
if [[ $LOGNAME != "root" ]]; then
log "Using sudo since user is '$LOGNAME'."
sudo="sudo"
fi
log "Check if any system dependencies need installed."
progs=""
if [[ ! `which php` ]]; then
echo "- Installing PHP"
progs="$progs php-cli"
fi
if [[ ! `which node` ]]; then
echo "- Installing Node"
progs="$progs nodejs"
fi
if [[ ! `which npm` ]]; then
echo "- Installing NPM"
progs="$progs npm"
fi
if [[ ! `which curl` ]]; then
echo "- Installing Curl"
progs="$progs curl"
fi
if [[ ! `which ps` ]]; then
echo "- Installing PS"
progs="$progs procps"
fi
if [[ -n "$progs" ]]; then
$sudo apt-get install -y $progs
fi
# Directories and allowed page types are executable, others are not.
log "Fix any strange file permissions."
find ./pages/ | while read file; do
if [[ $file == *".php" || $file == *".sh" || -d $file ]]; then
mode=755
else
mode=644
fi
chmod -c $mode $file
done
log "Check if any node modules need updated/installed."
npm install
# Reset generated index files.
log "Removing old index files."
find files/photos/ -name "*".html -print -delete
{
check_main photos
$DIR/check_photos.sh "$ports"
} &
## Main ##
log "Start check_git."
while true; do
check_main check_git
$DIR/check_git.sh
sleep 30
done &
log "Start website API."
node ./main.js $ports
status=$?
## Finish ##
log "Kill spawned processes."
pkill -eP $$
log "Exiting with status '$status'."
exit $status