-
Notifications
You must be signed in to change notification settings - Fork 0
/
darebox.sh
executable file
·228 lines (180 loc) · 3.78 KB
/
darebox.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
#!/bin/bash
# Checks to see if vagrant are installed
reqs () {
if command -v vagrant &> /dev/null
then
:
else
echo "[ERR!] Hashicorp's Vagrant is not installed!"
exit 11
fi
}
# Runs packer build
build () {
echo "Vagrant Build"
echo "Starting VM and Provisioning"
vagrant up --provision
echo "Packaging Box"
vagrant package --output "${BOXDIR}/${VERSION}-${BOXFILE}"
#link
echo "Stopping VM"
vagrant halt
echo "Box Built"
}
upload () {
echo "Uploading Box"
vagrant cloud auth login --token="${KEY}"
vagrant cloud publish "$BOX" "$VERSION" virtualbox "${BOXDIR}/${VERSION}-${BOXFILE}"
}
# Nothing seems to follow these symbolic links correctly so theres no point trying to do this
# link () {
# echo "Updating default box"
# if [[ -L "${BOXDIR}/${BOXFILE}" ]]
# then
# echo "removing old link : ${BOXDIR}/${BOXFILE}"
# rm "${BOXDIR}/${BOXFILE}"
# fi
# echo "adding new link ${BOXDIR}/${VERSION}-${BOXFILE} to ${BOXDIR}/${BOXFILE}"
# ln -s "${BOXDIR}/${VERSION}-${BOXFILE}" "${BOXDIR}/${BOXFILE}"
# }
# Brings an instance of our vagrant box up and runs our test script
test () {
## TODO ::
# Vagrant doesn't follow smylinks properly so we need to rewrite the Vagrant File.
echo "testing box"
cd "$TEST_DIR" || exit 20
sed -ri "s:(config.vm.box =) (\"../output/([0-9]|[a-b]|.)+):\1 \"../output/${VERSION}-${BOXFILE}\":" Vagrantfile
vagrant up
vagrant ssh -c /vagrant/test.sh
cd ..
}
test_clean () {
cd "$TEST_DIR" || exit 21
vagrant halt
vagrant destroy -f
if vagrant box list | grep -q "../${BOXDIR}/${BOXFILE}";
then
vagrant box remove "../${BOXDIR}/${BOXFILE}"
fi
if [[ -d .vagrant ]]
then
rm -r .vagrant/
fi
cd ..
}
build_clean () {
if [[ -d "${BOXDIR}/.vagrant" ]]
then
rm -r "${BOXDIR}/.vagrant/"
fi
if [[ -d "${BOXDIR}/Vagrantfile" ]]
then
rm "${BOXDIR}/Vagrantfile"
fi
vagrant destroy -f
# We don't want to remove the box file
}
clean () {
test_clean
build_clean
}
size () {
if [[ -f "$BOXDIR/$VERSION-$BOXFILE" ]]
then
box="$BOXDIR/$VERSION-$BOXFILE"
echo "Size: $(du --human-readable --apparent-size --time "$box")"
else
echo "No box found at [$BOXDIR/$VERSION-$BOXFILE]"
fi
}
load_key () {
if [[ -f "apikey.conf" ]]
then
k=$(grep -Po "(?<=apikey = )[\w|\.]+" apikey.conf | xargs)
echo "$k"
else
echo "Failed to load key"
exit 12
fi
}
load_var () {
if [[ -f "vars.conf" ]]
then
k=$(grep -Po "(?<=$1 = )[\w|\.|\/]+" vars.conf | xargs)
echo "$k"
else
echo "Failed to load $1"
exit 11
fi
}
report_config() {
echo "TEST_DIR = $TEST_DIR"
echo "BOX = $BOX"
echo "BOXFILE = $BOXFILE"
echo "BOXDIR = $BOXDIR"
echo "VERSION = $VERSION"
}
# check_version () {
# TODO: check to see if the version exist online before trying to create a new version
# }
usage () {
printf "\n./darebox.sh <command>\n\n"
printf "\t- build: Runs packer build generating a provisioned vagrant box \n"
printf "\t- test: Runs our custom shell script to see if everything installed correctly\n"
printf "\t- all: Runs build then test\n"
printf "\t- clean: Removes build and test artifacts after shuting vms down\n"
printf "\t- upload: Uploads the box to vagrant\n"
printf "\t- size: Reports the size of the packaged Box\n"
printf "\t- config: Reports the current config\n"
printf "\t- help: Print this info\n"
}
TEST_DIR="$(load_var TEST_DIR)"
BOX=$(load_var BOX)
BOXFILE=$(load_var BOXFILE)
BOXDIR=$(load_var BOXDIR)
VERSION=$(load_var VERSION)
KEY=$(load_key)
case $1 in
build)
reqs
report_config
clean
build
size
;;
test)
reqs
test
;;
upload)
reqs
size
upload
;;
all)
reqs
build
test
clean
size
;;
clean)
clean
;;
size)
size
;;
help)
usage
reqs
;;
config)
report_config
;;
*)
echo "incorrect args for darebox : $*"
usage
exit 1
;;
esac
exit 0