Advertisement
Guest User

Untitled

a guest
Mar 10th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.83 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. URLBASIC="https://plex.tv/api/downloads/1.json"
  4. URLPLEXPASS="https://plex.tv/api/downloads/1.json?channel=plexpass"
  5. DOWNLOADPATH="/tmp"
  6. LOGPATH="/tmp"
  7. LOGFILE="PMS_Updater.log"
  8. PMSPARENTPATH="/mnt/First/jails"
  9. PMSLIVEFOLDER="plex"
  10. PMSBAKFOLDER="plexmediaserver.bak"
  11. CERTFILE="/usr/local/share/certs/ca-root-nss.crt"
  12. AUTOUPDATE=0
  13. FORCEUPDATE=0
  14. VERBOSE=0
  15. REMOVE=0
  16. LOGGING=1
  17. PLEXPASS=1
  18.  
  19. # Initialize CURRENTVER to the script max so if reading the current version fails
  20. # for some reason we don't blindly clobber things
  21. CURRENTVER=9999.9999.9999.9999.9999
  22.  
  23.  
  24. usage()
  25. {
  26. cat << EOF
  27. usage: $0 options
  28.  
  29. This script will search the plex.tv download site for a download link
  30. and if it is newer than the currently installed version the script will
  31. download and optionaly install the new version.
  32.  
  33. OPTIONS:
  34. -u PlexPass username
  35. If -u is specified without -p then the script will
  36. prompt the user to enter the password when needed
  37. -p PlexPass password
  38. -c PlexPass user/password file
  39. When wget is run with username and password on the
  40. command line, that information is displayed in the
  41. process list for all to see. A more secure method
  42. is to create a file readable only by root that is
  43. formatted like this:
  44. user={Your Username Here}
  45. password={Your Password Here}
  46. -l Local file to install instead of latest from Plex.tv
  47. -d download folder (default /tmp) Ignored if -l is used
  48. -a Auto Update to newer version
  49. -f Force Update even if version is not newer
  50. -r Remove update packages older than current version
  51. Done before any update actions are taken.
  52. -v Verbose
  53. -n Use normal version (not PlexPass) version
  54. EOF
  55. }
  56.  
  57. ## LogMsg()
  58. ## READS: STDIN (Piped input) $1 (passed in string) $LOGPATH $LOGFILE
  59. ## MODIFIES: NONE
  60. ##
  61. ## Writes log entries to $LOGGINGPATH/$LOGGINGFILE
  62. LogMsg()
  63. {
  64. if [ "$1" = "-n" ]; then SWITCH="-n"; fi
  65. while read IN; do
  66. tdStamp=`date +"%Y-%m-%d %H:%M.%S"`
  67. if [ $LOGGING = 1 ]; then echo "$tdStamp $IN" >> $LOGPATH/$LOGFILE; fi
  68. if [ $VERBOSE = 1 ] || [ "$1" = "-f" ]; then echo $SWITCH $IN; fi
  69. done
  70. }
  71.  
  72.  
  73.  
  74.  
  75. ## verNum()
  76. ## READS: $1 (passed in string)
  77. ## MODIFIES: NONE
  78. ##
  79. ## Converts the Plex version string to a mathmatically comparable
  80. ## number by removing non numericals and padding each section with zeros
  81. ## so v0.9.9.10.485 becomes 00000009000900100485
  82. verNum()
  83. {
  84. echo "$@" | awk -F '[^0-9]+' '{ printf("%04d%04d%04d%04d%04d", $1,$2,$3,$4,$5)}'
  85. }
  86.  
  87.  
  88. ## removeOlder()
  89. ## READS: $DOWNLOADPATH $PMSPATTERN $CURRENTVER $VERBOSE $LOGGING
  90. ## MODIFIES: NONE
  91. ##
  92. ## Searches $DOWNLOADPATH for PMS install packages and removes versions older
  93. ## than $CURRENTVER
  94. removeOlder()
  95. {
  96. for FOUNDINSTALLFILE in `ls $DOWNLOADPATH/$PMSPATTERN`
  97. do {
  98. if [ $(verNum `basename $FOUNDINSTALLFILE`) -lt $(verNum $CURRENTVER) ]; then {
  99. echo Removing $FOUNDINSTALLFILE | LogMsg
  100. rm -f $FOUNDINSTALLFILE 2>&1 | LogMsg
  101. } fi
  102. } done
  103. }
  104.  
  105.  
  106. ## webGet()
  107. ## READS: $1 (URL) $DOWNLOADPATH $USERPASSFILE $USERNAME $PASSWORD $VERBOSE $LOGGING
  108. ## MODIFIES: NONE
  109. ##
  110. ## invoke wget with configured account info
  111. webGet()
  112. {
  113. local LOGININFO=""
  114. local QUIET="--quiet"
  115.  
  116. if [ $PLEXPASS = 1 ]; then
  117. if [ ! "x$USERPASSFILE" = "x" ] && [ -e $USERPASSFILE ]; then
  118. LOGININFO="--config=$USERPASSFILE"
  119. elif [ ! "x$USERNAME" = "x" ]; then
  120. if [ "x$PASSWORD" = "x" ]; then
  121. LOGININFO="--http-user=$USERNAME --ask-password"
  122. else
  123. LOGININFO="--http-user=$USERNAME --http-password=$PASSWORD"
  124. fi
  125. fi
  126. fi
  127.  
  128. if [ $VERBOSE = 1 ]; then QUIET=""; fi
  129. echo Downloading $1 | LogMsg
  130. wget $QUIET $LOGININFO --auth-no-challenge --ca-certificate=$CERTFILE --timestamping --directory-prefix="$DOWNLOADPATH" "$1"
  131. if [ $? -ne 0 ]; then
  132. echo Error downloading $1
  133. exit 1
  134. else
  135. echo Download Complete | LogMsg
  136. fi
  137. }
  138.  
  139.  
  140. ## findLatest()
  141. ## READS: $URLBASIC $URLPLEXPASS $DOWNLOADPATH $PMSPATTERN $VERBOSE $lOGGING
  142. ## MODIFIES: $DOWNLOADURL
  143. ##
  144. ## connects to the Plex.tv download site and scrapes for the latest download link
  145. findLatest()
  146. {
  147. if [ $PLEXPASS = 1 ]; then local URL=$URLPLEXPASS; else local URL=$URLBASIC; fi
  148. if [ $VERBOSE = 1 ]; then echo Using URL $URL; fi
  149. local SCRAPEFILE=`basename $URL`
  150.  
  151. webGet "$URL" || exit $?
  152. echo Searching $URL for the FreeBSD download URL ..... | LogMsg -n
  153. DOWNLOADURL=`cat $DOWNLOADPATH/$SCRAPEFILE | perl -MJSON::PP -E 'say decode_json(<STDIN>)->{computer}{FreeBSD}{releases}[0]{url}'`
  154. if [ "x$DOWNLOADURL" = "x" ]; then {
  155. # DOWNLOADURL is zero length, i.e. nothing matched PMSPATTERN. Error and exit
  156. echo Could not find a FreeBSD download link on page $URL | LogMsg -f
  157. exit 1
  158. } else {
  159. echo Done. | LogMsg -f
  160. } fi
  161. }
  162.  
  163.  
  164. ## applyUpdate()
  165. ## READS: $PMSPARENTPATH $PMSLIVEFOLDER $PMSBAKFOLDER $LOCALINSTALLFILE $VERBOSE $LOGGING
  166. ## MODIFIES: NONE
  167. ##
  168. ## Removes anything in the specified backup location, stops
  169. ## Plex, moves the current to backup, then tries to extract the new zip
  170. ## to the live location. If there is an error while unpacking the files
  171. ## are deleted and the backup is moved back. Plex is then started.
  172. ## It could be possible to check status after starting a new plex and
  173. ## rolling back if it does not start, should check that it is running
  174. ## properly before hand to avoid constantly trying to update a broken
  175. ## install
  176. applyUpdate()
  177. {
  178.  
  179. echo Removing previous PMS Backup ..... | LogMsg -n
  180. rm -rf $PMSPARENTPATH/$PMSBAKFOLDER 2>&1 | LogMsg
  181. echo Done. | LogMsg -f
  182. echo Stopping Plex Media Server .....| LogMsg -n
  183. service plexmediaserver stop 2>&1 | LogMsg
  184. echo Done. | LogMsg -f
  185. echo Moving current Plex Media Server to backup location .....| LogMsg -n
  186. mv $PMSPARENTPATH/$PMSLIVEFOLDER/ $PMSPARENTPATH/$PMSBAKFOLDER/ 2>&1 | LogMsg
  187. echo Done. | LogMsg -f
  188. echo Extracting $LOCALINSTALLFILE .....| LogMsg -n
  189. mkdir $PMSPARENTPATH/$PMSLIVEFOLDER/ 2>&1 | LogMsg
  190. tar -xj --strip-components 1 --file $LOCALINSTALLFILE --directory $PMSPARENTPATH/$PMSLIVEFOLDER/ 2>&1 | LogMsg -f
  191. if [ $? -ne 0 ]; then {
  192. echo Error exctracting $LOCALINSTALLFILE. Rolling back to previous version. | LogMsg -f
  193. rm -rf $PMSPARENTPATH/$PMSLIVEFOLDER/ 2>&1 | LogMsg -f
  194. mv $PMSPARENTPATH/$PMSBAKFOLDER/ $PMSPARENTPATH/$PMSLIVEFOLDER/ 2>&1 | LogMsg -f
  195. } else {
  196. echo Done. | LogMsg -f
  197. } fi
  198. ln -s $PMSPARENTPATH/$PMSLIVEFOLDER/Plex\ Media\ Server $PMSPARENTPATH/$PMSLIVEFOLDER/Plex_Media_Server 2>&1 | LogMsg
  199. ln -s $PMSPARENTPATH/$PMSLIVEFOLDER/libpython2.7.so.1 $PMSPARENTPATH/$PMSLIVEFOLDER/libpython2.7.so 2>&1 | LogMsg
  200. echo Starting Plex Media Server .....| LogMsg -n
  201. service plexmediaserver start
  202. echo Done. | LogMsg -f
  203. }
  204.  
  205. while getopts x."u:p:c:l:d:afvrn" OPTION
  206. do
  207. case $OPTION in
  208. u) USERNAME=$OPTARG ;;
  209. p) PASSWORD=$OPTARG ;;
  210. c) USERPASSFILE=$OPTARG ;;
  211. l) LOCALINSTALLFILE=$OPTARG ;;
  212. d) DOWNLOADPATH=$OPTARG ;;
  213. a) AUTOUPDATE=1 ;;
  214. f) FORCEUPDATE=1 ;;
  215. v) VERBOSE=1 ;;
  216. r) REMOVE=1 ;;
  217. n) PLEXPASS=0 ;;
  218. ?) usage; exit 1 ;;
  219. esac
  220. done
  221.  
  222. # Get the current version
  223. CURRENTVER=`export LD_LIBRARY_PATH=$PMSPARENTPATH/$PMSLIVEFOLDER; $PMSPARENTPATH/$PMSLIVEFOLDER/Plex\ Media\ Server --version`
  224. if [ $REMOVE = 1 ]; then removeOlder; fi
  225.  
  226. if [ "x$LOCALINSTALLFILE" = "x" ]; then {
  227. # No local source provided, check the web
  228. findLatest || exit $?
  229. if [ $FORCEUPDATE = 1 ] || [ $(verNum `basename $DOWNLOADURL`) -gt $(verNum $CURRENTVER) ]; then {
  230. webGet "$DOWNLOADURL" || exit $?
  231. LOCALINSTALLFILE="$DOWNLOADPATH/`basename $DOWNLOADURL`"
  232. } else {
  233. echo Already running latest version $CURRENTVER | LogMsg
  234. exit
  235. } fi
  236. } elif [ ! $FORCEUPDATE = 1 ] && [ $(verNum `basename $LOCALINSTALLFILE`) -le $(verNum $CURRENTVER) ]; then {
  237. echo Already running version $CURRENTVER | LogMsg
  238. echo Use -f to force install $LOCALINSTALLFILE | LogMsg
  239. exit
  240. } fi
  241.  
  242.  
  243. # If either update flag is set then verify archive integrity and install
  244. if [ $FORCEUPDATE = 1 ] || [ $AUTOUPDATE = 1 ]; then {
  245. echo Verifying $LOCALINSTALLFILE ..... | LogMsg -n
  246. bzip2 -t $LOCALINSTALLFILE
  247. if [ $? -ne 0 ]; then {
  248. echo $LOCALINSTALLFILE is not a valid archive, cannot update with this file. | LogMsg -f
  249. } else {
  250. echo Done | LogMsg -f
  251. applyUpdate
  252. } fi
  253. } fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement