Project

General

Profile

Download (6.98 KB) Statistics
| Branch: | Tag: | Revision:
1 6f73c362 Renato Botelho
#!/bin/sh
2
#
3
# build_snapshots.sh
4
#
5 8acd654a Renato Botelho
# part of pfSense (https://www.pfsense.org)
6 2a2396a6 Renato Botelho
# Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 8acd654a Renato Botelho
# All rights reserved.
8 6f73c362 Renato Botelho
#
9
# Redistribution and use in source and binary forms, with or without
10 8acd654a Renato Botelho
# modification, are permitted provided that the following conditions are met:
11 6f73c362 Renato Botelho
#
12 8acd654a Renato Botelho
# 1. Redistributions of source code must retain the above copyright notice,
13
#    this list of conditions and the following disclaimer.
14 6f73c362 Renato Botelho
#
15
# 2. Redistributions in binary form must reproduce the above copyright
16 8acd654a Renato Botelho
#    notice, this list of conditions and the following disclaimer in
17
#    the documentation and/or other materials provided with the
18
#    distribution.
19
#
20
# 3. All advertising materials mentioning features or use of this software
21
#    must display the following acknowledgment:
22
#    "This product includes software developed by the pfSense Project
23
#    for use in the pfSense® software distribution. (http://www.pfsense.org/).
24
#
25
# 4. The names "pfSense" and "pfSense Project" must not be used to
26
#    endorse or promote products derived from this software without
27
#    prior written permission. For written permission, please contact
28
#    coreteam@pfsense.org.
29
#
30
# 5. Products derived from this software may not be called "pfSense"
31
#    nor may "pfSense" appear in their names without prior written
32
#    permission of the Electric Sheep Fencing, LLC.
33
#
34
# 6. Redistributions of any form whatsoever must retain the following
35
#    acknowledgment:
36
#
37
# "This product includes software developed by the pfSense Project
38
# for use in the pfSense software distribution (http://www.pfsense.org/).
39 6f73c362 Renato Botelho
#
40
# THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
41
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
44
# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
# OF THE POSSIBILITY OF SUCH DAMAGE.
52
53 b90e12c2 Renato Botelho
usage() {
54 aef38b28 Renato Botelho
	echo "Usage: $(basename $0) [-l] [-n] [-r] [-U] [-p]"
55 b90e12c2 Renato Botelho
	echo "	-l: Build looped operations"
56 8238d674 Renato Botelho
	echo "	-n: Do not build images, only core pkg repo"
57 919c8486 Renato Botelho
	echo "	-p: Update poudriere repo"
58 b90e12c2 Renato Botelho
	echo "	-r: Do not reset local changes"
59 25c86b03 Renato Botelho
	echo "	-U: Upload snapshots"
60 b90e12c2 Renato Botelho
}
61
62 f36b26f8 Renato Botelho
export BUILDER_TOOLS=$(realpath $(dirname ${0}))
63
export BUILDER_ROOT=$(realpath "${BUILDER_TOOLS}/..")
64 6f73c362 Renato Botelho
65 8238d674 Renato Botelho
NO_IMAGES=""
66 b90e12c2 Renato Botelho
NO_RESET=""
67 25c86b03 Renato Botelho
UPLOAD=""
68 6f73c362 Renato Botelho
LOOPED_SNAPSHOTS=""
69 919c8486 Renato Botelho
POUDRIERE_SNAPSHOTS=""
70 6f73c362 Renato Botelho
71
# Handle command line arguments
72 aef38b28 Renato Botelho
while getopts lnprU opt; do
73 b90e12c2 Renato Botelho
	case ${opt} in
74 8238d674 Renato Botelho
		n)
75
			NO_IMAGES="none"
76
			;;
77 b90e12c2 Renato Botelho
		l)
78
			LOOPED_SNAPSHOTS=1
79
			;;
80 919c8486 Renato Botelho
		p)
81
			POUDRIERE_SNAPSHOTS=--poudriere-snapshots
82
			;;
83 b90e12c2 Renato Botelho
		r)
84
			NO_RESET=1
85
			;;
86 aef38b28 Renato Botelho
		U)
87 25c86b03 Renato Botelho
			UPLOAD="-U"
88 b90e12c2 Renato Botelho
			;;
89
		*)
90
			usage
91
			exit 1
92
			;;
93 6f73c362 Renato Botelho
	esac
94
done
95
96 919c8486 Renato Botelho
if [ -n "${POUDRIERE_SNAPSHOTS}" ]; then
97 da869279 Renato Botelho
	export minsleepvalue=${minsleepvalue:-"360"}
98 919c8486 Renato Botelho
else
99
	export minsleepvalue=${minsleepvalue:-"28800"}
100
fi
101
export maxsleepvalue=${maxsleepvalue:-"86400"}
102
103 6f73c362 Renato Botelho
# Keeps track of how many time builder has looped
104 7bc3be6f Renato Botelho
export BUILDCOUNTER=0
105 3e05b544 Renato Botelho
export COUNTER=0
106
107 7bc3be6f Renato Botelho
# Global variable used to control SIGINFO action
108
export _sleeping=0
109 6f73c362 Renato Botelho
110 e5f88c31 Renato Botelho
snapshot_update_status() {
111 25c86b03 Renato Botelho
	${BUILDER_ROOT}/build.sh ${UPLOAD} ${POUDRIERE_SNAPSHOTS} \
112 0cc93b74 Renato Botelho
		--snapshot-update-status "$*"
113 032171ee Renato Botelho
}
114
115 3ef21629 Renato Botelho
exec_and_update_status() {
116
	local _cmd="${@}"
117
118
	[ -z "${_cmd}" ] \
119
		&& return 1
120
121
	# Ref. https://stackoverflow.com/a/30658405
122
	exec 4>&1
123
	local _result=$( \
124
	    { { ${_cmd} 2>&1 3>&-; printf $? 1>&3; } 4>&- \
125
	    | while read -r LINE; do \
126
	    snapshot_update_status "${LINE}"; done 1>&4; } 3>&1)
127
	exec 4>&-
128
129
	return ${_result}
130
}
131
132 ab943fc9 Renato Botelho
git_last_commit() {
133 b90e12c2 Renato Botelho
	[ -z "${NO_RESET}" ] \
134
		&& git -C "${BUILDER_ROOT}" reset --hard >/dev/null 2>&1
135
	git -C "${BUILDER_ROOT}" pull -q
136 1076fc7a Renato Botelho
	if [ -n "${POUDRIERE_SNAPSHOTS}" ]; then
137
		local _remote_repo=$(${BUILDER_ROOT}/build.sh -V POUDRIERE_PORTS_GIT_URL)
138
		local _remote_branch=$(${BUILDER_ROOT}/build.sh -V POUDRIERE_PORTS_GIT_BRANCH)
139
		export CURRENT_COMMIT=$(git ls-remote ${_remote_repo} ${_remote_branch} | cut -f1)
140
	else
141
		export CURRENT_COMMIT=$(git -C ${BUILDER_ROOT} log -1 --format='%H')
142
	fi
143 ab943fc9 Renato Botelho
}
144 6f73c362 Renato Botelho
145 3e05b544 Renato Botelho
restart_build() {
146 7bc3be6f Renato Botelho
	if [ ${_sleeping} -ne 0 ]; then
147 032171ee Renato Botelho
		snapshot_update_status ">>> SIGNINFO received, restarting build"
148 3e05b544 Renato Botelho
		COUNTER=$((maxsleepvalue + 60))
149
	fi
150
}
151
152 6f73c362 Renato Botelho
# This routine is called in between runs. We
153
# will sleep for a bit and check for new commits
154
# in between sleeping for short durations.
155 ab943fc9 Renato Botelho
snapshots_sleep_between_runs() {
156 8a6723d5 Renato Botelho
	# Handle SIGINFO (ctrl+T) and restart build
157
	trap restart_build SIGINFO
158
159
	# Initialize variables that keep track of last commit
160
	[ -z "${LAST_COMMIT}" ] \
161
		&& export LAST_COMMIT=${CURRENT_COMMIT}
162
163 2251aa18 Renato Botelho
	snapshot_update_status ">>> Sleeping for at least $minsleepvalue," \
164 9447077e Renato Botelho
		"at most $maxsleepvalue in between snapshot builder runs."
165 032171ee Renato Botelho
	snapshot_update_status ">>> Last known commit: ${LAST_COMMIT}"
166
	snapshot_update_status ">>> Freezing build process at $(date)"
167 8a6723d5 Renato Botelho
	echo ">>> Press ctrl+T to start a new build"
168
	COUNTER=0
169 7bc3be6f Renato Botelho
	_sleeping=1
170 8a6723d5 Renato Botelho
	while [ ${COUNTER} -lt ${minsleepvalue} ]; do
171
		sleep 1
172
		COUNTER=$((COUNTER + 1))
173
	done
174
175
	if [ ${COUNTER} -lt ${maxsleepvalue} ]; then
176 2251aa18 Renato Botelho
		snapshot_update_status ">>> Thawing build process and" \
177 9447077e Renato Botelho
			"resuming checks for pending commits at $(date)."
178 8a6723d5 Renato Botelho
		echo ">>> Press ctrl+T to start a new build"
179
	fi
180
181 6f73c362 Renato Botelho
	while [ $COUNTER -lt $maxsleepvalue ]; do
182 7bc3be6f Renato Botelho
		sleep 1
183 9447077e Renato Botelho
		COUNTER=$(($COUNTER + 1))
184 7bc3be6f Renato Botelho
		# Update this repo each 60 seconds
185 9447077e Renato Botelho
		if [ "$((${COUNTER} % 60))" != "0" ]; then
186
			continue
187
		fi
188
		git_last_commit
189
		if [ "${LAST_COMMIT}" != "${CURRENT_COMMIT}" ]; then
190 2251aa18 Renato Botelho
			snapshot_update_status ">>> New commit:" \
191 7f330caa Renato Botelho
				"$CURRENT_COMMIT " \
192 9447077e Renato Botelho
				".. No longer sleepy."
193
			COUNTER=$(($maxsleepvalue + 60))
194
			export LAST_COMMIT="${CURRENT_COMMIT}"
195 6f73c362 Renato Botelho
		fi
196
	done
197 7bc3be6f Renato Botelho
	_sleeping=0
198 8a6723d5 Renato Botelho
199 6f73c362 Renato Botelho
	if [ $COUNTER -ge $maxsleepvalue ]; then
200 2251aa18 Renato Botelho
		snapshot_update_status ">>> Sleep timer expired." \
201 9447077e Renato Botelho
			"Restarting build."
202 6f73c362 Renato Botelho
		COUNTER=0
203
	fi
204 8a6723d5 Renato Botelho
205
	trap "-" SIGINFO
206 6f73c362 Renato Botelho
}
207
208 ab943fc9 Renato Botelho
# Main builder loop
209
while [ /bin/true ]; do
210
	BUILDCOUNTER=$((${BUILDCOUNTER}+1))
211 6f73c362 Renato Botelho
212 b90e12c2 Renato Botelho
	git_last_commit
213 670a33c5 Renato Botelho
214 77074d55 Renato Botelho
	OIFS=${IFS}
215
	IFS="
216
"
217 919c8486 Renato Botelho
	if [ -n "${POUDRIERE_SNAPSHOTS}" ]; then
218 3ef21629 Renato Botelho
		exec_and_update_status \
219 f9ba59d4 Renato Botelho
		    ${BUILDER_ROOT}/build.sh --update-poudriere-ports
220
		rc=$?
221
222
		if [ $rc -eq 0 ]; then
223
			exec_and_update_status \
224 25c86b03 Renato Botelho
			    ${BUILDER_ROOT}/build.sh ${UPLOAD} \
225 f9ba59d4 Renato Botelho
			    --update-pkg-repo
226
			rc=$?
227
		fi
228 919c8486 Renato Botelho
	else
229 3ef21629 Renato Botelho
		exec_and_update_status \
230 f9ba59d4 Renato Botelho
		    ${BUILDER_ROOT}/build.sh --clean-builder
231
		rc=$?
232
233
		if [ $rc -eq 0 ]; then
234
			exec_and_update_status \
235 25c86b03 Renato Botelho
			    ${BUILDER_ROOT}/build.sh ${UPLOAD} --flash-size \
236 f9ba59d4 Renato Botelho
			    '2g 4g' --snapshots \
237
			    ${IMAGES}
238
			rc=$?
239
		fi
240 919c8486 Renato Botelho
	fi
241 77074d55 Renato Botelho
	IFS=${OIFS}
242 6f73c362 Renato Botelho
243 ab943fc9 Renato Botelho
	if [ -z "${LOOPED_SNAPSHOTS}" ]; then
244
		# only one build required, exiting
245 f9ba59d4 Renato Botelho
		exit ${rc}
246 6f73c362 Renato Botelho
	fi
247
248 8a6723d5 Renato Botelho
	# Count some sheep or wait until a new commit turns up
249
	# for one days time.  We will wake up if a new commit
250
	# is detected during sleepy time.
251
	snapshots_sleep_between_runs
252 ab943fc9 Renato Botelho
done