Last active
March 4, 2016 21:09
-
-
Save patrickcrocker/5e371cf7d45820f3b0a4 to your computer and use it in GitHub Desktop.
This file contains 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 | |
[ "$1" = "-d" ] && debug=on || debug=off | |
# Make working directory | |
export wrkdir=/tmp/wrkdir.$$ | |
mkdir -p ${wrkdir} | |
# Clean up function | |
clean_up() | |
{ | |
trap ":" 1 2 3 15 | |
if [ "${SIGSET}" ] | |
then | |
printf "\n%s aborted...\n" $(basename $0) | |
[ -f "${dldir}/${target_file}" ] && rm -f ${dldir}/${target_file} | |
fi | |
[ "${wrkdir}" ] && rm -rf ${wrkdir} | |
} | |
# Clean up if the script is quit | |
trap "SIGSET=TRUE;clean_up;exit 1" 1 2 3 15 | |
# Get download directory or hard code and remove the next hash mark | |
#export dldir=/tmp | |
while [ -z ${dldir} ]; do | |
read -p "Enter the download directory : " dldir | |
if [ ! -d "${dldir}" ] | |
then | |
printf "\n%s does not exist...\n" ${dldir} | |
unset dldir | |
elif [ ! -w "${dldir}" ] | |
then | |
printf "\n%s is not writable...\n" ${dldir} | |
unset dldir | |
fi | |
done | |
[ ${debug} = 'on' ] && echo "dldir=\""${dldir}"\"" | |
# Get api_token or hard code and remove the next hash mark | |
#api_token="aAaAaAaAaAaAaAaAaAaA" | |
[ -z ${api_token} ] && read -p "Enter your network.pivotal.io API Token: " api_token | |
[ ${debug} = 'on' ] && echo "api_token=\""${api_token}"\"" | |
# Authenticate | |
curl --silent -i -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Token ${api_token}" -X GET https://network.pivotal.io/api/v2/authentication > ${wrkdir}/authenticate | |
if [ $(grep -c "HTTP/1.1 200 OK" ${wrkdir}/authenticate) -ne 1 ] | |
then | |
printf "Authentication failed, please check your API Token and try again. Exiting...\n" | |
cat ${wrkdir}/authenticate | |
clean_up | |
exit 1 | |
fi | |
read -p "Ops Manager Username: " opsmgr_user | |
read -s -p "Ops Manager Password: " opsmgr_pass | |
# Get products list | |
curl --silent -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Token ${api_token}" -X GET https://network.pivotal.io/api/v2/products | python -mjson.tool > ${wrkdir}/prod_list | |
grep '"name":' ${wrkdir}/prod_list | egrep -vi 'suite|foundry|buildpacks' | cut -d'"' -f4 | sort > ${wrkdir}/prod_name_list | |
# Get product name | |
printf "\nThese are the available products:\n\n" | |
column ${wrkdir}/prod_name_list | |
printf "\n" | |
while true; do | |
read -p "Which product do you want to download? " prod_name | |
if [ $(grep -c "^${prod_name}$" ${wrkdir}/prod_name_list) -eq 1 ] | |
then | |
break | |
else | |
printf "Sorry can't find that product, please re-enter the product name from the list above\n" | |
fi | |
done | |
# Get slug | |
export prod_slug=$(sed -n "/\"${prod_name}\"/,/\"slug\"/p" ${wrkdir}/prod_list | tail -1 | cut -d'"' -f4) | |
[ ${debug} = 'on' ] && echo "prod_slug=\""${prod_slug}"\"" | |
# Get product ID | |
export prod_id=$(curl --silent -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Token ${api_token}" -X GET https://network.pivotal.io/api/v2/products | python -mjson.tool | sed -n "/${prod_slug}\/releases/,/\"id\":/p" | tail -1 | tr -dc '[:digit:]') | |
[ ${debug} = 'on' ] && echo "prod_id=\""${prod_id}"\"" | |
# Get release ID | |
curl --silent -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Token ${api_token}" -X GET https://network.pivotal.io/api/v2/products/${prod_slug}/releases | python -mjson.tool > ${wrkdir}/prod_releases | |
# Get version | |
printf "\nThese are the available versions:\n\n" | |
grep '"version":' ${wrkdir}/prod_releases | cut -d'"' -f4 | |
printf "\n" | |
while true; do | |
read -p "Which version do you want to download? " prod_version | |
if [ $(grep -c "\"version\": \"${prod_version}\"" ${wrkdir}/prod_releases) -eq 1 ] | |
then | |
break | |
else | |
printf "Sorry can't find that version, please re-enter the product version from the list above\n" | |
fi | |
done | |
[ ${debug} = 'on' ] && echo "prod_version=\""${prod_version}"\"" | |
# Get release ID | |
export rel_id=$(tac ${wrkdir}/prod_releases | sed -n "/${prod_version}/,/id/p"|tail -1|tr -dc '[:digit:]') | |
[ ${debug} = 'on' ] && echo "rel_id=\""${rel_id}"\"" | |
# Get file ID | |
curl --silent -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Token ${api_token}" -X GET https://network.pivotal.io/api/v2/products/${prod_slug}/releases/${rel_id} | python -mjson.tool > ${wrkdir}/prod_fileid | |
# Get file to download | |
printf "\nThese are the available files:\n\n" | |
sed -n "/\"download\":/,/\"name\":/p" ${wrkdir}/prod_fileid | grep name | cut -d'"' -f4 | |
printf "\n" | |
while true; do | |
read -p "Which do you want to download? " prod_file | |
if [ $(grep -c "\"name\": \"${prod_file}\"" ${wrkdir}/prod_fileid) -eq 1 ] | |
then | |
break | |
else | |
printf "Sorry can't find that file, please re-enter the product file from the list above\n" | |
fi | |
done | |
export download_url=$(tac ${wrkdir}/prod_fileid | sed -n "/\"name\": \"${prod_file}\"/,/\/download\"/p"|tail -1|cut -d'"' -f4) | |
[ ${debug} = 'on' ] && echo "download_url=\""${download_url}"\"" | |
export target_file=$(basename $(tac ${wrkdir}/prod_fileid | sed -n "/\"name\": \"${prod_file}\"/,/aws_object_key/p"|tail -1|cut -d'"' -f4)) | |
[ ${debug} = 'on' ] && echo "target_file=\""${target_file}"\"" | |
# Accept EULA | |
curl --silent -H "Accept: application/json" -H "Content-Type: application/json" -H "Content-Length: 0" -H "Authorization: Token ${api_token}" -X POST https://network.pivotal.io/api/v2/products/${prod_slug}/releases/${rel_id}/eula_acceptance | python -mjson.tool > ${wrkdir}/eula_acceptance | |
if [ $(grep -c "accepted_at" ${wrkdir}/eula_acceptance) -ne 1 ] | |
then | |
if [ $(grep -c "\"status\": 401" ${wrkdir}/eula_acceptance) -eq 1 ] | |
then | |
printf "EULA acceptance failed, user could not be authenticated. Exiting...\n" | |
elif [ $(grep -c "\"status\": 404" ${wrkdir}/eula_acceptance) -eq 1 ] | |
then | |
printf "EULA acceptance failed, product or release cannot be found. Exiting...\n" | |
else | |
printf "EULA acceptance failed, command reults were:\n" | |
cat ${wrkdir}/eula_acceptance | |
printf "Exiting...\n" | |
fi | |
clean_up | |
exit 1 | |
fi | |
# Show environment variables if debug mode is on | |
if [ ${debug} = 'on' ] | |
then | |
echo | |
echo "Variables to export to match the executed environment" | |
echo "export dldir=\""${dldir}"\"" | |
echo "export api_token=\""${api_token}"\"" | |
echo "export prod_name=\""${prod_name}"\"" | |
echo "export prod_slug=\""${prod_slug}"\"" | |
echo "export prod_version=\""${prod_version}"\"" | |
echo "export prod_id=\""${prod_id}"\"" | |
echo "export rel_id=\""${rel_id}"\"" | |
echo "export prod_file=\""${prod_file}"\"" | |
echo "export download_url=\""${download_url}"\"" | |
echo "export target_file=\""${target_file}"\"" | |
fi | |
# Download product file | |
wget --output-document="${dldir}/${target_file}" --post-data="" --header="Authorization: Token ${api_token}" ${download_url} | |
echo "Uploading ${dldir}/${target_file} to Ops Manager" | |
curl "https://localhost/api/products" -F "product[file]=@${dldir}/${target_file}" -X POST -u ${opsmgr_user}:${opsmgr_pass} -k -# -o /dev/null | |
# Clean up on exit | |
clean_up | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment