Skip to content

Instantly share code, notes, and snippets.

@rjhornsby
Created May 14, 2021 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjhornsby/ad3dcf875395e65bc7c1660e5313af32 to your computer and use it in GitHub Desktop.
Save rjhornsby/ad3dcf875395e65bc7c1660e5313af32 to your computer and use it in GitHub Desktop.
Compare local cookbook versions to Chef server versions before `berks upload`
#!/bin/zsh
TMPDIR=$(mktemp -d)
# check for gsed usage
if [ "$(uname)" = "Darwin" ]; then
if [ ! -x "/usr/local/bin/gsed" ]; then
echo "macOS platform detected, but gsed not found. Please install it."
exit 1
fi
alias sed="/usr/local/bin/gsed"
fi
berks list -F json | jq -r '.cookbooks[] | [.name,.version] | @csv' | tr -d \" >"$TMPDIR/cookbooks_needed"
knife cookbook list -aF json | jq -r '.[]' >"$TMPDIR/cookbooks_available"
function cookbook_versions_available() {
name=$1
grep "$name" "$TMPDIR/cookbooks_available" | tr -s ' ' | cut -d ' ' -f2-
}
function has_cookbook_version() {
name=$1
version=$2
cookbook_versions_available "$name" | grep -q "\(^\s*$version\|\s$version\s\|\s$version$\)"
return $?
}
while read -r cookbook; do
name=$(echo "$cookbook" | awk -F, '{print $1}')
version=$(echo "$cookbook" | awk -F, '{print $2}')
echo -n "Checking for cookbook $name v${version}..."
if has_cookbook_version "$name" "$version"; then
echo 'ok'
else
echo -n "NOT FOUND. "
versions=$(cookbook_versions_available "$name")
if [ -z "$versions" ]; then
echo "nothing further located"
else
# pattern matching doesn't work properly in macos sed, so use gnu sed
# shellcheck disable=SC2001
echo "available versions: $(echo "$versions" | sed 's/\s\+/, /g')"
fi
fi
done <"$TMPDIR/cookbooks_needed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment