util.sh 2.77 KB
Newer Older
Shawn Webb's avatar
Shawn Webb committed
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
#!/bin/sh
#-
# Copyright (c) 2019 HardenedBSD
# Author: Shawn Webb <shawn.webb@hardenedbsd.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

is_locked() {
	if [ -e ${HBSD_LOCKFILE} ]; then
		return 0
	fi

	return 1
}

lock_build() {
	touch ${HBSD_LOCKFILE}
	return ${?}
}

unlock_build() {
	rm -f ${HBSD_LOCKFILE}
	return ${?}
}

assert_unlocked() {
	if is_locked; then
		echo "Build locked. Remove ${HBSD_LOCKFILE}"
		exit 1
	fi

	return 0
}

54
# The build_number function must be called EXACTLY ONCE.
Shawn Webb's avatar
Shawn Webb committed
55
56
57
58
59
60
build_number() {
	local n

	if [ -e ${HBSD_INDEX_FILE} ]; then
		n=$(cat ${HBSD_INDEX_FILE})
		n=$((${n} + 1))
Shawn Webb's avatar
Shawn Webb committed
61
62
	else
		n=1
Shawn Webb's avatar
Shawn Webb committed
63
64
	fi

Shawn Webb's avatar
Shawn Webb committed
65
	echo ${n} | tee ${HBSD_INDEX_FILE}
Shawn Webb's avatar
Shawn Webb committed
66
67
68
	return 0
}

69
70
71
72
73
74
75
76
77
78
79
80
81
82
codebase_hashish() {
	(
		cd ${HBSD_SRC}
		git rev-parse HEAD
		exit ${?}
	)
	return ${?}
}

cache_codebase_hashish() {
	codebase_hashish > ${HBSD_CACHEDIR}/last_build.txt
	return ${?}
}

Shawn Webb's avatar
Shawn Webb committed
83
84
85
86
87
88
89
90
91
update_codebase() {
	(
		set -ex

		cd ${HBSD_SRC}
		git pull
	)
	return ${?}
}
92
93
94
95

should_build() {
	local lastbuild
	local currenthashish
Shawn Webb's avatar
Shawn Webb committed
96
97
98
99
	local forcebuild

	forcebuild=${1}
	[ ${forcebuild} -gt 0 ] && return 0
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

	if [ ! -f ${HBSD_CACHEDIR}/last_build.txt ]; then
		return 0
	fi

	lastbuild=$(cat ${HBSD_CACHEDIR}/last_build.txt)
	currenthashish=$(codebase_hashish)

	if [ "${lastbuild}" = "${currenthashish}" ]; then
		return 1
	fi

	return 0
}

prune_old_builds() {
	[ ${HBSD_BUILDNUMBER} -lt ${HBSD_KEEP_NBUILDS} ] && return 0

	floor=$((${HBSD_BUILDNUMBER} - ${HBSD_KEEP_NBUILDS}))

	for d in $(find ${HBSD_PUBDIR} -maxdepth 1 -type d | sed 1d); do
		n=$(basename ${d})
		if [ ${n} -lt ${floor} ]; then
			rm -rf ${d}
		fi
	done

	return 0
}