-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.sh
executable file
·236 lines (200 loc) · 5.45 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
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
#!/bin/bash
SCRIPT_DIR=$(dirname $0)
# Functions
#----------------------------------
usage()
{
echo ""
echo "Usage build.sh -v <HPCC Version> [-r <regsion> | -i <region file>] [options] -c <codename> "
echo " -a <value>: arch: amd64 or x86_64"
echo " -c <value>: Linux distro codename. For example, xential, trusty, el7"
echo " -i <value>: region file. One region per line."
echo " -n no run. Generate build script only."
echo " -p <value> subnet file for VPC setting. The default is subnet-ids "
echo " -P package type. The default is platform-community."
echo " -r <value>: one region."
echo " -t <value>: amazone ec2 type: instance or ebs."
echo " -u <user>: ssh login user. ubuntu or centos"
echo " -v <value>: HPCC Version. For example 6.4.24-1"
echo ""
exit
}
configure_file()
{
echo "sed \"s|@HPCC_VERSION@|${hpcc_version}|g; \
s|@CODENAME@|${codename}|g; \
s|@ARCH@|${arch}|g; \
s|@BUILD_TYPE@|${type}|g; \
s|@SUBNET_ID@|${subnet_id}|g; \
s|@USER@|${os_user}|g; \
s|@PACKAGE@|${package_type}|g; \
s|@BASE_AMI@|${base_ami}|g; \
s|@AWS_REGION@|${region}|g\" < ${build_script_in} > ${build_script} "
sed "s|@HPCC_VERSION@|${hpcc_version}|g; \
s|@CODENAME@|${codename}|g; \
s|@ARCH@|${arch}|g; \
s|@BUILD_TYPE@|${type}|g; \
s|@SUBNET_ID@|${subnet_id}|g; \
s|@USER@|${os_user}|g; \
s|@PACKAGE@|${package_type}|g; \
s|@BASE_AMI@|${base_ami}|g; \
s|@AWS_REGION@|${region}|g" < ${build_script_in} > ${build_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-ami | grep -v "^[[:space:]]*#" | grep ${region} | cut -d' ' -f2)
subnet_id=$(cat ${wk_dir}/${subnet_file} | grep -v "^[[:space:]]*#" | grep ${region} | cut -d' ' -f2)
configure_file
[ "$dry_run_only" = "true" ] && return
if [ "$quiet" = "true" ]
then
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
hpcc_version=
codename=
type=instance
build_script=aws-build.json
region_file=
err_file_list=log_list
quiet=false
wk_dir=
package_type=platform-community
os_user=ubuntu
num_of_failure=0
num_of_success=0
num_of_build=0
dry_run_only=false
use_subnet=true
log=
subnet_file=subnet-ids
#subnet_file=subnet-ids-ris
# Parse input parameters
#----------------------------------
while getopts "*a:c:d:i:nP:p:qr:t:u:v:" arg
do
case "$arg" in
a) arch="$OPTARG"
;;
c) codename="$OPTARG"
;;
i) region_file="$OPTARG"
;;
n) dry_run_only=true
;;
P) package_type="$OPTARG"
;;
p) setnet_file="$OPTARG"
;;
q) quiet=true
;;
r) region="$OPTARG"
;;
t) type="$OPTARG"
;;
v) hpcc_version="$OPTARG"
;;
u) os_user="$OPTARG"
;;
?) usage
;;
esac
done
# Check variables
#----------------------------------
[ -z "${wk_dir}" ] && wk_dir=${SCRIPT_DIR}
if [ -z ${hpcc_version} ]
then
echo "Miss HPCC version."
exit 1
fi
ami_list="ami-${hpcc_version}"
[ -e ${ami_list} ] && rm -rf ${ami_list}
if [ -z ${codename} ]
then
echo "Miss Linux codename. For example xential, 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-build-vpc.json.in
else
build_script_in=${wk_dir}/${type}/aws-build.json.in
fi
if [ ! -e ${build_script_in} ]
then
echo "Cannot find build script template ${build_script_in}"
exit 1
fi
type="amazon-$type"
cp ${wk_dir}/${codename}/*.sh .
[ -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
#----------------------------------