2016-07-12 01:26:07 +02:00
|
|
|
#!/usr/bin/env bash
|
2022-01-12 22:30:09 +01:00
|
|
|
set -eu
|
|
|
|
set -o pipefail
|
2016-07-12 01:26:07 +02:00
|
|
|
|
2022-06-23 21:14:47 +02:00
|
|
|
usage() {
|
|
|
|
cat <<EOF
|
2022-06-23 21:15:40 +02:00
|
|
|
USAGE: $0 [--roles=roles] [--branch=main] [--debug-key=username] server
|
2022-06-23 21:14:47 +02:00
|
|
|
|
|
|
|
Installs an empty Ubuntu server in AWS with a Zulip server role.
|
|
|
|
|
|
|
|
* server is the local part of the hostname (e.g. postgres0)
|
|
|
|
|
|
|
|
* roles is a comma-separated list of Puppet profile names; these
|
2024-02-06 21:40:19 +01:00
|
|
|
will get 'kandra::profile::' prepended to them, and passed
|
2022-06-23 21:14:47 +02:00
|
|
|
to scripts/lib/install -- e.g. 'postgresql'
|
|
|
|
* branch is used to override the default branch to install from.
|
2022-06-23 21:15:40 +02:00
|
|
|
* username is the name of the AWS SSH key pair to allow logins as
|
|
|
|
'ubuntu' with during initial boot; this is purely for debugging the
|
|
|
|
bootstrapping process.
|
2022-06-23 21:14:47 +02:00
|
|
|
|
|
|
|
Reads configuration from $HOME/.zulip-install-server.conf, which should look like:
|
|
|
|
|
|
|
|
[repo]
|
|
|
|
repo_url=git@github.com:zulip/zulip.git
|
|
|
|
[aws]
|
|
|
|
zone_id=Z2U988IEXAMPLE
|
|
|
|
security_groups=sg-01234567
|
|
|
|
instance_type=m4.large
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2023-01-04 21:08:35 +01:00
|
|
|
args="$(getopt -o '' --long help,branch:,roles:,debug-key: -n "$0" -- "$@")" || {
|
|
|
|
usage >&2
|
|
|
|
exit 1
|
|
|
|
}
|
2022-06-23 21:14:47 +02:00
|
|
|
eval "set -- $args"
|
|
|
|
|
|
|
|
BRANCH="main"
|
|
|
|
ROLES="base"
|
2022-06-23 21:15:40 +02:00
|
|
|
DEBUG_KEY=""
|
2022-06-23 21:14:47 +02:00
|
|
|
while true; do
|
|
|
|
case "$1" in
|
|
|
|
--help)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
--roles)
|
|
|
|
shift
|
|
|
|
ROLES="$1"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--branch)
|
|
|
|
shift
|
|
|
|
BRANCH="$1"
|
|
|
|
shift
|
|
|
|
;;
|
2022-06-23 21:15:40 +02:00
|
|
|
--debug-key)
|
|
|
|
shift
|
|
|
|
DEBUG_KEY="$1"
|
|
|
|
shift
|
|
|
|
;;
|
2022-06-23 21:14:47 +02:00
|
|
|
--)
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ $# -ne 1 ]; then
|
2023-01-04 21:08:35 +01:00
|
|
|
usage >&2
|
2016-07-12 01:26:07 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
2022-06-23 21:14:47 +02:00
|
|
|
|
|
|
|
SERVER="$1"
|
|
|
|
|
2016-07-21 06:33:16 +02:00
|
|
|
set -x
|
2016-07-12 01:26:07 +02:00
|
|
|
|
2024-01-31 18:04:18 +01:00
|
|
|
cd "$(dirname "$0")/../.."
|
2016-07-12 01:26:07 +02:00
|
|
|
|
2024-02-06 21:40:19 +01:00
|
|
|
./puppet/kandra/files/install-aws-cli
|
2024-01-31 18:06:23 +01:00
|
|
|
AWS=/srv/zulip-aws-tools/bin/aws
|
2016-07-12 01:26:07 +02:00
|
|
|
|
2020-07-15 20:54:57 +02:00
|
|
|
zulip_install_config_file="$HOME/.zulip-install-server.conf"
|
|
|
|
if [ ! -f "$zulip_install_config_file" ]; then
|
|
|
|
echo "No configuration file found in $zulip_install_config_file"
|
2016-07-21 06:33:36 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
2021-05-18 19:58:45 +02:00
|
|
|
|
2020-07-15 20:54:57 +02:00
|
|
|
REPO_URL=$(crudini --get "$zulip_install_config_file" repo repo_url)
|
2016-07-12 01:26:07 +02:00
|
|
|
|
2022-06-23 21:00:40 +02:00
|
|
|
for role in ${ROLES//,/ }; do
|
2024-02-06 21:40:19 +01:00
|
|
|
if ! [ -f "./puppet/kandra/manifests/profile/$role.pp" ]; then
|
|
|
|
echo "No such role kandra::profile::$role !"
|
2022-06-23 21:00:40 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
2024-02-06 21:40:19 +01:00
|
|
|
FULL_ROLES=$(echo "$ROLES" | perl -pe '$_=join(",",map{"kandra::profile::$_"} split ",")')
|
2022-06-23 21:00:40 +02:00
|
|
|
|
2021-05-18 19:58:45 +02:00
|
|
|
function lookup() {
|
|
|
|
KEY="$1"
|
|
|
|
crudini --get "$zulip_install_config_file" "aws-$ROLES" "$KEY" 2>/dev/null \
|
|
|
|
|| crudini --get "$zulip_install_config_file" aws "$KEY"
|
|
|
|
}
|
|
|
|
|
|
|
|
AWS_ZONE_ID=$(lookup zone_id)
|
|
|
|
SECURITY_GROUPS=$(lookup security_groups)
|
|
|
|
INSTANCE_TYPE=$(lookup instance_type)
|
2022-01-12 22:23:42 +01:00
|
|
|
IAM_PROFILE=$(lookup iam_profile)
|
2022-01-12 22:26:15 +01:00
|
|
|
AZ=$(lookup availability_zone)
|
2022-01-12 22:29:10 +01:00
|
|
|
DISK_SIZE=$(lookup disk_size)
|
2020-07-15 20:54:57 +02:00
|
|
|
|
2024-01-29 21:53:39 +01:00
|
|
|
# Determine the architecture
|
|
|
|
ARCH=$($AWS ec2 describe-instance-types --instance-types "$INSTANCE_TYPE" --query 'InstanceTypes[*].ProcessorInfo.SupportedArchitectures[0]' --output text)
|
|
|
|
|
|
|
|
# Lookup the latest 22.04 image
|
|
|
|
AMI_ID=$($AWS ec2 describe-images --owners 099720109477 --filters 'Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-*-22.04*' "Name=architecture,Values=$ARCH" --query 'sort_by(Images, &CreationDate)[-1].ImageId' --output text)
|
|
|
|
|
2020-07-15 20:54:57 +02:00
|
|
|
# Verify it doesn't exist already
|
2020-10-15 04:55:57 +02:00
|
|
|
ZONE_NAME=$($AWS route53 get-hosted-zone --id "$AWS_ZONE_ID" | jq -r '.HostedZone.Name')
|
|
|
|
HOSTNAME="$SERVER.${ZONE_NAME%?}" # Remove trailing .
|
2020-07-15 20:54:57 +02:00
|
|
|
EXISTING_RECORDS=$($AWS route53 list-resource-record-sets \
|
2020-10-15 04:55:57 +02:00
|
|
|
--hosted-zone-id "$AWS_ZONE_ID" \
|
|
|
|
--query "ResourceRecordSets[?Name == '$HOSTNAME.']" \
|
|
|
|
| jq '. | length')
|
2020-07-15 20:54:57 +02:00
|
|
|
if [ "$EXISTING_RECORDS" != "0" ]; then
|
|
|
|
echo "$HOSTNAME already exists!"
|
|
|
|
exit 1
|
2017-10-06 06:57:18 +02:00
|
|
|
fi
|
2016-07-12 01:26:07 +02:00
|
|
|
|
2022-01-12 22:30:33 +01:00
|
|
|
# https://docs.aws.amazon.com/cli/latest/reference/ec2/run-instances.html
|
2022-01-12 22:24:39 +01:00
|
|
|
# shellcheck disable=SC2206 # We intentionally split $SECURITY_GROUPS
|
2022-01-12 22:20:56 +01:00
|
|
|
EXTRA_ARGS+=(
|
2022-01-12 22:23:42 +01:00
|
|
|
--iam-instance-profile "Name=\"$IAM_PROFILE\""
|
2022-01-12 22:20:56 +01:00
|
|
|
--image-id "$AMI_ID"
|
|
|
|
--instance-type "$INSTANCE_TYPE"
|
2022-01-12 22:24:39 +01:00
|
|
|
--security-group-ids $SECURITY_GROUPS
|
2022-01-12 22:20:56 +01:00
|
|
|
--monitoring Enabled=true
|
2022-01-12 22:26:15 +01:00
|
|
|
--placement "AvailabilityZone=$AZ"
|
2022-01-12 22:29:10 +01:00
|
|
|
--block-device-mappings "DeviceName=/dev/sda1,Ebs={VolumeSize=$DISK_SIZE,VolumeType=gp3,Throughput=125,Iops=3000,Encrypted=true}"
|
2024-01-31 04:37:29 +01:00
|
|
|
--metadata-options "InstanceMetadataTags=enabled"
|
2022-01-12 22:20:56 +01:00
|
|
|
)
|
2021-05-18 21:31:30 +02:00
|
|
|
|
2022-06-23 21:15:40 +02:00
|
|
|
if [ -n "$DEBUG_KEY" ]; then
|
|
|
|
EXTRA_ARGS+=(
|
|
|
|
--key-name "$DEBUG_KEY"
|
|
|
|
)
|
|
|
|
fi
|
|
|
|
|
2020-07-15 20:54:57 +02:00
|
|
|
# Build up the provisioning script
|
|
|
|
BOOTDATA=$(mktemp)
|
|
|
|
{
|
|
|
|
echo "#!/bin/bash"
|
|
|
|
echo "SERVER=$SERVER"
|
|
|
|
echo "HOSTNAME=$HOSTNAME"
|
2022-06-23 21:00:40 +02:00
|
|
|
echo "FULL_ROLES=$FULL_ROLES"
|
2020-07-15 20:54:57 +02:00
|
|
|
echo "REPO_URL=$REPO_URL"
|
|
|
|
echo "BRANCH=$BRANCH"
|
2024-01-31 18:06:23 +01:00
|
|
|
# Replace anything which looks like FOO="inline!bar/baz" with the
|
|
|
|
# output of pack-local-script, which will make "$FOO" inside the
|
|
|
|
# $BOOTDATA be the path to that script (smuggled inline and
|
|
|
|
# unpacked before use).
|
|
|
|
perl -ple 's|^(\w+)="inline!([^"]+)"|qx(./tools/setup/pack-local-script $1 $2)|e' ./tools/setup/bootstrap-aws-installer
|
2020-10-15 04:55:57 +02:00
|
|
|
} >>"$BOOTDATA"
|
2020-07-15 20:54:57 +02:00
|
|
|
|
2022-06-23 21:00:40 +02:00
|
|
|
TAG_ROLE_NAMES="$ROLES"
|
2021-05-18 05:37:39 +02:00
|
|
|
TAGS="[{Key=Name,Value=$SERVER},{Key=role,Value=\"$TAG_ROLE_NAMES\"}]"
|
2020-07-15 20:54:57 +02:00
|
|
|
INSTANCE_DATA=$($AWS ec2 run-instances \
|
2020-10-15 04:55:57 +02:00
|
|
|
--tag-specifications "ResourceType=instance,Tags=$TAGS" \
|
2021-05-18 21:31:30 +02:00
|
|
|
"${EXTRA_ARGS[@]}" \
|
2020-10-15 04:55:57 +02:00
|
|
|
--user-data "file://$BOOTDATA")
|
2020-07-15 20:54:57 +02:00
|
|
|
INSTANCEID=$(echo "$INSTANCE_DATA" | jq -r .Instances[0].InstanceId)
|
|
|
|
|
|
|
|
# Wait for public IP assignment
|
|
|
|
PUBLIC_DNS_NAME=""
|
|
|
|
while [ -z "$PUBLIC_DNS_NAME" ]; do
|
|
|
|
sleep 1
|
|
|
|
PUBLIC_DNS_NAME=$($AWS ec2 describe-instances --instance-ids "$INSTANCEID" \
|
2020-10-15 04:55:57 +02:00
|
|
|
| jq -r .Reservations[0].Instances[0].PublicDnsName)
|
2020-07-15 20:54:57 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
# Add the hostname to the zone
|
|
|
|
ROUTE53_CHANGES=$(mktemp)
|
2020-10-15 04:55:57 +02:00
|
|
|
cat >"$ROUTE53_CHANGES" <<EOF
|
2020-07-15 20:54:57 +02:00
|
|
|
{
|
|
|
|
"Comment": "Add the $HOSTNAME CNAME record",
|
|
|
|
"Changes": [
|
|
|
|
{
|
|
|
|
"Action": "CREATE",
|
|
|
|
"ResourceRecordSet": {
|
|
|
|
"Name": "$HOSTNAME",
|
|
|
|
"Type": "CNAME",
|
|
|
|
"TTL": 300,
|
|
|
|
"ResourceRecords": [{"Value": "$PUBLIC_DNS_NAME"}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2016-07-12 01:26:07 +02:00
|
|
|
EOF
|
2020-07-15 20:54:57 +02:00
|
|
|
$AWS route53 change-resource-record-sets --hosted-zone-id "$AWS_ZONE_ID" --change-batch "file://$ROUTE53_CHANGES"
|
|
|
|
rm "$ROUTE53_CHANGES"
|
2016-07-12 01:26:07 +02:00
|
|
|
|
|
|
|
set +x
|
2020-07-15 20:54:57 +02:00
|
|
|
echo
|
|
|
|
echo
|
|
|
|
echo ">>> Install started successfully! Provisioning takes 5-6min."
|
|
|
|
echo " sleep 360 && ssh root@$HOSTNAME"
|