1
|
#!/bin/sh
|
2
|
#
|
3
|
# build_snapshots.sh
|
4
|
#
|
5
|
# part of pfSense (https://www.pfsense.org)
|
6
|
# Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
|
7
|
# All rights reserved.
|
8
|
#
|
9
|
# Redistribution and use in source and binary forms, with or without
|
10
|
# modification, are permitted provided that the following conditions are met:
|
11
|
#
|
12
|
# 1. Redistributions of source code must retain the above copyright notice,
|
13
|
# this list of conditions and the following disclaimer.
|
14
|
#
|
15
|
# 2. Redistributions in binary form must reproduce the above copyright
|
16
|
# 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
|
#
|
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
|
usage() {
|
54
|
echo "Usage: $(basename $0) [-l] [-n] [-r] [-U] [-p]"
|
55
|
echo " -l: Build looped operations"
|
56
|
echo " -n: Do not build images, only core pkg repo"
|
57
|
echo " -p: Update poudriere repo"
|
58
|
echo " -r: Do not reset local changes"
|
59
|
echo " -U: Upload snapshots"
|
60
|
}
|
61
|
|
62
|
export BUILDER_TOOLS=$(realpath $(dirname ${0}))
|
63
|
export BUILDER_ROOT=$(realpath "${BUILDER_TOOLS}/..")
|
64
|
|
65
|
NO_IMAGES=""
|
66
|
NO_RESET=""
|
67
|
UPLOAD=""
|
68
|
LOOPED_SNAPSHOTS=""
|
69
|
POUDRIERE_SNAPSHOTS=""
|
70
|
|
71
|
# Handle command line arguments
|
72
|
while getopts lnprU opt; do
|
73
|
case ${opt} in
|
74
|
n)
|
75
|
NO_IMAGES="none"
|
76
|
;;
|
77
|
l)
|
78
|
LOOPED_SNAPSHOTS=1
|
79
|
;;
|
80
|
p)
|
81
|
POUDRIERE_SNAPSHOTS=--poudriere-snapshots
|
82
|
;;
|
83
|
r)
|
84
|
NO_RESET=1
|
85
|
;;
|
86
|
U)
|
87
|
UPLOAD="-U"
|
88
|
;;
|
89
|
*)
|
90
|
usage
|
91
|
exit 1
|
92
|
;;
|
93
|
esac
|
94
|
done
|
95
|
|
96
|
if [ -n "${POUDRIERE_SNAPSHOTS}" ]; then
|
97
|
export minsleepvalue=${minsleepvalue:-"360"}
|
98
|
else
|
99
|
export minsleepvalue=${minsleepvalue:-"28800"}
|
100
|
fi
|
101
|
export maxsleepvalue=${maxsleepvalue:-"86400"}
|
102
|
|
103
|
# Keeps track of how many time builder has looped
|
104
|
export BUILDCOUNTER=0
|
105
|
export COUNTER=0
|
106
|
|
107
|
# Global variable used to control SIGINFO action
|
108
|
export _sleeping=0
|
109
|
|
110
|
snapshot_update_status() {
|
111
|
${BUILDER_ROOT}/build.sh ${UPLOAD} ${POUDRIERE_SNAPSHOTS} \
|
112
|
--snapshot-update-status "$*"
|
113
|
}
|
114
|
|
115
|
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
|
git_last_commit() {
|
133
|
[ -z "${NO_RESET}" ] \
|
134
|
&& git -C "${BUILDER_ROOT}" reset --hard >/dev/null 2>&1
|
135
|
git -C "${BUILDER_ROOT}" pull -q
|
136
|
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
|
}
|
144
|
|
145
|
restart_build() {
|
146
|
if [ ${_sleeping} -ne 0 ]; then
|
147
|
snapshot_update_status ">>> SIGNINFO received, restarting build"
|
148
|
COUNTER=$((maxsleepvalue + 60))
|
149
|
fi
|
150
|
}
|
151
|
|
152
|
# 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
|
snapshots_sleep_between_runs() {
|
156
|
# 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
|
snapshot_update_status ">>> Sleeping for at least $minsleepvalue," \
|
164
|
"at most $maxsleepvalue in between snapshot builder runs."
|
165
|
snapshot_update_status ">>> Last known commit: ${LAST_COMMIT}"
|
166
|
snapshot_update_status ">>> Freezing build process at $(date)"
|
167
|
echo ">>> Press ctrl+T to start a new build"
|
168
|
COUNTER=0
|
169
|
_sleeping=1
|
170
|
while [ ${COUNTER} -lt ${minsleepvalue} ]; do
|
171
|
sleep 1
|
172
|
COUNTER=$((COUNTER + 1))
|
173
|
done
|
174
|
|
175
|
if [ ${COUNTER} -lt ${maxsleepvalue} ]; then
|
176
|
snapshot_update_status ">>> Thawing build process and" \
|
177
|
"resuming checks for pending commits at $(date)."
|
178
|
echo ">>> Press ctrl+T to start a new build"
|
179
|
fi
|
180
|
|
181
|
while [ $COUNTER -lt $maxsleepvalue ]; do
|
182
|
sleep 1
|
183
|
COUNTER=$(($COUNTER + 1))
|
184
|
# Update this repo each 60 seconds
|
185
|
if [ "$((${COUNTER} % 60))" != "0" ]; then
|
186
|
continue
|
187
|
fi
|
188
|
git_last_commit
|
189
|
if [ "${LAST_COMMIT}" != "${CURRENT_COMMIT}" ]; then
|
190
|
snapshot_update_status ">>> New commit:" \
|
191
|
"$CURRENT_COMMIT " \
|
192
|
".. No longer sleepy."
|
193
|
COUNTER=$(($maxsleepvalue + 60))
|
194
|
export LAST_COMMIT="${CURRENT_COMMIT}"
|
195
|
fi
|
196
|
done
|
197
|
_sleeping=0
|
198
|
|
199
|
if [ $COUNTER -ge $maxsleepvalue ]; then
|
200
|
snapshot_update_status ">>> Sleep timer expired." \
|
201
|
"Restarting build."
|
202
|
COUNTER=0
|
203
|
fi
|
204
|
|
205
|
trap "-" SIGINFO
|
206
|
}
|
207
|
|
208
|
# Main builder loop
|
209
|
while [ /bin/true ]; do
|
210
|
BUILDCOUNTER=$((${BUILDCOUNTER}+1))
|
211
|
|
212
|
git_last_commit
|
213
|
|
214
|
OIFS=${IFS}
|
215
|
IFS="
|
216
|
"
|
217
|
if [ -n "${POUDRIERE_SNAPSHOTS}" ]; then
|
218
|
exec_and_update_status \
|
219
|
${BUILDER_ROOT}/build.sh --update-poudriere-ports
|
220
|
rc=$?
|
221
|
|
222
|
if [ $rc -eq 0 ]; then
|
223
|
exec_and_update_status \
|
224
|
${BUILDER_ROOT}/build.sh ${UPLOAD} \
|
225
|
--update-pkg-repo
|
226
|
rc=$?
|
227
|
fi
|
228
|
else
|
229
|
exec_and_update_status \
|
230
|
${BUILDER_ROOT}/build.sh --clean-builder
|
231
|
rc=$?
|
232
|
|
233
|
if [ $rc -eq 0 ]; then
|
234
|
exec_and_update_status \
|
235
|
${BUILDER_ROOT}/build.sh ${UPLOAD} --flash-size \
|
236
|
'2g 4g' --snapshots \
|
237
|
${IMAGES}
|
238
|
rc=$?
|
239
|
fi
|
240
|
fi
|
241
|
IFS=${OIFS}
|
242
|
|
243
|
if [ -z "${LOOPED_SNAPSHOTS}" ]; then
|
244
|
# only one build required, exiting
|
245
|
exit ${rc}
|
246
|
fi
|
247
|
|
248
|
# 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
|
done
|