From 067488c3f84bb669e9657bd822195af110879b81 Mon Sep 17 00:00:00 2001 From: Chris Park Date: Fri, 25 Mar 2016 14:24:13 -0400 Subject: [PATCH] Docker and script cleanup. Added error detection. --- examples/docker/Dockerfile | 9 ++-- examples/docker/run_ruby.sh | 102 +++++++++++++++++++++++------------- 2 files changed, 71 insertions(+), 40 deletions(-) diff --git a/examples/docker/Dockerfile b/examples/docker/Dockerfile index da13d71..1639019 100644 --- a/examples/docker/Dockerfile +++ b/examples/docker/Dockerfile @@ -4,11 +4,12 @@ MAINTAINER Fiona Hasanaj # install necessary software RUN apt-get -y update && apt-get install -y vim && apt-get install -y git && gem install rubysl-securerandom -COPY run_ruby.sh /ruby/examples/run_ruby.sh -RUN chmod 755 /ruby/examples/run_ruby.sh -WORKDIR /ruby/examples +RUN mkdir /ruby +WORKDIR /ruby +COPY run_ruby.sh run_ruby.sh +RUN chmod 0755 run_ruby.sh # allow interactive bash inside docker container CMD ./run_ruby.sh $API_KEY $FILENAME $ALT_URL -VOLUME ["/source"] \ No newline at end of file +VOLUME ["/source"] diff --git a/examples/docker/run_ruby.sh b/examples/docker/run_ruby.sh index 5d59533..6b28e3d 100644 --- a/examples/docker/run_ruby.sh +++ b/examples/docker/run_ruby.sh @@ -1,5 +1,11 @@ #!/bin/bash +ping_url="https://api.rosette.com/rest/v1" +retcode=0 +errors=( "Exception" "processingFailure" ) + +#------------ Start Functions -------------------------- + #Gets called when the user doesn't provide any args function HELP { echo -e "\nusage: source_file.rb API_KEY [ALT_URL]" @@ -9,6 +15,56 @@ function HELP { exit 1 } +#Checks if Rosette API key is valid +function checkAPI { + match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "forbidden") + if [ ! -z $match ]; then + echo -e "\nInvalid Rosette API Key" + exit 1 + fi +} + + +# strip the trailing slash off of the alt_url if necessary +function cleanURL() { + if [ ! -z "${ALT_URL}" ]; then + case ${ALT_URL} in + */) ALT_URL=${ALT_URL::-1} + echo "Slash detected" + ;; + esac + ping_url=${ALT_URL} + fi +} + +#Checks for valid url +function validateURL() { + match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "Rosette API") + if [ "${match}" = "" ]; then + echo -e "\n${ping_url} server not responding\n" + exit 1 + fi +} + +function runExample() { + echo -e "\n---------- ${1} start -------------" + result="" + if [ -z ${ALT_URL} ]; then + result="$(ruby ${1} ${API_KEY} 2>&1 )" + else + result="$(ruby ${1} ${API_KEY} ${ALT_URL} 2>&1 )" + fi + echo "${result}" + echo -e "\n---------- ${1} end -------------" + for err in "${errors[@]}"; do + if [[ ${result} == *"${err}"* ]]; then + retcode=1 + fi + done +} + +#------------ End Functions ---------------------------- + #Gets API_KEY, FILENAME and ALT_URL if present while getopts ":API_KEY:FILENAME:ALT_URL" arg; do case "${arg}" in @@ -27,52 +83,26 @@ while getopts ":API_KEY:FILENAME:ALT_URL" arg; do esac done -ping_url="https://api.rosette.com/rest/v1" - -# strip the trailing slash off of the alt_url if necessary -if [ ! -z "${ALT_URL}" ]; then - case ${ALT_URL} in - */) ALT_URL=${ALT_URL::-1} - echo "Slash detected" - ;; - esac - ping_url=${ALT_URL} -fi - -#Checks for valid url -match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "Rosette API") -if [ "${match}" = "" ]; then - echo -e "\n${ping_url} server not responding\n" - exit 1 -fi - -#Checks if Rosette API key is valid -function checkAPI { - match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "forbidden") - if [ ! -z $match ]; then - echo -e "\nInvalid Rosette API Key" - exit 1 - fi -} +cleanURL +validateURL #Copy the mounted content in /source to current WORKDIR -cp /source/*.* . +cp -r -n /source/. . #Run the examples if [ ! -z ${API_KEY} ]; then checkAPI + cd examples if [ ! -z ${FILENAME} ]; then - if [ ! -z ${ALT_URL} ]; then - ruby ${FILENAME} ${API_KEY} ${ALT_URL} - else - ruby ${FILENAME} ${API_KEY} - fi - elif [ ! -z ${ALT_URL} ]; then - find -maxdepth 1 -name '*.rb' -print -exec ruby {} ${API_KEY} ${ALT_URL} \; + runExample ${FILENAME} else - find -maxdepth 1 -name '*.rb' -print -exec ruby {} ${API_KEY} \; + for file in *.rb; do + runExample ${file} + done fi else HELP fi + +exit ${retcode}