Project

General

Profile

Download (4.88 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]"
35
	echo "	-l: Build looped operations"
36
	echo "	-r: Do not reset local changes"
37
	echo "	-u: Do not upload snapshots"
38
}
39

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

    
43
NO_RESET=""
44
NO_UPLOAD=""
45
LOOPED_SNAPSHOTS=""
46
export minsleepvalue=${minsleepvalue:-"28800"}
47
export maxsleepvalue=${maxsleepvalue:-"86400"}
48

    
49
# Handle command line arguments
50
while getopts lur opt; do
51
	case ${opt} in
52
		l)
53
			LOOPED_SNAPSHOTS=1
54
			;;
55
		r)
56
			NO_RESET=1
57
			;;
58
		u)
59
			NO_UPLOAD="-u"
60
			;;
61
		*)
62
			usage
63
			exit 1
64
			;;
65
	esac
66
done
67

    
68
# Keeps track of how many time builder has looped
69
export BUILDCOUNTER=0
70
export COUNTER=0
71

    
72
# Global variable used to control SIGINFO action
73
export _sleeping=0
74

    
75
snapshot_update_status() {
76
	${BUILDER_ROOT}/build.sh ${NO_UPLOAD} --snapshot-update-status "${1}"
77
}
78

    
79
git_last_commit() {
80
	snapshot_update_status ">>> Updating ${PRODUCT_NAME} repository."
81
	[ -z "${NO_RESET}" ] \
82
		&& git -C "${BUILDER_ROOT}" reset --hard >/dev/null 2>&1
83
	git -C "${BUILDER_ROOT}" pull -q
84
	export CURRENT_COMMIT=$(git -C ${BUILDER_ROOT} log -1 --format='%H')
85
	export CURRENT_AUTHOR=$(git -C ${BUILDER_ROOT} log -1 --format='%an')
86
}
87

    
88
restart_build() {
89
	if [ ${_sleeping} -ne 0 ]; then
90
		snapshot_update_status ">>> SIGNINFO received, restarting build"
91
		COUNTER=$((maxsleepvalue + 60))
92
	fi
93
}
94

    
95
# This routine is called in between runs. We
96
# will sleep for a bit and check for new commits
97
# in between sleeping for short durations.
98
snapshots_sleep_between_runs() {
99
	# Handle SIGINFO (ctrl+T) and restart build
100
	trap restart_build SIGINFO
101

    
102
	# Initialize variables that keep track of last commit
103
	[ -z "${LAST_COMMIT}" ] \
104
		&& export LAST_COMMIT=${CURRENT_COMMIT}
105

    
106
	snapshot_update_status ">>> Sleeping for at least $minsleepvalue, at most $maxsleepvalue in between snapshot builder runs."
107
	snapshot_update_status ">>> Last known commit: ${LAST_COMMIT}"
108
	snapshot_update_status ">>> Freezing build process at $(date)"
109
	echo ">>> Press ctrl+T to start a new build"
110
	COUNTER=0
111
	_sleeping=1
112
	while [ ${COUNTER} -lt ${minsleepvalue} ]; do
113
		sleep 1
114
		COUNTER=$((COUNTER + 1))
115
	done
116

    
117
	if [ ${COUNTER} -lt ${maxsleepvalue} ]; then
118
		snapshot_update_status ">>> Thawing build process and resuming checks for pending commits at $(date)."
119
		echo ">>> Press ctrl+T to start a new build"
120
	fi
121

    
122
	while [ $COUNTER -lt $maxsleepvalue ]; do
123
		sleep 1
124
		# Update this repo each 60 seconds
125
		if [ "$((${COUNTER} % 60))" = "0" ]; then
126
			git_last_commit
127
			if [ "${LAST_COMMIT}" != "${CURRENT_COMMIT}" ]; then
128
				snapshot_update_status ">>> New commit: $CURRENT_AUTHOR - $CURRENT_COMMIT .. No longer sleepy."
129
				COUNTER=$(($maxsleepvalue + 60))
130
				export LAST_COMMIT="${CURRENT_COMMIT}"
131
			fi
132
		fi
133
		COUNTER=$(($COUNTER + 1))
134
	done
135
	_sleeping=0
136

    
137
	if [ $COUNTER -ge $maxsleepvalue ]; then
138
		snapshot_update_status ">>> Sleep timer expired. Restarting build."
139
		COUNTER=0
140
	fi
141

    
142
	trap "-" SIGINFO
143
}
144

    
145
# Main builder loop
146
while [ /bin/true ]; do
147
	BUILDCOUNTER=$((${BUILDCOUNTER}+1))
148

    
149
	git_last_commit
150

    
151
	(${BUILDER_ROOT}/build.sh --clean-builder 2>&1) | while read -r LINE; do
152
		snapshot_update_status "${LINE}"
153
	done
154

    
155
	(${BUILDER_ROOT}/build.sh ${NO_UPLOAD} --flash-size '1g 2g 4g' --snapshots 2>&1) | while read -r LINE; do
156
		snapshot_update_status "${LINE}"
157
	done
158

    
159
	if [ -z "${LOOPED_SNAPSHOTS}" ]; then
160
		# only one build required, exiting
161
		exit
162
	fi
163

    
164
	# Count some sheep or wait until a new commit turns up
165
	# for one days time.  We will wake up if a new commit
166
	# is detected during sleepy time.
167
	snapshots_sleep_between_runs
168
done
(1-1/3)