#!/bin/bash # Written by Jan Labanowski (jkl at ccl net) during his benevolent mood # on Aug 8, 2009 (this was on the weekend and Tyskie Beer mixed with Okocim # was to blame for this irrational behaviour...) # Copyright and disclaimer: # This script is wrong... Running it will break your machine, delete files, # waste your time and may even start the WWIII, unless it already started... # You have been warned and you cannot sue me, though you can do whatever you # want with this script, even modify it, put your name on it and sell it. # See... I am not even GNUish and I do not want to be famous since it sucks... # But I am a good man... Believe me... # Initialize the VOB_TAG to the one under which the view objects are kept # Both lines have to be custmized VOB_TAG="koolapp" VOB_PATH="/vobs/${VOB_TAG}"; # check how this is organized on your site if [[ ! -d $VOB_PATH ]]; then # is storage area available? echo There is no VOB dir: ${VOB_PATH} >&2 exit 2 fi ALLOWED_GROUPS=( ccusr deptxv ) # if empty () the groups will not be tested ALLOWED_USERS=( jkl bob tom ) # if empty () the users will not be tested USAGE="Usage: mk_usr_koolapp_view view_name" if [ "$#" != "1" ]; then echo $USAGE >&2 exit 1 fi # check if you are currently in the view. You cannot be in the view to run it CURRENT_VIEW=$(cleartool pwv -short) if [[ ! "$CURRENT_VIEW" =~ "\*" ]]; then echo You are currently running inside the view: $CURRENT_VIEW >&2 echo Please exit the view \( exit or CTRL/D \) and try again. exit 1 fi # take a view name from the command line VIEW_TAG=$1 # collect information about current user by running UNIX id command # Use "which" to find out where the id command resides on your system # E.g.: uid=1234(jkl) gid=321(deptxv) groups=321(deptxv),313(ccusrs),315(jerks) ID_A_OUTPUT=$(/usr/bin/id -a) # parse output of /usr/bin/id USER_UID=${ID_A_OUTPUT%% *} # uid=1234(jkl) TEMP=${ID_A_OUTPUT#* } # gid=321(deptxv) groups=321(deptA),313(ccus... USER_GID=${TEMP%% *} # gid=321(deptxv) USER_GROUPS=${TEMP#* } # groups=321(deptxv),313(ccusrs),315(jerks) # run tests if user is in a group authorized to run this script USER_AUTHORIZED=Yes if [[ ${#ALLOWED_GROUPS[@]} -gt 0 ]]; then USER_AUTHORIZED=No for group in ${ALLOWED_GROUPS[@]} do if [[ "$USER_GROUPS" =~ "\($group\)" ]]; then USER_AUTHORIZED=Yes break fi done fi # check if user is on the list of authorized users to run the script if [[ ${#ALLOWED_USERS[@]} -gt 0 && "$USER_AUTHORIZED" = "Yes" ]]; then USER_AUTHORIZED=No for user in ${ALLOWED_USERS[@]} do if [[ "$USER_UID" =~ "\($user\)" ]]; then USER_AUTHORIZED=Yes break fi done fi if [[ "USER_AUTHORIZED" = "No" ]]; then echo "You are not authorized to create a ClearCase view" >&2 exit 2 fi # extract bare user name USER_NAME=${USER_UID#*(} # jkl) USER_NAME=${USER_NAME%%)*} # jkl # VIEW_TAG has a format: # "${USER_NAME}_koolapp_X.Y.Z" for example: jkl_koolapp_3.2.1 if [[ ! "$VIEW_TAG" =~ "^${USER_NAME}_${VOB_TAG}_[0-9]+\.[0-9]+\.[0-9]+$" ]] then echo Correct view_name has a format "username_${VOB_TAG}_X.Y.Z" >&2 echo For example: ${USER_NAME}_${VOB_TAG}_3.12.2 >&2 echo Your view_name \($VIEW_TAG\) does not satisfy this requirement. >&2 exit 3 fi # check if the view was created before CHECK_VIEW=$(cleartool lsview -short | egrep "^${VIEW_TAG}$") if [[ "$CHECK_VIEW" = "$VIEW_TAG" ]]; then echo $VIEW_TAG view already exists. Please choose another view name >&2 exit 2 fi # Private developer branch names here are the same as their view names BRANCH_NAME="$VIEW_TAG" # check if branch already exists CHECK_BRANCH=$(cleartool lstype -short -kind brtype -invob $VOB_PATH | \ egrep "^${BRANCH_NAME}") if [[ "$CHECK_BRANCH" = "$BRANCH_NAME" ]]; then echo $BRANCH_NAME branch already exists for view $VIEW_TAG >&2; echo Choose other name for your view. exit 2 fi # create a future config spec file to go with this view in /tmp NOW=$(date) TEMP_CS_FILE="/tmp/my_temp_csfile_$$" echo "# config spec for view $VIEW_TAG" > $TEMP_CS_FILE echo "element * CHECKEDOUT" >> $TEMP_CS_FILE echo "element * .../${BRANCH_NAME}/LATEST" >> $TEMP_CS_FILE echo "element * /main/LATEST -mkbranch $BRANCH_NAME" >> $TEMP_CS_FILE echo "# Created: $NOW" >> $TEMP_CS_FILE # create view with a default config spec. Check if arguments are fine cleartool mkview -tag $VIEW_TAG -stgloc -auto ERR=$? if [ $ERR != 0 ]; then echo The cleartool mkview command failed in the mk_usr_koolapp_view >&2 echo Please review the script to see if mkview parameters are right >&2 exit 4 fi # create shell script to create a branch type and set the config spec for view TEMP_MAKE_VIEW_SCRIPT="/tmp/my_temp_mkview_$$" echo "#!/bin/bash" > $TEMP_MAKE_VIEW_SCRIPT echo "#" >> $TEMP_MAKE_VIEW_SCRIPT echo "cd $VOB_PATH" >> $TEMP_MAKE_VIEW_SCRIPT echo "cleartool mkbrtype -nc $BRANCH_NAME" >> $TEMP_MAKE_VIEW_SCRIPT echo "cleartool setcs $TEMP_CS_FILE" >> $TEMP_MAKE_VIEW_SCRIPT # ClearCase setview -exec requires that the script is executable chmod 755 $TEMP_MAKE_VIEW_SCRIPT echo The config spec file for your view $VIEW_TAG will be set to cat $TEMP_CS_FILE cleartool setview -exec $TEMP_MAKE_VIEW_SCRIPT $VIEW_TAG ERR=$? if [ $ERR != 0 ]; then echo The cleartool setview command failed in the mk_usr_koolapp_view >&2 echo Please review the script to see if setview parameters are right >&2 exit 4 fi