xref: /freebsd/release/scripts/atlas-upload.sh (revision a8089ea5aee578e08acab2438e82fc9a9ae50ed8)
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#
27
28ATLAS_API_URL=''
29ATLAS_UPLOAD_URL='https://app.vagrantup.com'
30DESCRIPTION="FreeBSD Snapshot Build"
31
32usage() {
33	echo "${0} usage:"
34	echo "-b box-name -d 'box description' -f box-to-upload -k api-key -p provider -u user -v version"
35	return 1
36}
37
38main () {
39	while getopts "b:d:f:k:p:u:v:" arg; do
40		case "${arg}" in
41			b)
42				BOX="${OPTARG}"
43				;;
44			d)
45				DESCRIPTION="${OPTARG}"
46				;;
47			f)
48				FILE="${OPTARG}"
49				;;
50			k)
51				KEY="${OPTARG}"
52				;;
53			p)
54				PROVIDER="${OPTARG}"
55				;;
56			u)
57				USERNAME="${OPTARG}"
58				;;
59			v)
60				VERSION="${OPTARG}"
61				;;
62			*)
63				;;
64		esac
65	done
66
67	if [ -z "${BOX}" -o \
68		-z "${FILE}" -o \
69		-z "${KEY}" -o \
70		-z "${PROVIDER}" -o \
71		-z "${USERNAME}" -o \
72		-z "${VERSION}" ];
73	then
74		usage || exit 0
75	fi
76
77	# Check to see if the box exists or create it
78	BOXRESULT=$(/usr/local/bin/curl -s "${ATLAS_UPLOAD_URL}/api/v1/box/${USERNAME}/${BOX}?access_token=${KEY}")
79	if [ $? != 0 ]; then
80		echo "Failed to connect to the API"
81		exit 2;
82	fi
83	echo $BOXRESULT | grep "\"name\":\"${BOX}\"" > /dev/null
84	if [ $? != 0 ]; then
85		echo "Creating box: ${BOX}"
86		/usr/local/bin/curl -s ${ATLAS_UPLOAD_URL}/api/v1/boxes -X POST -d "box[name]=${BOX}" -d "access_token=${KEY}" > /dev/null
87		/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
88		/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
89	else
90		echo "Box already exists"
91	fi
92
93	# Check to see if the version exists or create it
94	VERSIONRESULT=$(/usr/local/bin/curl -s "${ATLAS_UPLOAD_URL}/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}?access_token=${KEY}")
95	if [ $? != 0 ]; then
96		echo "Failed to connect to the API"
97		exit 2;
98	fi
99	echo $VERSIONRESULT | grep "version/${VERSION}" > /dev/null
100	if [ $? != 0 ]; then
101		echo "Creating version: ${VERSION}"
102		/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
103		/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
104		VERSIONRESULT=$(/usr/local/bin/curl -s "${ATLAS_UPLOAD_URL}/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}?access_token=${KEY}")
105		echo $VERSIONRESULT | grep "version/${VERSION}" > /dev/null
106		if [ $? != 0 ]; then
107			echo "Failed to create version"
108			exit 2
109		fi
110	else
111		echo "Version already exists"
112	fi
113
114	# Check to see if the provider exists or create it
115	PROVIDERRESULT=$(/usr/local/bin/curl -s "${ATLAS_UPLOAD_URL}/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}/provider/${PROVIDER}?access_token=${KEY}")
116	if [ $? != 0 ]; then
117		echo "Failed to connect to the API"
118		exit 2;
119	fi
120	echo $PROVIDERRESULT | grep "provider/${PROVIDER}" > /dev/null
121	if [ $? != 0 ]; then
122		echo "Creating provider: ${PROVIDER}"
123		/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
124	else
125		echo "Provider already exists"
126	fi
127
128	# Request an upload token
129	TOKENRESULT=$(/usr/local/bin/curl -s "${ATLAS_UPLOAD_URL}/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}/provider/${PROVIDER}/upload?access_token=${KEY}")
130	if [ $? != 0 ]; then
131		echo "Failed to get the token from the API"
132		exit 2;
133	fi
134	echo ${TOKENRESULT} | grep -E "upload_path" > /dev/null
135	if [ $? != 0 ]; then
136		echo "No token found from the API"
137		exit 2
138	else
139		TOKEN=$(echo $TOKENRESULT | sed -e 's/.*token":"//' -e 's/.*upload_path":"//' -e 's/}$//g' -e 's/"//g')
140		echo "Uploading to Atlas"
141		UPLOADRESULT=$(/usr/local/bin/curl -s -X PUT --upload-file ${FILE} "${TOKEN}")
142
143		# Validate the Upload
144		echo "Validating"
145		VALIDRESULT=$(/usr/local/bin/curl -s "${ATLAS_UPLOAD_URL}/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}/provider/${PROVIDER}?access_token=${KEY}")
146		HOSTED_TOKEN=$(echo $VALIDRESULT | sed -e 's/.*"hosted"://' -e 's/,.*$//')
147		if [ ! -z ${TOKEN} -a "${HOSTED_TOKEN}" != "true" ]; then
148			echo "Upload failed, try again."
149			exit 2
150		fi
151
152		# Release the version
153		echo "Releasing ${VERSION} of ${BOX} in Atlas"
154		/usr/local/bin/curl -s ${ATLAS_UPLOAD_URL}/api/v1/box/${USERNAME}/${BOX}/version/${VERSION}/release -X PUT -d "access_token=${KEY}" > /dev/null
155	fi
156}
157
158main "$@"
159