forked from base2Services/ciinabox-bakery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bakery
executable file
·163 lines (130 loc) · 4.31 KB
/
bakery
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
#!/usr/bin/env bash
#
# AMI Baking script
#
# base2Services <[email protected]>
#
set -e
[ -r /etc/environment ] && . /etc/environment
# Logging
function log() {
LEVEL="${1^^}"
MESSAGE="$2"
DATESTAMP="`date +%r`"
echo "${DATESTAMP} [${LEVEL}] -- ${MESSAGE}"
}
SCRIPT_PATH="`dirname \"$0\"`"
SCRIPT_PATH="`( cd \"$SCRIPT_PATH\" && pwd )`"
CLIENT=${1}
AMI_TYPE=${2}
PACKER_BUILD_TMPL=${3}
PACKER_BUILD_PARAMS=${4}
BUILD_NUMBER=${5}
SOURCE_AMI=${6}
mkdir -p build
if [[ x${AWS_ACCESS_KEY_ID} == "x" ]]; then
CLI_OPTS=""
else
CLI_OPTS="-var access_key=${AWS_ACCESS_KEY_ID} -var secret_key=${AWS_SECRET_ACCESS_KEY}"
fi
if [[ ! x${CHEF_VERSION} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var chef_version=${CHEF_VERSION}"
log info "using chef_version ${CHEF_VERSION}"
fi
if [[ "${DISABLE_CHEF_ACCEPT_LICENSE}" == "true" ]]; then
CLI_OPTS="${CLI_OPTS} -var chef_accept_license="""
log info "disabling the --chef-license flag"
fi
if [[ ! x${CB_BUILD_NO} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var cb_buld_no=${CB_BUILD_NO}"
log info "using cb_buld_no ${CB_BUILD_NO}"
fi
if [[ ! x${SOURCE_BUCKET} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var source_bucket=${SOURCE_BUCKET}"
log info "using source_bucket ${SOURCE_BUCKET}"
fi
if [[ ! x${BUCKET_REGION} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var bucket_region=${BUCKET_REGION}"
log info "using bucket_region ${BUCKET_REGION}"
fi
if [[ ! x${CHEF_PATH} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var chef_path=${CHEF_PATH}"
log info "using chef_path ${CHEF_PATH}"
fi
if [[ ! x${SSH_USERNAME} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var ssh_username=${SSH_USERNAME}"
log info "using ssh_username ${SSH_USERNAME}"
fi
if [[ ! x${DEVICE_NAME} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var device_name=${DEVICE_NAME}"
log info "using device_name ${DEVICE_NAME}"
fi
CLI_OPTS="${CLI_OPTS} -var ami_name=${AMI_TYPE}"
CLI_OPTS="${CLI_OPTS} -var build_no=${BUILD_NUMBER}"
if [[ ! x${CLIENT} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var client=${CLIENT}"
log info "using client name ${CLIENT}"
fi
if [[ ! x${SOURCE_AMI} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var source_ami=${SOURCE_AMI}"
log info "using custom base ami ${SOURCE_AMI}"
fi
if [[ ! x${PACKER_SSH_TIMEOUT} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var ssh_timeout=${PACKER_SSH_TIMEOUT}"
log info "using ssh timeout of ${PACKER_SSH_TIMEOUT} seconds"
fi
if [[ ! x${7} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var build_id=${7}"
log info "using build_id ${7}"
fi
if [[ ! x${8} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var build_sha=${8}"
log info "using build_sha ${8}"
fi
if [[ ! x${9} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var chef_run_list=${9}"
log info "using chef_run_list ${9}"
fi
if [[ ! x${10} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var instance_type=${10}"
log info "using instance_type ${10}"
fi
if [[ ! x${11} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var bake_volume_size=${11}"
log info "using bake_volume_size ${11}"
fi
if [[ ${DEBUG} == "true" ]]; then
CLI_OPTS="-debug ${CLI_OPTS}"
log info "debug is on"
fi
if [ ! -z "$COPY_REGIONS" ];then
CLI_OPTS="${CLI_OPTS} -var ami_regions=${COPY_REGIONS// /,}"
fi
if [[ ! x${OPT_VARS} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} ${OPT_VARS}"
fi
echo "{\"${CLIENT}\":{\"version\":\"${BUILD_NUMBER}\",\"build_id\":\"${7}\",\"build_sha\":\"${8}\"}}" > build/override.json
/opt/packer/packer build -machine-readable -var-file=${PACKER_BUILD_PARAMS} ${CLI_OPTS} ${PACKER_BUILD_TMPL} ${DEBUG_FLAG} | tee build/build.log
#extract base AMI-ID
CURRENT_REGION=`curl -s http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|awk -F\" '{print $4}'`
AMI_ID=`cat build/build.log | tr -d '\000' | grep 'artifact,0,id' | cut -d, -f6 | grep -o "${CURRENT_REGION}:ami-[0-9abcdef]\{8,18\}" | cut -d':' -f2`
if [[ x${AMI_ID} == 'x' ]]; then
log error "failed to create ${AMI_TYPE} AMI"
exit 1
fi
log info "Creating AMI Yaml Mapping file....."
cat <<EOT > ${AMI_TYPE}-ami-${BUILD_NUMBER}.yml
${AMI_TYPE}AMI:
${REGION}:
ami: ${AMI_ID}
EOT
if [ ! -z "$COPY_REGIONS" ];then
for region in $COPY_REGIONS; do
REGIONAL_AMI_ID=`cat build/build.log | grep "artifact,0,id" | grep -o "${region}:ami-[0-9abcdef]\{8,18\}" | cut -d: -f2`
cat <<EOT >> ${AMI_TYPE}-ami-${BUILD_NUMBER}.yml
${region}:
ami: ${REGIONAL_AMI_ID}
EOT
done
fi
exit 0