diff --git a/tools/scripts/request_tower_configuration.ps1 b/tools/scripts/request_tower_configuration.ps1 index 17f1e91012..db18bfa6bd 100644 --- a/tools/scripts/request_tower_configuration.ps1 +++ b/tools/scripts/request_tower_configuration.ps1 @@ -1,7 +1,8 @@ Param( [string]$tower_url, [string]$host_config_key, - [string]$job_template_id + [string]$job_template_id, + [string]$extra_vars ) Set-StrictMode -Version 2 @@ -10,24 +11,36 @@ $ErrorActionPreference = "Stop" If(-not $tower_url -or -not $host_config_key -or -not $job_template_id) { Write-Host "Requests server configuration from Ansible Tower" - Write-Host "Usage: $($MyInvocation.MyCommand.Name) [:server port] " - Write-Host "Example: $($MyInvocation.MyCommand.Name) example.towerhost.net 44d7507f2ead49af5fca80aa18fd24bc 38" + Write-Host "Usage: $($MyInvocation.MyCommand.Name) https://[:server port] " + 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 } $retry_attempts = 10 $attempt = 0 -$data = @{ - host_config_key=$host_config_key +If(-not $extra_vars) +{ + $data = @{ + host_config_key=$host_config_key + } +} Else { + $data = @{ + host_config_key=$host_config_key + extra_vars=$extra_vars + } } While ($attempt -lt $retry_attempts) { 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 + } ElseIf ($resp.StatusCode -eq 404) { + Write-Host "$resp.StatusCode received... encountered problem, halting" + Exit 1 } } Catch { @@ -37,4 +50,4 @@ While ($attempt -lt $retry_attempts) { } Start-Sleep -Seconds 60 } -Exit 1 \ No newline at end of file +Exit 1 diff --git a/tools/scripts/request_tower_configuration.sh b/tools/scripts/request_tower_configuration.sh index 05d75a69dd..1c0718ef49 100755 --- a/tools/scripts/request_tower_configuration.sh +++ b/tools/scripts/request_tower_configuration.sh @@ -1,10 +1,10 @@ #!/bin/bash fatal() { - if [ -n "${2}" ]; then - echo -e "Error: ${2}" - fi - exit ${1} + if [ -n "${2}" ]; then + echo -e "Error: ${2}" + fi + exit ${1} } usage() { @@ -29,31 +29,31 @@ INSECURE="" # Parse arguments while getopts “hks:c:t:s:e:” OPTION do - 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 + 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 done # Validate required arguments @@ -65,21 +65,32 @@ test -z ${TEMPLATE_ID} && fatal 1 "Missing required -t argument" # Generate curl --data parameter 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 - CURL_DATA="{\"host_config_key\": \"${HOST_CFG_KEY}\"}" + CURL_DATA="{\"host_config_key\": \"${HOST_CFG_KEY}\"}" fi -set -o pipefail -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}') -CURL_RC=$? -if [ ${CURL_RC} -ne 0 ]; then +# 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 fatal ${CURL_RC} "curl exited with ${CURL_RC}, halting." -fi + fi -# Extract http status code -if [[ ${HTTP_STATUS} -ge 300 ]]; then - fatal 1 "${HTTP_STATUS} received, encountered problem, halting." -else + # Extract http status code + if [[ ${HTTP_STATUS} =~ ^2[0-9]+$ ]]; then 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