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

     1  #!/usr/bin/env bash
     2  #
     3  # Prerequisites:
     4  # - Cockroach Labs employees: ask an admin to create an Azure account for you.
     5  # - Install the Azure XPlat CLI 2.0:
     6  #   https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest
     7  # - Run "az login" and sign in to your Azure account.
     8  
     9  set -euo pipefail
    10  
    11  cd "$(dirname "${0}")/.." && source build/shlib.sh
    12  
    13  LOCATION=${LOCATION-eastus}
    14  MACHINE_SIZE=${MACHINE_SIZE-Standard_F16}
    15  USER=${USER-$(id -un)}
    16  CLUSTER=azworker-${USER}
    17  NAME=${AZWORKER_NAME-${CLUSTER}}
    18  
    19  # Names for various resources just reuse cluster/vm name depending on scope.
    20  RG=${CLUSTER}
    21  NET=${CLUSTER}
    22  SUBNET=${CLUSTER}
    23  NIC=${NAME}
    24  IP=${NAME}
    25  DOMAIN=cockroach-${NAME}
    26  FQDN=${DOMAIN}.${LOCATION}.cloudapp.azure.com
    27  
    28  # TODO(radu): switch to the newer "az" CLI.
    29  
    30  case ${1-} in
    31    create)
    32      if [ $(az group show -n "${RG}" | wc -l) -gt 0 ]; then
    33        echo "Resource group ${RG} already exists; adding worker VM to it"
    34      else
    35        echo "Creating resource group ${RG}"
    36        az group create -n "${RG}" -l "${LOCATION}"
    37        az network vnet create -g "${RG}" -n "${NET}" --address-prefixes 192.168.0.0/16 -l "${LOCATION}"
    38        az network vnet subnet create -g "${RG}" --vnet-name "${NET}" -n "${SUBNET}" --address-prefix 192.168.1.0/24
    39      fi
    40  
    41      az network public-ip create -g "${RG}" -n "${IP}" -l "${LOCATION}" --dns-name "${DOMAIN}"
    42      az network nic create -g "${RG}" -n "${NIC}" -l "${LOCATION}" --public-ip-address "${IP}" --subnet "${SUBNET}" --vnet-name "${NET}"
    43  
    44      az vm create \
    45          --resource-group "${RG}" \
    46          --name "${NAME}" \
    47          --location "${LOCATION}" \
    48          --image canonical:UbuntuServer:16.04-LTS:latest \
    49          --ssh-key-value ~/.ssh/id_rsa.pub \
    50          --admin-username "${USER}" \
    51          --size "${MACHINE_SIZE}" \
    52          --nics "${NIC}"
    53  
    54      # Clear any cached host keys for this hostname and accept the new one.
    55      ssh-keygen -R "${FQDN}"
    56      # Retry while vm and sshd to start up.
    57      retry ssh -o StrictHostKeyChecking=no "${USER}@${FQDN}" true
    58  
    59      rsync -az "build/bootstrap/" "${USER}@${FQDN}:bootstrap/"
    60      rsync -az "build/disable-hyperv-timesync.sh" "${USER}@${FQDN}:bootstrap/"
    61      ssh -A "${USER}@${FQDN}" ./bootstrap/bootstrap-debian.sh
    62      ssh -A "${USER}@${FQDN}" ./bootstrap/disable-hyperv-timesync.sh
    63      ssh -A "${USER}@${FQDN}" ./bootstrap/install-azure-cli.sh
    64  
    65      # Copy the azure config (including credentials!).
    66      ssh -A "${USER}@${FQDN}" "mkdir .azure"
    67      rsync -az ~/.azure/*.json "${USER}@${FQDN}:.azure/"
    68  
    69      # Set up tmux configuration (for persistent SSH).
    70      if [ -e ~/.tmux.conf ]; then
    71        rsync -azL ~/.tmux.conf "${USER}@${FQDN}:./"
    72      else
    73        # Present a color terminal and disable tmux scrollback.
    74        ssh -A "${USER}@${FQDN}" "echo 'set -g default-terminal screen-256color' > .tmux.conf"
    75        ssh -A "${USER}@${FQDN}" "echo 'set -g terminal-overrides \"xterm*:XT:smcup@:rmcup@\"' >> .tmux.conf"
    76      fi
    77  
    78      # shellcheck disable=SC2029
    79      ssh "${USER}@${FQDN}" "echo '* * * * * /home/${USER}/bootstrap/autoshutdown.cron.sh 10 az vm deallocate --resource-group \"${RG}\" --name \"${NAME}\"' | crontab -"
    80  
    81      echo "VM now running at ${FQDN}"
    82      ;;
    83    start)
    84      az vm start -g "${RG}" -n "${NAME}"
    85      ;;
    86    stop)
    87      az vm deallocate -g "${RG}" -n "${NAME}"
    88      ;;
    89    delete|destroy)
    90      # The straightforward thing to do would be to first delete the VM, then
    91      # check if there are any virtual machines left in the group. However, the
    92      # deleted VM doesn't disappear right away from the listing. So we instead
    93      # count the initial number of VMs.
    94      NUM_VMS=$(az vm list -g "${RG}" | (grep -c "\"type\":.*virtualMachines" || true))
    95      az vm delete -g "${RG}" -n "${NAME}"
    96      if [ "$NUM_VMS" -gt 1 ]; then
    97        az network nic delete -g "${RG}" -n "${NIC}"
    98        az network public-ip delete -g "${RG}" -n "${IP}"
    99        echo "Resource group ${RG} still contains VMs; leaving in place"
   100      else
   101        echo "Deleting resource group ${RG}"
   102        az group delete -n "${RG}" --yes
   103      fi
   104      ;;
   105    ssh)
   106      shift
   107      # shellcheck disable=SC2029
   108      ssh -A "${USER}@${FQDN}" "$@"
   109      ;;
   110    sshmux)
   111      shift
   112      if [ -z "${1:-}" ]; then
   113        ssh -A "${USER}@${FQDN}" "tmux list-sessions"
   114      else
   115        # shellcheck disable=SC2029
   116        ssh -A -t "${USER}@${FQDN}" "tmux attach -t $1 || tmux new-session -s $1"
   117      fi
   118      ;;
   119    *)
   120      if [ -n "${1:-}" ]; then
   121        echo "$0: unknown command: ${1-}"
   122      fi
   123      cat <<EOF
   124  Usage:
   125    $0 create
   126       Creates a new azure worker VM.
   127  
   128    $0 start
   129       Powers on an azure worker VM.
   130  
   131    $0 stop
   132       Powers off an azure worker VM.
   133  
   134    $0 delete
   135       Deletes an azure worker VM.
   136  
   137    $0 ssh
   138       SSH into an azure worker VM.
   139  
   140    $0 sshmux <session-name>
   141       Creates or reconnects to a persistent SSH session with the given name.
   142  
   143    $0 sshmux
   144       List persistent SSH sessions.
   145  
   146  For all commands, worker VM name can be customized via AZWORKER_NAME.
   147  EOF
   148      exit 1
   149      ;;
   150  esac