From ca82239e51e5e8c2549595efa711ebf9792eb247 Mon Sep 17 00:00:00 2001 From: Tom Scavo Date: Fri, 5 May 2017 11:40:36 -0400 Subject: [PATCH] Add new shell script --- bin/list_all_entities_exported.sh | 258 ++++++++++++++++++++++++++++++ install.sh | 1 + 2 files changed, 259 insertions(+) create mode 100755 bin/list_all_entities_exported.sh diff --git a/bin/list_all_entities_exported.sh b/bin/list_all_entities_exported.sh new file mode 100755 index 0000000..f08ec83 --- /dev/null +++ b/bin/list_all_entities_exported.sh @@ -0,0 +1,258 @@ +#!/bin/bash + +####################################################################### +# Copyright 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. +####################################################################### + +####################################################################### +# Help message +####################################################################### + +display_help () { +/bin/cat <<- HELP_MSG + This script ... + + Usage: ${0##*/} [-hvq] -d OUT_DIR + + This script takes a single command-line argument... + + This script requires numerous environment variables... + + Options: + -h Display this help message + -v Enable DEBUG mode + -d Specify output directory + + Option -h is mutually exclusive of all other options. + + LIBRARY + + Environment variable LIB_DIR specifies a directory containing at + least the following library files... + + EXAMPLES + + out_dir= + ${0##*/} \$out_dir +HELP_MSG +} + +####################################################################### +# Bootstrap environment +####################################################################### + +script_name=${0##*/} # equivalent to basename $0 + +# check the bin directory +if [ -z "$BIN_DIR" ]; then + echo "ERROR: $script_name requires env var BIN_DIR" >&2 + exit 2 +fi +if [ ! -d "$BIN_DIR" ]; then + echo "ERROR: $script_name: directory does not exist: $BIN_DIR" >&2 + exit 2 +fi + +# check 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 + +# check the cache directory +if [ -z "$CACHE_DIR" ]; then + echo "ERROR: $script_name requires env var CACHE_DIR" >&2 + exit 2 +fi +if [ ! -d "$CACHE_DIR" ]; then + echo "ERROR: $script_name: directory does not exist: $CACHE_DIR" >&2 + exit 2 +fi + +# check the tmp directory +if [ -z "$TMPDIR" ]; then + echo "ERROR: $script_name requires env var TMPDIR" >&2 + exit 2 +fi +if [ ! -d "$TMPDIR" ]; then + echo "ERROR: $script_name: directory does not exist: $TMPDIR" >&2 + exit 2 +fi + +# check the log file +if [ -z "$LOG_FILE" ]; then + echo "ERROR: $script_name requires env var LOG_FILE" >&2 + exit 2 +fi +if [ ! -f "$LOG_FILE" ] && [[ $LOG_FILE != /dev/* ]]; then + echo "ERROR: $script_name: file does not exist: $LOG_FILE" >&2 + exit 2 +fi + +####################################################################### +# Process command-line options and arguments +####################################################################### + +help_mode=false; local_opts= +while getopts ":hvd:" opt; do + case $opt in + h) + help_mode=true + ;; + v) + LOG_LEVEL=4 + local_opts="$local_opts -$opt" + ;; + d) + out_dir="$OPTARG" + ;; + \?) + echo "ERROR: $script_name: Unrecognized option: -$OPTARG" >&2 + exit 2 + ;; + :) + echo "ERROR: $script_name: Option -$OPTARG requires an argument" >&2 + exit 2 + ;; + esac +done + +if $help_mode; then + display_help + exit 0 +fi + +# check the output directory +if [ ! -d "$out_dir" ]; then + echo "ERROR: $script_name: directory does not exist: $out_dir" >&2 + exit 2 +fi + +# check command-line arguments +shift $(( OPTIND - 1 )) +if [ $# -ne 0 ]; then + echo "ERROR: $script_name: wrong number of arguments: $# (0 required)" >&2 + exit 2 +fi + +####################################################################### +# Initialization +####################################################################### + +# executable script must exist +cget="$BIN_DIR/cget.sh" +if [ ! -f "$cget" ]; then + echo "ERROR: $script_name: executable file does not exist: $cget" >&2 + exit 2 +fi + +# source core library +lib_file="$LIB_DIR/core_lib.sh" +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 + +# XSL stylesheets must exist +xsl_file1=$LIB_DIR/list_all_IdPs_csv.xsl +if [ ! -f "$xsl_file1" ]; then + echo "ERROR: $script_name: lib file does not exist: $xsl_file1" >&2 + exit 2 +fi +xsl_file2=$LIB_DIR/list_all_SPs_csv.xsl +if [ ! -f "$xsl_file2" ]; then + echo "ERROR: $script_name: lib file does not exist: $xsl_file2" >&2 + exit 2 +fi + +# create a temporary subdirectory +tmp_dir="${TMPDIR%%/}/${script_name%%.*}_$$" +/bin/mkdir "$tmp_dir" +exit_code=$? +if [ $exit_code -ne 0 ]; then + echo "ERROR: $script_name failed ($exit_code) to create tmp dir $tmp_dir" >&2 + exit 2 +fi + +# input file +xml_file="${tmp_dir}/saml-metadata.xml" + +# output files +csv_file1="${tmp_dir}/all-exported-idps.csv" +csv_file2="${tmp_dir}/all-exported-sps.csv" + +####################################################################### +# Main processing +####################################################################### + +print_log_message -I "$script_name BEGIN" + +# XML metadata +md_location=http://md.incommon.org/InCommon/InCommon-metadata-export.xml +print_log_message -D "$script_name using metadata file: $md_location" + +# get a fresh metadata file +print_log_message -D "$script_name writing XML file: $xml_file" +$cget $local_opts -F $md_location > $xml_file 2> /dev/null +exit_code=$? +if [ $exit_code -eq 1 ]; then + # short-circuit if 304 response + print_log_message -I "$script_name END" + clean_up_and_exit -d "$tmp_dir" 0 +fi +if [ $exit_code -gt 1 ]; then + print_log_message -E "$script_name: cget.sh failed ($exit_code) on location: $md_location" + clean_up_and_exit -d "$tmp_dir" $exit_code +fi + +# create the first output file +print_log_message -D "$script_name writing output file: $csv_file1" +/usr/bin/xsltproc $xsl_file1 $xml_file > $csv_file1 2> /dev/null +exit_code=$? +if [ $exit_code -ne 0 ]; then + print_log_message -E "$script_name: xsltproc failed ($exit_code) on stylesheet: $xsl_file1" + clean_up_and_exit -d "$tmp_dir" $exit_code +fi + +# create the second output file +print_log_message -D "$script_name writing output file: $csv_file2" +/usr/bin/xsltproc $xsl_file2 $xml_file > $csv_file2 2> /dev/null +exit_code=$? +if [ $exit_code -ne 0 ]; then + print_log_message -E "$script_name: xsltproc failed ($exit_code) on stylesheet: $xsl_file2" + clean_up_and_exit -d "$tmp_dir" $exit_code +fi + +# move the output files to the web directory +print_log_message -D "$script_name moving output files to dir: $out_dir" +/bin/mv $csv_file1 $csv_file2 $out_dir 2> /dev/null +exit_code=$? +if [ $exit_code -ne 0 ]; then + print_log_message -E "$script_name: mv failed ($exit_code) to dir: $out_dir" + clean_up_and_exit -d "$tmp_dir" $exit_code +fi + +print_log_message -I "$script_name END" +clean_up_and_exit -d "$tmp_dir" 0 diff --git a/install.sh b/install.sh index a311d92..7a84183 100755 --- a/install.sh +++ b/install.sh @@ -93,6 +93,7 @@ while read script_file; do fi done <