forked from lostsnow/vagrant-centos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.sh
executable file
·241 lines (208 loc) · 5.71 KB
/
make.sh
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/bin/bash
# Abort script at first error
set -o errexit
# Setting environment variables
readonly CUR_DIR=$(cd $(dirname ${BASH_SOURCE:-$0}); pwd)
BOX_NAME="lostsnow/centos7"
BOX_PROVIDER="virtualbox"
ATLAS_API_URL=https://atlas.hashicorp.com/api/v1/box
# Flag paser
usage() {
case "$1" in
create | upload | release )
echo -e "Usage: $( basename $0 ) $1 [arg...]"
echo
echo -e "Options:"
echo -e " -v <version> box version"
if [ $1 == "create" ] || [ $1 == "upload" ]; then
echo -e " -p <provider> box provider, default: virtualbox"
fi
echo -e " -n <name> box name, default: lostsnow/centos7"
if [ $1 == "upload" ]; then
echo -e " -f <file> box file"
fi
echo
exit 1
;;
* )
echo -e "Usage: $( basename $0 ) <command> [arg...]"
echo
echo -e "Commands:"
echo -e " help <command> print command help message"
echo -e " create create box version and provider"
echo -e " upload upload box file"
echo -e " release release box"
echo
echo -e "Options:"
echo -e " --help give this help list"
echo
exit 1
;;
esac
}
catch_all () {
[[ ! -z "$1" ]] && warning "$1: command not found"
exit 1
}
function_exists () {
type $1 2> /dev/null | grep -q 'function'
}
cecho () {
local MSG=$1
local COLOR=$2
local PREFIX=
local SUFFIX="\e[0m"
case "$COLOR" in
red)
PREFIX="\e[0;31m"
;;
green)
PREFIX="\e[0;32m"
;;
brown)
PREFIX="\e[0;33m"
;;
purple)
PREFIX="\e[0;35m"
;;
*)
PREFIX=""
SUFFIX=""
;;
esac
MSG="$PREFIX$MSG$SUFFIX"
set builtin
echo -ne "$MSG"
}
ncecho () {
cecho "$@"
echo
}
now () {
cecho `date "+%Y/%m/%d:%H:%M:%S"` $1;
}
fatal () {
ncecho "[FATAL] $1" "red" && exit 1
}
error () {
ncecho "[ERROR] $1" red
}
success () {
ncecho "[SUCCESS] $1" green
}
info () {
ncecho "[INFO] $1" purple
}
json_val() {
temp=`echo $1 | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $2`
echo ${temp##*|}
}
# Checks for an Atlas API Error
check_error() {
if [[ $1 == '{"errors":'* ]] ;
then
error "$*"
exit 1
fi
}
run() {
if function_exists _$(basename $0) ; then
CMD=$(basename $0)
elif function_exists _$1 ; then
CMD=$1
shift
else
catch_all $@
fi
while true; do
case "$1" in
-v )
BOX_VERSION="$2"; shift 2 ;;
-p )
BOX_PROVIDER="$2"; shift 2 ;;
-b )
BOX_NAME="$2"; shift 2 ;;
-f )
BOX_FILE="$2"; shift 2 ;;
-- )
shift; break ;;
* )
break ;;
esac
done
if [ -z "${ATLAS_TOKEN}" ]; then
fatal "Atlas token required, Please set by environment variable: ATLAS_TOKEN."
fi
if [ -z "${BOX_VERSION}" ]; then
error "Box version required!"
usage $CMD
fi
if [ -z "${BOX_NAME}" ]; then
error "Box name required!"
usage $CMD
fi
_$CMD
}
_create() {
if [ -z "${BOX_PROVIDER}" ]; then
error "Box provider required!"
usage create
fi
info "Create version"
response=$(curl -s ${ATLAS_API_URL}/${BOX_NAME}/versions -X POST -d version[version]=${BOX_VERSION} -d access_token=${ATLAS_TOKEN})
check_error $response
echo $response
info "Create provider"
response=$(curl -s ${ATLAS_API_URL}/${BOX_NAME}/version/${BOX_VERSION}/providers -X POST -d provider[name]=${BOX_PROVIDER} -d access_token=${ATLAS_TOKEN})
check_error $response
echo $response
success "Create success"
}
_upload() {
if [ -z "${BOX_PROVIDER}" ]; then
error "Box provider required!"
usage upload
fi
if [ -z "${BOX_FILE}" ]; then
error "Box file required!"
usage upload
fi
if [ ! -f ${BOX_FILE} ]; then
error "Box file ${BOX_FILE} not found!"
usage upload
fi
case "$(uname -s)" in
Darwin | Linux )
null_device=/dev/null ;;
CYGWIN* | MINGW* | MSYS* )
null_device=NUL ;;
*)
fatal "OS not support: "$(uname -s)
;;
esac
info "Get upload token"
response=$(curl -s ${ATLAS_API_URL}/${BOX_NAME}/version/${BOX_VERSION}/provider/${BOX_PROVIDER}/upload?access_token=${ATLAS_TOKEN})
check_error ${response}
echo ${response}
TOKEN=$(json_val ${response}, "token")
info "Using token: "$TOKEN
info "Start upload"
response=$(curl -X PUT -f -T ${BOX_FILE} https://binstore.hashicorp.com/${TOKEN} --progress-bar -o ${null_device} -w "%{http_code}")
return_code=$?
if [ ${return_code} != 0 ] || [ x${response} != "x200" ]; then
fatal "Upload error"[${return_code},${response}]
fi
info "Upload success"
}
_release() {
info "Release box"
response=$(curl -s ${ATLAS_API_URL}/${BOX_NAME}/version/${BOX_VERSION}/release -X PUT -d access_token=${ATLAS_TOKEN})
check_error $response
echo $response
success "Release success"
}
case "$1" in
-h | --help | help ) usage $2 ;;
create | upload | release ) run "$@" ;;
* ) error "Invalid command -- $1"; usage ;;
esac