Simple Bash Script to Remove Clearcase Views (experimental)



So I just wanted to take a minute to say how awesome GNU Bash is, and that you can do a great many things with it. One of those things you can do is run commands from the cross-platform-compatible(mostly) IBM Rational Clearcase cleartool.


Since I was working on cleaning up some old user views, I thought I'd just make a quick writeup on a script for helping with this task. I did it because I like writing scripts in Bash and I'm working (very often) to get better at it. Please note, the script is experimental, and just meant for tinkering purposes. That said, I can't be held responsible for any damage you do to your systems as a result of using this script.

The script takes a parameter of the username. The script will search for views that are owned by the username, and will de-register, de-tag them. As for deleting the actual files at the view path, that part is NOT done by this script. Instead, the script will record the server name and view path into a file named "ViewsToDeleteReport..." for you to sort through for reference to the final removal.

The reason I left the deleting of the files off is two-fold, 1.) It's a bit tricky to remove the files from multiple view servers at once, and 2.) for safety - if you somehow remove the wrong view from clearcase, you can easily go back and re-register the existing view files back as a view again. I certainly hope someone can find something useful here.

 
#!/bin/bash
#Delete clearcase Views (clearcase parts, not physical data parts) Script
#License GPL v2.0 - More information is available at fsf.org
#By: Shannon VanWagner
#05-01-2012
#humans-enabled.com
#TODO - read multiple users based on file input

deleteUserViews()
{
if [ -x /opt/rational/clearcase/bin/cleartool ]
then
  ct=/opt/rational/clearcase/bin/cleartool
  rtimestamp=$(date +%Y_%b_%d_%H_%M-%S)
else
 echo "Fatal Error(31) no cleartool executable at /opt/rational/clearcase/bin/cleartool, script halted"
 exit 31 
fi
 
for item in $($ct lsview -long|egrep -B10 -i "view owner: \/$1|view owner: $1"|grep -i 'tag:'|cut -d" " -f2)
do
  if [ $item ]
  then
    vuid=$($ct lsview -long "$item"|grep -i 'view uuid:'|cut -d" " -f3)
    vpath=$($ct lsview -long "$item"|grep -i 'view server access path:'|cut -d" " -f5)
    vserver=$($ct lsview -long "$item"|grep -i "view on host:"|cut -d" " -f4)

    #Process clearcase view removal 
    echo "Removing clearcase references for view:$item" 
    $ct endview $item > /dev/null 2>&1
    $ct rmview -force -uuid $vuid -all > /dev/null 2>&1
    $ct unregister -view -uuid $vuid > /dev/null 2>&1
    $ct rmtag -view $item > /dev/null 2>&1

    #Call report output for view deletion info
    reportFilesToDelete $rtimestamp $vserver $vpath
  else
    echo "Fatal Error(43) Base cleartool lsview failed, script halted."
    exit 43
  fi
done
}

reportFilesToDelete() {
#Expects timestamp:$1, server:$2, path:$3 

if [ $1 ] && [ $2 ] && [ $3 ]
then 
  rname="$(echo $delViewReportName)_$1.txt"
  echo $2 $3 | tee -a $rname
else
  echo "Missing timestamp, servername, or path from function call"
fi

}

# main
if [ $1 ]
then
  usern=$1
  delViewReportName=ViewsToDeleteReport
  deleteUserViews $usern
else
 progname=$(basename $0)
 echo -e " What user views shall I delete? \n Script Usage: ./$progname username"
 #29 no parameter error code - defined by me
 exit 29
fi

echo "Done"
exit 0


Note: If you're looking for a FOSS solution for your source code repository solution instead. I would recommend you try "git" from http://git-scm.org instead.

Cheers!

Shannon VanWagner
05-24-2012