Skip to content

Instantly share code, notes, and snippets.

@harlanbarnes
Created February 4, 2012 19:57
Show Gist options
  • Save harlanbarnes/1739757 to your computer and use it in GitHub Desktop.
Save harlanbarnes/1739757 to your computer and use it in GitHub Desktop.
svn version of knife cookbook site install
#!/bin/bash
cookbook="$1"
if [ -z "$cookbook" ];
then
echo "ERROR: script needs a cookbook as an arg"
exit 1
fi
SVN_REPO='https://svn-host/svn-repo'
SVN_VENDOR_REPO="$SVN_REPO/vendor/cookbooks"
SVN_TRUNK_REPO="$SVN_REPO/trunk/chef-repo/cookbooks"
LOCAL_TRUNK_REPO="$HOME/chef-repo/cookbooks"
knife cookbook site download $cookbook
[ $? -gt "0" ] && echo "knife download failed" && exit 1
c_filename=$(ls -1 $cookbook-*.tar.gz)
[ ! -f "$c_filename" ] && echo "Could not find the name of the downloaded cookbook '$c_filename'"
c_version=$(echo "$c_filename" | sed -e "s/$cookbook-//" | sed -e 's/.tar.gz//')
[ -z "$c_version" ] && echo "Could not parse out version" && exit 1
branch_exists=$(svn list $SVN_VENDOR_REPO/$cookbook/$c_version)
if [ $? -gt "0" ];
then
t_dir=$(mktemp -d)
[ ! -d "${t_dir}" ] && echo "Could not make temp dir '$t_dir'" && exit 1
tar -xzvf $c_filename -C $t_dir
[ $? -gt "0" ] && echo "tar failed" && exit 1
branch_exists=$(svn list $SVN_VENDOR_REPO/$cookbook/current)
if [ $? -gt "0" ];
then
svn import $t_dir/$cookbook ${SVN_VENDOR_REPO}/$cookbook/current -m "importing vendor tag '$c_version' as 'current'"
[ $? -gt "0" ] && echo "svn import failed" && exit 1
svn copy ${SVN_VENDOR_REPO}/$cookbook/current ${SVN_VENDOR_REPO}/$cookbook/$c_version -m "creating vendor tag '$c_version'"
[ $? -gt "0" ] && echo "svn tag failed" && exit 1
else
echo "Using svn_load_dir to update '$cookbook' current and create a tag for '$c_version'"
svn_load_dirs -no_user_input -t $c_version ${SVN_VENDOR_REPO}/$cookbook/ current $t_dir/$cookbook
[ $? -gt "0" ] && echo "svn_load_dirs failed" && exit 1
fi
rm -rf $t_dir
# copy into main trunk
branch_exists=$(svn list $SVN_TRUNK_REPO/$cookbook)
if [ $? -gt "0" ];
then
echo "This cookbook doesn't exist in the main branch so bringing it there"
svn copy ${SVN_VENDOR_REPO}/$cookbook/$c_version ${SVN_TRUNK_REPO}/$cookbook -m "bringing in version '$c_version' of '$cookbook'"
else
echo "Looks like this cookbook exists in the main repo branch, so going to merge"
old_dir=$(pwd)
cd $LOCAL_TRUNK_REPO
current_version=$(grep "^version " $cookbook/metadata.rb | awk '{print $2}' | sed -e 's/\"//g')
svn merge ${SVN_VENDOR_REPO}/$cookbook/$current_version ${SVN_VENDOR_REPO}/$cookbook/current $cookbook
cd $old_dir
fi
else
echo "We already have this version '$c_version' for '$cookbook'"
fi
rm -rf $c_filename
@petebuletza
Copy link

I had to change line 60, specifically the sed portion to account for both double and single quotes in cookbook versions. The line should read:

current_version=$(grep "^version " $cookbook/metadata.rb  | awk '{print $2}' | sed -e "s/[\"\']//g")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment