115 lines
4.8 KiB
Bash
115 lines
4.8 KiB
Bash
#!/bin/sh
|
|
# Check if Git repo needs updating
|
|
if [ -d .git ]; then
|
|
echo "Pulling latest changes from the repository..."
|
|
git pull "https://${GIT_USERNAME}:${GIT_PAT}@${GIT_REPO_URL}" "${GIT_BRANCH}" || true
|
|
fi
|
|
|
|
# Constants
|
|
EXTENSIONS="yml json conf properties" # Space-separated list of extensions
|
|
ENV="production" # Change this to the environment you need
|
|
DRY_RUN="false" # Change to true if you want to simulate changes without modifying files
|
|
MAX_BACKUPS=3 # Maximum number of backup files to keep
|
|
# Load environment variables from .env.production file
|
|
ENV_FILE=".env.production"
|
|
if [ -f "$ENV_FILE" ]; then
|
|
echo "Loading environment variables from $ENV_FILE..."
|
|
# Read each line in .env.production
|
|
while IFS='=' read -r key value; do
|
|
# Skip empty lines and comments
|
|
if [ -z "$key" ] || [ "$(printf '%s' "$key" | cut -c1)" = "#" ]; then
|
|
continue
|
|
fi
|
|
# Remove potential spaces around the variable and export it
|
|
key=$(echo "$key" | xargs)
|
|
value=$(echo "$value" | xargs)
|
|
export "$key=$value"
|
|
done < "$ENV_FILE"
|
|
echo "Environment variables loaded successfully."
|
|
else
|
|
echo "Error: $ENV_FILE not found!"
|
|
exit 1
|
|
fi
|
|
# Function to escape special characters for sed
|
|
escape_sed_special_chars() {
|
|
echo "$1" | sed 's/[][\/.^$*]/\\&/g'
|
|
}
|
|
# Function to manage backups
|
|
manage_backups() {
|
|
local output_file="$1"
|
|
local timestamp
|
|
timestamp=$(date +"%Y%m%d_%H%M%S")
|
|
local backup_file="${output_file}_${timestamp}.bak"
|
|
|
|
# Create backup
|
|
cp "$output_file" "$backup_file"
|
|
echo "Backed up $output_file to $backup_file"
|
|
|
|
# Find and remove old backups, keeping only the most recent MAX_BACKUPS
|
|
find "$(dirname "$output_file")" -maxdepth 1 \
|
|
-name "$(basename "$output_file")_*.bak" | \
|
|
sort -r | \
|
|
tail -n +$((MAX_BACKUPS + 1)) | \
|
|
xargs -r rm
|
|
}
|
|
|
|
echo "Environment setup complete. Starting template processing..."
|
|
# Process template files for each extension
|
|
for EXT in $EXTENSIONS; do
|
|
echo "Processing files with extension .template.$EXT..."
|
|
|
|
# Find template files recursively
|
|
TEMPLATES=$(find . -type f -name "*.template.$EXT")
|
|
|
|
if [ -z "$TEMPLATES" ]; then
|
|
echo "No template files found for extension .template.$EXT"
|
|
continue
|
|
fi
|
|
|
|
echo "Found the following template files:"
|
|
echo "$TEMPLATES"
|
|
|
|
echo "$TEMPLATES" | while read -r TEMPLATE_FILE; do
|
|
# Remove .template from the end to generate output file
|
|
OUTPUT_FILE="${TEMPLATE_FILE%.template.$EXT}.$EXT"
|
|
echo "Processing $TEMPLATE_FILE into $OUTPUT_FILE..."
|
|
|
|
if [ "$DRY_RUN" = "true" ]; then
|
|
echo "Dry run: Would process $TEMPLATE_FILE into $OUTPUT_FILE"
|
|
continue
|
|
fi
|
|
|
|
# Create backup if output file already exists
|
|
if [ -f "$OUTPUT_FILE" ]; then
|
|
manage_backups "$OUTPUT_FILE"
|
|
fi
|
|
|
|
# Validate unresolved placeholders
|
|
UNRESOLVED=$(grep -o '\${[A-Za-z_][A-Za-z0-9_]*}' "$TEMPLATE_FILE" | sed 's/[${}]//g' | while read -r var; do
|
|
if [ -z "$(eval echo "\$$var")" ]; then
|
|
echo "$var"
|
|
fi
|
|
done)
|
|
if [ -n "$UNRESOLVED" ]; then
|
|
echo "Error: Unresolved variables detected in $TEMPLATE_FILE!"
|
|
echo "Ensure the following variables are defined in $ENV_FILE: $UNRESOLVED"
|
|
exit 1
|
|
fi
|
|
# Replace placeholders with environment variable values
|
|
sed_script=""
|
|
while IFS='=' read -r key value; do
|
|
if [ -n "$key" ] && [ "${key#\#}" = "$key" ]; then
|
|
escaped_key=$(escape_sed_special_chars "$key")
|
|
escaped_value=$(escape_sed_special_chars "$(eval echo "\$$key")")
|
|
sed_script="${sed_script}s|\${${escaped_key}}|${escaped_value}|g;"
|
|
fi
|
|
done < "$ENV_FILE"
|
|
# Generate the output file
|
|
sed -e "$sed_script" "$TEMPLATE_FILE" > "$OUTPUT_FILE"
|
|
echo "Processed $TEMPLATE_FILE into $OUTPUT_FILE"
|
|
done
|
|
done
|
|
echo "Template processing complete. Starting the server in 5 seconds..."
|
|
sleep 5
|
|
|
|
java -Xms${SERVER_MIN_MEMORY}M -Xmx${SERVER_MEMORY}M --add-modules=jdk.incubator.vector -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -jar ${SERVER_JARFILE} --nogui |