Created
June 9, 2017 07:00
-
-
Save ladar/8d188ee3324939e6bd7bae1c8ea9d145 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Name: links.sh | |
# Author: Ladar Levison | |
# | |
# Description: Used to verify packer configurations, and validate URLs before kicking off a build. | |
# The list of packer config files. | |
FILES="packer-docker.json packer-libvirt.json packer-vmware.json packer-virtualbox.json" | |
# Collect the list of ISO urls. | |
ISOURLS=(`grep --no-messages -E "iso_url|guest_additions_url" ${FILES} | awk -F'"' '{print $4}'`) | |
ISOSUMS=(`grep --no-messages -E "iso_checksum|guest_additions_sha256" ${FILES} | grep -v "iso_checksum_type" | awk -F'"' '{print $4}'`) | |
# Verify all of the ISO locations are still valid. | |
function verify_url { | |
# Grab just the response header and look for the 200 response code to indicate the link is valid. | |
curl --silent --head "$1" | head -1 | grep --silent --extended-regexp "HTTP/1\.1 200 OK|HTTP/2\.0 200 OK" | |
# The grep return code tells us whether it found a match in the header or not. | |
if [ $? != 0 ]; then | |
printf "Link Failure: $1\n\n" | |
exit 1 | |
fi | |
} | |
# Verify all of the ISO locations are valid and then download the ISO and verify the hash. | |
function verify_sum { | |
# Grab just the response header and look for the 200 response code to indicate the link is valid. | |
curl --silent --head "$1" | head -1 | grep --silent --extended-regexp "HTTP/1\.1 200 OK|HTTP/2\.0 200 OK" | |
# The grep return code tells us whether it found a match in the header or not. | |
if [ $? != 0 ]; then | |
printf "Link Failure: $1\n\n" | |
exit 1 | |
fi | |
# Grab the ISO and pipe the data through sha256sum, then compare the checksum value. | |
curl --silent "$1" | sha256sum | grep --silent "$2" | |
# The grep return code tells us whether it found a match in the header or not. | |
if [ $? != 0 ]; then | |
SUM=`curl --silent "$1" | sha256sum | awk -F' ' '{print $1}'` | |
printf "Hash Failure: $1\n" | |
printf "Found - $SUM\n" | |
printf "Expected - $SUM\n\n" | |
exit 1 | |
fi | |
printf "Validated : $1\n" | |
return 0 | |
} | |
# Validate the templates before building. | |
function verify_json() { | |
packer validate $1 | |
if [[ $? != 0 ]]; then | |
tput setaf 1; tput bold; printf "\n\nthe $1 packer template failed to validate...\n\n"; tput sgr0 | |
for i in 1 2 3; do printf "\a"; sleep 1; done | |
exit 1 | |
fi | |
} | |
function links() { | |
for ((i = 0; i < ${#ISOURLS[@]}; ++i)); do | |
verify_url "${ISOURLS[$i]}" "${ISOSUMS[$i]}" | |
done | |
# Let the user know all of the links passed. | |
printf "\nAll ${#ISOURLS[@]} of the install media locations are still valid...\n\n" | |
} | |
function sums() { | |
for ((i = 0; i < ${#ISOURLS[@]}; ++i)); do | |
verify_sum "${ISOURLS[$i]}" "${ISOSUMS[$i]}" | |
done | |
# Let the user know all of the links passed. | |
printf "\nAll ${#ISOURLS[@]} of the install media locations are still valid...\n\n" | |
} | |
function validate() { | |
LIST=($FILES) | |
for ((i = 0; i < ${#LIST[@]}; ++i)); do | |
verify_json "${LIST[$i]}" | |
done | |
} | |
# The generic functions. | |
if [[ $1 == "links" ]]; then links | |
elif [[ $1 == "sums" ]]; then sums | |
elif [[ $1 == "validate" ]]; then validate | |
# Catchall | |
else | |
echo "" | |
echo " Commands" | |
echo $" `basename $0` {sums|links|validate}" | |
echo "" | |
echo " Packer Files" | |
echo $" $FILES" | |
echo "" | |
echo " Please select a target and run this command again." | |
echo "" | |
exit 2 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment