Project

General

Profile

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