github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/scripts/gceworker.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  set -euo pipefail
     4  
     5  cd "$(dirname "${0}")/.."
     6  source build/shlib.sh
     7  
     8  export CLOUDSDK_CORE_PROJECT=${CLOUDSDK_CORE_PROJECT-${GCEWORKER_PROJECT-cockroach-workers}}
     9  export CLOUDSDK_COMPUTE_ZONE=${GCEWORKER_ZONE-${CLOUDSDK_COMPUTE_ZONE-us-east1-b}}
    10  NAME=${GCEWORKER_NAME-gceworker-$(id -un)}
    11  
    12  cmd=${1-}
    13  if [[ "${cmd}" ]]; then
    14    shift
    15  fi
    16  
    17  case "${cmd}" in
    18      gcloud)
    19      gcloud "$@"
    20      ;;
    21      create)
    22      if [[ "${COCKROACH_DEV_LICENSE:-}" ]]; then
    23        echo "Using dev license key from \$COCKROACH_DEV_LICENSE"
    24      else
    25        echo -n "Enter your dev license key (if any): "
    26        read COCKROACH_DEV_LICENSE
    27      fi
    28  
    29      gcloud compute instances \
    30             create "${NAME}" \
    31             --machine-type "custom-24-32768" \
    32             --network "default" \
    33             --maintenance-policy "MIGRATE" \
    34             --image-project "ubuntu-os-cloud" \
    35             --image-family "ubuntu-1804-lts" \
    36             --boot-disk-size "100" \
    37             --boot-disk-type "pd-ssd" \
    38             --boot-disk-device-name "${NAME}" \
    39             --scopes "cloud-platform"
    40      gcloud compute firewall-rules create "${NAME}-mosh" --allow udp:60000-61000
    41  
    42      # Retry while vm and sshd start up.
    43      retry gcloud compute ssh "${NAME}" --command=true
    44  
    45      gcloud compute scp --recurse "build/bootstrap" "${NAME}:bootstrap"
    46      gcloud compute ssh "${NAME}" --ssh-flag="-A" --command="./bootstrap/bootstrap-debian.sh"
    47  
    48      if [[ "$COCKROACH_DEV_LICENSE" ]]; then
    49        gcloud compute ssh "${NAME}" --command="echo COCKROACH_DEV_LICENSE=$COCKROACH_DEV_LICENSE >> ~/.bashrc_bootstrap"
    50      fi
    51  
    52      # Install automatic shutdown after ten minutes of operation without a
    53      # logged in user. To disable this, `sudo touch /.active`.
    54      gcloud compute ssh "${NAME}" --command="sudo cp bootstrap/autoshutdown.cron.sh /root/; echo '* * * * * /root/autoshutdown.cron.sh 10' | sudo crontab -i -"
    55  
    56      ;;
    57      start)
    58      gcloud compute instances start "${NAME}"
    59      echo "waiting for node to finish starting..."
    60      # Wait for vm and sshd to start up.
    61      retry gcloud compute ssh "${NAME}" --command=true || true
    62      # SSH into the node, since that's probably why we started it.
    63      gcloud compute ssh "${NAME}" --ssh-flag="-A" "$@"
    64      ;;
    65      stop)
    66      read -r -p "This will stop the VM. Are you sure? [yes] " response
    67      # Convert to lowercase.
    68      response=$(echo "$response" | tr '[:upper:]' '[:lower:]')
    69      if [[ $response != "yes" ]]; then
    70        echo Aborting
    71        exit 1
    72      fi
    73      gcloud compute instances stop "${NAME}"
    74      ;;
    75      delete|destroy)
    76      read -r -p "This will delete the VM! Are you sure? [yes] " response
    77      # Convert to lowercase.
    78      response=$(echo "$response" | tr '[:upper:]' '[:lower:]')
    79      if [[ $response != "yes" ]]; then
    80        echo Aborting
    81        exit 1
    82      fi
    83      status=0
    84      gcloud compute firewall-rules delete "${NAME}-mosh" --quiet || status=$((status+1))
    85      gcloud compute instances delete "${NAME}" --quiet || status=$((status+1))
    86      exit ${status}
    87      ;;
    88      ssh)
    89      gcloud compute ssh "${NAME}" --ssh-flag="-A" "$@"
    90      ;;
    91      mosh)
    92      # An alternative solution would be to run gcloud compute config-ssh after
    93      # starting or creating the vm, which adds stanzas to ~/.ssh/config that
    94      # make `ssh $HOST` (and by extension, hopefully, mosh).
    95      read -r -a arr <<< "$(gcloud compute ssh "${NAME}" --dry-run)"
    96      host="${arr[-1]}"
    97      unset 'arr[${#arr[@]}-1]'
    98      mosh --ssh=$(printf '%q' "${arr}") $host
    99      ;;
   100      scp)
   101      # Example: $0 scp gceworker-youruser:go/src/github.com/cockroachdb/cockroach/cockroach-data/logs gcelogs --recurse
   102      retry gcloud compute scp "$@"
   103      ;;
   104      ip)
   105      gcloud compute instances describe --format="value(networkInterfaces[0].accessConfigs[0].natIP)" "${NAME}"
   106      ;;
   107      sync)
   108      if ! hash unison 2>/dev/null; then
   109        echo 'unison not found (on macOS, run `brew install unison`)' >&2
   110        exit 1
   111      fi
   112      if ! hash unison-fsmonitor 2>/dev/null; then
   113        echo 'unison-fsmonitor not installed (on macOS, run `brew install eugenmayer/dockersync/unox`)'
   114        exit 1
   115      fi
   116      if (( $# == 0 )); then
   117        host=.  # Sync the Cockroach repo by default.
   118        worker=go/src/github.com/cockroachdb/cockroach
   119      elif (( $# == 2 )); then
   120        host=$1
   121        worker=$2
   122      else
   123        echo "usage: $0 mount [HOST-PATH WORKER-PATH]" >&2
   124        exit 1
   125      fi
   126      read -p "Warning: sync will overwrite files on the GCE worker with your local copy. Continue? (Y/n) "
   127      if [[ "$REPLY" && "$REPLY" != [Yy] ]]; then
   128        exit 1
   129      fi
   130      tmpfile=$(mktemp)
   131      trap 'rm -f ${tmpfile}' EXIT
   132      gcloud compute config-ssh --ssh-config-file "$tmpfile" > /dev/null
   133      unison "$host" "ssh://${NAME}.${CLOUDSDK_COMPUTE_ZONE}.${CLOUDSDK_CORE_PROJECT}/$worker" \
   134        -sshargs "-F ${tmpfile}" -auto -prefer "$host" -repeat watch \
   135        -ignore 'Path .git' \
   136        -ignore 'Path bin*' \
   137        -ignore 'Path build/builder_home' \
   138        -ignore 'Path pkg/sql/parser/gen' \
   139        -ignore 'Path pkg/ui/node_modules' \
   140        -ignore 'Path pkg/ui/.cache-loader' \
   141        -ignore 'Path cockroach-data' \
   142        -ignore 'Name *.d' \
   143        -ignore 'Name *.o' \
   144        -ignore 'Name zcgo_flags*.go'
   145      ;;
   146      *)
   147      echo "$0: unknown command: ${cmd}, use one of create, start, stop, delete, ssh, or sync"
   148      exit 1
   149      ;;
   150  esac