From 52766f60446e030ae88b89b14b9295061072ce35 Mon Sep 17 00:00:00 2001 From: Tom Scavo <trscavo@internet2.edu> Date: Sun, 7 May 2017 17:41:01 -0400 Subject: [PATCH] Require TMPDIR env var --- bin/cget.sh | 142 +++++++++++++++++++++++++--------------------------- 1 file changed, 67 insertions(+), 75 deletions(-) diff --git a/bin/cget.sh b/bin/cget.sh index 1e6b316..a3d2539 100755 --- a/bin/cget.sh +++ b/bin/cget.sh @@ -36,11 +36,6 @@ display_help () { requests the resource at the given URL using the curl command-line tool. - This script requires two environment variables. CACHE_DIR is - the absolute path to the cache directory (which may or may not - exist) whereas LIB_DIR specifies a directory containing various - helper scripts. - Options: -h Display this help message -v Log verbose messages @@ -62,6 +57,19 @@ display_help () { it is output on stdout, otherwise the script silently fails with exit code 1. + ENVIRONMENT + + This script leverages a handful of environment variables: + + LIB_DIR A source library directory + CACHE_DIR A persistent HTTP cache + TMPDIR A temporary directory + LOG_FILE A persistent log file + LOG_LEVEL The global log level [0..5] + + All of the above environment variables are REQUIRED + except LOG_LEVEL, which defaults to LOG_LEVEL=3. + LIBRARY Environment variable LIB_DIR specifies a directory containing at @@ -72,10 +80,10 @@ display_help () { EXAMPLES - url=http://md.incommon.org/InCommon/InCommon-metadata.xml - ${0##*/} \$url # Retrieve the resource using HTTP conditional GET - ${0##*/} -F \$url # Enable Force Output Mode - ${0##*/} -C \$url # Enable Cache Only Mode + \$ url=http://md.incommon.org/InCommon/InCommon-metadata.xml + \$ ${0##*/} \$url # Retrieve the resource using HTTP conditional GET + \$ ${0##*/} -F \$url # Enable Force Output Mode + \$ ${0##*/} -C \$url # Enable Cache Only Mode HELP_MSG } @@ -85,78 +93,48 @@ HELP_MSG script_name=${0##*/} # equivalent to basename $0 -# determine the source lib directory -if [ -z "$LIB_DIR" ]; then - echo "ERROR: $script_name requires env var LIB_DIR" >&2 - exit 2 -fi -if [ ! -d "$LIB_DIR" ]; then - echo "ERROR: $script_name: directory does not exist: $LIB_DIR" >&2 - exit 2 -fi - -# library filenames (always list core_lib first) -LIB_FILENAMES="core_lib.sh -http_tools.sh" +# required directories +env_vars="LIB_DIR +CACHE_DIR +TMPDIR" -# source lib files -for lib_filename in $LIB_FILENAMES; do - lib_file="$LIB_DIR/$lib_filename" - if [ ! -f "$lib_file" ]; then - echo "ERROR: $script_name: lib file does not exist: $lib_file" >&2 +# check required directories +for env_var in $env_vars; do + eval "env_var_val=\${$env_var}" + if [ -z "$env_var_val" ]; then + echo "ERROR: $script_name requires env var $env_var" >&2 exit 2 fi - source "$lib_file" - status_code=$? - if [ $status_code -ne 0 ]; then - echo "ERROR: $script_name failed to source lib file ($status_code) $lib_file" >&2 + if [ ! -d "$env_var_val" ]; then + echo "ERROR: $script_name: directory does not exist: $env_var_val" >&2 exit 2 fi done -# If env var TMPDIR exists, use it; otherwise -# create a new TMPDIR and use that instead. -if [ -z "$TMPDIR" ] || [ ! -d "$TMPDIR" ]; then - # create temporary directory - TMPDIR="$( make_temp_file -d )" - if [ ! -d "$TMPDIR" ] ; then - printf "ERROR: $script_name unable to create temporary dir\n" >&2 - exit 2 - fi -fi - -# use TMPDIR directory (remove trailing slash) -tmp_dir="${TMPDIR%%/}/${script_name%%.*}_$$" - -# every run of this script gets its own subdir -if [ -d "$tmp_dir" ]; then - echo "ERROR: $script_name: directory already exists: $tmp_dir" >&2 +# check the log file +if [ -z "$LOG_FILE" ]; then + echo "ERROR: $script_name requires env var LOG_FILE" >&2 exit 2 fi - -# determine the cache directory -if [ -z "$CACHE_DIR" ]; then - echo "ERROR: $script_name requires env var CACHE_DIR" >&2 +# devices such as /dev/tty and /dev/null are allowed +if [ ! -f "$LOG_FILE" ] && [[ $LOG_FILE != /dev/* ]]; then + echo "ERROR: $script_name: file does not exist: $LOG_FILE" >&2 exit 2 fi -if [ ! -d "$CACHE_DIR" ]; then - /bin/mkdir "$CACHE_DIR" - status_code=$? - if [ $status_code -ne 0 ]; then - echo "ERROR: $script_name failed to create dir $CACHE_DIR ($status_code)" >&2 - exit 2 - fi -fi - -####################################################################### -# Process command-line options and arguments -####################################################################### # default to INFO logging if [ -z "$LOG_LEVEL" ]; then LOG_LEVEL=3 fi +# library filenames (always list core_lib first) +LIB_FILENAMES="core_lib.sh +http_tools.sh" + +####################################################################### +# Process command-line options and arguments +####################################################################### + help_mode=false; local_opts= while getopts ":hvqFC" opt; do case $opt in @@ -204,33 +182,47 @@ location="$1" # Initialization ####################################################################### -# create temporary subdirectory +# source lib files +for lib_filename in $LIB_FILENAMES; do + lib_file="$LIB_DIR/$lib_filename" + if [ ! -f "$lib_file" ]; then + echo "ERROR: $script_name: lib file does not exist: $lib_file" >&2 + exit 2 + fi + source "$lib_file" + status_code=$? + if [ $status_code -ne 0 ]; then + echo "ERROR: $script_name failed ($status_code) to source lib file $lib_file" >&2 + exit 2 + fi +done + +# create a temporary subdirectory +tmp_dir="${TMPDIR%%/}/${script_name%%.*}_$$" /bin/mkdir "$tmp_dir" status_code=$? if [ $status_code -ne 0 ]; then - echo "ERROR: $script_name failed to create tmp dir ($status_code) $tmp_dir" >&2 + echo "ERROR: $script_name failed ($status_code) to create tmp dir $tmp_dir" >&2 exit 2 fi -print_log_message -D "$script_name creating temp dir: $tmp_dir" # temporary file -tmp_file="${tmp_dir}/http_resource_$$" +tmp_file="${tmp_dir}/http_resource" ####################################################################### # Main processing ####################################################################### -# Functions print_log_message and clean_up_and_exit defined in core_lib.sh -# Function conditional_get defined in http_tools.sh - -print_log_message "$script_name requesting resource: $location" +# Functions print_log_message and clean_up_and_exit are defined in core_lib.sh +# Function conditional_get is defined in http_tools.sh # get the resource +print_log_message -I "$script_name requesting resource: $location" conditional_get $local_opts -d "$CACHE_DIR" -T "$tmp_dir" "$location" > "$tmp_file" status_code=$? if [ $status_code -ne 0 ]; then if [ $status_code -gt 1 ]; then - echo "ERROR: $script_name failed to get resource ($status_code): $location" >&2 + print_log_message -E "$script_name failed ($status_code) on location: $location" fi clean_up_and_exit -d "$tmp_dir" $status_code fi @@ -239,8 +231,8 @@ fi /bin/cat "$tmp_file" status_code=$? if [ $status_code -ne 0 ]; then - echo "ERROR: $script_name unable to cat output ($status_code)" >&2 - clean_up_and_exit -d "$tmp_dir" 3 + print_log_message -E "$script_name: cat failed ($status_code)" + clean_up_and_exit -d "$tmp_dir" $status_code fi clean_up_and_exit -d "$tmp_dir" 0