#!/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-$(id -un | 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" publicNetwork=false zone="us-west1-b" leaderAddress= usage() { exitcode=0 if [[ -n "$1" ]]; then exitcode=1 echo "Error: $*" fi cat <> "$configFile" <> "$configFile" recordInstanceIp() { declare name="$1" declare publicIp="$3" declare privateIp="$4" declare arrayName="$6" echo "$arrayName+=($publicIp) # $name" >> "$configFile" if [[ $arrayName = "leaderIp" ]]; then if $publicNetwork; then echo "entrypointIp=$publicIp" >> "$configFile" else echo "entrypointIp=$privateIp" >> "$configFile" fi 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 [[ ${#instances[@]} -eq 0 ]] || { gcloud_PrepInstancesForSsh "$gcloud_username" "$sshPrivateKey" gcloud_ForEachInstance recordInstanceIp clientIpList } 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" "$leaderAddress" \ 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