-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·185 lines (156 loc) · 4.94 KB
/
build.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
#!/usr/bin/env bash
set -euo pipefail
_PAD=0
_BUILD_ADD="Enable/disable vagrant box add to local box repository (default: TRUE)"
_BUILD_BUILD="Enable/disable box build (default: TRUE)"
_BUILD_CLEAN="Enable/disable box removal before and after build (default: TRUE)"
_BUILD_PUBLISH="Enable/disable vagrant publish to vagrant cloud (default: FALSE)"
_BUILD_RELEASE="Enable/disable vagrant release to vagrant cloud appy only if publising) (default: TRUE)"
for name in `( set -o posix ; set ) | egrep '^_BUILD_.*' | cut -f 1 -d =`; do
name=${name##*_}
new_pad=`expr length "$name"`
if [ $new_pad -gt $_PAD ]; then
_PAD=$new_pad
fi
done
echo
echo "Flags available (name:description):"
echo
for name in `( set -o posix ; set ) | egrep '^_BUILD_.*' | cut -f 1 -d =`; do
flag=${name##*_}
flag=${flag,,}
flag="--${flag}/--no-${flag} "
default_value=${!name}
default_value=${default_value##*default: }
default_value=${default_value%%)*}
echo "${flag:0:$(( $_PAD * 2 + 8 ))} : ${!name}"
declare "${name}=${default_value}"
done
echo
echo
while [ $# -gt 0 ]; do
flag=$1
var_name=$flag
value=TRUE
if [[ $var_name == --no-* ]]; then
value=FALSE
fi
var_name=${var_name##*-}
var_name=${var_name^^}
var_name=_BUILD_${var_name}
if ( set -o posix ; set ) | grep "${var_name}="; then
declare -r "${var_name}=${value}"
else
echo "Unknown flag $var_name ($flag)"
exit 1
fi
shift
done
echo "Flags value for this build:"
for name in `( set -o posix ; set ) | egrep '^_BUILD_.*' | cut -f 1 -d =`; do
name_padded="${name##*_} "
echo "${name_padded:0:$_PAD} : ${!name}"
done
echo
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
echo "Script directory: $SCRIPT_DIR"
cd $SCRIPT_DIR
DISTROS_BUILD="$SCRIPT_DIR/distros.build"
echo "Building boxes ids:"
cat $DISTROS_BUILD
echo
echo '------------------'
for i in `seq 1 5`; do
echo -n '.'
sleep 1
done
echo
vagrant_cmd=vagrant
export RANDOM_PORTS=true
function build_box {
export BUILDING=true
envvars="`jq -r .env info.json`"
if [ -n "$envvars" ]; then
set -a # -o allexport
eval "$envvars"
echo "envfile --------------------------"
echo "$envvars"
echo "----------------------------------"
set +a # +o allexport
fi
name=$(basename `pwd`)
name=`jq .os -r info.json | cut -f 1 -d -`-${name}
echo "Box"
echo "Name: $name"
echo "Directory: `pwd`"
if [ $_BUILD_CLEAN = 'TRUE' -o $_BUILD_BUILD = 'TRUE' ]; then
$vagrant_cmd destroy -f
if [ -f "${name}.box" ]; then
rm "${name}.box"
fi
fi
if [ $_BUILD_BUILD = 'TRUE' ]; then
$vagrant_cmd up
$vagrant_cmd halt
$vagrant_cmd package \
--info info.json \
--output "${name}.box" \
--vagrantfile Vagrantfile
fi
checksum=`sha512sum ${name}.box | awk '{print $1}'`
if [ $_BUILD_ADD = 'TRUE' ]; then
$vagrant_cmd box add \
--name giflw/${name} \
${name}.box \
--checksum "${checksum}" \
--checksum-type sha512 \
--force
fi
if [ $_BUILD_PUBLISH = 'TRUE' ]; then
$vagrant_cmd cloud publish \
giflw/${name} \
"`jq .os -r info.json | cut -f 2 -d -`-`date --utc +%Y%m%d%H%M`" \
virtualbox \
${name}.box \
--version-description "`jq .description -r info.json`" \
--checksum "${checksum}" \
--checksum-type sha512 \
`test $_BUILD_RELEASE = 'TRUE' && echo --release || echo ''` \
--no-private \
--force
fi
if [ $_BUILD_BUILD = 'TRUE' ]; then
$vagrant_cmd destroy -f
fi
if [ $_BUILD_CLEAN = 'TRUE' -a -f "${name}.box" ]; then
rm "${name}.box"
fi
}
for distro in `ls -d */`; do
echo '=================================================='
echo '=================================================='
echo "MAIN: $distro"
(
cd $distro
for box in `ls -d */`; do
(
echo '--------------------------------------------------'
echo " SUB: $box"
boxid="${distro%*/}/${box%*/}"
echo " BOX ID: $boxid"
cd $box
echo " PATH: `pwd`"
if grep $boxid $DISTROS_BUILD &> /dev/null; then
echo " Starting build..."
build_box
echo " ...build finished."
else
echo " Skipping box missing on distros.build file"
fi
echo '--------------------------------------------------'
)
done
)
echo '=================================================='
echo '=================================================='
done