Advertisement
rockdrilla

factorio headless server w+ instant updates

Jul 17th, 2017
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.95 KB | None | 0 0
  1. #!/bin/sh
  2. ### [ -- v config here v -- ]
  3. INSTALL_ROOT='/home/factorio'
  4. BRANCH='experimental'
  5. URL_BASE='https://www.factorio.com'
  6. ### [ -- ^ config here ^ -- ]
  7.  
  8. ###
  9.  
  10. URL_WATCH=${URL_BASE}'/download-headless/'${BRANCH}
  11. REGEX_VERSION_URL='/get-download/([^/]+)/headless/linux64'
  12. REGEX_URL_HTML='href="('${REGEX_VERSION_URL}')"'
  13. PID_SRV=''
  14.  
  15. D=/dev/null
  16. readonly D
  17.  
  18. log() { echo "# $@"; }
  19.  
  20.  
  21. random() {
  22.   local n x
  23.   if [ $1 -lt 1 ]; then
  24.     echo 0
  25.     return
  26.   fi
  27.   if [ $1 -lt 255 ]; then
  28.     n=1
  29.   elif [ $1 -lt 65535 ]; then
  30.     n=2
  31.   else
  32.     n=4
  33.   fi
  34.   x='0x'$(dd if=/dev/urandom bs=$n count=1 2>$D | xxd -ps)
  35.   # x=$((x + 0))
  36.   echo $((x % $1))
  37. }
  38.  
  39. http_test() {
  40.   curl -kLI "$1" 2>$D | grep -Eqe '^HTTP/\S+ 2.. '
  41. }
  42.  
  43. http_get() {
  44.   curl -kL "$1" 2>$D
  45. }
  46.  
  47. get_relative_url_upstream() {
  48.   http_get "${URL_WATCH}" | grep -Eoe "${REGEX_URL_HTML}" | \
  49.   sed -re "s#${REGEX_URL_HTML}#\1#" | head -n 1
  50. }
  51.  
  52. get_version_local() {
  53.   "${INSTALL_ROOT}/bin/factorio" --version 2>&1 | \
  54.   sed -nre '/^Version:\s+(\S+)\s.*$/{s##\1#;p;}'
  55. }
  56.  
  57. get_version_upstream() {
  58.   get_relative_url_upstream | sed -re "s#${REGEX_VERSION_URL}#\1#"
  59. }
  60.  
  61. process_present() {
  62.   [ -n "$1" ] || return 1
  63.   /bin/ps -p "$1" 0<$D 1>$D 2>$D
  64. }
  65.  
  66. process_sig() {
  67.   /bin/kill -s "$2" "$1" 0<$D 1>$D 2>$D
  68. }
  69.  
  70. stop_server() {
  71.   [ -n "$1" ] || return
  72.  
  73.   if process_present "$1"; then
  74.     process_sig "$1" TERM
  75.     sleep 5
  76.     process_sig "$1" KILL
  77.   fi
  78. }
  79.  
  80. start_server() {
  81.   [ -n "${INSTALL_ROOT}" ] || return 1
  82.   [ -d "${INSTALL_ROOT}" ] || return 1
  83.  
  84.   cd "${INSTALL_ROOT}/"
  85.   "${INSTALL_ROOT}/bin/factorio" --verbose \
  86.     --bind 127.0.0.1:34197 \
  87.     --config ${INSTALL_ROOT}/cfg/server.ini \
  88.     --mod-directory ${INSTALL_ROOT}/var/mods \
  89.     --map-settings ${INSTALL_ROOT}/cfg/map-settings.json \
  90.     --start-server ${INSTALL_ROOT}/var/saves/my-awesome-server.zip \
  91.   1>&2 <$D &
  92.   echo $!
  93. }
  94.  
  95. ###
  96.  
  97. while :; do
  98.   log 'is upstream info is available?'
  99.   if http_test "${URL_WATCH}"; then
  100.     log 'OK, upstream info is available'
  101.  
  102.     VER_UPSTREAM=$(get_version_upstream)
  103.     VER_LOCAL=$(get_version_local)
  104.  
  105.     log 'local version: ' "${VER_LOCAL}"
  106.     log 'remote version:' "${VER_UPSTREAM}"
  107.  
  108.     if [ "${VER_LOCAL}" = "${VER_UPSTREAM}" ]; then
  109.       log "versions are equal, update isn't required"
  110.     else
  111.       log "versions are different, begin update"
  112.  
  113.       log 'is upstream tarball is available?'
  114.  
  115.       URL_TARBALL=${URL_BASE}$(get_relative_url_upstream)
  116.       if http_test "${URL_TARBALL}"; then
  117.         log 'OK, upstream tarball is available'
  118.  
  119.         DTMP=$(mktemp -d)
  120.         cd "${DTMP}/"
  121.  
  122.         log 'downloading tarball'
  123.  
  124.         http_get "${URL_TARBALL}" > "${VER_UPSTREAM}.t"
  125.         tar --strip-components=1 -xf "${VER_UPSTREAM}.t"
  126.  
  127.         log 'processing tarball'
  128.  
  129.         strip \
  130.           --remove-section=.comment \
  131.           --remove-section=.note \
  132.         bin/x64/factorio
  133.  
  134.         if [ -n "${PID_SRV}" ]; then
  135.           log 'stopping server'
  136.  
  137.           stop_server ${PID_SRV}; PID_SRV=''
  138.           sleep 5
  139.         fi
  140.  
  141.         log 'updating installation'
  142.  
  143.         cp bin/x64/factorio "${INSTALL_ROOT}/bin/factorio"
  144.         rm -rf "${INSTALL_ROOT}/data"
  145.         tar cf - data | (cd "${INSTALL_ROOT}/"; tar xf -;)
  146.  
  147.         log 'update is complete'
  148.  
  149.         cd /; rm -rf "${DTMP}/"
  150.       else
  151.         log 'FAIL, upstream tarball is NOT available'
  152.       fi
  153.     fi
  154.   else
  155.     log 'FAIL, upstream info is NOT available'
  156.   fi
  157.  
  158.   if [ -n "${PID_SRV}" ]; then
  159.     if ! process_present ${PID_SRV}; then
  160.       log 'server is gone away, try to restart'
  161.  
  162.       PID_SRV=''
  163.     fi
  164.   fi
  165.   if [ -z "${PID_SRV}" ]; then
  166.     log 'starting server'
  167.  
  168.     PID_SRV=$(start_server)
  169.     sleep 5
  170.  
  171.     if ! process_present "${PID_SRV}"; then
  172.       log 'server is gone away after startup, quit!'
  173.       exit 1
  174.     fi
  175.   fi
  176.  
  177.   wait=$(($(random 1800) + 1800))
  178.   log "sleep for a while (${wait} seconds)"
  179.   sleep ${wait}
  180. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement