#!/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.11 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" # Reading positional parms is a bit ugly, shifting parms or getopt would be nicer if [[ ! -z $3 ]]; then if [[ ! $3 == -* ]]; then NAMESPACE=$3 else PARMS=$3 fi fi if [[ ! -z $4 ]]; then if [[ ! $3 == -* ]]; then BRANCH=$4 else PARMS="$PARMS $4" fi fi if [[ $# -ge 5 ]]; then shift 4 PARMS="$PARMS $@" 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" fi $DOCBUILDER -c -i $TARGET.xml -o ../$TARGETPDF -x ../xslt/generate_$TARGET.xsl $PARMS if [[ $? -ne 0 ]]; then echo "[-] Sorry, failed to parse $TARGET. Use \`builder $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 } add_to_repo() { git add $TARGETPDF 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 "[+] listo! Check out $GITWEB/$NAMESPACE/$REPO/raw/$BRANCH/$TARGETPDF" exit 0