Add Support for uploading Vagrant images to Hashicorp Atlas.
Approved by: re (gjb)
This commit is contained in:
parent
160de052b2
commit
7761cfc358
@ -7,12 +7,11 @@
|
||||
|
||||
VAGRANT_IMG?= ${.OBJDIR}/vagrant.vmdk
|
||||
VAGRANT_UPLOAD_TGTS= vagrant-check-depends \
|
||||
vagrant-do-package-vmware
|
||||
# atlas-do-upload
|
||||
atlas-do-upload
|
||||
CLEANFILES+= ${VAGRANT_UPLOAD_TGTS}
|
||||
|
||||
.if defined(VAGRANT_UPLOAD_CONF) && !empty(VAGRANT_UPLOAD_CONF)
|
||||
. for VAR in _KEY _USERNAME _NAME _VERSION
|
||||
. for VAR in _KEY _USERNAME _VERSION
|
||||
VAGRANT${VAR}!= grep -E ^VAGRANT${VAR} ${VAGRANT_UPLOAD_CONF} | awk -F' ' '{print $$2}'
|
||||
ATLAS${VAR}:= ${VAGRANT${VAR}}
|
||||
. endfor
|
||||
@ -29,7 +28,7 @@ VAGRANT_PROVIDERS?= vmware_desktop
|
||||
vagrant-upload: ${VAGRANT_UPLOAD_TGTS}
|
||||
|
||||
vagrant-check-depends:
|
||||
.for VAR in _KEY _USERNAME _NAME _VERSION
|
||||
.for VAR in _KEY _USERNAME _VERSION
|
||||
. if !defined(VAGRANT${VAR}) || empty(VAGRANT${VAR})
|
||||
@echo "Variable VAGRANT${VAR} cannot be empty."
|
||||
@false
|
||||
@ -40,7 +39,7 @@ vagrant-check-depends:
|
||||
. if !exists(/usr/local/sbin/pkg-static)
|
||||
env ASSUME_ALWAYS_YES=yes pkg bootstrap -yf
|
||||
. endif
|
||||
env ASSUME_ALWAYS_YES=yes pkg install -y ftp/curl
|
||||
env ASSUME_ALWAYS_YES=yes pkg install -y curl
|
||||
. else
|
||||
make -C ${PORTSDIR}/ftp/curl BATCH=1 all install clean
|
||||
. endif
|
||||
@ -53,17 +52,12 @@ vagrant-do-package-vmware: vagrant-create-vmware-vmx vagrant-do-package
|
||||
cd ${.OBJDIR} && tar -czf ${VAGRANT_TARGET} metadata.json vagrant.vmx vagrant.vmdk
|
||||
touch ${.OBJDIR}/${.TARGET}
|
||||
|
||||
atlas-create-upload:
|
||||
atlas-do-upload: vagrant-do-package-vmware
|
||||
.for PROVIDER in ${VAGRANT_PROVIDERS}
|
||||
/usr/local/bin/curl "https://atlas.hashicorp.com/api/v1/box/${ATLAS_USERNAME}/${ATLAS_NAME}/version/${ATLAS_VERSION}/provider/${PROVIDER}/upload?access_token=${ATLAS_KEY}"
|
||||
${.CURDIR}/scripts/atlas-upload.sh -b FreeBSD-${REVISION}-${BRANCH} -f ${VAGRANT_TARGET} -p ${PROVIDER} -k ${VAGRANT_KEY} -u ${VAGRANT_USERNAME} -v 0.1
|
||||
.endfor
|
||||
touch ${.OBJDIR}/${.TARGET}
|
||||
|
||||
atlas-do-upload:
|
||||
/usr/local/bin/curl -X PUT --upload-file \
|
||||
${VAGRANT_IMG} ${ATLAS_UPLOAD_PATH}
|
||||
touch ${.OBJDIR}/${.TARGET}
|
||||
|
||||
vagrant-create-vmware-vmx:
|
||||
@cd ${.OBJDIR} && echo '.encoding = "UTF-8"' > vagrant.vmx
|
||||
@cd ${.OBJDIR} && echo 'bios.bootorder = "hdd,CDROM"' >> vagrant.vmx
|
||||
|
155
release/scripts/atlas-upload.sh
Executable file
155
release/scripts/atlas-upload.sh
Executable file
@ -0,0 +1,155 @@
|
||||
#!/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$
|
||||
#
|
||||
|
||||
ATLAS_API_URL=''
|
||||
ATLAS_UPLOAD_URL='https://binstore.hashicorp.com'
|
||||
VERSION_DESCRIPTION="FreeBSD Snapshot Build"
|
||||
|
||||
usage() {
|
||||
echo "${0} usage:"
|
||||
echo "-b box-name -f box-to-upload -k api-key -p provider -u user -v version"
|
||||
return 1
|
||||
}
|
||||
|
||||
main () {
|
||||
while getopts "b:f:k:p:u:v:" arg; do
|
||||
case "${arg}" in
|
||||
b)
|
||||
BOX="${OPTARG}"
|
||||
;;
|
||||
f)
|
||||
FILE="${OPTARG}"
|
||||
;;
|
||||
k)
|
||||
KEY="${OPTARG}"
|
||||
;;
|
||||
p)
|
||||
PROVIDER="${OPTARG}"
|
||||
;;
|
||||
u)
|
||||
USERNAME="${OPTARG}"
|
||||
;;
|
||||
v)
|
||||
VERSION="${OPTARG}"
|
||||
;;
|
||||
*)
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ -z "${BOX}" -o \
|
||||
-z "${FILE}" -o \
|
||||
-z "${KEY}" -o \
|
||||
-z "${PROVIDER}" -o \
|
||||
-z "${USERNAME}" -o \
|
||||
-z "${VERSION}" ];
|
||||
then
|
||||
usage || exit 0
|
||||
fi
|
||||
|
||||
# Check to see if the box exists or create it
|
||||
BOXRESULT=$(/usr/local/bin/curl -s "https://atlas.hashicorp.com/api/v1/box/${USERNAME}/${BOX}?access_token=${KEY}")
|
||||
if [ $? != 0 ]; then
|
||||
echo "Failed to connect to the API"
|
||||
exit 2;
|
||||
fi
|
||||
echo $BOXRESULT | grep "\"name\":\"${BOX}\"" > /dev/null
|
||||
if [ $? != 0 ]; then
|
||||
echo "Creating box: ${BOX}"
|
||||
/usr/local/bin/curl -s https://atlas.hashicorp.com/api/v1/boxes -X POST -d "box[name]=${BOX}" -d "access_token=${KEY}" > /dev/null
|
||||
/usr/local/bin/curl -s https://atlas.hashicorp.com/api/v1/box/${USERNAME}/${BOX} -X PUT -d "box[is_private]=false" -d "access_token=${KEY}" > /dev/null
|
||||
else
|
||||
echo "Box already exists"
|
||||
fi
|
||||
|
||||
# Check to see if the version exists or create it
|
||||
VERSIONRESULT=$(/usr/local/bin/curl -s "https://atlas.hashicorp.com/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}?access_token=${KEY}")
|
||||
if [ $? != 0 ]; then
|
||||
echo "Failed to connect to the API"
|
||||
exit 2;
|
||||
fi
|
||||
echo $VERSIONRESULT | grep "\"version\":\"${VERSION}\"" > /dev/null
|
||||
if [ $? != 0 ]; then
|
||||
echo "Creating version: ${VERSION}"
|
||||
/usr/local/bin/curl -s https://atlas.hashicorp.com/api/v1/box/${USERNAME}/${BOX}/versions -X POST -d "version[version]=${VERSION}" -d "access_token=${KEY}" > /dev/null
|
||||
/usr/local/bin/curl -s https://atlas.hashicorp.com/api/v1/box/${USERNAME}/${BOX}/version/${VERSION} -X PUT -d "version[description]=${VERSION_DESCRIPTION}" -d "access_token=${KEY}" > /dev/null
|
||||
VERSIONRESULT=$(/usr/local/bin/curl -s "https://atlas.hashicorp.com/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}?access_token=${KEY}")
|
||||
echo $VERSIONRESULT | grep "\"version\":\"${VERSION}\"" > /dev/null
|
||||
if [ $? != 0 ]; then
|
||||
echo "Failed to create version"
|
||||
exit 2
|
||||
fi
|
||||
else
|
||||
echo "Version already exists"
|
||||
fi
|
||||
|
||||
# Check to see if the provider exists or create it
|
||||
PROVIDERRESULT=$(/usr/local/bin/curl -s "https://atlas.hashicorp.com/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}/provider/${PROVIDER}?access_token=${KEY}")
|
||||
if [ $? != 0 ]; then
|
||||
echo "Failed to connect to the API"
|
||||
exit 2;
|
||||
fi
|
||||
echo $PROVIDERRESULT | grep "\"name\":\"${PROVIDER}\"" > /dev/null
|
||||
if [ $? != 0 ]; then
|
||||
echo "Creating provider: ${PROVIDER}"
|
||||
/usr/local/bin/curl -s https://atlas.hashicorp.com/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"
|
||||
fi
|
||||
|
||||
# Request an upload token
|
||||
TOKENRESULT=$(/usr/local/bin/curl -s "https://atlas.hashicorp.com/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}/provider/${PROVIDER}/upload?access_token=${KEY}")
|
||||
if [ $? != 0 ]; then
|
||||
echo "Failed to get the token from the API"
|
||||
exit 2;
|
||||
fi
|
||||
echo ${TOKENRESULT} | grep "\"token\":" > /dev/null
|
||||
if [ $? != 0 ]; then
|
||||
echo "No token found from the API"
|
||||
exit 2
|
||||
else
|
||||
TOKEN=$(echo $TOKENRESULT | sed -e 's/.*token":"//' -e 's/".*//')
|
||||
echo "Uploading to Atlas"
|
||||
UPLOADRESULT=$(/usr/local/bin/curl -s -X PUT --upload-file ${FILE} ${ATLAS_UPLOAD_URL}/${TOKEN})
|
||||
|
||||
# Validate the Upload
|
||||
echo "Validating"
|
||||
VALIDRESULT=$(/usr/local/bin/curl -s "https://atlas.hashicorp.com/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}/provider/${PROVIDER}?access_token=${KEY}")
|
||||
HOSTED_TOKEN=$(echo $VALIDRESULT | sed -e 's/.*hosted_token":"//' -e 's/".*//')
|
||||
if [ ! -z ${HOSTED_TOKEN} -a ! -z ${TOKEN} -a ${HOSTED_TOKEN} != ${TOKEN} ]; then
|
||||
echo "Upload failed, try again."
|
||||
exit 2
|
||||
fi
|
||||
|
||||
# Release the version
|
||||
echo "Releasing ${VERSION} of ${BOX} in Atlas"
|
||||
/usr/local/bin/curl -s https://atlas.hashicorp.com/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}/release -X PUT -d "access_token=${KEY}" > /dev/null
|
||||
fi
|
||||
}
|
||||
|
||||
main "$@"
|
Loading…
x
Reference in New Issue
Block a user