update.sh 2.74 KB
Newer Older
1
#!/bin/bash
2 3
set -e

4 5 6
CLEAR_CACHE=-1
INTERACTIVE=1

7 8 9 10
escape_sed() {
	sed -e 's/\//\\\//g' -e 's/\&/\\\&/g'
}

11 12
while [ $# -gt 0 ]; do
	case "$1" in
13 14 15 16
		--interactive ) INTERACTIVE=1;;
		--no-interactive ) INTERACTIVE=0;;
		--clear-cache ) CLEAR_CACHE=1;;
		--no-clear-cache ) CLEAR_CACHE=0;;
17
		--huginn ) HUGINN="$2"; shift;;
18
		--release-directory ) RELEASE_DIRECTORY="$2"; shift;;
19
		-h|--help )
20
			echo "Usage: $0 [--[no-]interactive] [--[no-]clear-cache] [--huginn HUGINN_ADDRESS] [--release-directory DIR]"
21 22
			exit;;
		* )
23
			echo "Unknown argument '$1'; use -h for help"
24 25
			exit -1;;
	esac
26
	shift
27 28 29 30 31 32 33
done

if [[ $INTERACTIVE -eq 0 ]] && [[ $CLEAR_CACHE -lt 0 ]]; then
	echo "When using --no-interactive you must use either --no-clear-cache or --clear-cache."
	exit -1
fi

34 35
echo "Pulling new commits..."

36
git checkout frontend/index.php
37 38 39 40 41
if [ $INTERACTIVE -eq 0 ]; then
	git pull origin master
else
	git pull --no-edit origin master
fi
42
git submodule update --init --recursive
43

44
COMMIT_INFO="$(git log -2 --decorate --pretty=oneline --no-color | tail -1 | escape_sed)"
45
sed -i "s/{{{COMMIT}}}/$COMMIT_INFO/g" "frontend/index.php"
46

47 48
echo "Updating containers..."

49
sudo stdbuf -o0 -e0 docker-compose build --force-rm --no-cache --pull &> /tmp/cloogle-build.log
50 51 52 53 54 55 56 57 58
RES=$?
cat /tmp/cloogle-build.log
if [ $RES -ne 0 ]; then
	if [[ "$HUGINN" != "" ]]; then
		curl -s -d text="Cloogle build failed." "$HUGINN"; echo
	fi
	echo "--> Cloogle build failed."
	exit $RES
fi
Mart Lubbers's avatar
Mart Lubbers committed
59
sudo docker-compose up -d
60
sudo docker image prune -f
61 62 63

echo "All done."

64
if [ $CLEAR_CACHE -lt 0 ]; then
65 66 67
	echo
	read -p "Do you want to clear the caches? (y/[n]) " confirm
	case "$confirm" in
68 69
		y|Y ) CLEAR_CACHE=1;;
		* ) CLEAR_CACHE=0;;
70 71 72
	esac
fi

73
if [ $CLEAR_CACHE -eq 1 ]; then
74 75 76 77 78
	echo "Clearing the cache..."
	sudo bash -c 'rm -f cache/*/*'
else
	echo "Not clearing the cache."
fi
79 80 81 82

if [[ "$HUGINN" != "" ]]; then
	curl -s -d text="Cloogle build succeeded." "$HUGINN"; echo
fi
83
if [[ "$RELEASE_DIRECTORY" != "" ]]; then
84
	DATE="$(date +%Y-%m-%d)"
85 86 87 88 89 90 91
	THIS_RELEASE="$RELEASE_DIRECTORY/~$DATE"
	mkdir -p "$THIS_RELEASE"
	rm -f "$RELEASE_DIRECTORY/latest"
	ln -s "~$DATE" "$RELEASE_DIRECTORY/latest"
	sed 's/\x1b\[[0-9;]*m//g' /tmp/cloogle-build.log \
		| sed -n '/^\.\/builddb /{:a;n;/^Execution: /{p;b};p;ba}' \
		> "$THIS_RELEASE/build-log.txt"
92 93 94
	timeout -k 10 10 sudo docker-compose exec -T backend cat rank_settings.json > "$THIS_RELEASE/rank_settings.json" || echo "could not publish rank settings"
	timeout -k 10 10 sudo docker-compose exec -T backend cat types.json > "$THIS_RELEASE/types.json" || echo "could not publish database"
	timeout -k 10 10 sudo docker-compose exec -T backend cat typetree.dot > "$THIS_RELEASE/typetree.dot" || echo "could not publish type tree"
95 96
fi
echo -en "\033[0m"
97
echo "--> Cloogle build succeeded."
98
rm /tmp/cloogle-build.log