github.com/kata-containers/tests@v0.0.0-20240307153542-772105b56064/metrics/report/makereport.sh (about)

     1  #!/bin/bash
     2  # Copyright (c) 2018-2019 Intel Corporation
     3  #
     4  # SPDX-License-Identifier: Apache-2.0
     5  
     6  # Take the data found in subdirectories of the metrics 'results' directory,
     7  # and turn them into a PDF report. Use a Dockerfile containing all the tooling
     8  # and scripts we need to do that.
     9  
    10  set -e
    11  
    12  SCRIPT_PATH=$(dirname "$(readlink -f "$0")")
    13  source "${SCRIPT_PATH}/../lib/common.bash"
    14  
    15  IMAGE="${IMAGE:-metrics-report}"
    16  DOCKERFILE="${SCRIPT_PATH}/report_dockerfile/Dockerfile"
    17  
    18  HOST_INPUT_DIR="${SCRIPT_PATH}/../results"
    19  R_ENV_FILE="${HOST_INPUT_DIR}/Env.R"
    20  HOST_OUTPUT_DIR="${SCRIPT_PATH}/output"
    21  
    22  GUEST_INPUT_DIR="/inputdir/"
    23  GUEST_OUTPUT_DIR="/outputdir/"
    24  
    25  # If in debugging mode, we also map in the scripts dir so you can
    26  # dynamically edit and re-load them at the R prompt
    27  HOST_SCRIPT_DIR="${SCRIPT_PATH}/report_dockerfile"
    28  GUEST_SCRIPT_DIR="/scripts/"
    29  
    30  setup() {
    31  	echo "Checking subdirectories"
    32  	check_subdir="$(ls -dx ${HOST_INPUT_DIR}/*/ 2> /dev/null | wc -l)"
    33  	if [ $check_subdir -eq 0 ]; then
    34  		die "No subdirs in [${HOST_INPUT_DIR}] to read results from."
    35  	fi
    36  
    37  	echo "Checking Dockerfile"
    38  	check_dockerfiles_images "$IMAGE" "$DOCKERFILE"
    39  
    40  	mkdir -p "$HOST_OUTPUT_DIR" && true
    41  
    42  	echo "inputdir=\"${GUEST_INPUT_DIR}\"" > ${R_ENV_FILE}
    43  	echo "outputdir=\"${GUEST_OUTPUT_DIR}\"" >> ${R_ENV_FILE}
    44  
    45  	# A bit of a hack to get an R syntax'd list of dirs to process
    46  	# Also, need it as not host-side dir path - so short relative names
    47  	resultdirs="$(cd ${HOST_INPUT_DIR}; ls -dx */)"
    48  	resultdirslist=$(echo ${resultdirs} | sed 's/ \+/", "/g')
    49  	echo "resultdirs=c(" >> ${R_ENV_FILE}
    50  	echo "	\"${resultdirslist}\"" >> ${R_ENV_FILE}
    51  	echo ")" >> ${R_ENV_FILE}
    52  }
    53  
    54  run() {
    55  	docker run -ti --rm -v ${HOST_INPUT_DIR}:${GUEST_INPUT_DIR} -v ${HOST_OUTPUT_DIR}:${GUEST_OUTPUT_DIR} ${extra_volumes} ${IMAGE} ${extra_command}
    56  	ls -la ${HOST_OUTPUT_DIR}/*
    57  }
    58  
    59  help() {
    60  	usage=$(cat << EOF
    61  Usage: $0 [-h] [options]
    62     Description:
    63          This script generates a metrics report document
    64          from the results directory one level up in the
    65          directory tree (../results).
    66     Options:
    67          -d,         Run in debug (interactive) mode
    68          -h,         Print this help
    69  EOF
    70  )
    71  	echo "$usage"
    72  }
    73  
    74  main() {
    75  
    76  	local OPTIND
    77  	while getopts "d" opt;do
    78  		case ${opt} in
    79  		d)
    80  			# In debug mode, run a shell instead of the default report generation
    81  			extra_command="bash"
    82  			extra_volumes="-v ${HOST_SCRIPT_DIR}:${GUEST_SCRIPT_DIR}"
    83  			;;
    84  		?)
    85  		    # parse failure
    86  		    help
    87  		    die "Failed to parse arguments"
    88  		    ;;
    89  		esac
    90  	done
    91  	shift $((OPTIND-1))
    92  
    93  	setup
    94  	run
    95  }
    96  
    97  main "$@"