Project

General

Profile

Download (5.07 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
# Use an env var to let build.sh know we are running build_snapshots.sh
41
# This will avoid build.sh to run in interactive mode and wait a key
42
# to be pressed when something goes wrong
43
export NOT_INTERACTIVE=1
44

    
45
export BUILDER_TOOLS=$(realpath $(dirname ${0}))
46
export BUILDER_ROOT=$(realpath "${BUILDER_TOOLS}/..")
47

    
48
NO_RESET=""
49
NO_UPLOAD=""
50
LOOPED_SNAPSHOTS=""
51
export minsleepvalue=${minsleepvalue:-"28800"}
52
export maxsleepvalue=${maxsleepvalue:-"86400"}
53

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

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

    
77
# Global variable used to control SIGINFO action
78
export _sleeping=0
79

    
80
snapshot_update_statu() {
81
	${BUILDER_ROOT}/build.sh --snapshot-update-status "${1}"
82
}
83

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

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

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

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

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

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

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

    
142
	if [ $COUNTER -ge $maxsleepvalue ]; then
143
		snapshot_update_status ">>> Sleep timer expired. Restarting build."
144
		COUNTER=0
145
	fi
146

    
147
	trap "-" SIGINFO
148
}
149

    
150
# Main builder loop
151
while [ /bin/true ]; do
152
	BUILDCOUNTER=$((${BUILDCOUNTER}+1))
153

    
154
	git_last_commit
155

    
156
	(${BUILDER_ROOT}/build.sh --clean-builder 2>&1) | while read -r LINE; do
157
		snapshot_update_status "${LINE}"
158
	done
159

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

    
164
	if [ -z "${LOOPED_SNAPSHOTS}" ]; then
165
		# only one build required, exiting
166
		exit
167
	fi
168

    
169
	# Count some sheep or wait until a new commit turns up
170
	# for one days time.  We will wake up if a new commit
171
	# is detected during sleepy time.
172
	snapshots_sleep_between_runs
173
done
(1-1/3)