2022-11-13 03:10:45 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
set -eu
|
|
|
|
|
cd -- "$(dirname "$0")/../../.."
|
|
|
|
|
. ./ci/sub/lib.sh
|
|
|
|
|
|
|
|
|
|
help() {
|
|
|
|
|
cat <<EOF
|
2022-11-14 06:59:09 +00:00
|
|
|
usage: $0 [--dry-run]
|
2022-11-13 03:10:45 +00:00
|
|
|
|
2022-11-13 04:39:14 +00:00
|
|
|
$0 creates and ensures the d2 builders in AWS.
|
2022-11-13 03:10:45 +00:00
|
|
|
EOF
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main() {
|
|
|
|
|
while :; do
|
|
|
|
|
flag_parse "$@"
|
|
|
|
|
case "$FLAG" in
|
|
|
|
|
h|help)
|
|
|
|
|
help
|
|
|
|
|
return 0
|
|
|
|
|
;;
|
2022-11-14 06:59:09 +00:00
|
|
|
dry-run)
|
2022-11-14 05:58:55 +00:00
|
|
|
flag_noarg && shift "$FLAGSHIFT"
|
2022-11-14 06:59:09 +00:00
|
|
|
DRY_RUN=1
|
2022-11-13 03:10:45 +00:00
|
|
|
;;
|
|
|
|
|
'')
|
|
|
|
|
shift "$FLAGSHIFT"
|
|
|
|
|
break
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
flag_errusage "unrecognized flag $FLAGRAW"
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
if [ $# -gt 0 ]; then
|
|
|
|
|
flag_errusage "no arguments are accepted"
|
|
|
|
|
fi
|
|
|
|
|
|
2022-11-13 04:39:14 +00:00
|
|
|
create_rhosts
|
|
|
|
|
init_rhosts
|
2022-11-13 03:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-13 04:39:14 +00:00
|
|
|
create_rhosts() {
|
2022-11-13 03:10:45 +00:00
|
|
|
KEY_NAME=$(aws ec2 describe-key-pairs | jq -r .KeyPairs[0].KeyName)
|
|
|
|
|
VPC_ID=$(aws ec2 describe-vpcs | jq -r .Vpcs[0].VpcId)
|
|
|
|
|
|
|
|
|
|
header security-group
|
|
|
|
|
SG_ID=$(aws ec2 describe-security-groups --group-names ssh 2>/dev/null \
|
|
|
|
|
| jq -r .SecurityGroups[0].GroupId)
|
|
|
|
|
if [ -z "$SG_ID" ]; then
|
|
|
|
|
SG_ID=$(sh_c aws ec2 create-security-group \
|
|
|
|
|
--group-name ssh \
|
|
|
|
|
--description ssh \
|
|
|
|
|
--vpc-id "$VPC_ID" | jq -r .GroupId)
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
header security-group-ingress
|
|
|
|
|
SG_RULES_COUNT=$(aws ec2 describe-security-groups --group-names ssh \
|
|
|
|
|
| jq -r '.SecurityGroups[0].IpPermissions | length')
|
|
|
|
|
if [ "$SG_RULES_COUNT" -eq 0 ]; then
|
|
|
|
|
sh_c aws ec2 authorize-security-group-ingress \
|
|
|
|
|
--group-id "$SG_ID" \
|
|
|
|
|
--protocol tcp \
|
|
|
|
|
--port 22 \
|
|
|
|
|
--cidr 0.0.0.0/0 >/dev/null
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
header linux-amd64
|
|
|
|
|
if ! aws ec2 describe-instances \
|
|
|
|
|
"--query=Reservations[*].Instances[?State.Name!='terminated']" \
|
|
|
|
|
| grep -q d2-builder-linux-amd64; then
|
|
|
|
|
sh_c aws ec2 run-instances \
|
|
|
|
|
--image-id=ami-0d593311db5abb72b \
|
|
|
|
|
--count=1 \
|
2022-11-13 04:39:14 +00:00
|
|
|
--instance-type=t2.small \
|
2022-11-13 03:10:45 +00:00
|
|
|
--security-groups=ssh \
|
|
|
|
|
"--key-name=$KEY_NAME" \
|
|
|
|
|
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=d2-builder-linux-amd64}]' \
|
|
|
|
|
'ResourceType=volume,Tags=[{Key=Name,Value=d2-builder-linux-amd64}]' >/dev/null
|
|
|
|
|
fi
|
2022-11-13 03:36:56 +00:00
|
|
|
while true; do
|
|
|
|
|
dnsname=$(sh_c aws ec2 describe-instances \
|
|
|
|
|
--filters 'Name=instance-state-name,Values=pending,running,shutting-down,stopping,stopped' 'Name=tag:Name,Values=d2-builder-linux-amd64' \
|
|
|
|
|
| jq -r '.Reservations[].Instances[].PublicDnsName')
|
|
|
|
|
if [ -n "$dnsname" ]; then
|
2022-11-13 04:39:14 +00:00
|
|
|
log "TSTRUCT_LINUX_AMD64_BUILDER=ec2-user@$dnsname"
|
|
|
|
|
export TSTRUCT_LINUX_AMD64_BUILDER=ec2-user@$dnsname
|
2022-11-13 03:36:56 +00:00
|
|
|
break
|
|
|
|
|
fi
|
|
|
|
|
sleep 5
|
|
|
|
|
done
|
2022-11-13 03:10:45 +00:00
|
|
|
|
|
|
|
|
header linux-arm64
|
|
|
|
|
if ! aws ec2 describe-instances \
|
|
|
|
|
"--query=Reservations[*].Instances[?State.Name!='terminated']" \
|
|
|
|
|
| grep -q d2-builder-linux-arm64; then
|
|
|
|
|
sh_c aws ec2 run-instances \
|
|
|
|
|
--image-id=ami-0efabcf945ffd8831 \
|
|
|
|
|
--count=1 \
|
2022-11-13 04:39:14 +00:00
|
|
|
--instance-type=t4g.small \
|
2022-11-13 03:10:45 +00:00
|
|
|
--security-groups=ssh \
|
|
|
|
|
"--key-name=$KEY_NAME" \
|
|
|
|
|
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=d2-builder-linux-arm64}]' \
|
|
|
|
|
'ResourceType=volume,Tags=[{Key=Name,Value=d2-builder-linux-arm64}]' >/dev/null
|
|
|
|
|
fi
|
2022-11-13 03:36:56 +00:00
|
|
|
while true; do
|
|
|
|
|
dnsname=$(sh_c aws ec2 describe-instances \
|
|
|
|
|
--filters 'Name=instance-state-name,Values=pending,running,shutting-down,stopping,stopped' 'Name=tag:Name,Values=d2-builder-linux-arm64' \
|
|
|
|
|
| jq -r '.Reservations[].Instances[].PublicDnsName')
|
|
|
|
|
if [ -n "$dnsname" ]; then
|
2022-11-13 04:39:14 +00:00
|
|
|
log "TSTRUCT_LINUX_ARM64_BUILDER=ec2-user@$dnsname"
|
|
|
|
|
export TSTRUCT_LINUX_ARM64_BUILDER=ec2-user@$dnsname
|
2022-11-13 03:36:56 +00:00
|
|
|
break
|
|
|
|
|
fi
|
|
|
|
|
sleep 5
|
|
|
|
|
done
|
2022-11-13 03:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-13 04:39:14 +00:00
|
|
|
init_rhosts() {
|
|
|
|
|
header linux-amd64
|
|
|
|
|
RHOST=$TSTRUCT_LINUX_AMD64_BUILDER init_rhost
|
|
|
|
|
header linux-arm64
|
|
|
|
|
RHOST=$TSTRUCT_LINUX_ARM64_BUILDER init_rhost
|
2022-11-13 05:07:11 +00:00
|
|
|
log "export TSTRUCT_LINUX_AMD64_BUILDER=$TSTRUCT_LINUX_AMD64_BUILDER"
|
|
|
|
|
log "export TSTRUCT_LINUX_ARM64_BUILDER=$TSTRUCT_LINUX_ARM64_BUILDER"
|
2022-11-13 04:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init_rhost() {
|
|
|
|
|
while true; do
|
|
|
|
|
if sh_c ssh "$RHOST" :; then
|
|
|
|
|
break
|
|
|
|
|
fi
|
|
|
|
|
sleep 5
|
|
|
|
|
done
|
|
|
|
|
sh_c ssh "$RHOST" 'sudo yum upgrade -y'
|
|
|
|
|
sh_c ssh "$RHOST" 'sudo yum install -y docker'
|
|
|
|
|
sh_c ssh "$RHOST" 'sudo systemctl start docker'
|
|
|
|
|
sh_c ssh "$RHOST" 'sudo systemctl enable docker'
|
|
|
|
|
sh_c ssh "$RHOST" 'sudo usermod -a -G docker ec2-user'
|
|
|
|
|
sh_c ssh "$RHOST" 'sudo reboot' || true
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-13 03:10:45 +00:00
|
|
|
main "$@"
|