| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# |
| 4 |
|
| 5 |
# TODO: Allow regular DAPS |
| 6 |
|
| 7 |
ME=$(basename "$0") |
| 8 |
|
| 9 |
|
| 10 |
SCHEMA="@datadir@/xml/daps/schema/daps-autobuild.rnc" |
| 11 |
#CONFIG="$(readlink -e "daps-autobuild.xml")" |
| 12 |
#SCHEMA="$(readlink -e "daps-autobuild.rnc")" |
| 13 |
|
| 14 |
#VERBOSE=1 |
| 15 |
CHECK_VCS=1 |
| 16 |
DEBUG=0 |
| 17 |
DO_RSYNC=1 |
| 18 |
RSYNC_DELETE="" |
| 19 |
SECTIONS="" |
| 20 |
SEND_REPORTS=0 |
| 21 |
|
| 22 |
RSYNC="/usr/bin/rsync" |
| 23 |
CCECHO="/usr/bin/ccecho" |
| 24 |
if [[ -x /usr/bin/xml ]]; then |
| 25 |
XMLSTARLET="/usr/bin/xml" |
| 26 |
elif [[ -x /usr/bin/xmlstarlet ]]; then |
| 27 |
XMLSTARLET="/usr/bin/xmlstarlet" |
| 28 |
else |
| 29 |
exit_on_error "Cannot find an xmlstarlet binary in /usr/bin/" |
| 30 |
fi |
| 31 |
|
| 32 |
FAILURES=0 |
| 33 |
#declare -A FAILED_BOOKS |
| 34 |
|
| 35 |
_BUILDDIR="" |
| 36 |
_DAPSROOT="" |
| 37 |
_RSYNC_TARGET="" |
| 38 |
_RSYNC_FLAGS="" |
| 39 |
|
| 40 |
declare -a ALL_SETIDS |
| 41 |
|
| 42 |
RUNDAPS="" |
| 43 |
|
| 44 |
function usage { |
| 45 |
cat <<EOFhelptext |
| 46 |
Usage: |
| 47 |
$ME --config <PATH TO CONFIG FILE> [OPTIONS] |
| 48 |
|
| 49 |
Automatically build books defined in a config file and sync them to a server. |
| 50 |
The config file is an XML file, see $ME --helpconfig for more information. |
| 51 |
|
| 52 |
Specifying a config file with --config is mandatory, all other options are |
| 53 |
optional. |
| 54 |
|
| 55 |
Options: |
| 56 |
|
| 57 |
--config=<file> |
| 58 |
Path to config file. Can be specified relative or absolute. |
| 59 |
Default: unset |
| 60 |
|
| 61 |
--dcfiles="dc-file1 dc-file2" |
| 62 |
Space-separated list of dcfiles that should be build. Requires |
| 63 |
--sections to be set. The dc-files specified need to be defined |
| 64 |
in the given section |
| 65 |
Default: not set (all dc-files from the section file will be build) |
| 66 |
|
| 67 |
--debug |
| 68 |
In case of a build failure, show a verbose DAPS log. |
| 69 |
Default: disabled |
| 70 |
|
| 71 |
--force |
| 72 |
Manuals will only be rebuilt when the git/svn repository has changed |
| 73 |
since the last build. Use this option to force a rebuild even when the |
| 74 |
repository has not changed. |
| 75 |
Default: disabled (rebuild on changes only) |
| 76 |
|
| 77 |
--help |
| 78 |
This help message |
| 79 |
|
| 80 |
--helpconfig |
| 81 |
Show help on the config file. |
| 82 |
|
| 83 |
--list-sections |
| 84 |
Show all sections defined in the given config file |
| 85 |
|
| 86 |
--nosync |
| 87 |
Do not rsync the manuals. |
| 88 |
Default: disabled (rsync will be done) |
| 89 |
|
| 90 |
--notify |
| 91 |
In case of build failures send a notification to the e-mail address |
| 92 |
specified with <maintainer/> in the config file. Will be ignored if |
| 93 |
<maintainer/> is not set. |
| 94 |
Default: disabled (No notification e-mails) |
| 95 |
|
| 96 |
--schema=<file> |
| 97 |
Path to the schema used to validate the config file. |
| 98 |
Default: @datadir@/xml/daps/schema/daps-autobuild.rnc |
| 99 |
|
| 100 |
--sections="sect1 sect2" |
| 101 |
Space-separated list of sets that should be build. The sets need to be |
| 102 |
configured in the config file. |
| 103 |
Default: not set (all sets from the config file will be build) |
| 104 |
|
| 105 |
--validate |
| 106 |
Validate the config file specified with --config. Other options except |
| 107 |
--schema will be ignored. |
| 108 |
|
| 109 |
EOFhelptext |
| 110 |
} |
| 111 |
|
| 112 |
function helpconfig { |
| 113 |
cat <<EOFhelptext |
| 114 |
The config file for $ME needs to look like the following example. The general |
| 115 |
section must only occur once, while the set section can occur multiple times. |
| 116 |
|
| 117 |
<?xml version="1.0" encoding="utf-8"?> |
| 118 |
<!DOCTYPE daps-autobuild> |
| 119 |
<daps-autobuild> |
| 120 |
<!-- ;;;;;;;;;;;;;;;;;;;; The General Section ;;;;;;;;;;;;;;;;;;; --> |
| 121 |
<general> |
| 122 |
<builddir>Path to builddir. Mandatory.</builddir> |
| 123 |
<dapsroot>Path to DAPS directory. Mandatory.</dapsroot> |
| 124 |
<rsync> |
| 125 |
<target>Rsync target. Mandatory unless using --nosync.</target> |
| 126 |
<flags>Rsync flags (do not use --delete here). Optional.</flags> |
| 127 |
</rsync> |
| 128 |
</general> |
| 129 |
|
| 130 |
<!;;;;;;;;;;;;;;;;;;;; A Set Section ;;;;;;;;;;;;;;;;;;; --> |
| 131 |
<set id="Unique ID" meta="0|1 (optional, default 0)" |
| 132 |
remarks="0|1 (optional, default 0)" |
| 133 |
draft="0|1 (optional, default 0)"> |
| 134 |
<dcfiles>Space-separated list of DC files. Mandatory.</dcfiles> |
| 135 |
<formats> |
| 136 |
Space-separated list of formats to build (html single-html pdf). Mandatory. |
| 137 |
</formats> |
| 138 |
<styleroot>Path to stylesheet directory. Optional.</styleroot> |
| 139 |
<fb_styleroot>Path to fallback stylesheet directory. Optional.</fb_styleroot> |
| 140 |
<maintainer>E-Mail address for notifications. Optional.</maintainer> |
| 141 |
<vcs type="git|svn (mandatory)"> |
| 142 |
<checkout>Checkout directory. Mandatory.</checkout> |
| 143 |
<lastrev><!-- Leave empty, will be filled by script. Mandatory.--></latsrev> |
| 144 |
</vcs> |
| 145 |
</set> |
| 146 |
EOFhelptext |
| 147 |
} |
| 148 |
|
| 149 |
function exit_on_error { |
| 150 |
# Print error message and exit with return code 1 |
| 151 |
# |
| 152 |
# Expects 1 argument: |
| 153 |
# * error message |
| 154 |
# |
| 155 |
echo -e "ERROR: ${1}" >&2 |
| 156 |
exit 1; |
| 157 |
} |
| 158 |
|
| 159 |
function validate { |
| 160 |
jing -c "$SCHEMA" "$CONFIG" && return 0 |
| 161 |
return 1 |
| 162 |
} |
| 163 |
|
| 164 |
function check_element { |
| 165 |
# checks if array contains an element |
| 166 |
# |
| 167 |
# Expects two arguments: |
| 168 |
# 1. Element to check |
| 169 |
# 2. list (${array[@]}) |
| 170 |
# |
| 171 |
local ELEMENT |
| 172 |
|
| 173 |
[[ -z $1 ]] && exit_on_error "Function check_element must be called with an item to check for" |
| 174 |
[[ -z $2 ]] && exit_on_error "Function check_element must be called with an array" |
| 175 |
|
| 176 |
for ELEMENT in "${@:2}"; do |
| 177 |
[[ "$ELEMENT" == "$1" ]] && return 0 |
| 178 |
done |
| 179 |
return 1 |
| 180 |
} |
| 181 |
|
| 182 |
function read_config_value { |
| 183 |
# reading element/attribute values from $CONFIG |
| 184 |
# |
| 185 |
# Expects 2 argument: |
| 186 |
# 1 xpath |
| 187 |
# 2 element name ("element") or attribute name ("@attribute") |
| 188 |
# |
| 189 |
local QUERY_OBJECT VALUE _XPATH |
| 190 |
|
| 191 |
[[ -z $1 ]] && exit_on_error "Function run_daps must be called with an xpath" |
| 192 |
[[ -z $2 ]] && exit_on_error "Function run_daps must be called with an element" |
| 193 |
|
| 194 |
_XPATH="$1" |
| 195 |
QUERY_OBJECT="$2" |
| 196 |
|
| 197 |
VALUE="$("$XMLSTARLET" sel -t -m "$_XPATH" -v "normalize-space($QUERY_OBJECT)" "$CONFIG")" |
| 198 |
echo "$VALUE" |
| 199 |
return 0 |
| 200 |
} |
| 201 |
|
| 202 |
function read_general { |
| 203 |
# Read values from the general section of the config file |
| 204 |
# |
| 205 |
local _START_XPATH |
| 206 |
_START_XPATH="/daps-autobuild/general" |
| 207 |
|
| 208 |
_BUILDDIR="$(read_config_value "$_START_XPATH" "builddir")" |
| 209 |
if [[ -n $_BUILDDIR ]]; then |
| 210 |
[[ -d $_BUILDDIR ]] || exit_on_error "BUILDDIR directory $_BUILDDIR does not exist" |
| 211 |
fi |
| 212 |
|
| 213 |
_DAPSROOT="$(read_config_value "$_START_XPATH" "dapsroot")" |
| 214 |
if [[ -n $_DAPSROOT ]]; then |
| 215 |
[[ -d $_DAPSROOT ]] || exit_on_error "DAPSROOT directory $_DAPSROOT does not exist" |
| 216 |
CCECHO="$_DAPSROOT/bin/ccecho" |
| 217 |
fi |
| 218 |
|
| 219 |
if [[ 1 -eq $DO_RSYNC ]]; then |
| 220 |
_RSYNC_TARGET="$(read_config_value "${_START_XPATH}/rsync" "target")" |
| 221 |
_RSYNC_FLAGS="$(read_config_value "${_START_XPATH}/rsync" "flags")" |
| 222 |
[[ "$_RSYNC_FLAGS" =~ --delete ]] && $CCECHO "warn" "rsync flags contain --delete, manuals which fail to build will be deleted from server" |
| 223 |
fi |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
function process_git { |
| 228 |
# Update a Git repo and determine whether a rebuild is needed |
| 229 |
# |
| 230 |
# Return values: |
| 231 |
# |
| 232 |
# 0: Success |
| 233 |
# 1: git fetch failed |
| 234 |
# 2: Getting latest SHA hash failed |
| 235 |
# 3: Checking out branch failed |
| 236 |
# 4: More than remote (we don't know which to pick) |
| 237 |
# 5: No remote (we can't update the repo at all) |
| 238 |
# |
| 239 |
local GITBRANCH |
| 240 |
# get branch |
| 241 |
GITBRANCH="$(read_config_value "/daps-autobuild/set[@id='$SET']/vcs" "@branch")" |
| 242 |
[[ -z "$GITBRANCH" ]] && GITBRANCH='main' |
| 243 |
|
| 244 |
remote=$(git remote) |
| 245 |
[[ $(echo -e "$remote" | wc -l ) -ge 2 ]] && return 4 |
| 246 |
[[ -z "$remote" ]] && return 5 |
| 247 |
|
| 248 |
# no clue why we'd ever have local changes but it just happened, so |
| 249 |
# reset those |
| 250 |
git clean --quiet --force |
| 251 |
git reset --quiet --hard HEAD |
| 252 |
|
| 253 |
git fetch --quiet --prune "$remote" || return 1 |
| 254 |
git checkout --quiet "remotes/$remote/$GITBRANCH" >/dev/null || return 3 |
| 255 |
echo " * Updated git branch $GITBRANCH" |
| 256 |
if [[ 1 -eq $CHECK_VCS ]]; then |
| 257 |
NEWREV="$(git rev-parse HEAD)" || return 2 |
| 258 |
[[ "$NEWREV" != "$_LASTREV" ]] && REBUILD="1" |
| 259 |
fi |
| 260 |
return 0 |
| 261 |
} |
| 262 |
|
| 263 |
function process_svn { |
| 264 |
# Update an SVN repo and determine whether a rebuild is needed |
| 265 |
# |
| 266 |
# Return values: |
| 267 |
# |
| 268 |
# 0: Success |
| 269 |
# 1: SVN up failed |
| 270 |
# 2: Getting latest revision number failed |
| 271 |
# |
| 272 |
svn up -q >/dev/null || return 1 |
| 273 |
echo " * Updated svn working copy" |
| 274 |
if [[ 1 -eq $CHECK_VCS ]]; then |
| 275 |
NEWREV="$(svnversion -c "$_CHECKOUT" . | sed -e 's/[MS]//g' -e 's/^[[:digit:]]*://')" || return 2 |
| 276 |
[[ "$NEWREV" != "$_LASTREV" ]] && REBUILD="1" |
| 277 |
fi |
| 278 |
return 0 |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
function run_daps { |
| 283 |
local BUILDDIR DAPS_BASE_CMD DCFILE _FORMAT NAME NAME_CMD_ARGS ORIG_IFS RESULT RUNDAPS _SET SUBCMD |
| 284 |
# |
| 285 |
# Create the DAPS command and run DAPS |
| 286 |
# Expects 4 Arguments: |
| 287 |
# 1. Format |
| 288 |
# 2. DC-file |
| 289 |
# 3. Set ID |
| 290 |
# |
| 291 |
# Return values: |
| 292 |
# 0: Success |
| 293 |
# 1: Failure (running DAPS failed) |
| 294 |
|
| 295 |
[[ -z $1 ]] && exit_on_error "Function run_daps must be called with a sub-command" |
| 296 |
[[ -z $2 ]] && exit_on_error "Function run_daps must be called with a DC-FILE" |
| 297 |
[[ -z $3 ]] && exit_on_error "Function run_daps must be called with a set ID" |
| 298 |
|
| 299 |
_FORMAT="$1" |
| 300 |
DCFILE="$2" |
| 301 |
_SET="$3" |
| 302 |
|
| 303 |
if [[ single-html = "$_FORMAT" ]]; then |
| 304 |
SUBCMD="html --single" |
| 305 |
else |
| 306 |
SUBCMD="$_FORMAT" |
| 307 |
fi |
| 308 |
|
| 309 |
NAME=${DCFILE//DC-/} |
| 310 |
BUILDDIR="${_BUILDDIR}/$_SET" |
| 311 |
|
| 312 |
# build the DAPS command |
| 313 |
# |
| 314 |
DAPS_BASE_CMD="${_DAPSROOT}/bin/daps --dapsroot $_DAPSROOT --builddir $BUILDDIR -d $DCFILE" |
| 315 |
[[ -n $_STYLEROOT ]] && DAPS_BASE_CMD="$DAPS_BASE_CMD --styleroot=$_STYLEROOT" |
| 316 |
[[ -n $_FB_STYLEROOT ]] && DAPS_BASE_CMD="$DAPS_BASE_CMD --fb_styleroot=$_FB_STYLEROOT" |
| 317 |
RUNDAPS="$DAPS_BASE_CMD" |
| 318 |
[[ 1 -eq $DEBUG ]] && RUNDAPS="$RUNDAPS --debug" |
| 319 |
RUNDAPS="$RUNDAPS $SUBCMD --name $NAME" |
| 320 |
[[ -n $_DRAFT ]] && RUNDAPS="$RUNDAPS --draft" |
| 321 |
[[ -n $_REMARKS ]] && RUNDAPS="$RUNDAPS --remarks" |
| 322 |
if [[ -n $_META ]]; then |
| 323 |
[[ $SUBCMD = "html" || $SUBCMD = "pdf" ]] && RUNDAPS="$RUNDAPS --meta" |
| 324 |
fi |
| 325 |
|
| 326 |
echo -n " * Building $_FORMAT version of $DCFILE ... " |
| 327 |
RESULT="$($RUNDAPS)" |
| 328 |
if [[ 0 -eq $? ]]; then |
| 329 |
$CCECHO "result" "Succeeded" |
| 330 |
SYNCDIR_DC="${SYNCDIR}/$NAME" |
| 331 |
NAME_CMD_ARGS="--name $NAME" |
| 332 |
mkdir -p "$SYNCDIR_DC" |
| 333 |
[[ -n $_DRAFT ]] && NAME_CMD_ARGS="$NAME_CMD_ARGS --draft" |
| 334 |
[[ -n $_REMARKS ]] && NAME_CMD_ARGS="$NAME_CMD_ARGS --remarks" |
| 335 |
if [[ -n $_META ]]; then |
| 336 |
[[ $SUBCMD = "html" || $SUBCMD = "pdf" ]] && NAME_CMD_ARGS="$NAME_CMD_ARGS --meta" |
| 337 |
fi |
| 338 |
NAME_CMD_ARGS="$NAME_CMD_ARGS 2>/dev/null" |
| 339 |
if [[ "pdf" = "$_FORMAT" ]]; then |
| 340 |
RESULT="$($DAPS_BASE_CMD pdf-name $NAME_CMD_ARGS)" |
| 341 |
mkdir -p "${SYNCDIR_DC}/$_FORMAT" |
| 342 |
elif [[ "html" = "$_FORMAT" ]]; then |
| 343 |
RESULT="$($DAPS_BASE_CMD html-dir-name $NAME_CMD_ARGS)" |
| 344 |
elif [[ "single-html" = "$_FORMAT" ]]; then |
| 345 |
RESULT="$($DAPS_BASE_CMD html-dir-name --single $NAME_CMD_ARGS)" |
| 346 |
fi |
| 347 |
mv "$RESULT" "${SYNCDIR_DC}/$_FORMAT" |
| 348 |
return 0 |
| 349 |
else |
| 350 |
$CCECHO "warn" "Failed" |
| 351 |
FAILURES=$((FAILURES + 1)) |
| 352 |
FAILED_BOOKS["$DC"]="${FAILED_BOOKS["$DC"]} $_FORMAT" |
| 353 |
ORIG_IFS="$IFS" |
| 354 |
IFS=$'\n' |
| 355 |
echo -e "\e[33m" |
| 356 |
for LINE in $RESULT; do |
| 357 |
echo -e "\t$LINE" |
| 358 |
done |
| 359 |
echo -e "\e[0m" |
| 360 |
IFS="$ORIG_IFS" |
| 361 |
return 1 |
| 362 |
fi |
| 363 |
} |
| 364 |
|
| 365 |
function failure_notification { |
| 366 |
|
| 367 |
# Sends a failure report by mail |
| 368 |
# |
| 369 |
# Expects 2 arguments: |
| 370 |
# 1. SET name |
| 371 |
# 2. Reason for notification: daps|svn|git |
| 372 |
# |
| 373 |
# Return values: |
| 374 |
# 0: Success |
| 375 |
# 1: Failure because Postfix is not running |
| 376 |
# 2: Failure of mail command |
| 377 |
# |
| 378 |
local EMAIL KEY REASON _SET SUBJECT TEXT |
| 379 |
|
| 380 |
# exit with return code 1 of postfix is not running |
| 381 |
systemctl -q is-active postfix || return 1 |
| 382 |
|
| 383 |
[[ -z $1 ]] && exit_on_error "Function failure_notification must be called with a set name" |
| 384 |
[[ -z $2 ]] && exit_on_error "Function failure_notification must be called with a reason" |
| 385 |
|
| 386 |
_SET="$1" |
| 387 |
REASON="$2" |
| 388 |
|
| 389 |
EMAIL="$(read_config_value "/daps-autobuild/set[@id='$SET']" "maintainer")" |
| 390 |
|
| 391 |
if [[ -n $EMAIL ]]; then |
| 392 |
|
| 393 |
SUBJECT="[DAPS Auto]: Error report on $_SET ($REASON)" |
| 394 |
|
| 395 |
case $REASON in |
| 396 |
daps) |
| 397 |
TEXT="While building manuals for set $_SET on ${FAILED_BOOKS["date"]}\n, errors occurred building the following DC-files\n\n" |
| 398 |
for KEY in "${!FAILED_BOOKS[@]}"; do |
| 399 |
[[ "date" == "$KEY" ]] && continue |
| 400 |
TEXT="$TEXT * $KEY:\t${FAILED_BOOKS["$KEY"]}\n" |
| 401 |
done |
| 402 |
;; |
| 403 |
git) |
| 404 |
TEXT="Updating the Git checkout for $_SET failed on ${FAILED_BOOKS["date"]}\n" |
| 405 |
;; |
| 406 |
svn) |
| 407 |
TEXT="Updating the SVN working copy for $_SET failed on ${FAILED_BOOKS["date"]}\n" |
| 408 |
;; |
| 409 |
esac |
| 410 |
|
| 411 |
TEXT="$TEXT\n\nYours, DAPS Auto" |
| 412 |
|
| 413 |
echo -e "$TEXT" | mail -s "$SUBJECT" "$EMAIL" || return 2 |
| 414 |
fi |
| 415 |
|
| 416 |
return 0 |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
################ MAIN ################ |
| 421 |
|
| 422 |
#-------------------- |
| 423 |
# Read command line options |
| 424 |
|
| 425 |
ARGS=$(getopt -o h -l config:,dcfiles:,debug,force,help,helpconfig,list-sections,nosync,notify,rsync,schema:,sections:,validate -n "$ME" -- "$@") |
| 426 |
|
| 427 |
# Exit when getopt returns errors |
| 428 |
# |
| 429 |
[[ 0 -ne $? ]] && exit_on_error "Wrong command line argument" |
| 430 |
|
| 431 |
eval set -- "$ARGS" |
| 432 |
|
| 433 |
while true ; do |
| 434 |
case "$1" in |
| 435 |
--config) |
| 436 |
[[ -f $2 ]] || exit_on_error "Not a valid path for --config" |
| 437 |
CONFIG="$(readlink -e "$2")" |
| 438 |
shift 2 |
| 439 |
;; |
| 440 |
--dcfiles) |
| 441 |
DC_FILES_USER="$2" |
| 442 |
shift 2 |
| 443 |
;; |
| 444 |
--debug) |
| 445 |
DEBUG=1 |
| 446 |
shift |
| 447 |
;; |
| 448 |
--force) |
| 449 |
CHECK_VCS=0 |
| 450 |
shift |
| 451 |
;; |
| 452 |
-h|--help) |
| 453 |
usage |
| 454 |
exit 0 |
| 455 |
;; |
| 456 |
--helpconfig) |
| 457 |
helpconfig |
| 458 |
exit 0 |
| 459 |
;; |
| 460 |
--list-sections) |
| 461 |
LIST_SECTIONS=1 |
| 462 |
shift |
| 463 |
;; |
| 464 |
--nosync) |
| 465 |
DO_RSYNC=0 |
| 466 |
shift |
| 467 |
;; |
| 468 |
--notify) |
| 469 |
SEND_REPORTS=1 |
| 470 |
shift |
| 471 |
;; |
| 472 |
--schema) |
| 473 |
SCHEMA="$2" |
| 474 |
shift 2 |
| 475 |
;; |
| 476 |
--sections) |
| 477 |
SECTIONS="$2" |
| 478 |
shift 2 |
| 479 |
;; |
| 480 |
--validate) |
| 481 |
validate |
| 482 |
if [[ 0 -ne $? ]]; then |
| 483 |
exit_on_error "$CONFIG does not validate" |
| 484 |
else |
| 485 |
echo "$CONFIG validates" |
| 486 |
exit 0 |
| 487 |
fi |
| 488 |
;; |
| 489 |
--) shift ; break ;; |
| 490 |
*) exit_on_error "Internal error while parsing the command line arguments!" ;; |
| 491 |
esac |
| 492 |
done |
| 493 |
|
| 494 |
# Check config/schema |
| 495 |
[[ -z $CONFIG ]] && exit_on_error "You must specify a config file with --config" |
| 496 |
if [[ -f $SCHEMA ]]; then |
| 497 |
SCHEMA="$(readlink -e "$SCHEMA")" |
| 498 |
else |
| 499 |
exit_on_error "Not a valid path for --schema" |
| 500 |
fi |
| 501 |
|
| 502 |
# Chech whether --sections was specified with --dcfiles |
| 503 |
[[ -n $DC_FILES_USER && -z $SECTIONS ]] && exit_on_error "You must specify one or more sections with --dcfiles" |
| 504 |
|
| 505 |
#-------------------- |
| 506 |
# Validate the config file |
| 507 |
|
| 508 |
jing -c "$SCHEMA" "$CONFIG" |
| 509 |
[[ 0 -ne $? ]] && exit_on_error "$CONFIG does not validate" |
| 510 |
|
| 511 |
#-------------------- |
| 512 |
# Get the set IDs |
| 513 |
|
| 514 |
ALL_SETIDS=( $(xml sel -t -v "/daps-autobuild/set/@id" "$CONFIG") ) |
| 515 |
[[ 0 -eq ${#ALL_SETIDS[*]} ]] && exit_on_error "Could not find any SET IDs in the config file" |
| 516 |
|
| 517 |
#---------- |
| 518 |
# If --list-sections was specified, print a list and exit |
| 519 |
|
| 520 |
if [[ 1 -eq $LIST_SECTIONS ]]; then |
| 521 |
for ID in ${ALL_SETIDS[@]}; do |
| 522 |
echo "$ID" |
| 523 |
done |
| 524 |
exit 0 |
| 525 |
fi |
| 526 |
#-- |
| 527 |
|
| 528 |
|
| 529 |
[[ -z $SECTIONS ]] && SECTIONS="${ALL_SETIDS[*]}" |
| 530 |
|
| 531 |
|
| 532 |
#-------------------- |
| 533 |
# read the general data |
| 534 |
|
| 535 |
read_general |
| 536 |
|
| 537 |
for SET in $SECTIONS; do |
| 538 |
|
| 539 |
# check if SET is a valid section in the config file |
| 540 |
check_element "$SET" "${ALL_SETIDS[@]}" |
| 541 |
if [[ 1 -eq $? ]]; then |
| 542 |
$CCECHO "warn" "$SET not configured in config file, skipping!" |
| 543 |
continue |
| 544 |
fi |
| 545 |
|
| 546 |
_CHECKOUT="" |
| 547 |
_DCFILES="" |
| 548 |
_DRAFT="" |
| 549 |
_FB_STYLEROOT="" |
| 550 |
_FORMATS="" |
| 551 |
_LASTREV="" |
| 552 |
_META="" |
| 553 |
_REMARKS="" |
| 554 |
_STYLEROOT="" |
| 555 |
_VCS="" |
| 556 |
|
| 557 |
NEWREV="" |
| 558 |
START_XPATH="/daps-autobuild/set[@id='${SET}']" |
| 559 |
FAILURES="0" |
| 560 |
declare -A FAILED_BOOKS |
| 561 |
|
| 562 |
FAILED_BOOKS["date"]="$(date)" |
| 563 |
|
| 564 |
# set rebuild to "1" if we do not check for revisions |
| 565 |
if [[ 1 -eq $CHECK_VCS ]]; then |
| 566 |
REBUILD="0" |
| 567 |
else |
| 568 |
REBUILD="1" |
| 569 |
fi |
| 570 |
|
| 571 |
echo -e "$SET\n-------------------------" |
| 572 |
|
| 573 |
#-------------------- |
| 574 |
# read the given set and store the data |
| 575 |
# |
| 576 |
|
| 577 |
# DC-files first |
| 578 |
# |
| 579 |
_DCFILES="$(read_config_value "$START_XPATH" "dcfiles")" |
| 580 |
|
| 581 |
# if DC-files were provided at the command-line, check whether |
| 582 |
# at least one of them appears in the current set |
| 583 |
# If so, set _DCFILES to the DC files common in both lists and |
| 584 |
# proceed. No only the DC-files provided by the user will be built |
| 585 |
# |
| 586 |
if [[ -n $DC_FILES_USER ]]; then |
| 587 |
# Check if user provided DC-file appears in _DCFILES |
| 588 |
_DCFILES=$(comm -1 -2 <(echo -e "$_DCFILES" | tr " " "\n" | sort -u) <(echo -e "$DC_FILES_USER" | tr " " "\n" | sort -u)) |
| 589 |
if [[ -z $_DCFILES ]]; then |
| 590 |
$CCECHO "warn" "$SET does not contain the DC-file(s) specified!\nSkipping $SET.\n" |
| 591 |
continue |
| 592 |
fi |
| 593 |
fi |
| 594 |
|
| 595 |
_CHECKOUT="$(read_config_value "${START_XPATH}/vcs" "checkout")" |
| 596 |
_DRAFT="$(read_config_value "$START_XPATH" "@draft")" |
| 597 |
_FB_STYLEROOT="$(read_config_value "$START_XPATH" "fb-styleroot")" |
| 598 |
_FORMATS="$(read_config_value "$START_XPATH" "formats")" |
| 599 |
_LASTREV="$(read_config_value "${START_XPATH}/vcs" "lastrev")" |
| 600 |
_META="$(read_config_value "$START_XPATH" "@meta")" |
| 601 |
_REMARKS="$(read_config_value "$START_XPATH" "@remarks")" |
| 602 |
_STYLEROOT="$(read_config_value "$START_XPATH" "styleroot")" |
| 603 |
_VCS="$(read_config_value "${START_XPATH}/vcs" "@type")" |
| 604 |
|
| 605 |
# validity checks |
| 606 |
if [[ -n $_FB_STYLEROOT ]]; then |
| 607 |
if [[ ! -d $_STYLEROOT ]]; then |
| 608 |
$CCECHO "warn" " FB_STYLEROOT directory '$_FB_STYLEROOT' does not exist,\n using default" |
| 609 |
_FB_STYLEROOT="" |
| 610 |
fi |
| 611 |
fi |
| 612 |
|
| 613 |
if [[ -n $_STYLEROOT ]]; then |
| 614 |
if [[ ! -d $_STYLEROOT ]]; then |
| 615 |
$CCECHO "warn" " STYLEROOT directory '$_STYLEROOT' does not exist,\n using default" |
| 616 |
_STYLEROOT="" |
| 617 |
fi |
| 618 |
fi |
| 619 |
|
| 620 |
if [[ ! -d $_CHECKOUT ]]; then |
| 621 |
$CCECHO "warn" " Checkout directory '$_CHECKOUT' does not exist!\n Skipping $SET" |
| 622 |
continue |
| 623 |
fi |
| 624 |
|
| 625 |
pushd "$_CHECKOUT" >/dev/null |
| 626 |
|
| 627 |
#-------------------- |
| 628 |
# update the checkout and check revisions |
| 629 |
if [[ $_VCS = "git" ]]; then |
| 630 |
process_git |
| 631 |
case $? in |
| 632 |
1) |
| 633 |
$CCECHO "warn" " Running 'git fetch' in $_CHECKOUT failed!\n Skipping $SET" |
| 634 |
failure_notification "$SET" "git" |
| 635 |
continue |
| 636 |
;; |
| 637 |
2) |
| 638 |
$CCECHO "warn" " Wasn't able to get the current SHA hash from Git" |
| 639 |
;; |
| 640 |
3) |
| 641 |
$CCECHO "warn" " Running 'git checkout <branch>' in $_CHECKOUT failed!\n Skipping $SET" |
| 642 |
failure_notification "$SET" "git" |
| 643 |
continue |
| 644 |
;; |
| 645 |
4) |
| 646 |
$CCECHO "warn" " There is more than one remote configured for $_CHECKOUT and daps-autobuild cannot decide which one to use.\n Skipping $SET" |
| 647 |
failure_notification "$SET" "git" |
| 648 |
continue |
| 649 |
;; |
| 650 |
5) |
| 651 |
$CCECHO "warn" " There is no remote configured in $_CHECKOUT and the repository cannot be updated.\n Skipping $SET" |
| 652 |
failure_notification "$SET" "git" |
| 653 |
continue |
| 654 |
;; |
| 655 |
esac |
| 656 |
elif [[ $_VCS = "svn" ]]; then |
| 657 |
process_svn |
| 658 |
case $? in |
| 659 |
1) |
| 660 |
$CCECHO "warn" " Running 'svn up' in $_CHECKOUT failed!\n Skipping $SET" |
| 661 |
failure_notification "$SET" "svn" |
| 662 |
continue |
| 663 |
;; |
| 664 |
2) |
| 665 |
$CCECHO "warn" " Wasn't able to get the current revision number from SVN" |
| 666 |
;; |
| 667 |
esac |
| 668 |
fi |
| 669 |
|
| 670 |
|
| 671 |
#-------------------- |
| 672 |
# build the manuals |
| 673 |
|
| 674 |
if [[ 1 -eq $REBUILD ]]; then |
| 675 |
|
| 676 |
# set up the sync directory; do not delete it if |
| 677 |
# $DC_FILES_USER ist set - in this case we only want to update |
| 678 |
# a single manual rather than replacing everything with a new build |
| 679 |
# |
| 680 |
SYNCDIR="${_BUILDDIR}/$SET/sync/$SET" |
| 681 |
if [[ -z $DC_FILES_USER ]]; then |
| 682 |
rm -rf "$SYNCDIR" |
| 683 |
fi |
| 684 |
mkdir -p "$SYNCDIR" |
| 685 |
|
| 686 |
for DC in $_DCFILES; do |
| 687 |
for _FORMAT in $_FORMATS; do |
| 688 |
# Build! |
| 689 |
run_daps "$_FORMAT" "$DC" "$SET" |
| 690 |
done |
| 691 |
done |
| 692 |
else |
| 693 |
echo " ==> Previous build still up-to-date, no rebuild needed" |
| 694 |
fi |
| 695 |
|
| 696 |
popd >/dev/null |
| 697 |
|
| 698 |
|
| 699 |
#-------------------- |
| 700 |
# check for build failures |
| 701 |
|
| 702 |
if [[ 0 -ne $FAILURES ]]; then |
| 703 |
$CCECHO "warn" " ==> Counted $FAILURES build failure(s)" |
| 704 |
# if there are build failures, do not run rsync with --delete |
| 705 |
RSYNC_DELETE="" |
| 706 |
# send an error report |
| 707 |
if [[ 1 -eq $SEND_REPORTS ]]; then |
| 708 |
failure_notification "$SET" "daps" |
| 709 |
case $? in |
| 710 |
1) |
| 711 |
$CCECHO "warn" "Could not send error report (Postfix not running)" |
| 712 |
;; |
| 713 |
2) |
| 714 |
$CCECHO "warn" "Could not send error report (mail command failed)" |
| 715 |
;; |
| 716 |
esac |
| 717 |
fi |
| 718 |
else |
| 719 |
# No build failures; |
| 720 |
# |
| 721 |
# if DC_FILES_USER was set, we do not want to change the revision in the |
| 722 |
# XML config file (because this affects the whole set). |
| 723 |
# We also do not want to run rsync with --delete |
| 724 |
# |
| 725 |
# if we rebuild a complete section, the revision number needs to get |
| 726 |
# updated and we also want rsny --delete |
| 727 |
# |
| 728 |
# In case a rebuild is forced, we do not check for a revision and |
| 729 |
# therefore NEWREV is is not set -> check if NEWREV is not empty |
| 730 |
# (seems better than to check for CHECK_VCS (from --force) - if |
| 731 |
# NEWREV is empty, we do not want to touch the lastrev entry in the |
| 732 |
# config file anyway |
| 733 |
# |
| 734 |
if [[ -z $DC_FILES_USER && -n $NEWREV ]]; then |
| 735 |
xml ed -P -L -u "/daps-autobuild/set[@id='$SET']/vcs/lastrev" -v "$NEWREV" "$CONFIG" |
| 736 |
RSYNC_DELETE="--delete" |
| 737 |
else |
| 738 |
RSYNC_DELETE="" |
| 739 |
fi |
| 740 |
fi |
| 741 |
|
| 742 |
#-------------------- |
| 743 |
# rsync |
| 744 |
|
| 745 |
if [[ 1 -eq $DO_RSYNC && 1 -eq $REBUILD ]]; then |
| 746 |
$RSYNC $_RSYNC_FLAGS $RSYNC_DELETE $SYNCDIR $_RSYNC_TARGET |
| 747 |
if [[ 0 -eq $? ]]; then |
| 748 |
$CCECHO "result" " Rsync successful" |
| 749 |
else |
| 750 |
$CCECHO "warn" " Rsync failed" |
| 751 |
fi |
| 752 |
else |
| 753 |
$CCECHO "info" " Skipping rsync" |
| 754 |
fi |
| 755 |
|
| 756 |
echo |
| 757 |
|
| 758 |
# unset the array containing the failures |
| 759 |
unset -v FAILED_BOOKS |
| 760 |
|
| 761 |
done |
| 762 |
|
| 763 |
exit 0 |