From 155cf68e991a54c4f97f71f9fcf9d74407d681d4 Mon Sep 17 00:00:00 2001 From: Ladar Levison Date: Sat, 4 Nov 2023 23:22:57 -0500 Subject: [PATCH] Updated delete script to use v2 and allow specific provider/arch deletion. --- res/scripts/delete.sh | 63 ++++++++++++++++++++----------------------- res/scripts/direct.sh | 62 +++++++++++++++++++++--------------------- 2 files changed, 60 insertions(+), 65 deletions(-) diff --git a/res/scripts/delete.sh b/res/scripts/delete.sh index f552c1a68..670ef7529 100755 --- a/res/scripts/delete.sh +++ b/res/scripts/delete.sh @@ -1,21 +1,34 @@ #!/bin/bash # Handle self referencing, sourcing etc. -if [[ $0 != $BASH_SOURCE ]]; then - export CMD=$BASH_SOURCE +if [[ $0 != "${BASH_SOURCE[0]}" ]]; then + export CMD="${BASH_SOURCE[0]}" else export CMD=$0 fi # Ensure a consistent working directory so relative paths work. -pushd `dirname $CMD` > /dev/null -BASE=`pwd -P` +pushd "$(dirname "$CMD")" > /dev/null +BASE=$(pwd -P) popd > /dev/null +if [ $# != 3 ] && [ $# != 5 ]; then + printf " Delete an entire box version.\n Usage: $0 ORG BOX VERSION\n\n" + printf " Delete a specific box provider.\n Usage: $0 ORG BOX PROVIDER ARCH VERSION\n\n" + exit 1 +fi + +if [ $# == 3 ]; then ORG="$1" -NAME="$2" +BOX="$2" +VERSION="$3" +elif [ $# == 5 ]; then +ORG="$1" +BOX="$2" PROVIDER="$3" -VERSION="$4" +ARCH="$4" +VERSION="$5" +fi if [ -f /opt/vagrant/embedded/bin/curl ]; then export CURL="/opt/vagrant/embedded/bin/curl" @@ -56,34 +69,16 @@ if [ -z ${VAGRANT_CLOUD_TOKEN} ]; then exit 2 fi -printf "\n\n" +printf "\n" -# Assume the position, while you create the version. -#${CURL} \ -# --tlsv1.2 \ -# --silent \ -# --retry 16 \ -# --retry-delay 60 \ -# --header "Content-Type: application/json" \ -# --header "Authorization: Bearer $VAGRANT_CLOUD_TOKEN" \ -# "https://app.vagrantup.com/api/v1/box/$ORG/$NAME/versions" \ -# --data " -# { -# \"version\": { -# \"version\": \"$VERSION\", -# \"description\": \"A build environment for use in cross platform development.\" -# } -# } -# " -#printf "\n\n" - -# Delete the existing provider, if it exists already. -${CURL} \ - --silent \ - --retry 16 \ - --retry-delay 60 \ +if [ $# == 5 ]; then + ${CURL} --tlsv1.2 --silent --retry 4 --retry-delay 2 --max-time 180 --request DELETE --fail \ + --header "Authorization: Bearer $VAGRANT_CLOUD_TOKEN" \ + "https://app.vagrantup.com/api/v2/box/$ORG/$BOX/version/$VERSION/provider/${PROVIDER}/${ARCH}" +else + ${CURL} --tlsv1.2 --silent --retry 4 --retry-delay 2 --max-time 180 --request DELETE --fail \ --header "Authorization: Bearer $VAGRANT_CLOUD_TOKEN" \ - --request DELETE \ - https://app.vagrantup.com/api/v1/box/$ORG/$NAME/version/$VERSION + "https://app.vagrantup.com/api/v2/box/$ORG/$BOX/version/$VERSION" +fi -printf "\n\n" +printf "\n" diff --git a/res/scripts/direct.sh b/res/scripts/direct.sh index bbd2844f6..ea1ab1d0e 100755 --- a/res/scripts/direct.sh +++ b/res/scripts/direct.sh @@ -168,6 +168,7 @@ fi FILENAME="$(basename "$1")" FILEPATH="$(realpath "$1")" +VAGRANTPATH="app.vagrantup.com" ORG="$(echo "$FILENAME" | sed "s/\([a-z]*\)[\-]*\([a-z0-9-]*\)-\(hyperv\|vmware\|libvirt\|docker\|parallels\|virtualbox\)-\([a-z0-9-]*\)-\([0-9\.]*\).box/\1/g")" BOX="$(echo "$FILENAME" | sed "s/\([a-z]*\)[-]*\([a-z0-9-]*\)-\(hyperv\|vmware\|libvirt\|docker\|parallels\|virtualbox\)-\([a-z0-9-]*\)-\([0-9\.]*\).box/\2/g")" @@ -332,6 +333,7 @@ function upload_box() { UPLOAD_FILE_WRITEOUT="\nFILE: $FILENAME\nREPO: $ORG/$BOX\nCODE: %{http_code}\nIP: %{remote_ip}\nBYTES: %{size_upload}\nRATE: %{speed_upload}\nTOTAL TIME: %{time_total}\n%{onerror}ERROR: %{errormsg}\n" UPLOAD_CALLBACK_WRITEOUT="%{onerror}FILE: $FILENAME\nREPO: $ORG/$BOX\nCODE: %{http_code}\nIP: %{remote_ip}\nBYTES: %{size_upload}\nRATE: %{speed_upload}\nTOTAL TIME: %{time_total}\nERROR: %{errormsg}\n" + # Detect older versions of cURL and avoid using the unsupported write out macros. if [ "$(${CURL} -so /dev/null --write-out "%{onerror}" https://lavabit.com 2>&1)" ] || \ [ "$(${CURL} -so /dev/null --write-out "%{errormsg}" https://lavabit.com 2>&1)" ]; then UPLOAD_FILE_WRITEOUT="\nFILE: $FILENAME\nREPO: $ORG/$BOX\nCODE: %{http_code}\nIP: %{remote_ip}\nBYTES: %{size_upload}\nRATE: %{speed_upload}\nTOTAL TIME: %{time_total}\n" @@ -339,18 +341,17 @@ function upload_box() { fi # Checks whether the version exists already, and creates it if necessary. - [ "`${CURL} --tlsv1.2 --silent --retry 4 --retry-delay 2 --max-time 180 --request GET --fail \ - --output /dev/null --header "Authorization: Bearer $VAGRANT_CLOUD_TOKEN" --write-out "%{http_code}" \ - "https://app.vagrantup.com/api/v2/box/$ORG/$BOX/version/$VERSION"`" != "200" ] || \ + [ "`${CURL} --tlsv1.2 --silent --retry 4 --retry-delay 2 --max-time 180 --request HEAD --fail \ + --output /dev/null --write-out "%{http_code}" \ + "https://${VAGRANTPATH}/api/v2/box/$ORG/$BOX/version/$VERSION"`" != "200" ] || \ [ "`${CURL} --tlsv1.2 --silent --retry 4 --retry-delay 2 --max-time 180 --request GET \ - --header "Authorization: Bearer $VAGRANT_CLOUD_TOKEN" \ - "https://app.vagrantup.com/api/v2/box/$ORG/$BOX/version/$VERSION" | \ + "https://${VAGRANTPATH}/api/v2/box/$ORG/$BOX/version/$VERSION" | \ jq -e -r ' (.version)? // (.success)? '`" != "$VERSION" ] && \ { ${CURL} --tlsv1.2 --silent --retry 4 --retry-delay 2 --max-time 180 --request POST --fail \ --output /dev/null --header "Authorization: Bearer $VAGRANT_CLOUD_TOKEN" \ --header "Content-Type: application/json" \ --data "{\"version\":{\"version\":\"4.3.3\",\"description\":\"A build environment for use in cross platform development.\"}}" \ - "https://app.vagrantup.com/api/v2/box/$ORG/$BOX/versions" && sleep 4 || \ + "https://${VAGRANTPATH}/api/v2/box/$ORG/$BOX/versions" && sleep 4 || \ { printf "${T_BYEL} Version creation failed. [ $ORG $BOX $PROVIDER $ARCH $VERSION / RECURSION = $RECURSION ]${T_RESET}\n" >&2 sleep 20 ; exec "$0" "$FILEPATH" $RECURSION @@ -359,16 +360,15 @@ function upload_box() { } # This checks whether provider/arch exists for this box, and if so, deletes it. - [ "`${CURL} --tlsv1.2 --silent --retry 4 --retry-delay 2 --max-time 180 --request GET --fail \ - --output /dev/null --header "Authorization: Bearer $VAGRANT_CLOUD_TOKEN" --write-out "%{http_code}" \ - "https://app.vagrantup.com/api/v2/box/$ORG/$BOX/version/$VERSION/provider/$PROVIDER/$ARCH"`" == "200" ] || \ + [ "`${CURL} --tlsv1.2 --silent --retry 4 --retry-delay 2 --max-time 180 --request HEAD --fail \ + --output /dev/null --write-out "%{http_code}" \ + "https://${VAGRANTPATH}/api/v2/box/$ORG/$BOX/version/$VERSION/provider/$PROVIDER/$ARCH"`" == "200" ] || \ [ "`${CURL} --tlsv1.2 --silent --retry 4 --retry-delay 2 --max-time 180 --request GET \ - --header "Authorization: Bearer $VAGRANT_CLOUD_TOKEN" \ - "https://app.vagrantup.com/api/v2/box/$ORG/$BOX/version/$VERSION/provider/$PROVIDER/$ARCH" | \ + "https://${VAGRANTPATH}/api/v2/box/$ORG/$BOX/version/$VERSION/provider/$PROVIDER/$ARCH" | \ jq -e -r ' (.name)? // (.success)? '`" != "false" ] && \ { retry ${CURL} --tlsv1.2 --silent --retry 4 --retry-delay 2 --max-time 180 --request DELETE --fail \ --output /dev/null --header "Authorization: Bearer $VAGRANT_CLOUD_TOKEN" \ - "https://app.vagrantup.com/api/v2/box/$ORG/$BOX/version/$VERSION/provider/${PROVIDER}/${ARCH}" && sleep 4 || \ + "https://${VAGRANTPATH}/api/v2/box/$ORG/$BOX/version/$VERSION/provider/${PROVIDER}/${ARCH}" && sleep 4 || \ { printf "${T_BYEL} Provider delete failed. [ $ORG $BOX $PROVIDER $ARCH $VERSION / RECURSION = $RECURSION ]${T_RESET}\n" >&2 sleep 20 ; exec "$0" "$FILEPATH" $RECURSION @@ -381,7 +381,7 @@ function upload_box() { --output /dev/null --header "Authorization: Bearer $VAGRANT_CLOUD_TOKEN" \ --header "Content-Type: application/json" \ --data "{\"provider\":{ \"name\":\"$PROVIDER\",\"checksum\":\"$HASH\",\"architecture\":\"$ARCH\",\"default_architecture\":\"$DEFAULT_ARCH\",\"checksum_type\":\"SHA256\"}}" \ - "https://app.vagrantup.com/api/v2/box/$ORG/$BOX/version/$VERSION/providers" && sleep 4 || \ + "https://${VAGRANTPATH}/api/v2/box/$ORG/$BOX/version/$VERSION/providers" && sleep 4 || \ { printf "${T_BYEL} Provider creation failed. [ $ORG $BOX $PROVIDER $ARCH $VERSION / RECURSION = $RECURSION ]${T_RESET}\n" >&2 sleep 20 ; exec "$0" "$FILEPATH" $RECURSION @@ -391,7 +391,7 @@ function upload_box() { # Request a direct upload URL. UPLOAD_RESPONSE=$( ${CURL} --tlsv1.2 --silent --retry 4 --retry-delay 2 --max-time 180 -request GET --fail \ --header "Authorization: Bearer $VAGRANT_CLOUD_TOKEN" \ - "https://app.vagrantup.com/api/v2/box/$ORG/$BOX/version/$VERSION/provider/$PROVIDER/$ARCH/upload/direct" ) + "https://${VAGRANTPATH}/api/v2/box/$ORG/$BOX/version/$VERSION/provider/$PROVIDER/$ARCH/upload/direct" ) UPLOAD_PATH="$(echo "$UPLOAD_RESPONSE" | jq -r .upload_path)" UPLOAD_CALLBACK="$(echo "$UPLOAD_RESPONSE" | jq -r .callback)" @@ -402,7 +402,7 @@ function upload_box() { exit $? fi - # If we move to quickly, the cloud will sometimes return an error. Waiting seems to reduce the error/failure rate. + # If we move too quickly, the cloud will sometimes return an error. Waiting seems to reduce the error/failure rate. sleep 4 retry ${CURL} --tlsv1.2 --silent --retry 4 --retry-delay 2 --max-time 7200 --request PUT --fail \ @@ -444,29 +444,29 @@ function upload_box() { upload_box if [ "$ORG" == "generic" ] && [ "$ARCH" == "amd64" ]; then - ORG="generic-x64" + sleep 8 ; VAGRANTPATH="vagrantcloud.com" ; ORG="generic-x64" upload_box elif [ "$ORG" == "generic" ] && [ "$ARCH" == "i386" ]; then - ORG="generic-x32" - upload_box -elif [ "$ORG" == "generic" ] && [ "$ARCH" == "arm64" ]; then - ORG="generic-a64" - upload_box -elif [ "$ORG" == "generic" ] && [ "$ARCH" == "arm" ]; then - ORG="generic-a32" + sleep 8 ; VAGRANTPATH="vagrantcloud.com" ; ORG="generic-x32" upload_box +# elif [ "$ORG" == "generic" ] && [ "$ARCH" == "arm64" ]; then +# sleep 8 ; VAGRANTPATH="vagrantcloud.com" ; ORG="generic-a64" +# upload_box +# elif [ "$ORG" == "generic" ] && [ "$ARCH" == "arm" ]; then +# sleep 8 ; VAGRANTPATH="vagrantcloud.com" ; ORG="generic-a32" +# upload_box elif [ "$ORG" == "roboxes" ] && [ "$ARCH" == "amd64" ]; then - ORG="roboxes-x64" + sleep 8 ; VAGRANTPATH="vagrantcloud.com" ; ORG="roboxes-x64" upload_box elif [ "$ORG" == "roboxes" ] && [ "$ARCH" == "i386" ]; then - ORG="roboxes-x32" - upload_box -elif [ "$ORG" == "roboxes" ] && [ "$ARCH" == "arm64" ]; then - ORG="roboxes-a64" - upload_box -elif [ "$ORG" == "roboxes" ] && [ "$ARCH" == "arm" ]; then - ORG="roboxes-a32" + sleep 8 ; VAGRANTPATH="vagrantcloud.com" ; ORG="roboxes-x32" upload_box +# elif [ "$ORG" == "roboxes" ] && [ "$ARCH" == "arm64" ]; then +# sleep 8 ; VAGRANTPATH="vagrantcloud.com" ; ORG="roboxes-a64" +# upload_box +# elif [ "$ORG" == "roboxes" ] && [ "$ARCH" == "arm" ]; then +# sleep 8 ; VAGRANTPATH="vagrantcloud.com" ; ORG="roboxes-a32" +# upload_box fi