1
|
#!/bin/sh -T
|
2
|
#
|
3
|
# build_snapshots.sh
|
4
|
#
|
5
|
# part of pfSense (https://www.pfsense.org)
|
6
|
# Copyright (c) 2004-2013 BSD Perimeter
|
7
|
# Copyright (c) 2013-2016 Electric Sheep Fencing
|
8
|
# Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
|
9
|
# All rights reserved.
|
10
|
#
|
11
|
# 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
|
#
|
15
|
# http://www.apache.org/licenses/LICENSE-2.0
|
16
|
#
|
17
|
# 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
|
|
23
|
usage() {
|
24
|
echo "Usage: $(basename $0) [-l] [-n] [-r] [-U] [-p] [-i]"
|
25
|
echo " -l: Build looped operations"
|
26
|
echo " -n: Do not build images, only core pkg repo"
|
27
|
echo " -p: Update poudriere repo"
|
28
|
echo " -r: Do not reset local changes"
|
29
|
echo " -U: Upload snapshots"
|
30
|
echo " -i: Skip rsync to final server"
|
31
|
}
|
32
|
|
33
|
export BUILDER_TOOLS=$(realpath $(dirname ${0}))
|
34
|
export BUILDER_ROOT=$(realpath "${BUILDER_TOOLS}/..")
|
35
|
|
36
|
IMAGES="all"
|
37
|
NO_RESET=""
|
38
|
UPLOAD=""
|
39
|
_SKIP_FINAL_RSYNC=""
|
40
|
LOOPED_SNAPSHOTS=""
|
41
|
POUDRIERE_SNAPSHOTS=""
|
42
|
|
43
|
# Handle command line arguments
|
44
|
while getopts lnprUi opt; do
|
45
|
case ${opt} in
|
46
|
n)
|
47
|
IMAGES="none"
|
48
|
;;
|
49
|
l)
|
50
|
LOOPED_SNAPSHOTS=1
|
51
|
;;
|
52
|
p)
|
53
|
POUDRIERE_SNAPSHOTS=--poudriere-snapshots
|
54
|
;;
|
55
|
r)
|
56
|
NO_RESET=1
|
57
|
;;
|
58
|
U)
|
59
|
UPLOAD="-U"
|
60
|
;;
|
61
|
i)
|
62
|
_SKIP_FINAL_RSYNC="-i"
|
63
|
;;
|
64
|
*)
|
65
|
usage
|
66
|
exit 1
|
67
|
;;
|
68
|
esac
|
69
|
done
|
70
|
|
71
|
if [ -n "${POUDRIERE_SNAPSHOTS}" ]; then
|
72
|
export minsleepvalue=${minsleepvalue:-"360"}
|
73
|
else
|
74
|
export minsleepvalue=${minsleepvalue:-"28800"}
|
75
|
fi
|
76
|
export maxsleepvalue=${maxsleepvalue:-"86400"}
|
77
|
|
78
|
# Keeps track of how many time builder has looped
|
79
|
export BUILDCOUNTER=0
|
80
|
export COUNTER=0
|
81
|
|
82
|
# Global variable used to control SIGINFO action
|
83
|
export _sleeping=0
|
84
|
|
85
|
snapshot_update_status() {
|
86
|
${BUILDER_ROOT}/build.sh ${_SKIP_FINAL_RSYNC} ${UPLOAD} \
|
87
|
${POUDRIERE_SNAPSHOTS} --snapshot-update-status "$*"
|
88
|
}
|
89
|
|
90
|
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
|
git_last_commit() {
|
108
|
[ -z "${NO_RESET}" ] \
|
109
|
&& git -C "${BUILDER_ROOT}" reset --hard >/dev/null 2>&1
|
110
|
git -C "${BUILDER_ROOT}" pull -q
|
111
|
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
|
}
|
119
|
|
120
|
restart_build() {
|
121
|
if [ ${_sleeping} -ne 0 ]; then
|
122
|
snapshot_update_status ">>> SIGNINFO received, restarting build"
|
123
|
COUNTER=$((maxsleepvalue + 60))
|
124
|
fi
|
125
|
}
|
126
|
|
127
|
# 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
|
snapshots_sleep_between_runs() {
|
131
|
# 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
|
snapshot_update_status ">>> Sleeping for at least $minsleepvalue," \
|
139
|
"at most $maxsleepvalue in between snapshot builder runs."
|
140
|
snapshot_update_status ">>> Last known commit: ${LAST_COMMIT}"
|
141
|
snapshot_update_status ">>> Freezing build process at $(date)"
|
142
|
echo ">>> Press ctrl+T to start a new build"
|
143
|
COUNTER=0
|
144
|
_sleeping=1
|
145
|
while [ ${COUNTER} -lt ${minsleepvalue} ]; do
|
146
|
sleep 1
|
147
|
COUNTER=$((COUNTER + 1))
|
148
|
done
|
149
|
|
150
|
if [ ${COUNTER} -lt ${maxsleepvalue} ]; then
|
151
|
snapshot_update_status ">>> Thawing build process and" \
|
152
|
"resuming checks for pending commits at $(date)."
|
153
|
echo ">>> Press ctrl+T to start a new build"
|
154
|
fi
|
155
|
|
156
|
while [ $COUNTER -lt $maxsleepvalue ]; do
|
157
|
sleep 1
|
158
|
COUNTER=$(($COUNTER + 1))
|
159
|
# Update this repo each 60 seconds
|
160
|
if [ "$((${COUNTER} % 60))" != "0" ]; then
|
161
|
continue
|
162
|
fi
|
163
|
git_last_commit
|
164
|
if [ "${LAST_COMMIT}" != "${CURRENT_COMMIT}" ]; then
|
165
|
snapshot_update_status ">>> New commit:" \
|
166
|
"$CURRENT_COMMIT " \
|
167
|
".. No longer sleepy."
|
168
|
COUNTER=$(($maxsleepvalue + 60))
|
169
|
export LAST_COMMIT="${CURRENT_COMMIT}"
|
170
|
fi
|
171
|
done
|
172
|
_sleeping=0
|
173
|
|
174
|
if [ $COUNTER -ge $maxsleepvalue ]; then
|
175
|
snapshot_update_status ">>> Sleep timer expired." \
|
176
|
"Restarting build."
|
177
|
COUNTER=0
|
178
|
fi
|
179
|
|
180
|
trap "-" SIGINFO
|
181
|
}
|
182
|
|
183
|
# Main builder loop
|
184
|
while [ /bin/true ]; do
|
185
|
BUILDCOUNTER=$((${BUILDCOUNTER}+1))
|
186
|
|
187
|
git_last_commit
|
188
|
|
189
|
OIFS=${IFS}
|
190
|
IFS="
|
191
|
"
|
192
|
if [ -n "${POUDRIERE_SNAPSHOTS}" ]; then
|
193
|
exec_and_update_status \
|
194
|
${BUILDER_ROOT}/build.sh --update-poudriere-ports
|
195
|
rc=$?
|
196
|
|
197
|
if [ $rc -eq 0 ]; then
|
198
|
exec_and_update_status \
|
199
|
${BUILDER_ROOT}/build.sh ${_SKIP_FINAL_RSYNC} \
|
200
|
${UPLOAD} --update-pkg-repo
|
201
|
rc=$?
|
202
|
fi
|
203
|
else
|
204
|
exec_and_update_status \
|
205
|
${BUILDER_ROOT}/build.sh --clean-builder
|
206
|
rc=$?
|
207
|
|
208
|
if [ $rc -eq 0 ]; then
|
209
|
exec_and_update_status \
|
210
|
${BUILDER_ROOT}/build.sh ${_SKIP_FINAL_RSYNC} \
|
211
|
${UPLOAD} --snapshots ${IMAGES}
|
212
|
rc=$?
|
213
|
fi
|
214
|
fi
|
215
|
IFS=${OIFS}
|
216
|
|
217
|
if [ -z "${LOOPED_SNAPSHOTS}" ]; then
|
218
|
# only one build required, exiting
|
219
|
exit ${rc}
|
220
|
fi
|
221
|
|
222
|
# 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
|
done
|