1 # Copyright 2012 Google Inc.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of Google Inc. nor the names of its contributors
14 # may be used to endorse or promote products derived from this software
15 # without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # \file sysbuild4cron.sh
30 # Entry point and main program logic.
35 # Paths to installed files.
37 # Can be overriden for test purposes only.
38 : ${SYSBUILD_BINDIR="@SYSBUILD_BINDIR@"}
41 # Sends an email notification upon a failure.
43 # \param logfile File containing the output of the failed command.
44 # \param recipient Who to send the email to.
45 # \param ... Command that failed.
46 sysbuild4cron_email
() {
47 local logfile
="${1}"; shift
48 local recipient
="${1}"; shift
52 echo "The following command has failed:"
56 echo "The output of the failed command has been left in:"
60 echo "The last ${maxlines} of the log follow:"
62 tail -n${maxlines} "${logfile}"
63 } |
mail -s "sysbuild failure report" "${recipient}"
67 # Entry point to the program.
69 # \param ... Command-line arguments to be processed.
71 # \return An exit code to be returned to the user.
72 sysbuild4cron_main
() {
73 local logdir
="${HOME}/sysbuild/log"
74 local recipient
="$(id -u -n)"
76 while getopts ':l:r:' arg
"${@}"; do
78 l
) # Location of logs.
80 /*) logdir
="${OPTARG}" ;;
81 *) logdir
="$(pwd)/${OPTARG}" ;;
90 shtk_cli_usage_error
"Unknown option -${OPTARG}"
94 shift $
((${OPTIND} - 1))
96 set -- "${SYSBUILD_BINDIR}/sysbuild" "${@}"
98 if [ ! -d "${logdir}" ]; then
99 mkdir
-p "$(dirname "${logdir}")" >/dev
/null
2>/dev
/null || true
101 || shtk_cli_error
"Failed to create directory '${logdir}'"
105 local logfile
="${logdir}/$(shtk_cli_progname).$(date +%Y%m%d%H%M%S).log"
106 "${@}" >"${logfile}" 2>&1 \
107 || sysbuild4cron_email
"${logfile}" "${recipient}" "${@}"