#!/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= clientNodeCount= 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" 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 } gcloud_FindInstances "name=$prefix-leader" show [[ ${#instances[@]} -eq 1 ]] || { echo "Unable to start leader" exit 1 } gcloud_FigureRemoteUsername "${instances[0]}" echo "sshUsername=$gcloud_username" >> "$configFile" gcloud_PrepInstancesForSsh "$gcloud_username" "$sshPrivateKey" echo "leaderIp=()" >> "$configFile" gcloud_ForEachInstance recordInstanceIp leaderIp 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" 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 ;; create) [[ -n $validatorNodeCount ]] || usage "Need number of nodes" gcloud_CreateInstances "$prefix-leader" 1 "$zone" "$imageName" gcloud_CreateInstances "$prefix-validator" "$validatorNodeCount" "$zone" "$imageName" if [[ -n $clientNodeCount ]]; then gcloud_CreateInstances "$prefix-client" "$clientNodeCount" "$zone" "$imageName" fi writeConfigFile ;; config) writeConfigFile ;; *) usage "Unknown command: $command" esac