#!/bin/sh #- # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # Upload a Vagrant image to Hashicorp's Atlas service # # $FreeBSD: stable/11/release/scripts/atlas-upload.sh 320697 2017-07-06 00:43:43Z gjb $ # setglobal ATLAS_API_URL = '''' setglobal ATLAS_UPLOAD_URL = ''https://app.vagrantup.com'' setglobal DESCRIPTION = '"FreeBSD Snapshot Build'" proc usage { echo "$(0) usage:" echo "-b box-name -d 'box description' -f box-to-upload -k api-key -p provider -u user -v version" return 1 } proc main { while getopts "b:d:f:k:p:u:v:" arg { match $(arg) { with b setglobal BOX = $(OPTARG) with d setglobal DESCRIPTION = $(OPTARG) with f setglobal FILE = $(OPTARG) with k setglobal KEY = $(OPTARG) with p setglobal PROVIDER = $(OPTARG) with u setglobal USERNAME = $(OPTARG) with v setglobal VERSION = $(OPTARG) with * } } if test -z $(BOX) -o \ -z $(FILE) -o \ -z $(KEY) -o \ -z $(PROVIDER) -o \ -z $(USERNAME) -o \ -z $(VERSION) { usage || exit 0 } # Check to see if the box exists or create it setglobal BOXRESULT = $[/usr/local/bin/curl -s "$(ATLAS_UPLOAD_URL)/api/v1/box/$(USERNAME)/$(BOX)?access_token=$(KEY)] if test $Status != 0 { echo "Failed to connect to the API" exit 2; } echo $BOXRESULT | grep "\"name\":\"$(BOX)\"" > /dev/null if test $Status != 0 { echo "Creating box: $(BOX)" /usr/local/bin/curl -s $(ATLAS_UPLOAD_URL)/api/v1/boxes -X POST -d "box[name]=$(BOX)" -d "access_token=$(KEY)" > /dev/null /usr/local/bin/curl -s $(ATLAS_UPLOAD_URL)/api/v1/box/$(USERNAME)/$(BOX) -X PUT -d "box[is_private]=false" -d "access_token=$(KEY)" > /dev/null /usr/local/bin/curl -s $(ATLAS_UPLOAD_URL)/api/v1/box/$(USERNAME)/$(BOX) -X PUT -d "box[description]='$(DESCRIPTION)'" -d "access_token=$(KEY)" > /dev/null } else { echo "Box already exists" } # Check to see if the version exists or create it setglobal VERSIONRESULT = $[/usr/local/bin/curl -s "$(ATLAS_UPLOAD_URL)/api/v1/box/$(USERNAME)/$(BOX)/version/$(VERSION)?access_token=$(KEY)] if test $Status != 0 { echo "Failed to connect to the API" exit 2; } echo $VERSIONRESULT | grep "version/$(VERSION)" > /dev/null if test $Status != 0 { echo "Creating version: $(VERSION)" /usr/local/bin/curl -s $(ATLAS_UPLOAD_URL)/api/v1/box/$(USERNAME)/$(BOX)/versions -X POST -d "version[version]=$(VERSION)" -d "access_token=$(KEY)" > /dev/null /usr/local/bin/curl -s $(ATLAS_UPLOAD_URL)/api/v1/box/$(USERNAME)/$(BOX)/version/$(VERSION) -X PUT -d "version[description]=$(DESCRIPTION)" -d "access_token=$(KEY)" > /dev/null setglobal VERSIONRESULT = $[/usr/local/bin/curl -s "$(ATLAS_UPLOAD_URL)/api/v1/box/$(USERNAME)/$(BOX)/version/$(VERSION)?access_token=$(KEY)] echo $VERSIONRESULT | grep "version/$(VERSION)" > /dev/null if test $Status != 0 { echo "Failed to create version" exit 2 } } else { echo "Version already exists" } # Check to see if the provider exists or create it setglobal PROVIDERRESULT = $[/usr/local/bin/curl -s "$(ATLAS_UPLOAD_URL)/api/v1/box/$(USERNAME)/$(BOX)/version/$(VERSION)/provider/$(PROVIDER)?access_token=$(KEY)] if test $Status != 0 { echo "Failed to connect to the API" exit 2; } echo $PROVIDERRESULT | grep "provider/$(PROVIDER)" > /dev/null if test $Status != 0 { echo "Creating provider: $(PROVIDER)" /usr/local/bin/curl -s $(ATLAS_UPLOAD_URL)/api/v1/box/$(USERNAME)/$(BOX)/version/$(VERSION)/providers -X POST -d "provider[name]=$(PROVIDER)" -d "access_token=$(KEY)" > /dev/null } else { echo "Provider already exists" } # Request an upload token setglobal TOKENRESULT = $[/usr/local/bin/curl -s "$(ATLAS_UPLOAD_URL)/api/v1/box/$(USERNAME)/$(BOX)/version/$(VERSION)/provider/$(PROVIDER)/upload?access_token=$(KEY)] if test $Status != 0 { echo "Failed to get the token from the API" exit 2; } echo $(TOKENRESULT) | grep -E "upload_path" > /dev/null if test $Status != 0 { echo "No token found from the API" exit 2 } else { setglobal TOKEN = $[echo $TOKENRESULT | sed -e 's/.*token":"//' -e 's/.*upload_path":"//' -e 's/}$//g' -e 's/"//g] echo "Uploading to Atlas" setglobal UPLOADRESULT = $[/usr/local/bin/curl -s -X PUT --upload-file $(FILE) $(TOKEN)] # Validate the Upload echo "Validating" setglobal VALIDRESULT = $[/usr/local/bin/curl -s "$(ATLAS_UPLOAD_URL)/api/v1/box/$(USERNAME)/$(BOX)/version/$(VERSION)/provider/$(PROVIDER)?access_token=$(KEY)] setglobal HOSTED_TOKEN = $[echo $VALIDRESULT | sed -e 's/.*"hosted"://' -e 's/,.*$//] if test ! -z $(TOKEN) -a $(HOSTED_TOKEN) != "true" { echo "Upload failed, try again." exit 2 } # Release the version echo "Releasing $(VERSION) of $(BOX) in Atlas" /usr/local/bin/curl -s $(ATLAS_UPLOAD_URL)/api/v1/box/$(USERNAME)/$(BOX)/version/$(VERSION)/release -X PUT -d "access_token=$(KEY)" > /dev/null } } main @Argv