Files

135 lines
4.4 KiB
Bash
Executable File

#!/bin/sh
# Self-update from remote source
SELF_URL="https://gitea.qortal.link/crowetic/qortal-DevNet-scripts/raw/branch/main/start-with-validation.sh"
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
TMP_DIR="${SCRIPT_DIR}/tmp"
mkdir -p "$TMP_DIR"
cleanup_tmp() {
rm -rf "$TMP_DIR"
}
trap cleanup_tmp EXIT
SELF_PATH="$0"
TARGET_NAME="start.sh"
if [ "$(basename -- "$SELF_PATH")" != "$TARGET_NAME" ]; then
SELF_PATH="${SCRIPT_DIR}/${TARGET_NAME}"
fi
TMP_SELF="${TMP_DIR}/start-with-validation.$$"
if command -v wget > /dev/null 2>&1; then
wget -qO "$TMP_SELF" "$SELF_URL"
elif command -v curl > /dev/null 2>&1; then
curl -s -o "$TMP_SELF" "$SELF_URL"
else
TMP_SELF=""
fi
if [ -n "$TMP_SELF" ] && [ -f "$TMP_SELF" ]; then
if [ ! -f "$SELF_PATH" ] || ! cmp -s "$TMP_SELF" "$SELF_PATH"; then
echo "Updating start.sh..."
cp "$TMP_SELF" "$SELF_PATH"
chmod +x "$SELF_PATH"
rm -f "$TMP_SELF"
cleanup_tmp
exec "$SELF_PATH" "$@"
fi
rm -f "$TMP_SELF"
fi
# There's no need to run as root, so don't allow it, for security reasons
if [ "$USER" = "root" ]; then
echo "Please su to non-root user before running"
exit
fi
# Validate Java is installed and the minimum version is available
MIN_JAVA_VER='11'
if command -v java > /dev/null 2>&1; then
# Example: openjdk version "11.0.6" 2020-01-14
version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}' | cut -d'.' -f1,2)
if echo "${version}" "${MIN_JAVA_VER}" | awk '{ if ($2 > 0 && $1 >= $2) exit 0; else exit 1}'; then
echo 'Passed Java version check'
else
echo "Please upgrade your Java to version ${MIN_JAVA_VER} or greater"
exit 1
fi
else
echo "Java is not available, please install Java ${MIN_JAVA_VER} or greater"
exit 1
fi
LOCAL_SETTINGS="settings.json"
if [ -f "$LOCAL_SETTINGS" ]; then
if command -v jq > /dev/null 2>&1; then
if ! jq -e . "$LOCAL_SETTINGS" > /dev/null 2>&1; then
echo "Warning: settings.json is not valid JSON; skipping fixedNetwork removal."
else
has_fixed=$(jq -r 'has("fixedNetwork")' "$LOCAL_SETTINGS" 2>/dev/null || echo "false")
if [ "$has_fixed" = "true" ]; then
TMP_SETTINGS="${TMP_DIR}/settings.json.tmp"
if jq 'del(.fixedNetwork)' "$LOCAL_SETTINGS" > "$TMP_SETTINGS"; then
mv "$TMP_SETTINGS" "$LOCAL_SETTINGS"
echo "Removed fixedNetwork from settings.json."
else
rm -f "$TMP_SETTINGS"
echo "Warning: failed to remove fixedNetwork from settings.json."
fi
fi
fi
elif command -v python3 > /dev/null 2>&1; then
LOCAL_SETTINGS_PATH="$LOCAL_SETTINGS" python3 - <<'PY'
import json
import os
import sys
settings_path = os.environ.get("LOCAL_SETTINGS_PATH")
try:
with open(settings_path, "r", encoding="utf-8") as fh:
data = json.load(fh)
except Exception:
print("Warning: settings.json is not valid JSON; skipping fixedNetwork removal.")
sys.exit(0)
if "fixedNetwork" not in data:
sys.exit(0)
data.pop("fixedNetwork", None)
with open(settings_path, "w", encoding="utf-8") as fh:
json.dump(data, fh, indent=2)
fh.write("\n")
print("Removed fixedNetwork from settings.json.")
PY
else
echo "Warning: jq/python3 not found; skipping fixedNetwork removal."
fi
fi
# No qortal.jar but we have a Maven built one?
# Be helpful and copy across to correct location
if [ ! -e qortal.jar -a -f target/qortal*.jar ]; then
echo "Copying Maven-built Qortal JAR to correct pathname"
cp target/qortal*.jar qortal.jar
fi
# Limits Java JVM stack size and maximum heap usage.
# Comment out for bigger systems, e.g. non-routers
# or when API documentation is enabled
# Uncomment (remove '#' sign) line below if your system has less than 12GB of RAM for optimal RAM defaults
JVM_MEMORY_ARGS="-XX:MaxRAMPercentage=80 -Xss5m -XX:+UseSerialGC"
# Although java.net.preferIPv4Stack is supposed to be false
# by default in Java 11, on some platforms (e.g. FreeBSD 12),
# it is overridden to be true by default. Hence we explicitly
# set it to false to obtain desired behaviour.
nohup nice -n 20 java \
-Djava.net.preferIPv4Stack=false \
${JVM_MEMORY_ARGS} \
-jar qortal.jar \
1>run.log 2>&1 &
# Save backgrounded process's PID
echo $! > run.pid
echo qortal running as pid $!