This repository has been archived by the owner on Apr 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
create-ami.sh
executable file
·221 lines (176 loc) · 6.21 KB
/
create-ami.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
#!/usr/bin/env bash
set -e
# finally terminate ec2 instance
function finally() {
if [ $DRY_RUN = true ]; then
echo "Dry run requested; skipping server termination"
else
# delete instance
echo "Terminating server..."
aws ec2 terminate-instances --region $region --instance-ids $instanceid > /dev/null
fi
}
function wait_for_server() {
local ip=$1
while [ true ]; do
echo "Waiting for server..."
set +e
ssh $ssh_args ubuntu@$ip echo >/dev/null
alive=$?
set -e
if [ $alive -eq 0 ]; then
break
fi
sleep 2
done
}
if [ -z "$NO_TERMINATE" ]; then
trap finally EXIT
fi
# argument parsing
if [ "$1" = "--dry-run" ]; then
echo "Dry run requested."
DRY_RUN=true
shift
else
DRY_RUN=false
fi
if [ -z "$1" ] || [ ! -r "$1" ] || [ -z "$2" ]; then
echo "Usage: $0 [--dry-run] <config-file> <taupage-version>" >&2
exit 1
fi
CONFIG_FILE=$1
TAUPAGE_VERSION=$2
# load configuration file
. $CONFIG_FILE
# start creation
set -e
# reset path
#cd $(dirname $0)
if [ ! -f "$secret_dir/secret-vars.sh" ]; then
echo "Missing secret-vars.sh in secret dir" >&2
exit 1
fi
# create server
echo "Starting a base server..."
result=$(aws ec2 run-instances \
--image-id $base_ami \
--count 1 \
--associate-public-ip-address \
--instance-type $instance_type \
--key-name $keypair \
--security-group-ids $security_group \
--output json \
--region $region \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Taupage,Value=BuildMachine}]' \
--subnet-id $subnet)
instanceid=$(echo $result | jq -r .Instances\[0\].InstanceId)
echo "Instance: $instanceid"
aws ec2 create-tags --region $region --resources $instanceid --tags "Key=Name,Value=Taupage AMI Builder, Key=Version,Value=$TAUPAGE_VERSION"
while [ true ]; do
result=$(aws ec2 describe-instances --region $region --instance-id $instanceid --output json)
ip=$(echo $result | jq -r .Reservations\[0\].Instances\[0\].PublicIpAddress)
[ ! -z "$ip" ] && [ "$ip" != "null" ] && break
echo "Waiting for public IP..."
sleep 5
done
echo "IP: $ip"
# wait for server
wait_for_server $ip
if [[ $OSTYPE == darwin* ]]; then
# Disable tar'ing resource forks on Macs
export COPYFILE_DISABLE=true
fi
# upload systemd purge script
scp $ssh_args $(dirname $0)/build/install_upstart.sh ubuntu@$ip:/tmp/install_upstart.sh
ssh $ssh_args ubuntu@$ip sudo /tmp/install_upstart.sh
sleep 2
wait_for_server $ip
# upload files
echo "Uploading runtime/* files to server..."
tar c -C $(dirname $0)/runtime --exclude=__pycache__ . | ssh $ssh_args ubuntu@$ip sudo tar x --no-same-owner --no-overwrite-dir -C /
echo "Set link to old taupage file"
ssh $ssh_args ubuntu@$ip sudo ln -s /meta/taupage.yaml /etc/taupage.yaml
echo "Uploading build/* files to server..."
tar c -C $(dirname $0) build | ssh $ssh_args ubuntu@$ip sudo tar x --no-same-owner -C /tmp
echo "Uploading secret/* files to server..."
tar c -C $secret_dir . | ssh $ssh_args ubuntu@$ip sudo tar x --no-same-owner -C /tmp/build
if [ ! -z "$proprietary_dir" ]; then
echo "Uploading proprietary/* files to server..."
ssh $ssh_args ubuntu@$ip sudo mkdir /opt/proprietary
tar c -C $proprietary_dir . | ssh $ssh_args ubuntu@$ip sudo tar x --no-same-owner -C /opt/proprietary
fi
if [ ! -z "$overlay_dir" ]; then
echo "Uploading overlaying files to server..."
tar c -C $overlay_dir --exclude=__pycache__ . | ssh $ssh_args ubuntu@$ip sudo tar x --no-same-owner --no-overwrite-dir -C /
fi
ssh $ssh_args ubuntu@$ip find /tmp/build
# execute setup script
echo "Executing setup script..."
set +e
ssh $ssh_args ubuntu@$ip sudo /tmp/build/setup.sh
build_status=$?
set -e
if [ $build_status -ne 0 ]; then
echo "Build failed"
exit 1
fi
if [ $DRY_RUN = true ]; then
echo "Dry run requested; skipping image creation and sharing!"
exit 0
fi
# cleanup build scripts
echo "Cleaning up build files from server..."
ssh $ssh_args ubuntu@$ip sudo rm -rf /tmp/build
# remove ubuntu user
# echo "Removing ubuntu user from system..."
# ssh $ssh_args ubuntu@$ip sudo /tmp/delete-ubuntu-user-wrapper.sh
# echo "Giving deluser some time..."
# sleep 15
echo "Stopping instance to enable ENA support"
aws ec2 stop-instances --region $region --instance-ids $instanceid
while [[ $(aws ec2 describe-instances --region $region --instance-id $instanceid --output json | jq -r '.Reservations[].Instances[].State.Name') != "stopped" ]]; do
echo "Waiting for Instance.State == 'stopped'"
sleep 5
done
echo "Setting EnaSupport flag"
aws ec2 modify-instance-attribute --region $region --instance-id $instanceid --ena-support
# create ami
build_date="$(date +%Y%m%d-%H%M%S)"
ami_name="TaupageBuild-${TAUPAGE_VERSION}"
echo "Creating $ami_name ..."
result=$(aws ec2 create-image \
--region $region \
--instance-id $instanceid \
--output json \
--name $ami_name \
--description "$ami_description")
imageid=$(echo $result | jq -r .ImageId)
echo "Image: $imageid"
function wait_for_ami() {
local region="$1"
local imageid="$2"
while true; do
local state=$(aws ec2 describe-images --region $region --output json --image-id $imageid | jq -r .Images\[0\].State)
echo "Waiting for AMI creation... ($state)"
if [ "$state" = "failed" ]; then
echo "Image creation failed."
exit 1
elif [ "$state" = "available" ]; then
echo "AMI $region/$ami_name ($imageid) successfully created."
# set AMI tags
image_tags="$(jq -n --arg version "$TAUPAGE_VERSION" --arg build_date "$build_date" '[{Key: "Version", Value: $version}, {Key: "BuildDate", Value: $build_date}]')"
aws ec2 create-tags --region $region --resources $imageid --tags "$image_tags"
return
fi
sleep 10
done
}
wait_for_ami "$region" "$imageid"
# copy to other regions
for target_region in $copy_regions; do
if [[ "$target_region" != "$region" ]]; then
target_imageid="$(aws ec2 copy-image --source-region "$region" --source-image-id "$imageid" --region "$target_region" --name "$ami_name" --description "$ami_description" --output text)"
wait_for_ami "$target_region" "$target_imageid"
fi
done