mirror of
https://github.com/ansible/awx.git
synced 2026-02-18 03:30:02 -03:30
Merge pull request #4498 from jlaska/request_configuration
Rewrite request_tower_configuration.sh
This commit is contained in:
104
tools/scripts/request_tower_configuration.sh
Normal file → Executable file
104
tools/scripts/request_tower_configuration.sh
Normal file → Executable file
@@ -1,28 +1,90 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
usage() {
|
fatal() {
|
||||||
echo -e "Requests server configuration from Ansible Tower\n"
|
if [ -n "${2}" ]; then
|
||||||
echo "Usage: $0 <server address>[:server port] <host config key> <job template id>"
|
echo -e "Error: ${2}"
|
||||||
echo "Example: $0 example.towerhost.net 44d7507f2ead49af5fca80aa18fd24bc 38"
|
fi
|
||||||
exit 1
|
exit ${1}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ $# -lt 3 ] && usage
|
usage() {
|
||||||
|
cat << EOF
|
||||||
|
Usage: $0 <options>
|
||||||
|
|
||||||
retry_attempts=10
|
Request server configuration from Ansible Tower.
|
||||||
attempt=0
|
|
||||||
while [[ $attempt -lt $retry_attempts ]]
|
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
|
||||||
|
-s Number of seconds between retries (default: ${NUM_SECONDS})
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
# Initialize variables
|
||||||
|
INSECURE=""
|
||||||
|
NUM_SECONDS=60
|
||||||
|
|
||||||
|
# Parse arguments
|
||||||
|
while getopts “hks:c:t:s:” OPTION
|
||||||
do
|
do
|
||||||
status_code=`curl -s -i --data "host_config_key=$2" http://$1/api/v1/job_templates/$3/callback/ | head -n 1 | awk '{print $2}'`
|
case ${OPTION} in
|
||||||
if [[ $status_code -ge 300 ]]
|
h)
|
||||||
then
|
usage
|
||||||
echo "${status_code} received, encountered problem, halting."
|
exit 1
|
||||||
exit 1
|
;;
|
||||||
else
|
s)
|
||||||
exit 0
|
TOWER_SERVER=${OPTARG}
|
||||||
fi
|
;;
|
||||||
attempt=$(( attempt + 1 ))
|
k)
|
||||||
echo "${status_code} received... retrying in 1 minute. (Attempt ${attempt})"
|
INSECURE="-k"
|
||||||
sleep 60
|
;;
|
||||||
|
c)
|
||||||
|
HOST_CFG_KEY=${OPTARG}
|
||||||
|
;;
|
||||||
|
t)
|
||||||
|
TEMPLATE_ID=${OPTARG}
|
||||||
|
;;
|
||||||
|
e)
|
||||||
|
EXTRA_VARS=${OPTARG}
|
||||||
|
;;
|
||||||
|
2)
|
||||||
|
NUM_SECONDS=${OPTARG}
|
||||||
|
;;
|
||||||
|
?)
|
||||||
|
usage
|
||||||
|
exit
|
||||||
|
;;
|
||||||
|
esac
|
||||||
done
|
done
|
||||||
exit 1
|
|
||||||
|
# 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
|
||||||
|
CURL_DATA="{\"host_config_key\": \"${HOST_CFG_KEY}\", \"extra_vars\": \"${EXTRA_VARS}\"}"
|
||||||
|
else
|
||||||
|
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
|
||||||
|
fatal ${CURL_RC} "curl exited with ${CURL_RC}, halting."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Extract http status code
|
||||||
|
if [[ ${HTTP_STATUS} -ge 300 ]]; then
|
||||||
|
fatal 1 "${HTTP_STATUS} received, encountered problem, halting."
|
||||||
|
else
|
||||||
|
echo "Success: ${HTTP_STATUS} received."
|
||||||
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user