Chad 8bf7981b90
Clean Log Warnings, Enhance Runnability (#25)
* Only check the variable if it is set, avoid a log error.

* Pipe errors to SDOUT in case that's why Docker is not showing NPM progress.

* Two messages seemed very similar. Differentiate them.

* Check and install each dependency separately in case a lighter version is already installed.

* Don't require sudo if running as root.

* Use node instead of relying on shebang.

* Allow doing "npm start".

* Remove redirects, did not help see progress.

* Add a note when sudo is used.

* Fix node interpretor location to be arbitrary.

* Woops, need to keep advisories if the value is not set to false.

* Remove warning about apt CLI. Allow npm start to do dependencies.

* `npm start` is not magic, still need to run the install.

* Have `npm start` execute the main run script rather than bypassing dependency checks.

* Use PHP-CLI, smaller install size and more appropriate since only running local scripts.
2024-04-03 20:02:58 -07:00

88 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
# 2022-09-14 Hyperling
# Ensure dependencies are met and start the webserver.
## Setup ##
DIR=`dirname $0`
PROG=`basename $0`
## 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
}
## 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
## Build Environment ##
# Ensure we are executing from this file's directory.
cd $DIR
sudo=""
if [[ $LOGNAME != "root" ]]; then
echo "`date` - Using sudo since user is '$LOGNAME'."
sudo="sudo"
fi
echo "`date` - Check if any system dependencies need installed."
if [[ ! `which php` ]]; then
echo "- Installing PHP"
$sudo apt-get install -y php-cli
fi
if [[ ! `which node` ]]; then
echo "- Installing Node"
$sudo apt-get install -y nodejs
fi
if [[ ! `which npm` ]]; then
echo "- Installing NPM"
$sudo apt-get install -y npm
fi
# Directories and allowed page types are executable, others are not.
echo "`date` - 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
echo "`date` - Check if any node modules need updated/installed."
npm install
## Main ##
echo "`date` - Start website API."
node ./main.js $ports
status=$?
## Finish ##
echo "`date` - Exiting with status '$status'."
exit $status