Last active
September 3, 2017 18:31
-
-
Save scottrigby/4cba8b7454c3ecb38a1136cd06261092 to your computer and use it in GitHub Desktop.
kubernetes/charts dependency update script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# @file | |
# See HELP(). | |
# This script is useful until functionality is adopted into Helm. | |
# | |
# Follow @link https://github.com/kubernetes/helm/issues/1947 the issue. @endlink | |
# Define SCRIPT var for help and validation output. | |
SCRIPT=`basename ${BASH_SOURCE[0]}` | |
# Use man(1) syntax. | |
function HELP() { | |
cat <<EOF | |
Help documentation for ${SCRIPT}. | |
NAME | |
${SCRIPT} - Bumps a dependency version existing in all kubernetes charts. | |
REQUIRES | |
- bash | |
- git | |
- github/hub | |
- kubernetes/charts (github local checkout and fork) | |
- kubernetes/helm | |
- perl | |
- fsaintjacques/semver-tool (as "semver" in your $PATH) | |
SYNOPSIS | |
${SCRIPT} [-d <DEPENDENCY>] [-v <VERSION>] [-f <FORK|\$USER>] | |
DESCRIPTION | |
Must be run from kubernetes/charts repo root. | |
OPTIONS | |
-d, -dependency | |
Required. The chart dependency name (example: mariadb). | |
-v, -version | |
Required. The chart dependency version (example: 0.5.9 or 0.5.x). | |
-f, -fork | |
Optional. Your git fork remote name. Defaults to current $USER name. | |
-g, --git-automation | |
Optional. Automate creation of git branches locally and on your fork, and open PRs in the kubernetes/charts repo. Not setting this is good for testing all but the local, fork, and upstream git automation. | |
-l, --list-all | |
List all dependent charts, with their dependencies. | |
-h, --help | |
Show this help message | |
SEE ALSO | |
bash(1), git(1), helm(1), perl(1), semver --help | |
EOF | |
} | |
# Let's make sure the user has the right tool requirements defined in HELP. | |
function validate_requirements() { | |
local TOOLS=( | |
'bash' | |
'git' | |
'hub' | |
'helm' | |
'perl' | |
'semver' | |
) | |
for TOOL in "${TOOLS[@]}"; do | |
which $TOOL &> /dev/null | |
if [ $? == 1 ]; then | |
echo "You must have $TOOL in your \$PATH" | |
local EXIT=true | |
fi | |
done | |
if [ "${EXIT:-}" == 'true' ]; then | |
exit 1 | |
fi | |
} | |
# Validates that user is authenticated to GitHub CLI. | |
function validate_github() { | |
# Ensure user is authenticated with GitHub. | |
ssh -T [email protected] &> /dev/null | |
# If this command succeeds, there will be a 1 exit status. If it fails, the | |
# exit status will be 255. | |
if [ $? != 1 ]; then | |
echo 'You must authenticate with GitHub to run this script.' | |
exit 1 | |
fi | |
} | |
# Validates that required args are set. | |
function validate_args() { | |
# Default git fork remote name to current user if not passed as an option. | |
FORK=$USER | |
# Transform long options to short ones. Sick trick. | |
# http://stackoverflow.com/a/30026641/4096495 | |
for arg in "$@"; do | |
shift | |
case "$arg" in | |
"--dependency") set -- "$@" "-d" ;; | |
"--version") set -- "$@" "-v" ;; | |
"--fork") set -- "$@" "-f" ;; | |
"--git-automation") set -- "$@" "-g" ;; | |
"--list-all") set -- "$@" "-l" ;; | |
"--help") set -- "$@" "-h" ;; | |
*) set -- "$@" "$arg" | |
esac | |
done | |
while getopts :hgld:v:f: OPT; do | |
case $OPT in | |
d ) DEPENDENCY=$OPTARG;; | |
v ) VERSION=$OPTARG;; | |
f ) FORK=$OPTARG;; | |
g ) GIT_AUTOMATE=true;; | |
l ) LIST_ALL=true;; | |
h ) SHOW_HELP=true;; | |
\?) echo "Unknown option: -$OPTARG" >&2; exit 1;; | |
: ) echo "Missing option argument for -$OPTARG" >&2; exit 1;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
# To-do: Fix this opt validation. For now comment out so we can --list-all. | |
# Ensure needed variables have been set. | |
if [[ ${SHOW_HELP:-} == 'true' ]]; then | |
HELP; | |
exit 1 | |
fi | |
} | |
function list_all() { | |
local ALL_DEPENDENT_CHART_PATHS=$(grep -l "dependencies:" stable/*/requirements.yaml | xargs -I {} dirname {}) | |
for CHART_PATH in $ALL_DEPENDENT_CHART_PATHS; do | |
local DEPENDENCIES=$(sed -n 's/- name: //p' $CHART_PATH/requirements.yaml) | |
echo $CHART_PATH: $DEPENDENCIES | |
done | |
exit 0 | |
} | |
# Bumps version string in all applicable charts requiremnents.yaml files. | |
function bump_dependency_version() { | |
# Using Perl for @link http://superuser.com/a/597389/680624 multi-line (\n) replacement. @endlink | |
perl -0pi -w -e "s/- name: $DEPENDENCY\n version: .*/- name: $DEPENDENCY\n version: $VERSION/g;" stable/*/requirements.yaml | |
} | |
# When we update versions for any dependencies of a chart, we must also update | |
# the chart's version. | |
function bump_chart_version() { | |
local CHART_VERSION=$(sed -n 's/version: //p' $1/Chart.yaml) | |
local BUMP_CHART_VERSION=$(semver bump patch $CHART_VERSION) | |
perl -0pi -w -e "s/version: $CHART_VERSION/version: $BUMP_CHART_VERSION/g;" $1/Chart.yaml | |
} | |
function helm_update_charts() { | |
for CHART_PATH in $CHART_PATHS; do | |
helm dependency update $CHART_PATH | |
bump_chart_version $CHART_PATH | |
done | |
} | |
# Git and GitHub PR operations on updated, dependent charts. | |
# @todo: Optionally associate all PRs to a single GitHub issue. | |
function github_pr_charts() { | |
for CHART_PATH in $CHART_PATHS; do | |
CHART_NAME=$(basename "$CHART_PATH") | |
BRANCH="$CHART_NAME-bump-dependency-$DEPENDENCY-$VERSION" | |
MESSAGE="[stable/$CHART_NAME] bump dependency $DEPENDENCY $VERSION" | |
git checkout master | |
git checkout -b "$BRANCH" | |
git add $CHART_PATH/requirements.yaml $CHART_PATH/requirements.lock | |
git commit -m "$MESSAGE" | |
# Commit bump chart PATCH version. | |
git add $CHART_PATH/Chart.yaml | |
git commit -m "Bump stable/$CHART_NAME version" | |
# Hub requires pushing a branch to your fork before calling "pull-request". | |
git push -u "$FORK" | |
# Note hub pull-request assumes opens a PR on GitHub for the project that | |
# the "origin" remote points to. | |
# See @link https://hub.github.com/hub.1.html hub manual. @endlink | |
hub pull-request -m "$MESSAGE" | |
done | |
} | |
# Validation. | |
validate_requirements | |
validate_args "$@" | |
# List all dependent charts by dependency. | |
if [[ ${LIST_ALL:-} == 'true' ]]; then | |
list_all | |
fi | |
# Get dependent charts. | |
CHART_PATHS=$(grep -l "\- name: $DEPENDENCY" stable/*/requirements.yaml | xargs -I {} dirname {}) | |
# Update each dependent chart's requirements.yaml file. | |
bump_dependency_version | |
# Update each dependent chart's requirements.lock file. | |
helm_update_charts | |
# Locally create a branch per dependent chart update, commit, and open a PR. | |
if [[ ${GIT_AUTOMATE:-} == 'true' ]]; then | |
validate_github | |
github_pr_charts | |
fi |
Prep:
- Ensure I'm on master, it's clean, and up to date:
git stash git checkout master git fetch origin git reset --hard origin/master
- Check if there are needed files in
stable/
git status
- Once needed files are dealt with, clean out any test files in
stable/
git clean -fd stable/
- Check that I have no open dependency bump PRs (otherwise wouldn't want to try to delete remote branch).
- If no open PRs ^, delete local dependency branches for kubernetes/charts.
git branch | grep dependency | xargs git push scottrigby --delete git branch | grep dependency | xargs git branch -D
- Download this gist into charts git root, and make executable
- Run
./k8s_charts_bump_dependency_version.sh -d <dependency> -v <version>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script is for kubernetes/charts PRs (see helm/helm#1947), because helm/helm#2041 is not ready yet (also see helm/helm#2205 for future issue discussion).