New version

- Fixed issue where removing and recreating pm2 processes resulted in pm2 ID changing after some time. Script now uses the process name even if the user configures it using process ID. - elroys specialty. (does require you to go through configuration again to avoid this issue)
- Fixed issue where running the script from within the bot folder resulted in profittrailer.jar being deleted. 
- script can be run without having to cd to the correct directory (i.e ./var/opt/linux-update.sh is fine)
- Script will self update now. (note that until this version is live youll be overwriting with an old script.
This commit is contained in:
SgtBatten 2019-02-09 15:51:27 +11:00 committed by taniman
parent 861f904be3
commit e5038c0d0f
1 changed files with 84 additions and 32 deletions

102
update.sh
View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
### Linux .jar Update Script for ProfitTrailer ### Linux .jar Update Script for ProfitTrailer
### LAST UPDATED 04 January 2019 ### LAST UPDATED 10 Feb 2019
### Place this script in the root folder where all your individual bot folders are and then execute it. ### Place this script in the root folder where all your individual bot folders are and then execute it.
### For simplicity each ProfitTrailer.jar file should be nested exactly one subfolder. ### For simplicity each ProfitTrailer.jar file should be nested exactly one subfolder.
@ -17,6 +17,10 @@
### execute using ./update.sh https://cdn.discordapp.com/exampleurlonly/ProfitTrailer-2.0.4-B1.jar.zip to download a beta version from a link ### execute using ./update.sh https://cdn.discordapp.com/exampleurlonly/ProfitTrailer-2.0.4-B1.jar.zip to download a beta version from a link
### the link must contain the "ProfitTrailer-2.0.4-B1.jar.zip" portion to work though the ".jar" and "-B#" can be missing. ### the link must contain the "ProfitTrailer-2.0.4-B1.jar.zip" portion to work though the ".jar" and "-B#" can be missing.
###Get the Directory and filename of the script###
DIR="$(dirname $(readlink -f $0))"
script="$(basename $0)"
### Clear screen and print header ### Clear screen and print header
clear clear
echo $(tput setaf 3) echo $(tput setaf 3)
@ -73,8 +77,11 @@ if ! [ -x "$(command -v curl)" ]; then
fi fi
fi fi
### Set loc variable to No, If user ends up running this script from within a Bot folder we wont delete their jar file ###
loc=N
### If no previous config can be found enter setup automatically ### ### If no previous config can be found enter setup automatically ###
if [ ! -f updatescript/name.txt ]; then if [ ! -f "$DIR"/updatescript/name.txt ]; then
skipsetup=N skipsetup=N
echo "$(tput setaf 2)No Previous Setup Found - Entering Setup $(tput sgr0)" echo "$(tput setaf 2)No Previous Setup Found - Entering Setup $(tput sgr0)"
echo echo
@ -82,7 +89,7 @@ else
echo Loading from saved setup... echo Loading from saved setup...
echo echo
### load arrary of app names/ID/pid saved previously ### ### load arrary of app names/ID/pid saved previously ###
mapfile -t name < updatescript/name.txt mapfile -t name < "$DIR"/updatescript/name.txt
for i in $(seq ${#name[*]}); do for i in $(seq ${#name[*]}); do
[[ ${name[$i-1]} = $name ]] [[ ${name[$i-1]} = $name ]]
done done
@ -91,7 +98,7 @@ else
PTinstances="$((${#name[@]}-1))" PTinstances="$((${#name[@]}-1))"
### load arrary of paths saved previously ### ### load arrary of paths saved previously ###
mapfile -t path < updatescript/path.txt mapfile -t path < "$DIR"/updatescript/path.txt
for ((i=1; i<=PTinstances; i++)); do for ((i=1; i<=PTinstances; i++)); do
[[ ${path[$i-1]} = $path ]] [[ ${path[$i-1]} = $path ]]
### check that the directories actually exist and an empty string is not provided ### ### check that the directories actually exist and an empty string is not provided ###
@ -120,6 +127,7 @@ if [[ "$skipsetup" == "n" ]] || [[ "$skipsetup" == "N" ]]; then
### Set proceed variable to No, causing loop unitil user confirms proceed later ### ### Set proceed variable to No, causing loop unitil user confirms proceed later ###
proceed=N proceed=N
echo $(tput setaf 3) echo $(tput setaf 3)
echo "##################################################" echo "##################################################"
echo " Setup" echo " Setup"
@ -142,24 +150,37 @@ if [[ "$skipsetup" == "n" ]] || [[ "$skipsetup" == "N" ]]; then
echo echo
### ask user for the name and path of each instance they wish to update ### ### ask user for the name and path of each instance they wish to update ###
for ((i=1; i<=$PTinstances; i++)); do for ((i=1; i<=$PTinstances; i++)); do
read -p "What is the PM2 App name/ id or pid for instance $i? " name[$i] ### use the lack of profittrailer.jar to loop through setup.
while [[ ! -f "${path[$i]}"/ProfitTrailer.jar ]]; do
read -p "What is the PM2 App name/ id for instance $i? " newname
chars="a-zA-Z0-9_\/-" chars="a-zA-Z0-9_\/-"
re="\/[$chars]+" re="\/[$chars]+"
execpath=$(pm2 info "${name[$i]}" | grep 'exec cwd'); execpath=$(pm2 info "$newname" | grep 'exec cwd');
if [[ $execpath =~ $re ]]; if [[ $execpath =~ $re ]]; then
then
path[$i]=$BASH_REMATCH path[$i]=$BASH_REMATCH
fi fi
### if user entered only a number (pm2 ID) store the app name instead. ###
### dunno why but if you alter the below echo and then structure it literally breaks the if statement ###
if [[ $newname =~ "^[0-9]+$" ]]
echo
then
### use grep, sed and cut to find the field the pm2 process name is in. Future PM2 updates might break this and need changing ###
rename=$(pm2 info "$newname" | grep 'name' | sed -n '1p' | cut -d ' ' -f 9-)
### remove any leading or trailing spaces or tabs ###
nowhitespace=$(echo "$rename" | xargs)
name[$i]=$nowhitespace
fi
### If the directory provided doesnt exist or nothing is entered, ask again ### ### If the directory provided doesnt exist or nothing is entered, ask again ###
while [[ ! -f "${path[$i]}"/ProfitTrailer.jar ]]; do
if [[ ! -d "${path[$i]}" ]] || [[ -z "${path[$i]}" ]]; then if [[ ! -d "${path[$i]}" ]] || [[ -z "${path[$i]}" ]]; then
echo "$(tput setaf 1)Path for instance $i does not exist. Try again... $(tput sgr0)" echo "$(tput setaf 1)Path for instance $i does not exist. Try again... $(tput sgr0)"
read -p "What is the Directory for ProfitTrailer instance #$i? " path[$i] ### If the directory does not contain the ProfitTrailer.jar file, confirm intentions ###
else elif [[ ! -f "${path[$i]}"/ProfitTrailer.jar ]]; then
### If the directory does not contain the ProfitTrailer.jar file, ask again ### read -p "$(tput setaf 1)Path for instance $i (${path[$i]}) does not contain ProfitTrailer.jar. Do you wish to use it anyway? (Y/N) $(tput sgr0)" empty
echo "$(tput setaf 1)Path for instance $i does not contain ProfitTrailer.jar. Try Again... $(tput sgr0)" if [[ "$empty" == "y" ]] || [[ "$empty" == "Y" ]] || [[ "$empty" == "yes" ]] || [[ "$empty" == "Yes" ]]; then
read -p "What is the Directory for ProfitTrailer instance #$i? " path[$i] break
fi
fi fi
done done
done done
@ -193,11 +214,11 @@ if [[ "$skipsetup" == "n" ]] || [[ "$skipsetup" == "N" ]]; then
done done
if [[ "$savetofile" == "Y" ]] || [[ "$savetofile" == "y" ]]; then if [[ "$savetofile" == "Y" ]] || [[ "$savetofile" == "y" ]]; then
### create the folder for the config is necessary ### ### create the folder for the config if necessary ###
mkdir -p updatescript mkdir -p "$DIR"/updatescript
### print each array to a file, one element per row ### ### print each array to a file, one element per row ###
printf "%s\n" "${name[@]}" > updatescript/name.txt printf "%s\n" "${name[@]}" > "$DIR"/updatescript/name.txt
printf "%s\n" "${path[@]}" > updatescript/path.txt printf "%s\n" "${path[@]}" > "$DIR"/updatescript/path.txt
echo $(tput setaf 2) echo $(tput setaf 2)
echo Configuration Saved echo Configuration Saved
echo $(tput sgr0) echo $(tput sgr0)
@ -251,18 +272,26 @@ if [[ ! $1 ]]; then
echo echo
echo "$(tput setaf 2) === Extracting download === $(tput sgr0)" echo "$(tput setaf 2) === Extracting download === $(tput sgr0)"
### unzip the jar file only from the zip. -q for quiet, -j to prevent extracting directories ### ### unzip the jar file only from the zip. -q for quiet, -j to prevent extracting directories ###
unzip -q -j ProfitTrailer-$version.zip '*jar' unzip -q -j ProfitTrailer-$version.zip '*jar' '*sh'
mv linux-update.sh "$DIR"/"$script"
### Stop each BOT and Copy ProfitTrailer.jar to each instance, then restart it ### ### Stop each BOT and Copy ProfitTrailer.jar to each instance, then restart it ###
for ((i=1; i<=PTinstances; i++)); do for ((i=1; i<=PTinstances; i++)); do
### If the user is running this script inside their bots folder we avoid deleting the jar during cleanup ###
if [[ "$DIR" == "${path[$i]}" ]]; then
loc=Y
fi
echo echo
echo "$(tput setaf 2) === Stopping ${name[$i]} === $(tput sgr0)" echo "$(tput setaf 2) === Stopping ${name[$i]} === $(tput sgr0)"
pm2 stop "${name[$i]}" pm2 stop "${name[$i]}"
echo echo
echo "$(tput setaf 2) === Replacing jar file === $(tput sgr0)" echo "$(tput setaf 2) === Replacing jar file === $(tput sgr0)"
cp ProfitTrailer.jar "${path[$i]}" cp ProfitTrailer.jar "${path[$i]}"
mkdir -p updatescript/"$(date +%m%d_%H%M)"/"${name[$i]}" mkdir -p "$DIR"/updatescript/"$(date +%m%d_%H%M)"/"${name[$i]}"
cp "${path[$i]}"/data/ptdb.mv.db updatescript/"$(date +%m%d_%H%M)"/"${name[$i]}"/ptdb.mv.db cp "${path[$i]}"/data/ptdb.mv.db "$DIR"/updatescript/"$(date +%m%d_%H%M)"/"${name[$i]}"/ptdb.mv.db
echo echo
echo "$(tput setaf 2) === Restarting ${name[$i]} === $(tput sgr0)" echo "$(tput setaf 2) === Restarting ${name[$i]} === $(tput sgr0)"
pm2 reload "${name[$i]}" pm2 reload "${name[$i]}"
@ -271,7 +300,13 @@ if [[ ! $1 ]]; then
### Remove downloaded Files ### ### Remove downloaded Files ###
echo echo
echo "$(tput setaf 2) === Cleaning up === $(tput sgr0)" echo "$(tput setaf 2) === Cleaning up === $(tput sgr0)"
rm -rf ProfitTrailer-$version.zip ProfitTrailer.jar
if [[ "$loc" == "Y" ]]; then
rm -rf ProfitTrailer-$version*.zip
else
rm -rf ProfitTrailer-$version*.zip ProfitTrailer.jar
fi
echo $(tput setaf 3) echo $(tput setaf 3)
echo "##################################################" echo "##################################################"
echo " Finished updating to ProfitTrailer $version" echo " Finished updating to ProfitTrailer $version"
@ -310,18 +345,25 @@ if [ -n "${1}" ]; then
echo echo
echo "$(tput setaf 2) === Extracting download === $(tput sgr0)" echo "$(tput setaf 2) === Extracting download === $(tput sgr0)"
### unzip the jar file only from the zip. -q for quiet, -j to prevent extracting directories ### ### unzip the jar file only from the zip. -q for quiet, -j to prevent extracting directories ###
unzip -q -j ProfitTrailer-$version*.zip '*jar' unzip -q -j ProfitTrailer-$version.zip '*jar' '*sh'
mv linux-update.sh "$DIR"/"$script"
### Copy ProfitTrailer.jar to each instance ### ### Copy ProfitTrailer.jar to each instance ###
for ((i=1; i<=PTinstances; i++)); do for ((i=1; i<=PTinstances; i++)); do
### If the user is running this script inside their bots folder we avoid deleting the jar during cleanup ###
if [[ "$DIR" == "${path[$i]}" ]]; then
loc=Y
fi
echo echo
echo "$(tput setaf 2) === Stopping ${name[$i]} === $(tput sgr0)" echo "$(tput setaf 2) === Stopping ${name[$i]} === $(tput sgr0)"
pm2 stop "${name[$i]}" pm2 stop "${name[$i]}"
echo echo
echo "$(tput setaf 2) === Replacing jar file === $(tput sgr0)" echo "$(tput setaf 2) === Replacing jar file === $(tput sgr0)"
cp ProfitTrailer*.jar "${path[$i]}"/ProfitTrailer.jar cp ProfitTrailer*.jar "${path[$i]}"/ProfitTrailer.jar
mkdir -p updatescript/"$(date +%m%d_%H%M)"/"${name[$i]}" mkdir -p "$DIR"/updatescript/"$(date +%m%d_%H%M)"/"${name[$i]}"
cp "${path[$i]}"/data/ptdb.mv.db updatescript/"$(date +%m%d_%H%M)"/"${name[$i]}"/ptdb.mv.db cp "${path[$i]}"/data/ptdb.mv.db "$DIR"/updatescript/"$(date +%m%d_%H%M)"/"${name[$i]}"/ptdb.mv.db
echo echo
echo "$(tput setaf 2) === Restarting ${name[$i]} === $(tput sgr0)" echo "$(tput setaf 2) === Restarting ${name[$i]} === $(tput sgr0)"
pm2 reload "${name[$i]}" pm2 reload "${name[$i]}"
@ -330,7 +372,13 @@ if [ -n "${1}" ]; then
### Remove downloaded Files ### ### Remove downloaded Files ###
echo "$(tput setaf 2) === Cleaning up === $(tput sgr0)" echo "$(tput setaf 2) === Cleaning up === $(tput sgr0)"
if [[ "$loc" == "Y" ]]; then
rm -rf ProfitTrailer-$version*.zip
else
rm -rf ProfitTrailer-$version*.zip ProfitTrailer*.jar rm -rf ProfitTrailer-$version*.zip ProfitTrailer*.jar
fi
echo $(tput setaf 3) echo $(tput setaf 3)
echo "##################################################" echo "##################################################"
echo " Finished updating to ProfitTrailer $version" echo " Finished updating to ProfitTrailer $version"
@ -347,3 +395,7 @@ if [ -n "${1}" ]; then
exit exit
fi fi
fi fi
### Remove all but the 5 most recent backups ###
ls -dt "$DIR"/updatescript/*/ | tail -n +6 | xargs rm -r