solana/net/gce.sh

409 lines
10 KiB
Bash
Raw Normal View History

#!/bin/bash -e
here=$(dirname "$0")
# shellcheck source=net/common.sh
source "$here"/common.sh
2018-09-16 14:46:08 -07:00
cloudProvider=$(basename "$0" .sh)
bootDiskType=""
2018-09-16 14:46:08 -07:00
case $cloudProvider in
gce)
# shellcheck source=net/scripts/gce-provider.sh
source "$here"/scripts/gce-provider.sh
imageName="ubuntu-16-04-cuda-9-2-new"
2018-09-17 08:25:10 -07:00
cpuLeaderMachineType=n1-standard-16
gpuLeaderMachineType="$cpuLeaderMachineType --accelerator count=4,type=nvidia-tesla-k80"
leaderMachineType=$cpuLeaderMachineType
2018-09-16 14:46:08 -07:00
validatorMachineType=n1-standard-4
clientMachineType=n1-standard-16
;;
ec2)
# shellcheck source=net/scripts/ec2-provider.sh
source "$here"/scripts/ec2-provider.sh
2018-09-17 08:25:10 -07:00
imageName="ami-0466e26ccc0e752c1"
cpuLeaderMachineType=m4.4xlarge
gpuLeaderMachineType=p2.xlarge
leaderMachineType=$cpuLeaderMachineType
2018-09-16 14:46:08 -07:00
validatorMachineType=m4.xlarge
clientMachineType=m4.4xlarge
;;
*)
echo "Error: Unknown cloud provider: $cloudProvider"
;;
esac
prefix=testnet-dev-${USER//[^A-Za-z0-9]/}
validatorNodeCount=5
clientNodeCount=1
2018-09-16 14:46:08 -07:00
leaderBootDiskSizeInGb=1000
validatorBootDiskSizeInGb=$leaderBootDiskSizeInGb
2018-09-17 08:25:10 -07:00
clientBootDiskSizeInGb=75
2018-09-16 14:46:08 -07:00
publicNetwork=false
2018-09-16 14:46:08 -07:00
enableGpu=false
leaderAddress=
usage() {
exitcode=0
if [[ -n "$1" ]]; then
exitcode=1
echo "Error: $*"
fi
cat <<EOF
usage: $0 [create|config|delete] [common options] [command-specific options]
2018-09-16 14:46:08 -07:00
Manage testnet instances
create - create a new testnet (implies 'config')
config - configure the testnet and write a config file describing it
delete - delete the testnet
common options:
2018-09-06 10:08:34 -07:00
-p [prefix] - Optional common prefix for instance names to avoid
collisions (default: $prefix)
create-specific options:
-n [number] - Number of validator nodes (default: $validatorNodeCount)
-c [number] - Number of client nodes (default: $clientNodeCount)
-P - Use public network IP addresses (default: $publicNetwork)
2018-09-16 14:46:08 -07:00
-z [zone] - Zone for the nodes (default: $zone)
-g - Enable GPU (default: $enableGpu)
-a [address] - Set the leader node's external IP address to this value.
For GCE, [address] is the "name" of the desired External
IP Address.
For EC2, [address] is the "allocation ID" of the desired
Elastic IP.
-d [disk-type] - Specify a boot disk type (default None) Use pd-ssd to get ssd on GCE.
config-specific options:
none
delete-specific options:
2018-09-07 08:57:14 -07:00
none
EOF
exit $exitcode
}
command=$1
[[ -n $command ]] || usage
shift
[[ $command = create || $command = config || $command = delete ]] || usage "Invalid command: $command"
while getopts "h?p:Pn:c:z:ga:d:" opt; do
case $opt in
h | \?)
usage
;;
p)
2018-09-06 10:08:34 -07:00
[[ ${OPTARG//[^A-Za-z0-9-]/} == "$OPTARG" ]] || usage "Invalid prefix: \"$OPTARG\", alphanumeric only"
prefix=$OPTARG
;;
P)
publicNetwork=true
;;
n)
validatorNodeCount=$OPTARG
;;
c)
clientNodeCount=$OPTARG
;;
z)
2018-09-16 14:46:08 -07:00
cloud_SetZone "$OPTARG"
;;
2018-09-04 08:17:41 -07:00
g)
2018-09-16 14:46:08 -07:00
enableGpu=true
2018-09-17 08:25:10 -07:00
leaderMachineType="$gpuLeaderMachineType"
2018-09-04 08:17:41 -07:00
;;
a)
leaderAddress=$OPTARG
;;
d)
bootDiskType=$OPTARG
;;
*)
usage "Error: unhandled option: $opt"
;;
esac
done
2018-09-06 20:57:05 -07:00
shift $((OPTIND - 1))
2018-09-06 20:57:05 -07:00
[[ -z $1 ]] || usage "Unexpected argument: $1"
sshPrivateKey="$netConfigDir/id_$prefix"
2018-09-16 14:46:08 -07:00
# cloud_ForEachInstance [cmd] [extra args to cmd]
#
# Execute a command for each element in the `instances` array
#
# cmd - The command to execute on each instance
# The command will receive arguments followed by any
# additionl arguments supplied to cloud_ForEachInstance:
# name - name of the instance
# publicIp - The public IP address of this instance
# privateIp - The priate IP address of this instance
# count - Monotonically increasing count for each
# invocation of cmd, starting at 1
# ... - Extra args to cmd..
#
#
cloud_ForEachInstance() {
declare cmd="$1"
shift
[[ -n $cmd ]] || { echo cloud_ForEachInstance: cmd not specified; exit 1; }
declare count=1
for info in "${instances[@]}"; do
declare name publicIp privateIp
IFS=: read -r name publicIp privateIp < <(echo "$info")
eval "$cmd" "$name" "$publicIp" "$privateIp" "$count" "$@"
count=$((count + 1))
done
}
2018-09-03 21:15:55 -07:00
prepareInstancesAndWriteConfigFile() {
2018-09-06 13:00:01 -07:00
$metricsWriteDatapoint "testnet-deploy net-config-begin=1"
cat >> "$configFile" <<EOF
# autogenerated at $(date)
netBasename=$prefix
publicNetwork=$publicNetwork
sshPrivateKey=$sshPrivateKey
EOF
2018-09-03 21:15:55 -07:00
buildSshOptions
recordInstanceIp() {
declare name="$1"
2018-09-16 14:46:08 -07:00
declare publicIp="$2"
declare privateIp="$3"
2018-09-16 14:46:08 -07:00
declare arrayName="$5"
echo "$arrayName+=($publicIp) # $name" >> "$configFile"
if [[ $arrayName = "leaderIp" ]]; then
if $publicNetwork; then
echo "entrypointIp=$publicIp" >> "$configFile"
else
echo "entrypointIp=$privateIp" >> "$configFile"
fi
fi
}
waitForStartupComplete() {
declare name="$1"
2018-09-16 14:46:08 -07:00
declare publicIp="$2"
echo "Waiting for $name to finish booting..."
(
for i in $(seq 1 30); do
2018-09-16 14:46:08 -07:00
if (set -x; ssh "${sshOptions[@]}" "$publicIp" "test -f /.instance-startup-complete"); then
break
fi
sleep 2
echo "Retry $i..."
done
)
2018-09-16 14:46:08 -07:00
echo "$name has booted."
}
echo "Looking for leader instance..."
2018-09-16 14:46:08 -07:00
cloud_FindInstance "$prefix-leader"
[[ ${#instances[@]} -eq 1 ]] || {
echo "Unable to find leader"
exit 1
}
2018-09-03 21:15:55 -07:00
(
declare leaderName
declare leaderIp
2018-09-16 14:46:08 -07:00
IFS=: read -r leaderName leaderIp _ < <(echo "${instances[0]}")
2018-09-16 14:46:08 -07:00
# Try to ping the machine first.
timeout 60s bash -c "set -o pipefail; until ping -c 3 $leaderIp | tr - _; do echo .; done"
2018-09-16 14:46:08 -07:00
if [[ ! -r $sshPrivateKey ]]; then
echo "Fetching $sshPrivateKey from $leaderName"
2018-09-16 14:46:08 -07:00
# Try to scp in a couple times, sshd may not yet be up even though the
# machine can be pinged...
set -x -o pipefail
for i in $(seq 1 30); do
if cloud_FetchFile "$leaderName" "$leaderIp" /solana-id_ecdsa "$sshPrivateKey"; then
break
fi
sleep 1
echo "Retry $i..."
done
2018-09-16 14:46:08 -07:00
chmod 400 "$sshPrivateKey"
ls -l "$sshPrivateKey"
fi
)
echo "leaderIp=()" >> "$configFile"
2018-09-16 14:46:08 -07:00
cloud_ForEachInstance recordInstanceIp leaderIp
cloud_ForEachInstance waitForStartupComplete
echo "Looking for validator instances..."
2018-09-16 14:46:08 -07:00
cloud_FindInstances "$prefix-validator"
[[ ${#instances[@]} -gt 0 ]] || {
echo "Unable to find validators"
exit 1
}
echo "validatorIpList=()" >> "$configFile"
2018-09-16 14:46:08 -07:00
cloud_ForEachInstance recordInstanceIp validatorIpList
cloud_ForEachInstance waitForStartupComplete
echo "clientIpList=()" >> "$configFile"
echo "Looking for client instances..."
2018-09-16 14:46:08 -07:00
cloud_FindInstances "$prefix-client"
[[ ${#instances[@]} -eq 0 ]] || {
2018-09-16 14:46:08 -07:00
cloud_ForEachInstance recordInstanceIp clientIpList
cloud_ForEachInstance waitForStartupComplete
}
echo "Wrote $configFile"
2018-09-06 12:14:04 -07:00
$metricsWriteDatapoint "testnet-deploy net-config-complete=1"
}
2018-09-16 14:46:08 -07:00
delete() {
2018-09-06 13:00:01 -07:00
$metricsWriteDatapoint "testnet-deploy net-delete-begin=1"
2018-09-06 12:14:04 -07:00
2018-09-07 08:56:43 -07:00
# Delete the leader node first to prevent unusual metrics on the dashboard
# during shutdown.
# TODO: It would be better to fully cut-off metrics reporting before any
# instances are deleted.
2018-09-16 14:46:08 -07:00
for filter in "$prefix-leader" "$prefix-"; do
echo "Searching for instances: $filter"
cloud_FindInstances "$filter"
2018-09-07 08:56:43 -07:00
if [[ ${#instances[@]} -eq 0 ]]; then
echo "No instances found matching '$filter'"
else
2018-09-16 14:46:08 -07:00
cloud_DeleteInstances true
2018-09-07 08:56:43 -07:00
fi
done
2018-09-04 09:21:03 -07:00
rm -f "$configFile"
2018-09-06 12:14:04 -07:00
$metricsWriteDatapoint "testnet-deploy net-delete-complete=1"
2018-09-16 14:46:08 -07:00
}
case $command in
delete)
delete
;;
create)
[[ -n $validatorNodeCount ]] || usage "Need number of nodes"
2018-09-16 14:46:08 -07:00
if [[ $validatorNodeCount -le 0 ]]; then
usage "One or more validator nodes is required"
fi
delete
2018-09-06 13:00:01 -07:00
$metricsWriteDatapoint "testnet-deploy net-create-begin=1"
2018-09-06 12:14:04 -07:00
rm -rf "$sshPrivateKey"{,.pub}
2018-09-16 14:46:08 -07:00
# Note: using rsa because |aws ec2 import-key-pair| seems to fail for ecdsa
ssh-keygen -t rsa -N '' -f "$sshPrivateKey"
printNetworkInfo() {
cat <<EOF
========================================================================================
Network composition:
2018-09-16 14:46:08 -07:00
Leader = $leaderMachineType (GPU=$enableGpu)
Validators = $validatorNodeCount x $validatorMachineType
Client(s) = $clientNodeCount x $clientMachineType
========================================================================================
EOF
}
printNetworkInfo
2018-09-16 14:46:08 -07:00
declare startupScript="$netConfigDir"/instance-startup-script.sh
cat > "$startupScript" <<EOF
#!/bin/bash -ex
# autogenerated at $(date)
cat > /etc/motd <<EOM
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
This instance has not been fully configured.
2018-09-16 14:46:08 -07:00
See startup script log messages in /var/log/syslog for status:
$ sudo cat /var/log/syslog | egrep \\(startup-script\\|cloud-init\)
To block until setup is complete, run:
2018-09-16 14:46:08 -07:00
$ until [[ -f /.instance-startup-complete ]]; do sleep 1; done
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
EOM
# Place the generated private key at /solana-id_ecdsa so it's retrievable by anybody
# who is able to log into this machine
cat > /solana-id_ecdsa <<EOK
$(cat "$sshPrivateKey")
EOK
cat > /solana-id_ecdsa.pub <<EOK
$(cat "$sshPrivateKey.pub")
EOK
chmod 444 /solana-id_ecdsa
USER=\$(id -un)
$(
cd "$here"/scripts/
cat \
disable-background-upgrades.sh \
create-solana-user.sh \
2018-09-16 14:46:08 -07:00
add-solana-user-authorized_keys.sh \
install-earlyoom.sh \
install-libssl-compatability.sh \
2018-09-12 16:22:22 -07:00
install-rsync.sh \
)
cat > /etc/motd <<EOM
$(printNetworkInfo)
EOM
2018-09-16 14:46:08 -07:00
touch /.instance-startup-complete
EOF
2018-09-16 14:46:08 -07:00
cloud_CreateInstances "$prefix" "$prefix-leader" 1 \
2018-09-17 08:25:10 -07:00
"$imageName" "$leaderMachineType" "$leaderBootDiskSizeInGb" \
"$startupScript" "$leaderAddress" "$bootDiskType"
2018-09-16 14:46:08 -07:00
cloud_CreateInstances "$prefix" "$prefix-validator" "$validatorNodeCount" \
2018-09-17 08:25:10 -07:00
"$imageName" "$validatorMachineType" "$validatorBootDiskSizeInGb" \
"$startupScript" "" "$bootDiskType"
if [[ $clientNodeCount -gt 0 ]]; then
2018-09-16 14:46:08 -07:00
cloud_CreateInstances "$prefix" "$prefix-client" "$clientNodeCount" \
2018-09-17 08:25:10 -07:00
"$imageName" "$clientMachineType" "$clientBootDiskSizeInGb" \
"$startupScript" "" "$bootDiskType"
fi
2018-09-06 12:14:04 -07:00
$metricsWriteDatapoint "testnet-deploy net-create-complete=1"
2018-09-03 21:15:55 -07:00
prepareInstancesAndWriteConfigFile
;;
config)
2018-09-03 21:15:55 -07:00
prepareInstancesAndWriteConfigFile
;;
*)
usage "Unknown command: $command"
esac