2
2
set -euo pipefail
3
3
4
4
# Copies an AMI to all other regions and outputs a build/mappings.yml file
5
- # Local Usage: .buildkite/steps/copy.sh <ami_id >
5
+ # Local Usage: .buildkite/steps/copy.sh <linux_ami_id> <windows_ami_id >
6
6
7
7
copy_ami_to_region () {
8
8
local source_image_id=" $1 "
@@ -38,6 +38,17 @@ wait_for_ami_to_be_available() {
38
38
done
39
39
}
40
40
41
+ get_image_name () {
42
+ local image_id=" $1 "
43
+ local region=" $2 "
44
+
45
+ aws ec2 describe-images \
46
+ --image-ids " $image_id " \
47
+ --output text \
48
+ --region " $region " \
49
+ --query ' Images[*].Name'
50
+ }
51
+
41
52
make_ami_public () {
42
53
local image_id=" $1 "
43
54
local region=" $2 "
@@ -76,13 +87,16 @@ IMAGES=(
76
87
)
77
88
78
89
# Configuration
79
- source_image_id=" ${1:- } "
90
+ linux_source_image_id=" ${1:- } "
91
+ windows_source_image_id=" ${2:- } "
92
+
80
93
source_region=" ${AWS_REGION} "
81
94
mapping_file=" build/mappings.yml"
82
95
83
- # Read the source_image_id from meta-data if empty
84
- if [[ -z " $source_image_id " ]] ; then
85
- source_image_id=$( buildkite-agent meta-data get image_id)
96
+ # Read the source images from meta-data if no arguments are provided
97
+ if [ $# -eq 0 ] ; then
98
+ linux_source_image_id=$( buildkite-agent meta-data get " linux_image_id" )
99
+ windows_source_image_id=$( buildkite-agent meta-data get " windows_image_id" )
86
100
fi
87
101
88
102
# If we're not on the master branch or a tag build skip the copy
@@ -92,33 +106,37 @@ if [[ $BUILDKITE_BRANCH != "master" ]] && [[ "$BUILDKITE_TAG" != "$BUILDKITE_BRA
92
106
cat << EOF > "$mapping_file "
93
107
Mappings:
94
108
AWSRegion2AMI:
95
- ${AWS_REGION} : { AMI : $source_image_id }
109
+ ${AWS_REGION} : { linux : $linux_source_image_id , windows: $windows_source_image_id }
96
110
EOF
97
111
exit 0
98
112
fi
99
113
100
- s3_mappings_cache=" s3://${BUILDKITE_AWS_STACK_BUCKET} /mappings-${source_image_id} -${BUILDKITE_BRANCH} .yml"
114
+ s3_mappings_cache=$( printf " s3://%s/mappings-%s-%s-%s.yml" \
115
+ " ${BUILDKITE_AWS_STACK_BUCKET} " \
116
+ " ${linux_source_image_id} " \
117
+ " ${windows_source_image_id} " \
118
+ " ${BUILDKITE_BRANCH} " )
101
119
102
120
# Check if there is a previously copy in the cache bucket
103
121
if aws s3 cp " ${s3_mappings_cache} " " $mapping_file " ; then
104
122
echo " --- Skipping AMI copy, was previously copied"
105
123
exit 0
106
124
fi
107
125
108
- # Get the image name to copy to other amis
109
- source_image_name=$( aws ec2 describe-images \
110
- --image-ids " $source_image_id " \
111
- --output text \
112
- --region " $source_region " \
113
- --query ' Images[*].Name' )
126
+ # Get the image names to copy to other regions
127
+ linux_source_image_name=$( get_image_name " $linux_source_image_id " " $source_region " )
128
+ windows_source_image_name=$( get_image_name " $windows_source_image_id " " $source_region " )
114
129
115
130
# Copy to all other regions
116
131
for region in ${ALL_REGIONS[*]} ; do
117
132
if [[ $region != " $source_region " ]] ; then
118
- echo " --- Copying $source_image_id to $region " >&2
119
- IMAGES+=(" $( copy_ami_to_region " $source_image_id " " $source_region " " $region " " ${source_image_name} -${region} " ) " )
133
+ echo " --- Copying :linux: $linux_source_image_id to $region " >&2
134
+ IMAGES+=(" $( copy_ami_to_region " $linux_source_image_id " " $source_region " " $region " " ${linux_source_image_name} -${region} " ) " )
135
+
136
+ echo " --- Copying :windows: $windows_source_image_id to $region " >&2
137
+ IMAGES+=(" $( copy_ami_to_region " $windows_source_image_id " " $source_region " " $region " " ${windows_source_image_name} -${region} " ) " )
120
138
else
121
- IMAGES+=(" $source_image_id " )
139
+ IMAGES+=(" $linux_source_image_id " " $windows_source_image_id " )
122
140
fi
123
141
done
124
142
@@ -130,20 +148,32 @@ Mappings:
130
148
EOF
131
149
132
150
echo " --- Waiting for AMIs to become available" >&2
133
- for (( i= 0 ; i< ${# IMAGES[*]} ; i++ )) ; do
134
- region=" ${ALL_REGIONS[i]} "
135
- image_id=" ${IMAGES[i]} "
136
151
137
- wait_for_ami_to_be_available " $image_id " " $region " >&2
152
+ for region in ${ALL_REGIONS[*]} ; do
153
+ linux_image_id=" ${IMAGES[0]} "
154
+ windows_image_id=" ${IMAGES[1]} "
155
+
156
+ wait_for_ami_to_be_available " $linux_image_id " " $region " >&2
138
157
139
- # Make the AMI public if it's not the source image
140
- if [[ $image_id != " $source_image_id " ]] ; then
141
- echo " Making ${image_id} public" >&2
142
- make_ami_public " $image_id " " $region "
158
+ # Make the linux AMI public if it's not the source image
159
+ if [[ $linux_image_id != " $linux_source_image_id " ]] ; then
160
+ echo " Making :linux: ${linux_image_id} public" >&2
161
+ make_ami_public " $linux_image_id " " $region "
162
+ fi
163
+
164
+ wait_for_ami_to_be_available " $windows_image_id " " $region " >&2
165
+
166
+ # Make the windows AMI public if it's not the source image
167
+ if [[ $windows_image_id != " $windows_source_image_id " ]] ; then
168
+ echo " Making :windows: ${windows_image_id} public" >&2
169
+ make_ami_public " $windows_image_id " " $region "
143
170
fi
144
171
145
172
# Write yaml to file
146
- echo " $region : { AMI: $image_id }" >> " $mapping_file "
173
+ echo " $region : { linux: $linux_image_id , windows: $windows_image_id }" >> " $mapping_file "
174
+
175
+ # Shift off the processed images
176
+ IMAGES=(" ${IMAGES[@]: 2} " )
147
177
done
148
178
149
179
echo " --- Uploading mapping to s3 cache"
0 commit comments