Project

General

Profile

Download (5.56 KB) Statistics
| Branch: | Tag: | Revision:
1 9020723c Renato Botelho do Couto
#!/bin/sh -T
2 6f73c362 Renato Botelho
#
3
# build_snapshots.sh
4
#
5 ac24dc24 Renato Botelho
# part of pfSense (https://www.pfsense.org)
6 38809d47 Renato Botelho do Couto
# Copyright (c) 2004-2013 BSD Perimeter
7
# Copyright (c) 2013-2016 Electric Sheep Fencing
8 0284d79e jim-p
# Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9 ac24dc24 Renato Botelho
# All rights reserved.
10 6f73c362 Renato Botelho
#
11 b12ea3fb Renato Botelho
# Licensed under the Apache License, Version 2.0 (the "License");
12
# you may not use this file except in compliance with the License.
13
# You may obtain a copy of the License at
14 6f73c362 Renato Botelho
#
15 b12ea3fb Renato Botelho
# http://www.apache.org/licenses/LICENSE-2.0
16 6f73c362 Renato Botelho
#
17 b12ea3fb Renato Botelho
# Unless required by applicable law or agreed to in writing, software
18
# distributed under the License is distributed on an "AS IS" BASIS,
19
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
# See the License for the specific language governing permissions and
21
# limitations under the License.
22 6f73c362 Renato Botelho
23 b90e12c2 Renato Botelho
usage() {
24 c31ef6aa Renato Botelho do Couto
	echo "Usage: $(basename $0) [-l] [-n] [-r] [-U] [-p] [-i]"
25 b90e12c2 Renato Botelho
	echo "	-l: Build looped operations"
26 ddf56629 Renato Botelho
	echo "	-n: Do not build images, only core pkg repo"
27 919c8486 Renato Botelho
	echo "	-p: Update poudriere repo"
28 b90e12c2 Renato Botelho
	echo "	-r: Do not reset local changes"
29 f6c83d05 Renato Botelho
	echo "	-U: Upload snapshots"
30 158999e9 Renato Botelho
	echo "	-i: Skip rsync to final server"
31 b90e12c2 Renato Botelho
}
32
33 f36b26f8 Renato Botelho
export BUILDER_TOOLS=$(realpath $(dirname ${0}))
34
export BUILDER_ROOT=$(realpath "${BUILDER_TOOLS}/..")
35 6f73c362 Renato Botelho
36 b4b446a6 Renato Botelho
IMAGES="all"
37 b90e12c2 Renato Botelho
NO_RESET=""
38 f6c83d05 Renato Botelho
UPLOAD=""
39 158999e9 Renato Botelho
_SKIP_FINAL_RSYNC=""
40 6f73c362 Renato Botelho
LOOPED_SNAPSHOTS=""
41 919c8486 Renato Botelho
POUDRIERE_SNAPSHOTS=""
42 6f73c362 Renato Botelho
43
# Handle command line arguments
44 c31ef6aa Renato Botelho do Couto
while getopts lnprUi opt; do
45 b90e12c2 Renato Botelho
	case ${opt} in
46 ddf56629 Renato Botelho
		n)
47 b4b446a6 Renato Botelho
			IMAGES="none"
48 ddf56629 Renato Botelho
			;;
49 b90e12c2 Renato Botelho
		l)
50
			LOOPED_SNAPSHOTS=1
51
			;;
52 919c8486 Renato Botelho
		p)
53
			POUDRIERE_SNAPSHOTS=--poudriere-snapshots
54
			;;
55 b90e12c2 Renato Botelho
		r)
56
			NO_RESET=1
57
			;;
58 1f6d1861 Renato Botelho
		U)
59 f6c83d05 Renato Botelho
			UPLOAD="-U"
60 b90e12c2 Renato Botelho
			;;
61 158999e9 Renato Botelho
		i)
62
			_SKIP_FINAL_RSYNC="-i"
63
			;;
64 b90e12c2 Renato Botelho
		*)
65
			usage
66
			exit 1
67
			;;
68 6f73c362 Renato Botelho
	esac
69
done
70
71 919c8486 Renato Botelho
if [ -n "${POUDRIERE_SNAPSHOTS}" ]; then
72 da869279 Renato Botelho
	export minsleepvalue=${minsleepvalue:-"360"}
73 919c8486 Renato Botelho
else
74
	export minsleepvalue=${minsleepvalue:-"28800"}
75
fi
76
export maxsleepvalue=${maxsleepvalue:-"86400"}
77
78 6f73c362 Renato Botelho
# Keeps track of how many time builder has looped
79 7bc3be6f Renato Botelho
export BUILDCOUNTER=0
80 3e05b544 Renato Botelho
export COUNTER=0
81
82 7bc3be6f Renato Botelho
# Global variable used to control SIGINFO action
83
export _sleeping=0
84 6f73c362 Renato Botelho
85 e5f88c31 Renato Botelho
snapshot_update_status() {
86 158999e9 Renato Botelho
	${BUILDER_ROOT}/build.sh ${_SKIP_FINAL_RSYNC} ${UPLOAD} \
87
		${POUDRIERE_SNAPSHOTS} --snapshot-update-status "$*"
88 032171ee Renato Botelho
}
89
90 08ec32cf Renato Botelho
exec_and_update_status() {
91
	local _cmd="${@}"
92
93
	[ -z "${_cmd}" ] \
94
		&& return 1
95
96
	# Ref. https://stackoverflow.com/a/30658405
97
	exec 4>&1
98
	local _result=$( \
99
	    { { ${_cmd} 2>&1 3>&-; printf $? 1>&3; } 4>&- \
100
	    | while read -r LINE; do \
101
	    snapshot_update_status "${LINE}"; done 1>&4; } 3>&1)
102
	exec 4>&-
103
104
	return ${_result}
105
}
106
107 ab943fc9 Renato Botelho
git_last_commit() {
108 b90e12c2 Renato Botelho
	[ -z "${NO_RESET}" ] \
109
		&& git -C "${BUILDER_ROOT}" reset --hard >/dev/null 2>&1
110
	git -C "${BUILDER_ROOT}" pull -q
111 1076fc7a Renato Botelho
	if [ -n "${POUDRIERE_SNAPSHOTS}" ]; then
112
		local _remote_repo=$(${BUILDER_ROOT}/build.sh -V POUDRIERE_PORTS_GIT_URL)
113
		local _remote_branch=$(${BUILDER_ROOT}/build.sh -V POUDRIERE_PORTS_GIT_BRANCH)
114
		export CURRENT_COMMIT=$(git ls-remote ${_remote_repo} ${_remote_branch} | cut -f1)
115
	else
116
		export CURRENT_COMMIT=$(git -C ${BUILDER_ROOT} log -1 --format='%H')
117
	fi
118 ab943fc9 Renato Botelho
}
119 6f73c362 Renato Botelho
120 3e05b544 Renato Botelho
restart_build() {
121 7bc3be6f Renato Botelho
	if [ ${_sleeping} -ne 0 ]; then
122 032171ee Renato Botelho
		snapshot_update_status ">>> SIGNINFO received, restarting build"
123 3e05b544 Renato Botelho
		COUNTER=$((maxsleepvalue + 60))
124
	fi
125
}
126
127 6f73c362 Renato Botelho
# This routine is called in between runs. We
128
# will sleep for a bit and check for new commits
129
# in between sleeping for short durations.
130 ab943fc9 Renato Botelho
snapshots_sleep_between_runs() {
131 8a6723d5 Renato Botelho
	# Handle SIGINFO (ctrl+T) and restart build
132
	trap restart_build SIGINFO
133
134
	# Initialize variables that keep track of last commit
135
	[ -z "${LAST_COMMIT}" ] \
136
		&& export LAST_COMMIT=${CURRENT_COMMIT}
137
138 2251aa18 Renato Botelho
	snapshot_update_status ">>> Sleeping for at least $minsleepvalue," \
139 9447077e Renato Botelho
		"at most $maxsleepvalue in between snapshot builder runs."
140 032171ee Renato Botelho
	snapshot_update_status ">>> Last known commit: ${LAST_COMMIT}"
141
	snapshot_update_status ">>> Freezing build process at $(date)"
142 8a6723d5 Renato Botelho
	echo ">>> Press ctrl+T to start a new build"
143
	COUNTER=0
144 7bc3be6f Renato Botelho
	_sleeping=1
145 8a6723d5 Renato Botelho
	while [ ${COUNTER} -lt ${minsleepvalue} ]; do
146
		sleep 1
147
		COUNTER=$((COUNTER + 1))
148
	done
149
150
	if [ ${COUNTER} -lt ${maxsleepvalue} ]; then
151 2251aa18 Renato Botelho
		snapshot_update_status ">>> Thawing build process and" \
152 9447077e Renato Botelho
			"resuming checks for pending commits at $(date)."
153 8a6723d5 Renato Botelho
		echo ">>> Press ctrl+T to start a new build"
154
	fi
155
156 6f73c362 Renato Botelho
	while [ $COUNTER -lt $maxsleepvalue ]; do
157 7bc3be6f Renato Botelho
		sleep 1
158 9447077e Renato Botelho
		COUNTER=$(($COUNTER + 1))
159 7bc3be6f Renato Botelho
		# Update this repo each 60 seconds
160 9447077e Renato Botelho
		if [ "$((${COUNTER} % 60))" != "0" ]; then
161
			continue
162
		fi
163
		git_last_commit
164
		if [ "${LAST_COMMIT}" != "${CURRENT_COMMIT}" ]; then
165 2251aa18 Renato Botelho
			snapshot_update_status ">>> New commit:" \
166 7f330caa Renato Botelho
				"$CURRENT_COMMIT " \
167 9447077e Renato Botelho
				".. No longer sleepy."
168
			COUNTER=$(($maxsleepvalue + 60))
169
			export LAST_COMMIT="${CURRENT_COMMIT}"
170 6f73c362 Renato Botelho
		fi
171
	done
172 7bc3be6f Renato Botelho
	_sleeping=0
173 8a6723d5 Renato Botelho
174 6f73c362 Renato Botelho
	if [ $COUNTER -ge $maxsleepvalue ]; then
175 2251aa18 Renato Botelho
		snapshot_update_status ">>> Sleep timer expired." \
176 9447077e Renato Botelho
			"Restarting build."
177 6f73c362 Renato Botelho
		COUNTER=0
178
	fi
179 8a6723d5 Renato Botelho
180
	trap "-" SIGINFO
181 6f73c362 Renato Botelho
}
182
183 ab943fc9 Renato Botelho
# Main builder loop
184
while [ /bin/true ]; do
185
	BUILDCOUNTER=$((${BUILDCOUNTER}+1))
186 6f73c362 Renato Botelho
187 b90e12c2 Renato Botelho
	git_last_commit
188 670a33c5 Renato Botelho
189 acf4481f Renato Botelho
	OIFS=${IFS}
190
	IFS="
191
"
192 919c8486 Renato Botelho
	if [ -n "${POUDRIERE_SNAPSHOTS}" ]; then
193 08ec32cf Renato Botelho
		exec_and_update_status \
194 c31ef6aa Renato Botelho do Couto
		    ${BUILDER_ROOT}/build.sh --update-poudriere-ports
195 5339da65 Renato Botelho
		rc=$?
196
197
		if [ $rc -eq 0 ]; then
198
			exec_and_update_status \
199 158999e9 Renato Botelho
			    ${BUILDER_ROOT}/build.sh ${_SKIP_FINAL_RSYNC} \
200 c31ef6aa Renato Botelho do Couto
			    ${UPLOAD} --update-pkg-repo
201 5339da65 Renato Botelho
			rc=$?
202
		fi
203 919c8486 Renato Botelho
	else
204 08ec32cf Renato Botelho
		exec_and_update_status \
205 5339da65 Renato Botelho
		    ${BUILDER_ROOT}/build.sh --clean-builder
206
		rc=$?
207
208
		if [ $rc -eq 0 ]; then
209
			exec_and_update_status \
210 158999e9 Renato Botelho
			    ${BUILDER_ROOT}/build.sh ${_SKIP_FINAL_RSYNC} \
211 c31ef6aa Renato Botelho do Couto
			    ${UPLOAD} --snapshots ${IMAGES}
212 5339da65 Renato Botelho
			rc=$?
213
		fi
214 919c8486 Renato Botelho
	fi
215 acf4481f Renato Botelho
	IFS=${OIFS}
216 6f73c362 Renato Botelho
217 ab943fc9 Renato Botelho
	if [ -z "${LOOPED_SNAPSHOTS}" ]; then
218
		# only one build required, exiting
219 5339da65 Renato Botelho
		exit ${rc}
220 6f73c362 Renato Botelho
	fi
221
222 8a6723d5 Renato Botelho
	# Count some sheep or wait until a new commit turns up
223
	# for one days time.  We will wake up if a new commit
224
	# is detected during sleepy time.
225
	snapshots_sleep_between_runs
226 ab943fc9 Renato Botelho
done