Updates to callback scripts that include retry functionality for bash and extra_vars handling for PowerShell

Signed-off-by: Rob Ruma <robruma@users.noreply.github.com>
This commit is contained in:
Rob Ruma
2018-03-21 08:22:08 -04:00
parent e58038b056
commit e8b026648f
2 changed files with 73 additions and 49 deletions

View File

@@ -1,7 +1,8 @@
Param( Param(
[string]$tower_url, [string]$tower_url,
[string]$host_config_key, [string]$host_config_key,
[string]$job_template_id [string]$job_template_id,
[string]$extra_vars
) )
Set-StrictMode -Version 2 Set-StrictMode -Version 2
@@ -10,24 +11,36 @@ $ErrorActionPreference = "Stop"
If(-not $tower_url -or -not $host_config_key -or -not $job_template_id) If(-not $tower_url -or -not $host_config_key -or -not $job_template_id)
{ {
Write-Host "Requests server configuration from Ansible Tower" Write-Host "Requests server configuration from Ansible Tower"
Write-Host "Usage: $($MyInvocation.MyCommand.Name) <server address>[:server port] <host config key> <job template id>" Write-Host "Usage: $($MyInvocation.MyCommand.Name) https://<server address>[:server port] <host config key> <job template id>"
Write-Host "Example: $($MyInvocation.MyCommand.Name) example.towerhost.net 44d7507f2ead49af5fca80aa18fd24bc 38" Write-Host "Example: $($MyInvocation.MyCommand.Name) https://example.towerhost.net 44d7507f2ead49af5fca80aa18fd24bc 38"
Write-Host "Example with extra_vars: $($MyInvocation.MyCommand.Name) https://example.towerhost.net 44d7507f2ead49af5fca80aa18fd24bc 38 '{ key: value, dict: { key: value }}'"
Exit 1 Exit 1
} }
$retry_attempts = 10 $retry_attempts = 10
$attempt = 0 $attempt = 0
$data = @{ If(-not $extra_vars)
host_config_key=$host_config_key {
$data = @{
host_config_key=$host_config_key
}
} Else {
$data = @{
host_config_key=$host_config_key
extra_vars=$extra_vars
}
} }
While ($attempt -lt $retry_attempts) { While ($attempt -lt $retry_attempts) {
Try { Try {
$resp = Invoke-WebRequest -Method POST -Body $data -Uri http://$tower_url/api/v1/job_templates/$job_template_id/callback/ -UseBasicParsing $resp = Invoke-WebRequest -ContentType application/json -Method POST -Body (ConvertTo-Json $data) -Uri $tower_url/api/v2/job_templates/$job_template_id/callback/
If($resp.StatusCode -eq 202) { If ($resp.StatusCode -match '^2[0-9]+$') {
Exit 0 Exit 0
} ElseIf ($resp.StatusCode -eq 404) {
Write-Host "$resp.StatusCode received... encountered problem, halting"
Exit 1
} }
} }
Catch { Catch {

View File

@@ -1,10 +1,10 @@
#!/bin/bash #!/bin/bash
fatal() { fatal() {
if [ -n "${2}" ]; then if [ -n "${2}" ]; then
echo -e "Error: ${2}" echo -e "Error: ${2}"
fi fi
exit ${1} exit ${1}
} }
usage() { usage() {
@@ -29,31 +29,31 @@ INSECURE=""
# Parse arguments # Parse arguments
while getopts “hks:c:t:s:e:” OPTION while getopts “hks:c:t:s:e:” OPTION
do do
case ${OPTION} in case ${OPTION} in
h) h)
usage usage
exit 1 exit 1
;; ;;
s) s)
TOWER_SERVER=${OPTARG} TOWER_SERVER=${OPTARG}
;; ;;
k) k)
INSECURE="-k" INSECURE="-k"
;; ;;
c) c)
HOST_CFG_KEY=${OPTARG} HOST_CFG_KEY=${OPTARG}
;; ;;
t) t)
TEMPLATE_ID=${OPTARG} TEMPLATE_ID=${OPTARG}
;; ;;
e) e)
EXTRA_VARS=${OPTARG} EXTRA_VARS=${OPTARG}
;; ;;
?) ?)
usage usage
exit exit
;; ;;
esac esac
done done
# Validate required arguments # Validate required arguments
@@ -65,21 +65,32 @@ test -z ${TEMPLATE_ID} && fatal 1 "Missing required -t argument"
# Generate curl --data parameter # Generate curl --data parameter
if [ -n "${EXTRA_VARS}" ]; then if [ -n "${EXTRA_VARS}" ]; then
CURL_DATA="{\"host_config_key\": \"${HOST_CFG_KEY}\", \"extra_vars\": \"${EXTRA_VARS}\"}" CURL_DATA="{\"host_config_key\": \"${HOST_CFG_KEY}\", \"extra_vars\": \"${EXTRA_VARS}\"}"
else else
CURL_DATA="{\"host_config_key\": \"${HOST_CFG_KEY}\"}" CURL_DATA="{\"host_config_key\": \"${HOST_CFG_KEY}\"}"
fi fi
set -o pipefail # Success on any 2xx status received, failure on only 404 status received, retry any other status every min for up to 10 min
HTTP_STATUS=$(curl ${INSECURE} -s -i -X POST -H 'Content-Type:application/json' --data "$CURL_DATA" ${TOWER_SERVER}/api/v1/job_templates/${TEMPLATE_ID}/callback/ 2>&1 | head -n1 | awk '{print $2}') RETRY_ATTEMPTS=10
CURL_RC=$? ATTEMPT=0
if [ ${CURL_RC} -ne 0 ]; then 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
fatal ${CURL_RC} "curl exited with ${CURL_RC}, halting." fatal ${CURL_RC} "curl exited with ${CURL_RC}, halting."
fi fi
# Extract http status code # Extract http status code
if [[ ${HTTP_STATUS} -ge 300 ]]; then if [[ ${HTTP_STATUS} =~ ^2[0-9]+$ ]]; then
fatal 1 "${HTTP_STATUS} received, encountered problem, halting."
else
echo "Success: ${HTTP_STATUS} received." echo "Success: ${HTTP_STATUS} received."
fi 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