161 lines
4.8 KiB
Bash
Executable File
161 lines
4.8 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# handler_build - builds PDF quotes and reports from XML files
|
|
#
|
|
# This script is part of the PenText framework
|
|
# https://pentext.org
|
|
#
|
|
# Copyright (C) 2016 Radically Open Security
|
|
# https://www.radicallyopensecurity.com
|
|
#
|
|
# Author(s): Peter Mosmans
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
|
|
|
|
VERSION=0.12
|
|
DOCBUILDER=/usr/local/bin/docbuilder.py
|
|
TEMPLOC=$(mktemp -d)
|
|
|
|
# These variables should be set environment-specific
|
|
[ -z $GITSERVER ] && GITSERVER=gitlab.local
|
|
[ -z $GITWEB ] && GITWEB=https://${GITSERVER}
|
|
[ -z $NAMESPACE ] && NAMESPACE=ros
|
|
BRANCH=master
|
|
|
|
# Read standard 'command line' variables
|
|
[[ ! -z $1 ]] && TARGET=$1
|
|
[[ ! -z $2 ]] && REPO=$2
|
|
|
|
# Set the default PDF name based on the target name
|
|
TARGETPDF="target/$TARGET-latest.pdf"
|
|
|
|
# Read optional arguments
|
|
# Reading positional parms is a bit ugly, shifting parms or getopt would be nicer
|
|
if [[ ! -z $3 ]]; then
|
|
if [[ ! $3 == -* ]]; then
|
|
NAMESPACE=$3
|
|
else
|
|
PARAMS=$3
|
|
fi
|
|
fi
|
|
if [[ ! -z $4 ]]; then
|
|
if [[ ! $3 == -* ]]; then
|
|
BRANCH=$4
|
|
else
|
|
PARAMS="$PARAMS $4"
|
|
fi
|
|
fi
|
|
if [[ $# -ge 5 ]]; then
|
|
shift 4
|
|
PARAMS="$PARAMS $@"
|
|
fi
|
|
|
|
trap cleanup EXIT QUIT
|
|
|
|
# Make sure that the temporary files are always removed
|
|
cleanup() {
|
|
trap '' EXIT INT QUIT
|
|
[ -d $TEMPLOC ] && rm -rf $TEMPLOC &>/dev/null
|
|
exit
|
|
}
|
|
|
|
# As quote used to be called offer or even offer,
|
|
# this function retains backward compatibility - v0.1
|
|
backwards_compatible() {
|
|
if [[ $TARGET == "quote" ]] && [ ! -f $TARGET.xml ]; then
|
|
TARGET="offerte"
|
|
fi
|
|
}
|
|
|
|
# Clones repo using global (!) variables - v0.2
|
|
clone_repo() {
|
|
pushd $TEMPLOC 1>/dev/null
|
|
git clone -b $BRANCH --depth=1 -q ssh://git@${GITSERVER}/${NAMESPACE}/${REPO}.git &>/dev/null
|
|
if [ ! -d $TEMPLOC/$REPO ]; then
|
|
echo "[-] could not clone repo ${NAMESPACE}/${REPO}"
|
|
exit 1
|
|
else
|
|
cd $REPO
|
|
fi
|
|
}
|
|
|
|
# Preflight checks using global (!) variables - v0.2
|
|
preflight_checks() {
|
|
if ([[ $TARGET != "quote" ]] && [[ $TARGET != "report" ]]) || [ -z $REPO ]; then
|
|
echo "Usage: build quote|report REPOSITORY [NAMESPACE [BRANCH] [-v]"
|
|
exit
|
|
fi
|
|
if [ ! -f $DOCBUILDER ]; then
|
|
echo "[-] this script needs docbuilder.py ($DOCBUILDER)"
|
|
fi
|
|
}
|
|
|
|
build() {
|
|
if [ ! -d source ]; then
|
|
echo "[-] missing necessary pentext framework files"
|
|
exit 1
|
|
fi
|
|
pushd source &>/dev/null
|
|
backwards_compatible
|
|
if ([[ $TARGET == "quote" ]] || [[ $TARGET == "offerte" ]]); then
|
|
TARGETPDF="target/quote_${REPO/off-/}.pdf"
|
|
elif [[ $TARGET == "report" ]]; then
|
|
TARGETPDF="target/report_${REPO/pen-/}.pdf"
|
|
TARGETHTML="target/report_${REPO/pen-/}.html"
|
|
TARGETMD="target/report_${REPO/pen-/}.md"
|
|
fi
|
|
TARGETFO="target/${TARGET}.fo"
|
|
$DOCBUILDER -c -i $TARGET.xml -f $TARGETFO -o ../$TARGETPDF -x ../xslt/generate_$TARGET.xsl $PARAMS $NOPRINT
|
|
if [[ $? -ne 0 ]]; then
|
|
echo "[-] Sorry, failed to parse $TARGET. Use \`build $TARGET $REPO $NAMESPACE $BRANCH -v\` for more information."
|
|
exit 1
|
|
fi
|
|
popd &>/dev/null
|
|
if [ ! -f $TARGETPDF ]; then
|
|
echo "[-] hmmm... failed to build PDF file (could not find $TARGETPDF)"
|
|
exit 1
|
|
fi
|
|
if [ $TARGET == "report" ]; then
|
|
java -jar /usr/local/bin/saxon/saxon9he.jar -s:source/$TARGET.xml -o:$TARGETHTML -xsl:xslt/generate_html_$TARGET.xsl -xi
|
|
if [ ! -f $TARGETHTML ]; then
|
|
echo "[-] Note: failed to build HTML file (could not find $TARGETHTML)"
|
|
else
|
|
pandoc $TARGETHTML -t markdown_strict -o $TARGETMD
|
|
if [ ! -f $TARGETMD ]; then
|
|
echo "[-] Note: failed to build markdown file (could not find $TARGETMD)"
|
|
fi
|
|
fi
|
|
fi
|
|
}
|
|
|
|
add_to_repo() {
|
|
git add $TARGETPDF
|
|
git add $TARGETHTML &>/dev/null
|
|
git add $TARGETMD &>/dev/null
|
|
git add target/waiver_?*.pdf &>/dev/null
|
|
git add target/execsummary.pdf &>/dev/null
|
|
git commit -q -m "$TARGETPDF proudly manufactured using ChatOps" &>/dev/null
|
|
git push -q >/dev/null
|
|
}
|
|
|
|
preflight_checks
|
|
echo "builder v$VERSION - Rocking your world, one build at a time..."
|
|
clone_repo
|
|
build
|
|
add_to_repo
|
|
echo " [+] Get PDF: $GITWEB/$NAMESPACE/$REPO/raw/$BRANCH/$TARGETPDF"
|
|
if [[ -f target/execsummary.pdf ]]; then
|
|
echo " [+] Get exec summary PDF: $GITWEB/$NAMESPACE/$REPO/raw/$BRANCH/target/execsummary.pdf"
|
|
fi
|
|
if [[ $TARGET == "report" && -f $TARGETHTML ]]; then
|
|
echo " [+] Download HTML: $GITWEB/$NAMESPACE/$REPO/raw/$BRANCH/$TARGETHTML"
|
|
fi
|
|
if [[ $TARGET == "report" && -f $TARGETMD ]]; then
|
|
echo " [+] Quick look (rendered MarkDown): $GITWEB/$NAMESPACE/$REPO/blob/$BRANCH/$TARGETMD"
|
|
fi
|
|
exit 0
|