forked from base2Services/ciinabox-bakery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbakery
executable file
·116 lines (90 loc) · 2.81 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
#!/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
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${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 [[ ! x${SOURCE_BUCKET} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var source_bucket=${SOURCE_BUCKET}"
log info "using s3 bucket ${SOUCE_BUCKET}"
fi
if [[ ! x${AMI_REGIONS} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var ami_regions=${AMI_REGIONS}"
log info "using ami regions ${AMI_REGIONS}"
fi
if [[ ! x${CONTAINER_VERSION} == "x" ]]; then
CLI_OPTS="${CLI_OPTS} -var container_version=${CONTAINER_VERSION}"
log info "using container version ${CONTAINER_VERSION}"
fi
echo "{\"${CLIENT}\":{\"version\":\"${BUILD_NUMBER}\",\"build_id\":\"${7}\",\"build_sha\":\"${8}\"}}" > build/override.json
/opt/packer/packer build -debug -machine-readable -var-file=${PACKER_BUILD_PARAMS} ${CLI_OPTS} ${PACKER_BUILD_TMPL} | tee build/build.log
#extract base AMI-ID
AMI_ID=`cat build/build.log | tr -d '\000' | grep 'artifact,0,id' | cut -d, -f6 | 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
exit 0