#!/bin/bash -e here=$(dirname "$0") # shellcheck source=scripts/gcloud.sh source "$here"/../scripts/gcloud.sh # shellcheck source=net/common.sh source "$here"/common.sh prefix=testnet-dev-$(whoami | sed -e s/[^a-z0-9].*//) validatorNodeCount=5 clientNodeCount=1 leaderMachineType=n1-standard-16 leaderAccelerator= validatorMachineType=n1-standard-4 validatorAccelerator= clientMachineType=n1-standard-16 clientAccelerator= imageName="ubuntu-16-04-cuda-9-2-new" internalNetwork=false zone="us-west1-b" usage() { exitcode=0 if [[ -n "$1" ]]; then exitcode=1 echo "Error: $*" fi cat <> "$configFile" echo "netBasename=$prefix" >> "$configFile" declare sshPrivateKey="$netConfigDir/id_$prefix" rm -rf "$sshPrivateKey"{,.pub} ( set -x ssh-keygen -t ecdsa -N '' -f "$sshPrivateKey" ) echo "sshPrivateKey=$sshPrivateKey" >> "$configFile" recordInstanceIp() { declare name="$1" declare publicIp="$3" declare privateIp="$4" declare arrayName="$6" if $internalNetwork; then echo "$arrayName+=($privateIp) # $name" >> "$configFile" else echo "$arrayName+=($publicIp) # $name" >> "$configFile" fi } echo "Looking for leader instance..." gcloud_FindInstances "name=$prefix-leader" show [[ ${#instances[@]} -eq 1 ]] || { echo "Unable to start leader" exit 1 } gcloud_FigureRemoteUsername "${instances[0]}" sshUsername=$gcloud_username echo "sshUsername=$sshUsername" >> "$configFile" buildSshOptions gcloud_PrepInstancesForSsh "$gcloud_username" "$sshPrivateKey" echo "leaderIp=()" >> "$configFile" gcloud_ForEachInstance recordInstanceIp leaderIp echo "Looking for validator instances..." gcloud_FindInstances "name~^$prefix-validator" show [[ ${#instances[@]} -gt 0 ]] || { echo "Unable to start validators" exit 1 } echo "validatorIpList=()" >> "$configFile" gcloud_PrepInstancesForSsh "$gcloud_username" "$sshPrivateKey" gcloud_ForEachInstance recordInstanceIp validatorIpList echo "clientIpList=()" >> "$configFile" echo "Looking for client instances..." gcloud_FindInstances "name~^$prefix-client" show if [[ ${#instances[@]} -gt 0 ]]; then gcloud_PrepInstancesForSsh "$gcloud_username" "$sshPrivateKey" gcloud_ForEachInstance recordInstanceIp clientIpList fi echo "Wrote $configFile" } case $command in delete) gcloud_FindInstances "name~^$prefix-" if [[ ${#instances[@]} -eq 0 ]]; then echo "No instances found matching '^$prefix-'" exit 0 fi gcloud_DeleteInstances rm -f "$configFile" ;; create) [[ -n $validatorNodeCount ]] || usage "Need number of nodes" echo "Network composition:" echo "Leader = $leaderMachineType (GPU=${leaderAccelerator:-none})" echo "Validators = $validatorNodeCount x $validatorMachineType (GPU=${validatorAccelerator:-none})" echo "Client(s) = $clientNodeCount x $clientMachineType (GPU=${clientAccelerator:-none})" echo ================================================================== echo gcloud_CreateInstances "$prefix-leader" 1 \ "$zone" "$imageName" "$leaderMachineType" "$leaderAccelerator" "$here/remote/remote-startup.sh" gcloud_CreateInstances "$prefix-validator" "$validatorNodeCount" \ "$zone" "$imageName" "$validatorMachineType" "$validatorAccelerator" "$here/remote/remote-startup.sh" if [[ -n $clientNodeCount ]]; then gcloud_CreateInstances "$prefix-client" "$clientNodeCount" \ "$zone" "$imageName" "$clientMachineType" "$clientAccelerator" "$here/remote/remote-startup.sh" fi prepareInstancesAndWriteConfigFile ;; config) prepareInstancesAndWriteConfigFile ;; *) usage "Unknown command: $command" esac