Project

General

Profile

Download (5.63 KB) Statistics
| Branch: | Tag: | Revision:
1 6f73c362 Renato Botelho
#!/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 b90e12c2 Renato Botelho
usage() {
34 919c8486 Renato Botelho
	echo "Usage: $(basename $0) [-l] [-r] [-u] [-p]"
35 b90e12c2 Renato Botelho
	echo "	-l: Build looped operations"
36 919c8486 Renato Botelho
	echo "	-p: Update poudriere repo"
37 b90e12c2 Renato Botelho
	echo "	-r: Do not reset local changes"
38
	echo "	-u: Do not upload snapshots"
39
}
40
41 f36b26f8 Renato Botelho
export BUILDER_TOOLS=$(realpath $(dirname ${0}))
42
export BUILDER_ROOT=$(realpath "${BUILDER_TOOLS}/..")
43 6f73c362 Renato Botelho
44 b90e12c2 Renato Botelho
NO_RESET=""
45 6f73c362 Renato Botelho
NO_UPLOAD=""
46
LOOPED_SNAPSHOTS=""
47 919c8486 Renato Botelho
POUDRIERE_SNAPSHOTS=""
48 6f73c362 Renato Botelho
49
# Handle command line arguments
50 919c8486 Renato Botelho
while getopts lpru opt; do
51 b90e12c2 Renato Botelho
	case ${opt} in
52
		l)
53
			LOOPED_SNAPSHOTS=1
54
			;;
55 919c8486 Renato Botelho
		p)
56
			POUDRIERE_SNAPSHOTS=--poudriere-snapshots
57
			;;
58 b90e12c2 Renato Botelho
		r)
59
			NO_RESET=1
60
			;;
61
		u)
62
			NO_UPLOAD="-u"
63
			;;
64
		*)
65
			usage
66
			exit 1
67
			;;
68 6f73c362 Renato Botelho
	esac
69
done
70
71 919c8486 Renato Botelho
if [ -n "${POUDRIERE_SNAPSHOTS}" ]; then
72 da869279 Renato Botelho
	export minsleepvalue=${minsleepvalue:-"360"}
73 919c8486 Renato Botelho
else
74
	export minsleepvalue=${minsleepvalue:-"28800"}
75
fi
76
export maxsleepvalue=${maxsleepvalue:-"86400"}
77
78 6f73c362 Renato Botelho
# Keeps track of how many time builder has looped
79 7bc3be6f Renato Botelho
export BUILDCOUNTER=0
80 3e05b544 Renato Botelho
export COUNTER=0
81
82 7bc3be6f Renato Botelho
# Global variable used to control SIGINFO action
83
export _sleeping=0
84 6f73c362 Renato Botelho
85 e5f88c31 Renato Botelho
snapshot_update_status() {
86 919c8486 Renato Botelho
	${BUILDER_ROOT}/build.sh ${NO_UPLOAD} ${POUDRIERE_SNAPSHOTS} \
87 0cc93b74 Renato Botelho
		--snapshot-update-status "$*"
88 032171ee Renato Botelho
}
89
90 ab943fc9 Renato Botelho
git_last_commit() {
91 b90e12c2 Renato Botelho
	[ -z "${NO_RESET}" ] \
92
		&& git -C "${BUILDER_ROOT}" reset --hard >/dev/null 2>&1
93
	git -C "${BUILDER_ROOT}" pull -q
94 1076fc7a Renato Botelho
	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 ab943fc9 Renato Botelho
}
102 6f73c362 Renato Botelho
103 3e05b544 Renato Botelho
restart_build() {
104 7bc3be6f Renato Botelho
	if [ ${_sleeping} -ne 0 ]; then
105 032171ee Renato Botelho
		snapshot_update_status ">>> SIGNINFO received, restarting build"
106 3e05b544 Renato Botelho
		COUNTER=$((maxsleepvalue + 60))
107
	fi
108
}
109
110 6f73c362 Renato Botelho
# 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 ab943fc9 Renato Botelho
snapshots_sleep_between_runs() {
114 8a6723d5 Renato Botelho
	# 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 2251aa18 Renato Botelho
	snapshot_update_status ">>> Sleeping for at least $minsleepvalue," \
122 9447077e Renato Botelho
		"at most $maxsleepvalue in between snapshot builder runs."
123 032171ee Renato Botelho
	snapshot_update_status ">>> Last known commit: ${LAST_COMMIT}"
124
	snapshot_update_status ">>> Freezing build process at $(date)"
125 8a6723d5 Renato Botelho
	echo ">>> Press ctrl+T to start a new build"
126
	COUNTER=0
127 7bc3be6f Renato Botelho
	_sleeping=1
128 8a6723d5 Renato Botelho
	while [ ${COUNTER} -lt ${minsleepvalue} ]; do
129
		sleep 1
130
		COUNTER=$((COUNTER + 1))
131
	done
132
133
	if [ ${COUNTER} -lt ${maxsleepvalue} ]; then
134 2251aa18 Renato Botelho
		snapshot_update_status ">>> Thawing build process and" \
135 9447077e Renato Botelho
			"resuming checks for pending commits at $(date)."
136 8a6723d5 Renato Botelho
		echo ">>> Press ctrl+T to start a new build"
137
	fi
138
139 6f73c362 Renato Botelho
	while [ $COUNTER -lt $maxsleepvalue ]; do
140 7bc3be6f Renato Botelho
		sleep 1
141 9447077e Renato Botelho
		COUNTER=$(($COUNTER + 1))
142 7bc3be6f Renato Botelho
		# Update this repo each 60 seconds
143 9447077e Renato Botelho
		if [ "$((${COUNTER} % 60))" != "0" ]; then
144
			continue
145
		fi
146
		git_last_commit
147
		if [ "${LAST_COMMIT}" != "${CURRENT_COMMIT}" ]; then
148 2251aa18 Renato Botelho
			snapshot_update_status ">>> New commit:" \
149 7f330caa Renato Botelho
				"$CURRENT_COMMIT " \
150 9447077e Renato Botelho
				".. No longer sleepy."
151
			COUNTER=$(($maxsleepvalue + 60))
152
			export LAST_COMMIT="${CURRENT_COMMIT}"
153 6f73c362 Renato Botelho
		fi
154
	done
155 7bc3be6f Renato Botelho
	_sleeping=0
156 8a6723d5 Renato Botelho
157 6f73c362 Renato Botelho
	if [ $COUNTER -ge $maxsleepvalue ]; then
158 2251aa18 Renato Botelho
		snapshot_update_status ">>> Sleep timer expired." \
159 9447077e Renato Botelho
			"Restarting build."
160 6f73c362 Renato Botelho
		COUNTER=0
161
	fi
162 8a6723d5 Renato Botelho
163
	trap "-" SIGINFO
164 6f73c362 Renato Botelho
}
165
166 ab943fc9 Renato Botelho
# Main builder loop
167
while [ /bin/true ]; do
168
	BUILDCOUNTER=$((${BUILDCOUNTER}+1))
169 6f73c362 Renato Botelho
170 b90e12c2 Renato Botelho
	git_last_commit
171 670a33c5 Renato Botelho
172 919c8486 Renato Botelho
	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 7d9c553c Renato Botelho
		(${BUILDER_ROOT}/build.sh ${NO_UPLOAD} --flash-size '2g 4g' \
189 919c8486 Renato Botelho
		    --snapshots 2>&1) | while read -r LINE; do
190
			snapshot_update_status "${LINE}"
191
		done
192
	fi
193 6f73c362 Renato Botelho
194 ab943fc9 Renato Botelho
	if [ -z "${LOOPED_SNAPSHOTS}" ]; then
195
		# only one build required, exiting
196
		exit
197 6f73c362 Renato Botelho
	fi
198
199 8a6723d5 Renato Botelho
	# 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 ab943fc9 Renato Botelho
done