Add update functionality for helper scripts

This commit is contained in:
Jiří Štefka 2023-02-25 20:52:02 +01:00
parent 7284efa542
commit 5654555ee9
3 changed files with 92 additions and 47 deletions

@ -1,3 +1,3 @@
#!/bin/bash
CURRENT_SCRIPTS_VERSION="v1.0.3"
EXTRA_SCRIPTS_VERSION="v1.0.3"

@ -41,7 +41,7 @@ mem="6000M"
mc_launchoptions="-nogui"
# If you want to get rid of script updates notifications, set the below to false
check_for_script_updates=false
check_for_script_updates=true
# The options below are for advanced users, modification is not needed in most cases
# ----------------------------------------------------------------------------------

@ -202,6 +202,38 @@ function launch_server {
java $java_launchoptions -jar "$(basename ./paper-*.jar)" $mc_launchoptions
}
# Helper scripts update
function helper_scripts_update {
# Download matching version of helper scripts
echo "Updating helper scripts..."
# Download the file into ms-manager.tar.gz
curl -LJ -w '%{http_code}\n' "https://github.com/$REPO_OWNER/$REPO_NAME/releases/download/$CURRENT_SCRIPT_VERSION/ms-man-helper.tar.gz" > ms-man-helper.tar.gz
# Check if the download was successful by checking the last line of the file for 200
if [[ $(cat ma-man.tar.gz | tail -n 1) == 200 ]]; then
# Remove the last line of the file
sed -i '$d' ms-man-helper.tar.gz
# Extract the files from ms-man-helper.tar.gz
tar -xzf ms-man-helper.tar.gz
# Remove the old script
echo "Removing old helper scripts..."
rm -rf .ms-manager
echo "Removed old script"
echo "Moving new helper scripts into place..."
mv ms-manager .ms-manager
echo "Removing temporary files..."
rm ms-manager-helper.tar.gz
echo "Helper scripts updated successfully."
echo
echo
else
echo "Failed to update helper scripts."
rm -rf ms-manager
rm ms-manager-helper.tar.gz
exit 5
fi
}
# Download the update for the script
function self_update {
# Download the latest version of the script
@ -222,16 +254,6 @@ function self_update {
echo "Renamed new script"
echo "Script updated successfully."
echo
read -p "Do you want to restart the script? [y/N] " restart
if [ "$restart" == "y" ] || [ "$restart" == "Y" ]; then
echo "Restarting..."
# Execute the new script
exec "./start.sh" "$@"
exit 0
else
echo "Exiting..."
exit 0
fi
else
echo "Failed to update script."
rm start_new.sh
@ -239,16 +261,35 @@ function self_update {
fi
}
# Function to update the script
function check_self_update {
# Send a request to the GitHub API to retrieve the latest release
# Check helper scripts update
function check_helper_scripts {
if [[ $CURRENT_SCRIPT_VERSION != $EXTRA_SCRIPTS_VERSION ]]; then
echo "Helper script verion mismatch!"
echo "Main script version: $CURRENT_SCRIPT_VERSION"
echo "Helper script version: $EXTRA_SCRIPTS_VERSION"
echo
echo "The script will now download the same version of the helper scripts as the main script."
helper_scripts_update
fi
}
# Get latest script version
function get_latest_script_release {
response=$(curl -sL "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/latest")
if [[ $response =~ "API rate limit exceeded" ]]; then
echo "API limit exceeded. Will not check for updates."
LATEST_SCRIPT_VERSION=$CURRENT_SCRIPT_VERSION
else
# Extract the latest version from the response
LATEST_VERSION=$(echo $response | jq -r ".tag_name")
LATEST_SCRIPT_VERSION=$(echo $response | jq -r ".tag_name")
fi
}
# Function to update the script
function check_self_update {
# Get the latest version of the script
get_latest_script_release
# Compare the current version with the latest version
if [[ "$CURRENT_SCRIPT_VERSION" != "$LATEST_VERSION" ]]; then
@ -273,6 +314,8 @@ function check_self_update {
read -t 15 -p "Do you want to update? [y/N] " update
if [ "$update" == "y" ] || [ "$update" == "Y" ]; then
self_update
CURRENT_VERSION=$LATEST_VERSION
check_helper_scripts
else
echo "Skipping update."
return
@ -281,7 +324,6 @@ function check_self_update {
echo
echo
fi
fi
}
# Load config
@ -383,10 +425,6 @@ function main {
# Load config
load_config
# Load the rest of the script
# Get the current server file, version and build
load_script
if [[ $check_for_script_updates == true ]]; then
# Check for script updates
check_self_update
@ -394,6 +432,13 @@ function main {
echo "Skipping script update check."
fi
# Check helper scripts version mismatch
check_helper_scripts
# Load the rest of the script
# Get the current server file, version and build
load_script
# Gets the installed server info
get_existing_server