From 5c8d0b24b711c9225e0fdd3bf7c00c3001bf2182 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Mon, 25 May 2020 21:12:27 -0400 Subject: [PATCH] make: Update release dest, check in restart script This is the infamous "safe deployment" script I've been using for a while, tuned up for the preprod vs prod deployment. It basically boils down to using pgrep/pkill to kill the old server (if running, and if no players are on the server), then starting the server at the right port. So the general workflow is to run "make release" to deploy to preprod, and to cut a prod release, you need to ssh into the server, replace "prod-el" with "preprod-el" and then call "restart.sh" manually. A future commit will trim down some of these steps... but for now I wanted to check in the WIP so that it wasn't lost (if I jump VPS providers any time soon). --- Makefile | 4 ++-- bin/restart.sh | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100755 bin/restart.sh diff --git a/Makefile b/Makefile index 3c4d26d..44810fc 100644 --- a/Makefile +++ b/Makefile @@ -33,8 +33,8 @@ build: .public.ts $(LIB_FILES) gloat.pl .PHONY: release release: build test - rsync -av --delete build/ www@euchre.live:/var/www/euchre-live/ - ssh www@euchre.live env FORCE=$(FORCE) sh /var/www/deploy.sh + rsync -av --delete build/ www@euchre.live:/var/www/preprod-el/ + ssh www@euchre.live env FORCE=$(FORCE) sh /var/www/restart.sh preprod .PHONY: clean clean: diff --git a/bin/restart.sh b/bin/restart.sh new file mode 100755 index 0000000..0f165f1 --- /dev/null +++ b/bin/restart.sh @@ -0,0 +1,43 @@ +#!/bin/sh +# no more tmux for daemons +# A safe restart script to be run on the server +set -e +die() { + echo "$1" 1>&2 + exit 1 +} + +HOST=localhost + +restart() { + name="$1" + port="$2" + if pgrep -f "gloat.pl.*$port"; then + if [ -z "$FORCE" ]; then + curl -s $HOST:$port/stats | grep -q "0 Players" || die "Players on server" + fi + + printf "%s" "Killing old server... " + pkill -f "gloat.pl.*$port" + sleep 2 # not needed? + echo "Check!" + fi + + if [ $name = "preprod" ]; then + export PREPROD=1 + fi + + printf "%s" "Starting new server... " + $HOME/${name}-el/gloat.pl daemon -l http://*:$port >> /var/log/gloat/$name.log 2>&1 & + sleep 2 + echo "Check!" + + curl -s $HOST:$port/stats | grep "Server Start" +} + +# Decide which server to restart based on the first arg +case "$1" in + preprod) restart "preprod" 3001 ;; + prod) restart "prod" 3000 ;; + *) die "usage: restart.sh prod|preprod" ;; +esac -- libgit2 1.1.1