aboutsummaryrefslogtreecommitdiff
path: root/release/scripts/atlas-upload.sh
blob: 63a5ba16b741f328047da49a6fbcf304d1cdfbd9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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 "$@"