210 lines
7.1 KiB
Bash
210 lines
7.1 KiB
Bash
#!/bin/sh
|
|
|
|
# Self-update from remote source
|
|
SELF_URL="https://gitea.qortal.link/crowetic/qortal-DevNet-scripts/raw/branch/main/start-mac.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-mac.$$"
|
|
|
|
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-mac.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 [ "$(id -u)" -eq 0 ] || [ "$USER" = "root" ]; then
|
|
echo "Please su to non-root user before running"
|
|
exit 1
|
|
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
|
|
|
|
# Check if qortal.jar exists
|
|
if [ ! -e qortal.jar ]; then
|
|
echo "qortal.jar not found. Downloading latest version..."
|
|
# Download the latest qortal.jar
|
|
if command -v wget > /dev/null 2>&1; then
|
|
wget -qO qortal.jar https://cloud.qortal.org/s/QortalDevNetJarLatest/download/qortal.jar
|
|
elif command -v curl > /dev/null 2>&1; then
|
|
curl -s -o qortal.jar https://cloud.qortal.org/s/QortalDevNetJarLatest/download/qortal.jar
|
|
else
|
|
echo "Neither wget nor curl found. Please install one of them."
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
HASH_CMD=""
|
|
if command -v md5sum > /dev/null 2>&1; then
|
|
HASH_CMD="md5sum"
|
|
elif command -v md5 > /dev/null 2>&1; then
|
|
HASH_CMD="md5"
|
|
fi
|
|
|
|
if [ -n "$HASH_CMD" ]; then
|
|
HASH_FILE="${TMP_DIR}/hash.txt"
|
|
COMPARE_FILE="${TMP_DIR}/compare.txt"
|
|
# Download hash.txt for comparison
|
|
echo "Downloading hash.txt for validation..."
|
|
if command -v wget > /dev/null 2>&1; then
|
|
wget -qO "$HASH_FILE" https://cloud.qortal.org/s/QortalDevNetJarHashFile/download/hash.txt
|
|
elif command -v curl > /dev/null 2>&1; then
|
|
curl -s -o "$HASH_FILE" https://cloud.qortal.org/s/QortalDevNetJarHashFile/download/hash.txt
|
|
else
|
|
echo "Neither wget nor curl found. Please install one of them."
|
|
exit 1
|
|
fi
|
|
|
|
# Generate md5sum of local qortal.jar
|
|
echo "Generating MD5 checksum of local qortal.jar..."
|
|
if [ "$HASH_CMD" = "md5sum" ]; then
|
|
md5sum qortal.jar | awk '{print $1}' > "$COMPARE_FILE"
|
|
else
|
|
md5 -q qortal.jar > "$COMPARE_FILE"
|
|
fi
|
|
|
|
# Compare the hashes
|
|
echo "Validating qortal.jar file..."
|
|
if cmp -s "$HASH_FILE" "$COMPARE_FILE"; then
|
|
echo "qortal.jar is up to date!"
|
|
else
|
|
echo "qortal.jar is outdated. Downloading latest version..."
|
|
# Download the latest qortal.jar
|
|
if command -v wget > /dev/null 2>&1; then
|
|
wget -qO qortal.jar https://cloud.qortal.org/s/QortalDevNetJarLatest/download/qortal.jar
|
|
elif command -v curl > /dev/null 2>&1; then
|
|
curl -s -o qortal.jar https://cloud.qortal.org/s/QortalDevNetJarLatest/download/qortal.jar
|
|
else
|
|
echo "Neither wget nor curl found. Please install one of them."
|
|
exit 1
|
|
fi
|
|
echo "qortal.jar has been updated to the latest version."
|
|
fi
|
|
|
|
# Clean up temporary files
|
|
rm -f "$COMPARE_FILE" "$HASH_FILE"
|
|
else
|
|
echo "md5sum/md5 not found; skipping qortal.jar validation."
|
|
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 ] && [ -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.
|
|
if [ -f run.pid ] && command -v ps > /dev/null 2>&1; then
|
|
pid=$(cat run.pid 2>/dev/null || true)
|
|
if [ -n "$pid" ] && ps -p "$pid" -o args= 2>/dev/null | grep -q 'java .* -jar qortal\.jar'; then
|
|
echo "qortal appears to be running already (run.pid); skipping start."
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
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 $!
|