Skip to content

Commit

Permalink
Updated delete script to use v2 and allow specific provider/arch dele…
Browse files Browse the repository at this point in the history
…tion.
  • Loading branch information
ladar committed Nov 5, 2023
1 parent 15521ef commit 155cf68
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 65 deletions.
63 changes: 29 additions & 34 deletions res/scripts/delete.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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"
62 changes: 31 additions & 31 deletions res/scripts/direct.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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")"
Expand Down Expand Up @@ -332,25 +333,25 @@ 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"
UPLOAD_CALLBACK_WRITEOUT=""
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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)"
Expand All @@ -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 \
Expand Down Expand Up @@ -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


0 comments on commit 155cf68

Please sign in to comment.