github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/bump.sh (about)

     1  #!/bin/bash
     2  # Copyright 2016 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  # bump.sh will
    17  # * ensure there are no pending changes
    18  # * optionally activate GOOGLE_APPLICATION_CREDENTIALS and configure-docker if set
    19  # * run //prow:release-push to build and push prow images
    20  # * update all the cluster/*.yaml files to use the new image tags
    21  
    22  set -o errexit
    23  set -o nounset
    24  set -o pipefail
    25  
    26  # TODO(fejta): rewrite this in a better language REAL SOON
    27  
    28  # See https://misc.flogisoft.com/bash/tip_colors_and_formatting
    29  
    30  color-image() { # Bold magenta
    31    echo -e "\x1B[1;35m${@}\x1B[0m"
    32  }
    33  
    34  color-version() { # Bold blue
    35    echo -e "\x1B[1;34m${@}\x1B[0m"
    36  }
    37  
    38  color-target() { # Bold cyan
    39    echo -e "\x1B[1;33m${@}\x1B[0m"
    40  }
    41  
    42  # darwin is great
    43  SED=sed
    44  if which gsed &>/dev/null; then
    45    SED=gsed
    46  fi
    47  if ! ($SED --version 2>&1 | grep -q GNU); then
    48    echo "!!! GNU sed is required.  If on OS X, use 'brew install gnu-sed'." >&2
    49    exit 1
    50  fi
    51  
    52  TAC=tac
    53  if which gtac &>/dev/null; then
    54    TAC=gtac
    55  fi
    56  if ! which "$TAC" &>/dev/null; then
    57    echo "tac (reverse cat) required. If on OS X then 'brew install coreutils'." >&2
    58    exit 1
    59  fi
    60  
    61  cd "$(dirname "${BASH_SOURCE}")"
    62  
    63  usage() {
    64    echo "Usage: "$(basename "$0")" [--list || --latest || vYYYYMMDD-deadbeef] [image subset...]" >&2
    65    exit 1
    66  }
    67  
    68  if [[ -n "${GOOGLE_APPLICATION_CREDENTIALS:-}" ]]; then
    69    echo "Detected GOOGLE_APPLICATION_CREDENTIALS, activating..." >&2
    70    gcloud auth activate-service-account --key-file="$GOOGLE_APPLICATION_CREDENTIALS"
    71    gcloud auth configure-docker
    72  fi
    73  
    74  cmd=
    75  if [[ $# != 0 ]]; then
    76    cmd="$1"
    77    shift
    78  fi
    79  
    80  # List the $1 most recently pushed prow versions
    81  list-options() {
    82    count="$1"
    83    gcloud container images list-tags gcr.io/k8s-prow/plank --limit="$count" --format='value(tags)' \
    84        | grep -o -E 'v[^,]+' | "$TAC"
    85  }
    86  
    87  # Print 10 most recent prow versions, ask user to select one, which becomes new_version
    88  list() {
    89    echo "Listing recent versions..." >&2
    90    echo "Recent versions of prow:" >&2
    91    options=(
    92      $(list-options 10)
    93      )
    94    if [[ -z "${options[@]}" ]]; then
    95      echo "No versions found" >&2
    96      exit 1
    97    fi
    98    new_version=
    99    for o in "${options[@]}"; do
   100      def_opt="$o"
   101      echo -e "  $(color-version "$o")"
   102    done
   103    read -p "Select version [$(color-version "$def_opt")]: " new_version
   104    if [[ -z "${new_version:-}" ]]; then
   105      new_version="$def_opt"
   106    else
   107      found=
   108      for o in "${options[@]}"; do
   109        if [[ "$o" == "$new_version" ]]; then
   110          found=yes
   111          break
   112        fi
   113      done
   114      if [[ -z "$found" ]]; then
   115        echo "Invalid version: $new_version" >&2
   116        exit 1
   117      fi
   118    fi
   119  }
   120  
   121  if [[ "$cmd" == "--push" ]]; then
   122    echo "WARNING: --push is depreacated please use push.sh instead"
   123    "$(dirname "$0")/push.sh"
   124    exit 0
   125  fi
   126  
   127  if [[ -z "$cmd" || "$cmd" == "--list" ]]; then
   128    list
   129  elif [[ "$cmd" =~ v[0-9]{8}-[a-f0-9]{6,9} ]]; then
   130    new_version="$cmd"
   131  elif [[ "$cmd" == "--latest" ]]; then
   132    new_version="$(list-options 1)"
   133  else
   134    usage
   135  fi
   136  
   137  # Determine what deployment images we need to update
   138  echo -n "images: " >&2
   139  images=("$@")
   140  if [[ "${#images[@]}" == 0 ]]; then
   141    echo -e "querying bazel for $(color-target :image) targets under $(color-target //prow/...) ..." >&2
   142    images=($(bazel query 'filter(".*:image", //prow/...)' | cut -d : -f 1 | xargs -n 1 basename))
   143    echo -n "images: " >&2
   144  fi
   145  echo -e "$(color-image ${images[@]})" >&2
   146  
   147  echo -e "Bumping: $(color-image ${images[@]}) to $(color-version ${new_version}) ..." >&2
   148  
   149  for i in "${images[@]}"; do
   150    echo -e "  $(color-image $i): $(color-version $new_version)" >&2
   151    $SED -i "s/\(${i}:\)v[a-f0-9-]\+/\1${new_version}/I" cluster/*.yaml
   152    $SED -i "s/\(${i}:\)v[a-f0-9-]\+/\1${new_version}/I" config.yaml
   153  done
   154  
   155  echo "Deploy with:" >&2
   156  echo -e "  $(color-target bazel run //prow/cluster:production.apply --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64)"