~skiqqy/gwiki

ref: 822640f2da50ebc9389a159f05a43218d99eb721 gwiki/entry.sh -rw-r--r-- 2.7 KiB
822640f2Stephen Cochrane Added times to print, for better logging. 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
# Run the API
# Written by Stephen Skiqqy Cochrane

# Basic initing of the container.
init()
{
	bash --version
	mkdir -p /etc/gwiki

	if [ ! -f /etc/gwiki/config ]
	then
		printf 'Generating config file /etc/gwiki/config please edit it.\n'
		cat << EOF > /etc/gwiki/config
CONFIG_HOME='$CONFIG_HOME'
EOF
	fi
}

# Return 0 if behind, 1 if not
git_behind()
{
	local stat
	stat=

	git remote update > /dev/null
	stat="$(git status | grep behind)"

	if [ -z "$stat" ]
	then
		return 1 # We are up to date.
	fi
}

# Watcher attempts to pull new changes to wiki, and sub wikis if root wiki.
# Usage: watch PATH
# No path -> root wiki
# NOTE: This function is recursive.
watch()
{
	local timer
	local build
	local wdir

	wikis=( )
	wdir="${1:-$root}"
	timer="${TIMER:-60}" # How long we wait (in seconds) till (re)fresh.
	printf 'Watching %s...\n' "$wdir"
	
	while [ "$wdir" = "$root" ] && [ ! -d "$wdir/.git" ]
	do
		sleep 1 # Wait for the repo to be cloned, incase it is still being cloned.
	done

	pushd "$wdir" || return 1
	# Main event loop.
	while true
	do
		printf '%s (Re)starting event loop for %s\n' "$(date '+[ %e/%m/%Y %X ]')" "$wdir"
		build=false

		if git_behind
		then
			git pull
			build=true
		fi

		# Build if we have to.
		if "$build"
		then
			printf '[ DISK IO ] %s Rebuilding %s\n' "$(date '+[ %e/%m/%Y %X ]')" "$wdir"
			make         # Try make
			mkdocs build # Try mkdocs
		fi

		# check Max depth reached (wikis dont have nested wikis, except for root wiki)
		[ ! "$wdir" = "$root" ] && popd && \
			printf '%s Exiting event loop for %s\n' "$(date '+[ %e/%m/%Y %X ]')" "$wdir" && \
			return

		# check children. IE Recursion.
		[ ! -f config ] && printf 'WARN: %s/config missing.\n' "$wdir"
		[ -f config ] && . config

		for wiki in "${wikis[@]}"
		do
			# If the wiki is new, clone it.
			if [ ! -d "$root/wiki/$(basename "$wiki")" ]
			then
				git clone "$wiki" "$root/wiki/$(basename "$wiki")"
				pushd "$root/wiki/$(basename "$wiki")"
				make         # Try make
				mkdocs build # Try mkdocs
				popd
			fi

			watch "$root/wiki/$(basename "$wiki")"
		done

		sleep "$timer" # Wait until next cycle.
	done
}

# Production Deploy
deploy () {
	if [ -z "$(ls -A "$root")" ]
	then
		[ -z "$CONFIG_HOME" ] && printf 'WARNING, $CONFIG_HOME not set, aborting deploy.\n' && return 1
		git clone "$CONFIG_HOME" "$root" ||
			printf 'Could not clone '\''%s'\'', does the repo exist?\n' "$CONFIG_HOME"
	fi

	cd "$root" || return 1

	# Try different builds
	make         # Try make
	mkdocs build # Try mkdocs

	# Start the web server.
	printf 'Starting Web Server\n'
	/usr/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.conf
	printf 'Closing Web Server\n'
}

init

. /etc/gwiki/config || return 1

root='/deploy'
watch & # Setup watcher
deploy  # Deploy