Project

General

Profile

Download (5.63 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
#
3
# build_snapshots.sh
4
#
5
# Copyright (c) 2007-2015 Electric Sheep Fencing, LLC
6
# All rights reserved
7
#
8
# Redistribution and use in source and binary forms, with or without
9
# modification, are permitted provided that the following conditions
10
# are met:
11
#
12
# 1. Redistributions of source code must retain the above copyright
13
#    notice, 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 the
17
#    documentation and/or other materials provided with the distribution.
18
#
19
# THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
20
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
23
# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30
# OF THE POSSIBILITY OF SUCH DAMAGE.
31
#
32

    
33
usage() {
34
	echo "Usage: $(basename $0) [-l] [-r] [-u] [-p]"
35
	echo "	-l: Build looped operations"
36
	echo "	-p: Update poudriere repo"
37
	echo "	-r: Do not reset local changes"
38
	echo "	-u: Do not upload snapshots"
39
}
40

    
41
export BUILDER_TOOLS=$(realpath $(dirname ${0}))
42
export BUILDER_ROOT=$(realpath "${BUILDER_TOOLS}/..")
43

    
44
NO_RESET=""
45
NO_UPLOAD=""
46
LOOPED_SNAPSHOTS=""
47
POUDRIERE_SNAPSHOTS=""
48

    
49
# Handle command line arguments
50
while getopts lpru opt; do
51
	case ${opt} in
52
		l)
53
			LOOPED_SNAPSHOTS=1
54
			;;
55
		p)
56
			POUDRIERE_SNAPSHOTS=--poudriere-snapshots
57
			;;
58
		r)
59
			NO_RESET=1
60
			;;
61
		u)
62
			NO_UPLOAD="-u"
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 ${NO_UPLOAD} ${POUDRIERE_SNAPSHOTS} \
87
		--snapshot-update-status "$*"
88
}
89

    
90
git_last_commit() {
91
	[ -z "${NO_RESET}" ] \
92
		&& git -C "${BUILDER_ROOT}" reset --hard >/dev/null 2>&1
93
	git -C "${BUILDER_ROOT}" pull -q
94
	if [ -n "${POUDRIERE_SNAPSHOTS}" ]; then
95
		local _remote_repo=$(${BUILDER_ROOT}/build.sh -V POUDRIERE_PORTS_GIT_URL)
96
		local _remote_branch=$(${BUILDER_ROOT}/build.sh -V POUDRIERE_PORTS_GIT_BRANCH)
97
		export CURRENT_COMMIT=$(git ls-remote ${_remote_repo} ${_remote_branch} | cut -f1)
98
	else
99
		export CURRENT_COMMIT=$(git -C ${BUILDER_ROOT} log -1 --format='%H')
100
	fi
101
}
102

    
103
restart_build() {
104
	if [ ${_sleeping} -ne 0 ]; then
105
		snapshot_update_status ">>> SIGNINFO received, restarting build"
106
		COUNTER=$((maxsleepvalue + 60))
107
	fi
108
}
109

    
110
# This routine is called in between runs. We
111
# will sleep for a bit and check for new commits
112
# in between sleeping for short durations.
113
snapshots_sleep_between_runs() {
114
	# Handle SIGINFO (ctrl+T) and restart build
115
	trap restart_build SIGINFO
116

    
117
	# Initialize variables that keep track of last commit
118
	[ -z "${LAST_COMMIT}" ] \
119
		&& export LAST_COMMIT=${CURRENT_COMMIT}
120

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

    
133
	if [ ${COUNTER} -lt ${maxsleepvalue} ]; then
134
		snapshot_update_status ">>> Thawing build process and" \
135
			"resuming checks for pending commits at $(date)."
136
		echo ">>> Press ctrl+T to start a new build"
137
	fi
138

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

    
157
	if [ $COUNTER -ge $maxsleepvalue ]; then
158
		snapshot_update_status ">>> Sleep timer expired." \
159
			"Restarting build."
160
		COUNTER=0
161
	fi
162

    
163
	trap "-" SIGINFO
164
}
165

    
166
# Main builder loop
167
while [ /bin/true ]; do
168
	BUILDCOUNTER=$((${BUILDCOUNTER}+1))
169

    
170
	git_last_commit
171

    
172
	if [ -n "${POUDRIERE_SNAPSHOTS}" ]; then
173
		(${BUILDER_ROOT}/build.sh --update-poudriere-ports 2>&1) \
174
		    | while read -r LINE; do
175
			snapshot_update_status "${LINE}"
176
		done
177

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

    
188
		(${BUILDER_ROOT}/build.sh ${NO_UPLOAD} --flash-size '2g 4g' \
189
		    --snapshots 2>&1) | while read -r LINE; do
190
			snapshot_update_status "${LINE}"
191
		done
192
	fi
193

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

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