2014-04-15 23:58:41 +04:00
#!/bin/bash
2016-12-19 19:34:13 +03:00
fatal( ) {
2018-03-21 15:22:08 +03:00
if [ -n " ${ 2 } " ] ; then
echo -e " Error: ${ 2 } "
fi
exit ${ 1 }
2016-12-19 19:34:13 +03:00
}
2014-04-15 23:58:41 +04:00
usage( ) {
2016-12-19 19:34:13 +03:00
cat << EOF
Usage: $0 <options>
Request server configuration from Ansible Tower.
OPTIONS:
-h Show this message
-s Tower server ( e.g. https://tower.example.com) ( required)
-k Allow insecure SSL connections and transfers
-c Host config key ( required)
-t Job template ID ( required)
-e Extra variables
EOF
2014-04-15 23:58:41 +04:00
}
2016-12-19 19:34:13 +03:00
# Initialize variables
INSECURE = ""
2014-04-15 23:58:41 +04:00
2016-12-19 19:34:13 +03:00
# Parse arguments
2017-02-03 21:33:11 +03:00
while getopts “hks:c:t:s:e:” OPTION
2014-04-15 23:58:41 +04:00
do
2018-03-21 15:22:08 +03:00
case ${ OPTION } in
h)
usage
exit 1
; ;
s)
TOWER_SERVER = ${ OPTARG }
; ;
k)
INSECURE = "-k"
; ;
c)
HOST_CFG_KEY = ${ OPTARG }
; ;
t)
TEMPLATE_ID = ${ OPTARG }
; ;
e)
EXTRA_VARS = ${ OPTARG }
; ;
?)
usage
exit
; ;
esac
2014-04-15 23:58:41 +04:00
done
2016-12-19 19:34:13 +03:00
# Validate required arguments
test -z ${ TOWER_SERVER } && fatal 1 "Missing required -s argument"
# Make sure TOWER_SERVER starts with http:// or https://
[ [ " ${ TOWER_SERVER } " = ~ ^https?:// ] ] || fatal 1 "Tower server must begin with http:// or https://"
test -z ${ HOST_CFG_KEY } && fatal 1 "Missing required -c argument"
test -z ${ TEMPLATE_ID } && fatal 1 "Missing required -t argument"
# Generate curl --data parameter
if [ -n " ${ EXTRA_VARS } " ] ; then
2018-03-21 15:22:08 +03:00
CURL_DATA = " {\"host_config_key\": \" ${ HOST_CFG_KEY } \", \"extra_vars\": \" ${ EXTRA_VARS } \"} "
2016-12-19 19:34:13 +03:00
else
2018-03-21 15:22:08 +03:00
CURL_DATA = " {\"host_config_key\": \" ${ HOST_CFG_KEY } \"} "
2016-12-19 19:34:13 +03:00
fi
2018-03-21 15:22:08 +03:00
# Success on any 2xx status received, failure on only 404 status received, retry any other status every min for up to 10 min
RETRY_ATTEMPTS = 10
ATTEMPT = 0
while [ [ $ATTEMPT -lt $RETRY_ATTEMPTS ] ]
do
set -o pipefail
HTTP_STATUS = $( curl ${ INSECURE } -s -i -X POST -H 'Content-Type:application/json' --data " $CURL_DATA " ${ TOWER_SERVER } /api/v2/job_templates/${ TEMPLATE_ID } /callback/ 2>& 1 | head -n1 | awk '{print $2}' )
CURL_RC = $?
if [ ${ CURL_RC } -ne 0 ] ; then
2016-12-19 19:34:13 +03:00
fatal ${ CURL_RC } " curl exited with ${ CURL_RC } , halting. "
2018-03-21 15:22:08 +03:00
fi
2016-12-19 19:34:13 +03:00
2018-03-21 15:22:08 +03:00
# Extract http status code
if [ [ ${ HTTP_STATUS } = ~ ^2[ 0-9] +$ ] ] ; then
2016-12-19 19:34:13 +03:00
echo " Success: ${ HTTP_STATUS } received. "
2018-03-21 15:22:08 +03:00
break
elif [ [ ${ HTTP_STATUS } = ~ ^404$ ] ] ; then
fatal 1 " Failed: ${ HTTP_STATUS } received, encountered problem, halting. "
else
ATTEMPT = $(( ATTEMPT + 1 ))
echo " Failed: ${ HTTP_STATUS } received, executing retry # ${ ATTEMPT } in 1 minute. "
sleep 60
fi
done