xref: /freebsd/release/scripts/atlas-upload.sh (revision d93a896ef95946b0bf1219866fcb324b78543444)
1#!/bin/sh
2#-
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions
5# are met:
6# 1. Redistributions of source code must retain the above copyright
7#    notice, this list of conditions and the following disclaimer.
8# 2. Redistributions in binary form must reproduce the above copyright
9#    notice, this list of conditions and the following disclaimer in the
10#    documentation and/or other materials provided with the distribution.
11#
12# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
16# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22# SUCH DAMAGE.
23#
24# Upload a Vagrant image to Hashicorp's Atlas service
25#
26# $FreeBSD$
27#
28
29ATLAS_API_URL=''
30ATLAS_UPLOAD_URL='https://app.vagrantup.com'
31DESCRIPTION="FreeBSD Snapshot Build"
32
33usage() {
34	echo "${0} usage:"
35	echo "-b box-name -d 'box description' -f box-to-upload -k api-key -p provider -u user -v version"
36	return 1
37}
38
39main () {
40	while getopts "b:d:f:k:p:u:v:" arg; do
41		case "${arg}" in
42			b)
43				BOX="${OPTARG}"
44				;;
45			d)
46				DESCRIPTION="${OPTARG}"
47				;;
48			f)
49				FILE="${OPTARG}"
50				;;
51			k)
52				KEY="${OPTARG}"
53				;;
54			p)
55				PROVIDER="${OPTARG}"
56				;;
57			u)
58				USERNAME="${OPTARG}"
59				;;
60			v)
61				VERSION="${OPTARG}"
62				;;
63			*)
64				;;
65		esac
66	done
67
68	if [ -z "${BOX}" -o \
69		-z "${FILE}" -o \
70		-z "${KEY}" -o \
71		-z "${PROVIDER}" -o \
72		-z "${USERNAME}" -o \
73		-z "${VERSION}" ];
74	then
75		usage || exit 0
76	fi
77
78	# Check to see if the box exists or create it
79	BOXRESULT=$(/usr/local/bin/curl -s "${ATLAS_UPLOAD_URL}/api/v1/box/${USERNAME}/${BOX}?access_token=${KEY}")
80	if [ $? != 0 ]; then
81		echo "Failed to connect to the API"
82		exit 2;
83	fi
84	echo $BOXRESULT | grep "\"name\":\"${BOX}\"" > /dev/null
85	if [ $? != 0 ]; then
86		echo "Creating box: ${BOX}"
87		/usr/local/bin/curl -s ${ATLAS_UPLOAD_URL}/api/v1/boxes -X POST -d "box[name]=${BOX}" -d "access_token=${KEY}" > /dev/null
88		/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
89		/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
90	else
91		echo "Box already exists"
92	fi
93
94	# Check to see if the version exists or create it
95	VERSIONRESULT=$(/usr/local/bin/curl -s "${ATLAS_UPLOAD_URL}/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}?access_token=${KEY}")
96	if [ $? != 0 ]; then
97		echo "Failed to connect to the API"
98		exit 2;
99	fi
100	echo $VERSIONRESULT | grep "version/${VERSION}" > /dev/null
101	if [ $? != 0 ]; then
102		echo "Creating version: ${VERSION}"
103		/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
104		/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
105		VERSIONRESULT=$(/usr/local/bin/curl -s "${ATLAS_UPLOAD_URL}/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}?access_token=${KEY}")
106		echo $VERSIONRESULT | grep "version/${VERSION}" > /dev/null
107		if [ $? != 0 ]; then
108			echo "Failed to create version"
109			exit 2
110		fi
111	else
112		echo "Version already exists"
113	fi
114
115	# Check to see if the provider exists or create it
116	PROVIDERRESULT=$(/usr/local/bin/curl -s "${ATLAS_UPLOAD_URL}/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}/provider/${PROVIDER}?access_token=${KEY}")
117	if [ $? != 0 ]; then
118		echo "Failed to connect to the API"
119		exit 2;
120	fi
121	echo $PROVIDERRESULT | grep "provider/${PROVIDER}" > /dev/null
122	if [ $? != 0 ]; then
123		echo "Creating provider: ${PROVIDER}"
124		/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
125	else
126		echo "Provider already exists"
127	fi
128
129	# Request an upload token
130	TOKENRESULT=$(/usr/local/bin/curl -s "${ATLAS_UPLOAD_URL}/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}/provider/${PROVIDER}/upload?access_token=${KEY}")
131	if [ $? != 0 ]; then
132		echo "Failed to get the token from the API"
133		exit 2;
134	fi
135	echo ${TOKENRESULT} | grep -E "upload_path" > /dev/null
136	if [ $? != 0 ]; then
137		echo "No token found from the API"
138		exit 2
139	else
140		TOKEN=$(echo $TOKENRESULT | sed -e 's/.*token":"//' -e 's/.*upload_path":"//' -e 's/}$//g' -e 's/"//g')
141		echo "Uploading to Atlas"
142		UPLOADRESULT=$(/usr/local/bin/curl -s -X PUT --upload-file ${FILE} "${TOKEN}")
143
144		# Validate the Upload
145		echo "Validating"
146		VALIDRESULT=$(/usr/local/bin/curl -s "${ATLAS_UPLOAD_URL}/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}/provider/${PROVIDER}?access_token=${KEY}")
147		HOSTED_TOKEN=$(echo $VALIDRESULT | sed -e 's/.*"hosted"://' -e 's/,.*$//')
148		if [ ! -z ${TOKEN} -a "${HOSTED_TOKEN}" != "true" ]; then
149			echo "Upload failed, try again."
150			exit 2
151		fi
152
153		# Release the version
154		echo "Releasing ${VERSION} of ${BOX} in Atlas"
155		/usr/local/bin/curl -s ${ATLAS_UPLOAD_URL}/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}/release -X PUT -d "access_token=${KEY}" > /dev/null
156	fi
157}
158
159main "$@"
160