From 75fe9edcf1cdf3762b713aede294d0ee8847d9eb Mon Sep 17 00:00:00 2001 From: Tom Scavo Date: Thu, 20 Apr 2017 15:20:29 -0400 Subject: [PATCH] Implement core library script --- install.sh | 1 + lib/command_paths.sh | 4 + lib/compatible_mktemp.sh | 4 + lib/core_lib.sh | 208 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 217 insertions(+) create mode 100755 lib/core_lib.sh diff --git a/install.sh b/install.sh index 575e468..388ba6b 100755 --- a/install.sh +++ b/install.sh @@ -109,6 +109,7 @@ $script_bin/lib/command_paths.sh $script_bin/lib/compatible_date.sh $script_bin/lib/compatible_mktemp.sh $script_bin/lib/config_tools.sh +$script_bin/lib/core_lib.sh $script_bin/lib/entity_endpoints_txt.xsl $script_bin/lib/entity_identifiers_txt.xsl $script_bin/lib/entity_idp_names_txt.xsl diff --git a/lib/command_paths.sh b/lib/command_paths.sh index 3b8f646..dc69e0f 100755 --- a/lib/command_paths.sh +++ b/lib/command_paths.sh @@ -16,6 +16,10 @@ # limitations under the License. ####################################################################### +####################################################################### +# This script is deprecated. Use core_lib.sh instead. +####################################################################### + export COMMAND_PATHS=true ####################################################################### diff --git a/lib/compatible_mktemp.sh b/lib/compatible_mktemp.sh index 431ad42..8026df7 100755 --- a/lib/compatible_mktemp.sh +++ b/lib/compatible_mktemp.sh @@ -16,6 +16,10 @@ # limitations under the License. ####################################################################### +####################################################################### +# This script is deprecated. Use core_lib.sh instead. +####################################################################### + ####################################################################### # A simple compatibility wrapper around the mktemp command. # diff --git a/lib/core_lib.sh b/lib/core_lib.sh new file mode 100755 index 0000000..90d3896 --- /dev/null +++ b/lib/core_lib.sh @@ -0,0 +1,208 @@ +#!/bin/bash + +####################################################################### +# Copyright 2013--2017 Internet2 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +####################################################################### + +#export COMMAND_PATHS=true +_COMPATIBILITY_MODE=true + +####################################################################### +# Specify the absolute path to each shell command file. +# +# For example, instead of writing code such as: +# +# cat $file | grep $pattern +# +# we write this instead: +# +# $_CAT $file | $_GREP $pattern +# +# Scripts written in this way do not depend on the underlying PATH +# and therefore function well across platforms and as cron jobs. +# +# This script is compatible with Mac OS and GNU/Linux. +####################################################################### + +# commands with compatible paths +_BASE64=/usr/bin/base64 +_BC=/usr/bin/bc +_CAT=/bin/cat +_CMP=/usr/bin/cmp +_CP=/bin/cp +_CURL=/usr/bin/curl +_DATE=/bin/date +_DD=/bin/dd +_DIFF=/usr/bin/diff +_DIRNAME=/usr/bin/dirname +_ECHO=/bin/echo # also a bash builtin +_FIND=/usr/bin/find +_HEAD=/usr/bin/head +_LS=/bin/ls +_MKDIR=/bin/mkdir +_MV=/bin/mv +_OPENSSL=/usr/bin/openssl +_PRINTF=/usr/bin/printf # also a bash builtin +_RM=/bin/rm +_RMDIR=/bin/rmdir +_TAIL=/usr/bin/tail +_TEE=/usr/bin/tee +_TR=/usr/bin/tr +_UNIQ=/usr/bin/uniq +_WC=/usr/bin/wc +_XARGS=/usr/bin/xargs +_XSLTPROC=/usr/bin/xsltproc + +# commands with incompatible paths +if [[ ${OSTYPE} = darwin* ]] ; then + + _AWK=/usr/bin/awk + _BASENAME=/usr/bin/basename + _CUT=/usr/bin/cut + _GREP=/usr/bin/grep + _GZIP=/usr/bin/gzip + _GUNZIP=/usr/bin/gunzip + _MKTEMP=/usr/bin/mktemp + _MORE=/usr/bin/more + _SED=/usr/bin/sed + _SEDEXT="/usr/bin/sed -E" + _SORT=/usr/bin/sort + _TOUCH=/usr/bin/touch + +elif [[ ${OSTYPE} = linux* ]] ; then + + _AWK=/bin/awk + _BASENAME=/bin/basename + _CUT=/bin/cut + _GREP=/bin/grep + _GZIP=/bin/gzip + _GUNZIP=/bin/gunzip + _MKTEMP=/bin/mktemp + _MORE=/bin/more + _SED=/bin/sed + _SEDEXT="/bin/sed -r" + _SORT=/bin/sort + _TOUCH=/bin/touch + +else + echo "ERROR: OS not supported: ${OSTYPE}" >&2 + exit 1 +fi + +####################################################################### +# A simple compatibility wrapper around the mktemp command. +# +# Usage: +# $ make_temp_file [-d] [PREFIX] +# +# By default, creates a temporary file (use the -d option to +# create a directory). Takes an optional prefix argument that +# is used to construct the temporary file (or directory) name +# (defaults to some unspecified prefix if the argument is omitted). +# +# This script is compatible with Mac OS and GNU/Linux. +####################################################################### + +make_temp_file () { + local prefix + local _path_mktemp + local mktemp_arg + local temp_file + local return_code + + # process command-line options (if any) + local OPTARG + local OPTIND + local local_opts= + while getopts "d" opt; do + case $opt in + d) + local_opts=-d + ;; + \?) + echo "ERROR: $FUNCNAME: Unrecognized option: -$OPTARG" >&2 + return 1 + ;; + esac + done + + # determine the prefix + shift $((OPTIND-1)) + if [ $# -eq 0 ]; then + prefix="temp" + else + prefix="$1" + fi + + if [[ ${OSTYPE} = darwin* ]] ; then + _path_mktemp=/usr/bin/mktemp + # on Mac OS, mktemp takes a prefix + mktemp_arg="${prefix}" + elif [[ ${OSTYPE} = linux* ]] ; then + _path_mktemp=/bin/mktemp + # on Linux, mktemp takes a template + mktemp_arg="${prefix}.XXXXXXXX" + else + echo "ERROR: OS not supported: ${OSTYPE}" >&2 + return 1 + fi + + # create temporary file + temp_file=$( ${_path_mktemp} ${local_opts} -t ${mktemp_arg} ) + return_code=$? + if [ $return_code -ne 0 ] ; then + echo "ERROR: $FUNCNAME: failed to make temp file" >&2 + return $return_code + fi + + echo $temp_file + return 0 +} + +####################################################################### +# Prints a message to a log file. +# +# Usage: +# $ print_log_message LOG_MESSAGE LOG_FILE +# +# Computes a timestamp and prepends the timestamp to the message +# before appending the message to the log file. +# +# This script is compatible with Mac OS and GNU/Linux. +####################################################################### + +print_log_message () { + + # local vars + local log_message + local log_file + local tstamp + + # make sure there are two command-line arguments + if [ $# -ne 2 ]; then + echo "ERROR: $FUNCNAME: incorrect number of arguments: $# (2 required)" >&2 + return 2 + fi + + # get the log message + log_message="$1" + log_file="$2" + + # compute timestamp + tstamp=$( /bin/date -u +%Y-%m-%dT%TZ ) + + # prepend timestamp to the log message + printf "%s %s\n" "$tstamp" "$log_message" >> "$log_file" +}