github.com/GoogleCloudPlatform/testgrid@v0.0.174/cluster/deploy.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2020 The Kubernetes Authors.
     3  #
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  #
     8  #     http://www.apache.org/licenses/LICENSE-2.0
     9  #
    10  # Unless required by applicable law or agreed to in writing, software
    11  # distributed under the License is distributed on an "AS IS" BASIS,
    12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  
    16  # deploy.sh will
    17  # * optionally activate GOOGLE_APPLICATION_CREDENTIALS and configure-docker if set
    18  # * ensure the kubectl context exists
    19  # * run //cluster:prod.apply to update to the specified version
    20  
    21  set -o errexit
    22  set -o nounset
    23  set -o pipefail
    24  
    25  # set USE_GKE_CLOUD_AUTH_PLUGIN to True to fix gcp auth plugin warnings
    26  export USE_GKE_GCLOUD_AUTH_PLUGIN=True
    27  
    28  if [[ -n "${GOOGLE_APPLICATION_CREDENTIALS:-}" ]]; then
    29    echo "Detected GOOGLE_APPLICATION_CREDENTIALS, activating..." >&2
    30    gcloud auth activate-service-account --key-file="$GOOGLE_APPLICATION_CREDENTIALS"
    31  fi
    32  
    33  case "${1:-}" in
    34  "--confirm")
    35    shift
    36    ;;
    37  "")
    38    read -p "Deploy testgrid to prod [no]: " confirm
    39    if [[ "${confirm}" != y* ]]; then
    40      echo "ABORTING" >&2
    41      exit 1
    42    fi
    43    ;;
    44  *)
    45    echo "Usage: $(basename "$0") [--confirm [target]]"
    46    exit 1
    47  esac
    48  
    49  bazel=$(command -v bazelisk 2>/dev/null || command -v bazel)
    50  
    51  # See https://misc.flogisoft.com/bash/tip_colors_and_formatting
    52  
    53  color-green() { # Green
    54    echo -e "\x1B[1;32m${@}\x1B[0m"
    55  }
    56  
    57  color-context() { # Bold blue
    58    echo -e "\x1B[1;34m${@}\x1B[0m"
    59  }
    60  
    61  color-missing() { # Yellow
    62    echo -e "\x1B[1;33m${@}\x1B[0m"
    63  }
    64  
    65  ensure-context() {
    66    local proj=$1
    67    local zone=$2
    68    local cluster=$3
    69    local context="gke_${proj}_${zone}_${cluster}"
    70    echo -n " $(color-context "$context")"
    71    kubectl config get-contexts "$context" &> /dev/null && return 0
    72    echo ": $(color-missing MISSING), getting credentials..."
    73    gcloud container clusters get-credentials --project="$proj" --zone="$zone" "$cluster"
    74    kubectl config get-contexts "$context" > /dev/null
    75    echo -n "Ensuring contexts exist:"
    76  }
    77  
    78  echo -n "Ensuring contexts exist:"
    79  current_context=$(kubectl config current-context 2>/dev/null || true)
    80  restore-context() {
    81    if [[ -n "$current_context" ]]; then
    82      kubectl config set-context "$current_context"
    83    fi
    84  }
    85  trap restore-context EXIT
    86  ensure-context k8s-testgrid us-central1 auto
    87  echo " $(color-green done), Deploying testgrid..."
    88  for s in {5..1}; do
    89      echo -n $'\r'"in $s..."
    90      sleep 1s
    91  done
    92  if [[ "$#" == 0 ]]; then
    93    WHAT=("//cluster/prod:prod.apply")
    94  else
    95    WHAT=("$@")
    96  fi
    97  "$bazel" run --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 "${WHAT[@]}"
    98  echo "$(color-green SUCCESS)"