forked from aws/amazon-ecs-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathec2-stage.sh
More file actions
executable file
·208 lines (183 loc) · 5.49 KB
/
ec2-stage.sh
File metadata and controls
executable file
·208 lines (183 loc) · 5.49 KB
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
#!/bin/bash
# Copyright 2014-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the
# "License"). You may not use this file except in compliance
# with the License. A copy of the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and
# limitations under the License.
set -e
DRYRUN=true
AWS_PROFILE=""
AWS_REGION="us-west-2"
AMI_ID="ami-9ff7e8af" #amzn-ami-hvm-2015.09.0.x86_64-gp2
ARTIFACT_BUCKET=""
SOURCE_BUCKET=""
KEY_NAME=""
SECURITY_GROUP=""
INSTANCE_PROFILE=""
source $(dirname "${0}")/publish-functions.sh
usage() {
echo "Usage: ${0} -a BUCKET -k SSH_KEY -s SECURITY_GROUP -i IAM_ROLE [OPTIONS]"
echo
echo "This script stages a new version of the Amazon ECS CLI"
echo "built on a freshly-launched EC2 instance."
echo "To run this script, you must have the following resources:"
echo "* A local AWS profile with permissions for \"ec2:run-instances\","
echo " \"ec2:describe-instances\", \"s3:PutObject\", and \"s3:ListObjects\""
echo "* An S3 bucket for the source"
echo "* An S3 bucket for the artifact destination"
echo "* An IAM profile with permissions to read from and write to the"
echo " source bucket (\"s3:GetObject\", \"s3:PutObject\", and \"s3:PutObjectAcl\") "
echo " and write to the destination S3 bucket (\"s3:PutObject\" and "
echo " \"s3:PutObjectAcl\")"
echo
echo "Options"
echo " -d true|false Dryrun (default is true)"
echo " -p PROFILE AWS CLI Profile (default is none)"
echo " -a BUCKET Artifact (Destination) AWS S3 Bucket"
echo " -t BUCKET Source AWS S3 Bucket"
echo " -k SSH_KEY SSH key name"
echo " -s SECURITY_GROUP Security group name"
echo " -i IAM_ROLE IAM Role name"
echo " -h Display this help message"
}
while getopts ":d:p:a:t:k:s:i:h" opt; do
case ${opt} in
d)
if [[ "${OPTARG}" = "false" ]]; then
DRYRUN=false
fi
;;
p)
AWS_PROFILE="${OPTARG}"
;;
a)
ARTIFACT_BUCKET="${OPTARG}"
;;
t)
SOURCE_BUCKET="${OPTARG}"
;;
k)
KEY_NAME="${OPTARG}"
;;
s)
SECURITY_GROUP="${OPTARG}"
;;
i)
INSTANCE_PROFILE="${OPTARG}"
;;
\?)
echo "Invalid option -${OPTARG}" >&2
usage
exit 1
;;
:)
echo "Option -${OPTARG} requires an argument." >&2
usage
exit 1
;;
h)
usage
exit 0
;;
esac
done
if [ -z "${ARTIFACT_BUCKET}" ] \
|| [ -z "${KEY_NAME}" ] \
|| [ -z "${INSTANCE_PROFILE}" ] \
|| [ -z "${SOURCE_BUCKET}" ]; then
usage
exit 1
fi
DIRTY_WARNING=$(cat <<EOW
***ERROR***
You currently have uncommitted or unstaged changes in your git repository.
Please commit, stash, or remove all uncommitted or unstaged files before
running this script.
EOW
)
if [[ -n "$(git status --porcelain)" ]]; then
echo "$DIRTY_WARNING"
exit 1
fi
commit=$(git rev-parse HEAD)
echo "Packaging ${commit} for staging..."
cli_dir=$(pwd)
tmpdir=$(mktemp -d)
# Fresh clone to ensure our build doesn't rely on anything outside of vcs
git clone --quiet "${cli_dir}" "${tmpdir}"
cd "${tmpdir}"
s3_source_prefix="s3://${SOURCE_BUCKET}/$(hostname)-${RANDOM}"
s3_source_gz="${s3_source_prefix}/${commit}.tgz"
source_gz=$(mktemp)
tar -cz -f "${source_gz}" "."
echo "Uploading ${source_gz} to ${s3_source_gz}..."
S3_ACL_OVERRIDE="private"
dexec s3_cp "${source_gz}" "${s3_source_gz}"
rm "${source_gz}"
cd "${cli_dir}"
rm -rf "${tmpdir}"
ec2_work_root="/var/lib/ec2-stage"
ec2_gopath="${ec2_work_root}/go"
ec2_build_root="${ec2_gopath}/src/github.com/aws"
userdata=$(cat << EOU
#!/bin/bash
mkdir -p ${ec2_work_root}
exec > >(tee -a ${ec2_work_root}/userdata.log)
yum -y install docker golang git
service docker start
aws s3 cp "${s3_source_gz}" "${ec2_work_root}/source.tgz"
mkdir -p ${ec2_build_root}
export GOPATH=${ec2_gopath}
cd ${ec2_build_root}
mkdir -p amazon-ecs-cli
cd amazon-ecs-cli
tar -xvz -f "${ec2_work_root}/source.tgz"
git checkout "${commit}"
scripts/stage.sh -b ${ARTIFACT_BUCKET} -d false
aws s3 cp "${ec2_work_root}/userdata.log" "${s3_source_prefix}/userdata.log"
shutdown -hP now
EOU
)
encoded_user_data=$(base64 <<< "${userdata}")
profile=""
if [[ -n "${AWS_PROFILE}" ]]; then
profile="--profile=${AWS_PROFILE}"
fi
echo "Starting an EC2 instance with the following user-data:"
echo "======================================================"
echo "${userdata}"
echo "======================================================"
echo
ec2_instance_id=$(dexec aws ${profile} "--region=${AWS_REGION}" \
ec2 run-instances \
"--count=1" \
"--image-id=${AMI_ID}" \
"--key-name=${KEY_NAME}" \
"--security-groups=${SECURITY_GROUP}" \
"--user-data=${encoded_user_data}" \
"--instance-type=t2.medium" \
"--instance-initiated-shutdown-behavior=terminate" \
"--iam-instance-profile=Name=${INSTANCE_PROFILE}" \
"--query=Instances[0].InstanceId" \
"--output=text" )
if ${DRYRUN} ; then
ec2_instance_id="dryrun-ec2-instance-id"
fi
echo "Waiting for ${ec2_instance_id} to start..."
dexec aws ${profile} "--region=${AWS_REGION}" \
"ec2" "wait" "instance-running" "--instance-ids=${ec2_instance_id}"
echo "Waiting for ${ec2_instance_id} to terminate..."
dexec aws ${profile} "--region=${AWS_REGION}" \
"ec2" "wait" "instance-terminated" "--instance-ids=${ec2_instance_id}"
log=$(mktemp)
dexec s3_cp "${s3_source_prefix}/userdata.log" "${log}"
cat "${log}"
rm "${log}"