-
Notifications
You must be signed in to change notification settings - Fork 2
/
build-dev.sh
executable file
·281 lines (243 loc) · 7.18 KB
/
build-dev.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/bin/bash
SCRIPT_DIR=$(dirname $0)
# Functions
#----------------------------------
usage()
{
echo ""
echo "Usage build.sh -v <HPCC Version> [-r <regsion> | -i <region file>] [options] -c <codename> -P <gpg passphrase> "
echo " -a <value>: arch: amd64 or x86_64"
echo " -c <value>: Linux distro codename. For example, xenial, trusty, el7"
echo " -D <value>: Device name. Either sda1 or xvda (for amzn2)"
echo " -f <value>: provision script flag. Default is empty string. If not empy the script will be in format:"
echo " hpcc-dev-${codename}-${arch}-<flag value>.sh.in"
echo " -i <value>: region file. One region per line."
echo " -I <value>: IAM Instance profile. The default is hpcc-build"
echo " -n no run. Generate build script only."
echo " -p subnet file for VPC setting. The default is subnet-ids"
echo " -P GPG passphrase."
echo " -r <value>: one region."
echo " -s <value>: file_server."
echo " -S <value>: AWS S3 bucket name. The default is hpcc-build-ca-central-1"
echo " -t <value>: amazone ec2 type: instance or ebs."
echo " -T <value>: instance type: t2.micro (default), t2 large, etc."
echo " -u <value>: Linux user. 'ubuntu' for Ubuntu and 'centos' for CentOS"
echo " -z <value>: security gorup id. "
echo ""
exit 3
}
configure_file()
{
echo "Template: ${build_script_in}"
echo "Generated file ${build_script}"
sed "s|@CODENAME@|${codename}|g; \
s|@ARCH@|${arch}|g; \
s|@BUILD_TYPE@|${type2}|g; \
s|@PROVISION_SCRIPT@|${provision_script}|g; \
s|@DEVICE_NAME@|${device_name}|g; \
s|@INSTANCE_TYPE@|${instance_type}|g; \
s|@SUBNET_ID@|${subnet_id}|g; \
s|@INSTANCE_PROFILE@|${instance_profile}|g; \
s|@BASE_AMI@|${base_ami}|g; \
s|@USER@|${os_user}|g; \
s|@AMI_NAME@|${ami_name}|g; \
s|@SECURITY_GROUP_ID@|${security_group_id}|g; \
s|@AWS_REGION@|${region}|g" < ${build_script_in} > ${build_script}
}
configure_provision()
{
sed "s|@RINSIDE_URL@|${RINSIDE_URL}|g; \
s|@CMAKE_URL@|${CMAKE_URL}|g; \
s|@GPG_URL@|${GPG_URL}|g; \
s|@S3_BUCKET@|${s3_bucket_name}|g; \
s|@GPG_PASSPHRASE@|${gpg_passphrase}|g" < ${provision_script}.in > ${provision_script}
chmod +x ${provision_script}
}
run_packer_build()
{
now=$(date -u +%Y%m%d-%H%M)
log=ami-${region}-log-${now}.log
num_of_build=$(expr $num_of_build \+ 1)
build_script=ami-build-json-${region}
base_ami=$(cat ${wk_dir}/${codename}/base-${type}-ami | grep -v "^#" | grep ${region} | cut -d' ' -f2)
subnet_id=$(cat ${wk_dir}/${subnet_file} | grep -v "^#" | grep ${region} | cut -d' ' -f2)
configure_file
[ "$dry_run_only" = "true" ] && return
if [ "$quiet" = "true" ]
then
echo "Build for $region with $build_script"
packer build ${build_script} > $log 2>&1
rc=$?
else
echo ""
echo "Build for $region with $build_script"
packer build ${build_script} 2>&1 | tee -a $log ; rc=${PIPESTATUS[0]}
# To debug usig following. When error happens the instance will be kept in AWS EC2 region instances
# Use hpcc-build key file ssh to the instance to debug the problem
#packer build -on-error=abort -debug ${build_script} 2>&1 | tee -a $log ; rc=${PIPESTATUS[0]}
fi
if [ ${rc} -eq 0 ]
then
num_of_success=$(expr $num_of_success \+ 1)
cat $log | tail -n 2 | head -n 1 >> ${ami_list}
else
num_of_failure=$(expr $num_of_failure \+ 1)
echo "${region} $log" >> $err_file_list
fi
}
# Variables
#----------------------------------
arch=amd64
codename=
type=ebs
build_script=aws-build.json
region_file=
err_file_list=log_list
quiet=false
file_server=10.240.32.242
wk_dir=
gpg_passphrase=
os_user=ubuntu
num_of_failure=0
num_of_success=0
num_of_build=0
dry_run_only=false
use_subnet=true
instance_profile=hpcc-build
subnet_file=subnet-ids
s3_bucket_name=hpcc-build-ca-central-1
log=
device_name=sda1 #/dev/sda1 /dev/xvda
instance_type=t2.micro
script_flag=
ami_name=
security_group_id=sg-05b735c2cf177c8cc
# Parse input parameters
#----------------------------------
while getopts "*a:c:d:D:f:I:i:np:P:qr:S:s:t:T:v:u:z:" arg
do
case "$arg" in
a) arch="$OPTARG"
;;
c) codename="$OPTARG"
;;
D) device_name="$OPTARG"
;;
f) script_flag="$OPTARG"
;;
I) instance_profile="$OPTARG"
;;
i) region_file="$OPTARG"
;;
n) dry_run_only=true
;;
p) subnet_file="$OPTARG"
;;
P) gpg_passphrase="$OPTARG"
;;
q) quiet=true
;;
r) region="$OPTARG"
;;
S) s3_bucket_name="$OPTARG"
;;
s) file_server="$OPTARG"
;;
t) type="aws_$OPTARG"
;;
T) instance_type="$OPTARG"
;;
u) os_user="$OPTARG"
;;
z) security_group_id="$OPTARG"
;;
?) usage
;;
esac
done
if [ -z "$gpg_passphrase" ]
then
echo "Missing GPG passphrase"
usage
fi
RINSIDE_URL="http://${file_server}/data3/software/R"
GPG_URL="http://${file_server}/data3/build/gpg"
CMAKE_URL="http://${file_server}/data3/software/cmake"
# Check variables
#----------------------------------
[ -z "${wk_dir}" ] && wk_dir=${SCRIPT_DIR}
if [ -z ${codename} ]
then
echo "Miss Linux codename. For example xenial, el7, etc."
exit 1
fi
if [ -z ${region} ] && [ -z ${region_file} ]
then
echo "Miss region. Provide either region with -r or region list file with -i."
exit 1
fi
if [ "$use_subnet" = "true" ]
then
build_script_in=${wk_dir}/${type}/aws-hpcc-dev-build-vpc.json.in
else
build_script_in=${wk_dir}/${type}/aws-hpcc-dev-build.json.in
fi
if [ ! -e ${build_script_in} ]
then
echo "Cannot find build script template ${build_script_in}"
exit 1
fi
ami_list="ami_dev_${type}_${arch}"
type2="amazon-$type"
provision_script="hpcc-dev-${codename}-${arch}.sh"
[ -n "${script_flag}" ] && provision_script="hpcc-dev-${codename}-${arch}-${script_flag}.sh"
cp ${wk_dir}/${codename}/${provision_script}.in .
ami_name="hpcc-systems-dev-${codename}-${arch}"
[ -n "${script_flag}" ] && ami_name="hpcc-systems-dev-${codename}-${arch}-${script_flag}"
configure_provision
if [ -e ${ami_list} ]
then
rm -rf ${ami_list}
fi
[ -e ${err_file_list} ] && rm -rf ${err_file_list}
# Build AMI
#----------------------------------
if [ -n "$region" ]
then
run_packer_build
else
if [ ! -e ${region_file} ]
then
echo "Region file $region_file doesn't exists"
exit 1
fi
for line in $(cat ${region_file} )
do
line=$(echo $line | sed 's/[[:space:]]//g')
[[ $line == \#* ]] && continue
region=$(echo $line | cut -d' ' -f1 )
run_packer_build
done
fi
[ "$dry_run_only" = "true" ] && exit 0
# Summary
#----------------------------------
echo ""
echo " Total number of region build: $num_of_build"
echo " Total number of sucess : $num_of_success"
echo " Total number of failure : $num_of_failure"
echo ""
if [ ${num_of_failure} -gt 0 ]
then
echo "Error logs:"
cat ${err_file_list}
echo ""
fi
if [ ${num_of_success} -gt 0 ]
then
echo "Generated AMI list:"
cat ${ami_list}
echo ""
fi
# End
#----------------------------------