Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- ### [ -- v config here v -- ]
- INSTALL_ROOT='/home/factorio'
- BRANCH='experimental'
- URL_BASE='https://www.factorio.com'
- ### [ -- ^ config here ^ -- ]
- ###
- URL_WATCH=${URL_BASE}'/download-headless/'${BRANCH}
- REGEX_VERSION_URL='/get-download/([^/]+)/headless/linux64'
- REGEX_URL_HTML='href="('${REGEX_VERSION_URL}')"'
- PID_SRV=''
- D=/dev/null
- readonly D
- log() { echo "# $@"; }
- random() {
- local n x
- if [ $1 -lt 1 ]; then
- echo 0
- return
- fi
- if [ $1 -lt 255 ]; then
- n=1
- elif [ $1 -lt 65535 ]; then
- n=2
- else
- n=4
- fi
- x='0x'$(dd if=/dev/urandom bs=$n count=1 2>$D | xxd -ps)
- # x=$((x + 0))
- echo $((x % $1))
- }
- http_test() {
- curl -kLI "$1" 2>$D | grep -Eqe '^HTTP/\S+ 2.. '
- }
- http_get() {
- curl -kL "$1" 2>$D
- }
- get_relative_url_upstream() {
- http_get "${URL_WATCH}" | grep -Eoe "${REGEX_URL_HTML}" | \
- sed -re "s#${REGEX_URL_HTML}#\1#" | head -n 1
- }
- get_version_local() {
- "${INSTALL_ROOT}/bin/factorio" --version 2>&1 | \
- sed -nre '/^Version:\s+(\S+)\s.*$/{s##\1#;p;}'
- }
- get_version_upstream() {
- get_relative_url_upstream | sed -re "s#${REGEX_VERSION_URL}#\1#"
- }
- process_present() {
- [ -n "$1" ] || return 1
- /bin/ps -p "$1" 0<$D 1>$D 2>$D
- }
- process_sig() {
- /bin/kill -s "$2" "$1" 0<$D 1>$D 2>$D
- }
- stop_server() {
- [ -n "$1" ] || return
- if process_present "$1"; then
- process_sig "$1" TERM
- sleep 5
- process_sig "$1" KILL
- fi
- }
- start_server() {
- [ -n "${INSTALL_ROOT}" ] || return 1
- [ -d "${INSTALL_ROOT}" ] || return 1
- cd "${INSTALL_ROOT}/"
- "${INSTALL_ROOT}/bin/factorio" --verbose \
- --bind 127.0.0.1:34197 \
- --config ${INSTALL_ROOT}/cfg/server.ini \
- --mod-directory ${INSTALL_ROOT}/var/mods \
- --map-settings ${INSTALL_ROOT}/cfg/map-settings.json \
- --start-server ${INSTALL_ROOT}/var/saves/my-awesome-server.zip \
- 1>&2 <$D &
- echo $!
- }
- ###
- while :; do
- log 'is upstream info is available?'
- if http_test "${URL_WATCH}"; then
- log 'OK, upstream info is available'
- VER_UPSTREAM=$(get_version_upstream)
- VER_LOCAL=$(get_version_local)
- log 'local version: ' "${VER_LOCAL}"
- log 'remote version:' "${VER_UPSTREAM}"
- if [ "${VER_LOCAL}" = "${VER_UPSTREAM}" ]; then
- log "versions are equal, update isn't required"
- else
- log "versions are different, begin update"
- log 'is upstream tarball is available?'
- URL_TARBALL=${URL_BASE}$(get_relative_url_upstream)
- if http_test "${URL_TARBALL}"; then
- log 'OK, upstream tarball is available'
- DTMP=$(mktemp -d)
- cd "${DTMP}/"
- log 'downloading tarball'
- http_get "${URL_TARBALL}" > "${VER_UPSTREAM}.t"
- tar --strip-components=1 -xf "${VER_UPSTREAM}.t"
- log 'processing tarball'
- strip \
- --remove-section=.comment \
- --remove-section=.note \
- bin/x64/factorio
- if [ -n "${PID_SRV}" ]; then
- log 'stopping server'
- stop_server ${PID_SRV}; PID_SRV=''
- sleep 5
- fi
- log 'updating installation'
- cp bin/x64/factorio "${INSTALL_ROOT}/bin/factorio"
- rm -rf "${INSTALL_ROOT}/data"
- tar cf - data | (cd "${INSTALL_ROOT}/"; tar xf -;)
- log 'update is complete'
- cd /; rm -rf "${DTMP}/"
- else
- log 'FAIL, upstream tarball is NOT available'
- fi
- fi
- else
- log 'FAIL, upstream info is NOT available'
- fi
- if [ -n "${PID_SRV}" ]; then
- if ! process_present ${PID_SRV}; then
- log 'server is gone away, try to restart'
- PID_SRV=''
- fi
- fi
- if [ -z "${PID_SRV}" ]; then
- log 'starting server'
- PID_SRV=$(start_server)
- sleep 5
- if ! process_present "${PID_SRV}"; then
- log 'server is gone away after startup, quit!'
- exit 1
- fi
- fi
- wait=$(($(random 1800) + 1800))
- log "sleep for a while (${wait} seconds)"
- sleep ${wait}
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement