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