Skip to content

Instantly share code, notes, and snippets.

@mnemesh-pivotal
Last active June 22, 2016 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnemesh-pivotal/940d4358e421fd19caad42d03a1af7c3 to your computer and use it in GitHub Desktop.
Save mnemesh-pivotal/940d4358e421fd19caad42d03a1af7c3 to your computer and use it in GitHub Desktop.
New version which uses jq to work with the json data instead of grep/awk/sed/etc
#!/bin/bash
[ "$1" = "-d" ] && debug=on || debug=off
for i in curl wget jq; do
if [ $(which ${i} > /dev/null 2>&1) ]
then
printf "\n%s requires the ${i} command line utility. Exiting...\n" $(basename $0)
exit 0
fi
done
# Make working directory
export wrkdir=/tmp/wrkdir.$$
mkdir -p ${wrkdir}
# You can hardcode variables for convenience
# export api_token="aAaAaAaAaAaAaAaAaAaA"
# export download_dir=/downloads
# Clean up function
clean_up()
{
trap ":" 1 2 3 15
if [ "${SIGSET}" ]
then
printf "\n%s aborted...\n" $(basename $0)
[ -f "${download_dir}/${target_file}" ] && rm -f ${download_dir}/${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
while [ -z ${download_dir} ]; do
read -p "Enter the download directory : " download_dir
if [ ! -d "${download_dir}" ]
then
printf "\n%s does not exist...\n" ${download_dir}
unset download_dir
elif [ ! -w "${download_dir}" ]
then
printf "\n%s is not writable...\n" ${download_dir}
unset download_dir
fi
done
while true; do
[ ${debug} = 'on' ] && echo "download_dir=\""${download_dir}"\""
# Get api_token or hard code and remove the next hash mark
[ -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 -o ${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
# Get products information json file
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 -o ${wrkdir}/products.json
# Create file of product names
jq -r '.products[] | .name' ${wrkdir}/products.json | sort > ${wrkdir}/product_name_list
# Get product name to download
printf "\nThese are the available products:\n\n"
column ${wrkdir}/product_name_list
printf "\n"
while true; do
read -p "Which product do you want to download? " product_name
if [ $(grep -c "^${product_name}$" ${wrkdir}/product_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 product_slug=$(jq -r ".products[] | select(.name == \"${product_name}\") | .slug" ${wrkdir}/products.json)
[ ${debug} = 'on' ] && echo "product_slug=\""${product_slug}"\""
# Get product ID
export product_id=$(jq -r ".products[] | select(.name == \"${product_name}\") | .id" ${wrkdir}/products.json)
[ ${debug} = 'on' ] && echo "product_id=\""${product_id}"\""
# Get product releases json file
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/${product_slug}/releases -o ${wrkdir}/releases.json
# Create release version file
jq -r '.releases[] | .version' ${wrkdir}/releases.json | sort -r > ${wrkdir}/product_versions
# Get version
printf "\nThese are the available versions:\n\n"
column ${wrkdir}/product_versions
printf "\n"
while true; do
read -p "Which version do you want to download? " product_version
if [ $(jq -r ".releases[] | select(.version == \"${product_version}\" ) | {id} | length" ${wrkdir}/releases.json) -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 "product_version=\""${product_version}"\""
# Get release ID
export rel_id=$(jq -r ".releases[] | select(.version == \"${product_version}\") | .id" ${wrkdir}/releases.json)
[ ${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/${product_slug}/releases/${rel_id} > ${wrkdir}/product_fileid.json
# Get file to download
jqstr1=".product_files[]"
jqstr2=".file_groups[].product_files[]"
if [ $(jq -r "${jqstr1}" ${wrkdir}/product_fileid.json | wc -l) -ne 0 ]; then
jqstr=${jqstr1}
else
jqstr=${jqstr2}
fi
printf "\nThese are the available files:\n\n"
jq -r "${jqstr}.name" ${wrkdir}/product_fileid.json
printf "\n"
while true; do
read -p "Which do you want to download? " product_file
if [ $(jq -r "${jqstr} | select(.name == \"${product_file}\") | {name} | length" ${wrkdir}/product_fileid.json) -eq 1 ]; then
echo -e "INFO - Setting up request to download the file: " $product_file
export download_url=$(jq -r "${jqstr} | select(.name == \"${product_file}\") | ._links.download.href " ${wrkdir}/product_fileid.json)
[ ${debug} = 'on' ] && echo "download_url=\""${download_url}"\""
export target_file=$(basename $(jq -r "${jqstr} | select(.name == \"${product_file}\") | .aws_object_key" ${wrkdir}/product_fileid.json))
[ ${debug} = 'on' ] && echo "target_file=\""${target_file}"\""
break
else
printf "Sorry can't find that file, please re-enter the product file from the list above\n"
fi
done
# 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/${product_slug}/releases/${rel_id}/eula_acceptance -o ${wrkdir}/eula_acceptance.json
if [ -z "$(jq '.accepted_at' ${wrkdir}/eula_acceptance.json)" ]
then
printf "EULA acceptance failed with message: $(jq '.message' ${wrkdir}/eula_acceptance.json)\n"
printf "Exiting...\n"
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 download_dir=\""${download_dir}"\""
echo "export api_token=\""${api_token}"\""
echo "export product_name=\""${product_name}"\""
echo "export product_slug=\""${product_slug}"\""
echo "export product_version=\""${product_version}"\""
echo "export product_id=\""${product_id}"\""
echo "export rel_id=\""${rel_id}"\""
echo "export product_file=\""${product_file}"\""
echo "export download_url=\""${download_url}"\""
echo "export target_file=\""${target_file}"\""
fi
# Download product file
wget --output-document="${download_dir}/${target_file}" --post-data="" --header="Authorization: Token ${api_token}" ${download_url}
unset ans
while [ -z "${ans}" ]; do
read -p "Would you like to download another product? (Y/N) " ans
yorn=$(echo ${ans} | tr [:upper:] [:lower:])
case ${yorn} in
y|n)
break
;;
*)
unset ans
;;
esac
done
if [ ${yorn} = "n" ]; then break; fi
done
# 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