Project

General

Profile

Download (5.34 KB) Statistics
| Branch: | Tag: | Revision:
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
# 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
#
13
# http://www.apache.org/licenses/LICENSE-2.0
14
#
15
# 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

    
21
usage() {
22
	echo "Usage: $(basename $0) [-l] [-n] [-r] [-u] [-p]"
23
	echo "	-l: Build looped operations"
24
	echo "	-n: Do not build images, only core pkg repo"
25
	echo "	-p: Update poudriere repo"
26
	echo "	-r: Do not reset local changes"
27
	echo "	-u: Do not upload snapshots"
28
}
29

    
30
export BUILDER_TOOLS=$(realpath $(dirname ${0}))
31
export BUILDER_ROOT=$(realpath "${BUILDER_TOOLS}/..")
32

    
33
NO_IMAGES=""
34
NO_RESET=""
35
NO_UPLOAD=""
36
LOOPED_SNAPSHOTS=""
37
POUDRIERE_SNAPSHOTS=""
38

    
39
# Handle command line arguments
40
while getopts lnpru opt; do
41
	case ${opt} in
42
		n)
43
			NO_IMAGES="none"
44
			;;
45
		l)
46
			LOOPED_SNAPSHOTS=1
47
			;;
48
		p)
49
			POUDRIERE_SNAPSHOTS=--poudriere-snapshots
50
			;;
51
		r)
52
			NO_RESET=1
53
			;;
54
		u)
55
			NO_UPLOAD="-u"
56
			;;
57
		*)
58
			usage
59
			exit 1
60
			;;
61
	esac
62
done
63

    
64
if [ -n "${POUDRIERE_SNAPSHOTS}" ]; then
65
	export minsleepvalue=${minsleepvalue:-"360"}
66
else
67
	export minsleepvalue=${minsleepvalue:-"28800"}
68
fi
69
export maxsleepvalue=${maxsleepvalue:-"86400"}
70

    
71
# Keeps track of how many time builder has looped
72
export BUILDCOUNTER=0
73
export COUNTER=0
74

    
75
# Global variable used to control SIGINFO action
76
export _sleeping=0
77

    
78
snapshot_update_status() {
79
	${BUILDER_ROOT}/build.sh ${NO_UPLOAD} ${POUDRIERE_SNAPSHOTS} \
80
		--snapshot-update-status "$*"
81
}
82

    
83
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
git_last_commit() {
101
	[ -z "${NO_RESET}" ] \
102
		&& git -C "${BUILDER_ROOT}" reset --hard >/dev/null 2>&1
103
	git -C "${BUILDER_ROOT}" pull -q
104
	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
}
112

    
113
restart_build() {
114
	if [ ${_sleeping} -ne 0 ]; then
115
		snapshot_update_status ">>> SIGNINFO received, restarting build"
116
		COUNTER=$((maxsleepvalue + 60))
117
	fi
118
}
119

    
120
# 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
snapshots_sleep_between_runs() {
124
	# 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
	snapshot_update_status ">>> Sleeping for at least $minsleepvalue," \
132
		"at most $maxsleepvalue in between snapshot builder runs."
133
	snapshot_update_status ">>> Last known commit: ${LAST_COMMIT}"
134
	snapshot_update_status ">>> Freezing build process at $(date)"
135
	echo ">>> Press ctrl+T to start a new build"
136
	COUNTER=0
137
	_sleeping=1
138
	while [ ${COUNTER} -lt ${minsleepvalue} ]; do
139
		sleep 1
140
		COUNTER=$((COUNTER + 1))
141
	done
142

    
143
	if [ ${COUNTER} -lt ${maxsleepvalue} ]; then
144
		snapshot_update_status ">>> Thawing build process and" \
145
			"resuming checks for pending commits at $(date)."
146
		echo ">>> Press ctrl+T to start a new build"
147
	fi
148

    
149
	while [ $COUNTER -lt $maxsleepvalue ]; do
150
		sleep 1
151
		COUNTER=$(($COUNTER + 1))
152
		# Update this repo each 60 seconds
153
		if [ "$((${COUNTER} % 60))" != "0" ]; then
154
			continue
155
		fi
156
		git_last_commit
157
		if [ "${LAST_COMMIT}" != "${CURRENT_COMMIT}" ]; then
158
			snapshot_update_status ">>> New commit:" \
159
				"$CURRENT_COMMIT " \
160
				".. No longer sleepy."
161
			COUNTER=$(($maxsleepvalue + 60))
162
			export LAST_COMMIT="${CURRENT_COMMIT}"
163
		fi
164
	done
165
	_sleeping=0
166

    
167
	if [ $COUNTER -ge $maxsleepvalue ]; then
168
		snapshot_update_status ">>> Sleep timer expired." \
169
			"Restarting build."
170
		COUNTER=0
171
	fi
172

    
173
	trap "-" SIGINFO
174
}
175

    
176
# Main builder loop
177
while [ /bin/true ]; do
178
	BUILDCOUNTER=$((${BUILDCOUNTER}+1))
179

    
180
	git_last_commit
181

    
182
	OIFS=${IFS}
183
	IFS="
184
"
185
	if [ -n "${POUDRIERE_SNAPSHOTS}" ]; then
186
		exec_and_update_status \
187
		    ${BUILDER_ROOT}/build.sh --update-poudriere-ports \
188
		    || exit $?
189

    
190
		exec_and_update_status \
191
		    ${BUILDER_ROOT}/build.sh ${NO_UPLOAD} --update-pkg-repo \
192
		    || exit $?
193
	else
194
		exec_and_update_status \
195
		    ${BUILDER_ROOT}/build.sh --clean-builder \
196
		    || exit $?
197

    
198
		exec_and_update_status \
199
		    ${BUILDER_ROOT}/build.sh ${NO_UPLOAD} --snapshots \
200
		    ${NO_IMAGES} "memstick memstickadi memstickserial iso" \
201
		    || exit $?
202
	fi
203
	IFS=${OIFS}
204

    
205
	if [ -z "${LOOPED_SNAPSHOTS}" ]; then
206
		# only one build required, exiting
207
		exit
208
	fi
209

    
210
	# Count some sheep or wait until a new commit turns up
211
	# for one days time.  We will wake up if a new commit
212
	# is detected during sleepy time.
213
	snapshots_sleep_between_runs
214
done
(1-1/3)