Project

General

Profile

Download (5.14 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
git_last_commit() {
84
	[ -z "${NO_RESET}" ] \
85
		&& git -C "${BUILDER_ROOT}" reset --hard >/dev/null 2>&1
86
	git -C "${BUILDER_ROOT}" pull -q
87
	if [ -n "${POUDRIERE_SNAPSHOTS}" ]; then
88
		local _remote_repo=$(${BUILDER_ROOT}/build.sh -V POUDRIERE_PORTS_GIT_URL)
89
		local _remote_branch=$(${BUILDER_ROOT}/build.sh -V POUDRIERE_PORTS_GIT_BRANCH)
90
		export CURRENT_COMMIT=$(git ls-remote ${_remote_repo} ${_remote_branch} | cut -f1)
91
	else
92
		export CURRENT_COMMIT=$(git -C ${BUILDER_ROOT} log -1 --format='%H')
93
	fi
94
}
95

    
96
restart_build() {
97
	if [ ${_sleeping} -ne 0 ]; then
98
		snapshot_update_status ">>> SIGNINFO received, restarting build"
99
		COUNTER=$((maxsleepvalue + 60))
100
	fi
101
}
102

    
103
# This routine is called in between runs. We
104
# will sleep for a bit and check for new commits
105
# in between sleeping for short durations.
106
snapshots_sleep_between_runs() {
107
	# Handle SIGINFO (ctrl+T) and restart build
108
	trap restart_build SIGINFO
109

    
110
	# Initialize variables that keep track of last commit
111
	[ -z "${LAST_COMMIT}" ] \
112
		&& export LAST_COMMIT=${CURRENT_COMMIT}
113

    
114
	snapshot_update_status ">>> Sleeping for at least $minsleepvalue," \
115
		"at most $maxsleepvalue in between snapshot builder runs."
116
	snapshot_update_status ">>> Last known commit: ${LAST_COMMIT}"
117
	snapshot_update_status ">>> Freezing build process at $(date)"
118
	echo ">>> Press ctrl+T to start a new build"
119
	COUNTER=0
120
	_sleeping=1
121
	while [ ${COUNTER} -lt ${minsleepvalue} ]; do
122
		sleep 1
123
		COUNTER=$((COUNTER + 1))
124
	done
125

    
126
	if [ ${COUNTER} -lt ${maxsleepvalue} ]; then
127
		snapshot_update_status ">>> Thawing build process and" \
128
			"resuming checks for pending commits at $(date)."
129
		echo ">>> Press ctrl+T to start a new build"
130
	fi
131

    
132
	while [ $COUNTER -lt $maxsleepvalue ]; do
133
		sleep 1
134
		COUNTER=$(($COUNTER + 1))
135
		# Update this repo each 60 seconds
136
		if [ "$((${COUNTER} % 60))" != "0" ]; then
137
			continue
138
		fi
139
		git_last_commit
140
		if [ "${LAST_COMMIT}" != "${CURRENT_COMMIT}" ]; then
141
			snapshot_update_status ">>> New commit:" \
142
				"$CURRENT_COMMIT " \
143
				".. No longer sleepy."
144
			COUNTER=$(($maxsleepvalue + 60))
145
			export LAST_COMMIT="${CURRENT_COMMIT}"
146
		fi
147
	done
148
	_sleeping=0
149

    
150
	if [ $COUNTER -ge $maxsleepvalue ]; then
151
		snapshot_update_status ">>> Sleep timer expired." \
152
			"Restarting build."
153
		COUNTER=0
154
	fi
155

    
156
	trap "-" SIGINFO
157
}
158

    
159
# Main builder loop
160
while [ /bin/true ]; do
161
	BUILDCOUNTER=$((${BUILDCOUNTER}+1))
162

    
163
	git_last_commit
164

    
165
	OIFS=${IFS}
166
	IFS="
167
"
168
	if [ -n "${POUDRIERE_SNAPSHOTS}" ]; then
169
		(${BUILDER_ROOT}/build.sh --update-poudriere-ports 2>&1) \
170
		    | while read -r LINE; do
171
			snapshot_update_status "${LINE}"
172
		done
173

    
174
		(${BUILDER_ROOT}/build.sh ${NO_UPLOAD} --update-pkg-repo 2>&1) \
175
		    | while read -r LINE; do
176
			snapshot_update_status "${LINE}"
177
		done
178
	else
179
		(${BUILDER_ROOT}/build.sh --clean-builder 2>&1) \
180
		    | while read -r LINE; do
181
			snapshot_update_status "${LINE}"
182
		done
183

    
184
		(${BUILDER_ROOT}/build.sh ${NO_UPLOAD} \
185
		    --snapshots ${NO_IMAGES} "memstick memstickadi memstickserial iso" 2>&1) \
186
		    | while read -r LINE; do
187
			snapshot_update_status "${LINE}"
188
		done
189
	fi
190
	IFS=${OIFS}
191

    
192
	if [ -z "${LOOPED_SNAPSHOTS}" ]; then
193
		# only one build required, exiting
194
		exit
195
	fi
196

    
197
	# Count some sheep or wait until a new commit turns up
198
	# for one days time.  We will wake up if a new commit
199
	# is detected during sleepy time.
200
	snapshots_sleep_between_runs
201
done
(1-1/3)