|
#!/bin/bash |
|
# wazuh-docs-build.sh |
|
# Author: Corey McCandless <[email protected]> |
|
# License: MIT |
|
# Description: This script will initialize a virtual |
|
# environment, install build dependencies, |
|
# and build every make target listed in `make help`. |
|
# |
|
# Output is logged to build.log |
|
# Each make target has a separate $TARGET.log |
|
# |
|
# usage: wazuh-docs-build.sh [python] [timeout] [target] |
|
# arguments: |
|
# python path to python binary (default: python) |
|
# timeout make target timeout (default: 0s) |
|
# target make target (default: all) |
|
# |
|
# examples: |
|
# ./wazuh-docs-build.sh |
|
# ./wazuh-docs-build.sh python2.7 60s |
|
# ./wazuh-docs-build.sh python3 1m |
|
# ./wazuh-docs-build.sh python3 1m html |
|
|
|
PYTHON=${1:-python} |
|
TIMEOUT=${2:-0s} |
|
MAKE_TARGET=${3:-all} |
|
|
|
BUILD_BASE=build |
|
BUILDDIR="${BUILD_BASE}/${PYTHON}" |
|
LOG_BASE=logs |
|
LOG_DIR="${LOG_BASE}/${PYTHON}" |
|
LOG_TARGET_RESULTS="${LOG_DIR}/target_results.log" |
|
EXIT_ON_FAIL=0 |
|
TEMPFILE=/tmp/$$.tmp |
|
echo '0' > $TEMPFILE |
|
|
|
RED='\033[0;31m' |
|
GREEN='\033[0;32m' |
|
NC='\033[0m' |
|
CURSOR_HOME='\033[999D' |
|
ERASE_TO_END='\033[K' |
|
|
|
if [ -d ${LOG_DIR} ]; then |
|
rm -rf ${LOG_DIR} |
|
fi |
|
mkdir -p "${LOG_DIR}" |
|
LOG="${LOG_DIR}/build.log" |
|
|
|
cleanup() |
|
{ |
|
unlink $TEMPFILE |
|
} |
|
|
|
abort() |
|
{ |
|
if [ $EXIT_ON_FAIL -eq 1 ]; then |
|
cleanup |
|
echo "ABORTED" >> $LOG |
|
exit ${1:-1} |
|
fi |
|
} |
|
|
|
trap 'kill -INT -$pid; exit 1' INT |
|
|
|
TIMEOUTS=0 |
|
|
|
do_make() |
|
{ |
|
TARGET=$1 |
|
TARGET_LOG="${LOG_DIR}/${TARGET}.log" |
|
printf "Building $TARGET..." |
|
timeout $TIMEOUT make $TARGET BUILDDIR=$BUILDDIR &> ${TARGET_LOG} & |
|
pid=$! |
|
wait $pid |
|
ret=$? |
|
printf "${CURSOR_HOME}${ERASE_TO_END}" |
|
case "$ret" in |
|
2) |
|
printf "${RED}[ FAILED ]${NC} ${TARGET}; see ${TARGET_LOG} for more information\n" |
|
echo "[ FAILED ] ${TARGET}; see ${TARGET_LOG} for more information" >> $LOG |
|
echo "$TARGET failed" >> $LOG_TARGET_RESULTS |
|
abort $ret |
|
;; |
|
124) |
|
printf "${RED}[ TIMEOUT]${NC} ${TARGET}\n" |
|
echo "[ TIMEOUT] ${TARGET}" >> $LOG |
|
echo "$TARGET timed_out" >> $LOG_TARGET_RESULTS |
|
abort $ret |
|
echo $(($(cat $TEMPFILE) + 1)) > $TEMPFILE |
|
;; |
|
*) |
|
printf "${GREEN}[ PASSED ]${NC} ${TARGET}\n" |
|
echo "[ PASSED ] ${TARGET}" >> $LOG |
|
echo "$TARGET passed" >> $LOG_TARGET_RESULTS |
|
;; |
|
esac |
|
return 0 |
|
} |
|
|
|
VENV_BASE="venv" |
|
mkdir -p "$VENV_BASE" |
|
|
|
VENV="$VENV_BASE/$PYTHON" |
|
if [ -d "$VENV" ]; then |
|
rm -rf "$VENV" |
|
fi |
|
|
|
echo "Creating virtualenv in $VENV..." | tee $LOG |
|
virtualenv "$VENV" --python=${PYTHON} >> $LOG |
|
echo >> $LOG |
|
|
|
echo "Activating virtualenv..." | tee -a $LOG |
|
source "$VENV/bin/activate" >> $LOG |
|
echo >> $LOG |
|
|
|
if [ ! -f requirements.txt ]; then |
|
echo "Generating requirements.txt..." | tee $LOG |
|
echo "sphinx==1.6.1" > requirements.txt |
|
echo "sphinx-rtd-theme==0.2.4" >> requirements.txt |
|
echo "sphinxcontrib-images==0.7.0" >> requirements.txt |
|
fi |
|
|
|
echo "Installing requirements..." | tee -a $LOG |
|
pip -vvv install -r requirements.txt >> $LOG |
|
echo "Installed Packages:" |
|
pip freeze |
|
echo >> $LOG |
|
|
|
mkdir -p "$BUILD_BASE" |
|
if [ "$MAKE_TARGET" != "all" ]; then |
|
do_make $MAKE_TARGET |
|
else |
|
make help | tail -n +2 | cut -d' ' -f3 | while read TARGET; do |
|
do_make $TARGET; |
|
done |
|
TIMEOUTS=$(cat $TEMPFILE) |
|
if [ $TIMEOUTS -gt 1 ]; then |
|
echo "${TIMEOUTS} targets timed out. Consider raising timeout value." |
|
fi |
|
fi |
|
cleanup |
|
echo "Done" >> $LOG |