Friday, February 14, 2014

Automating Subversion repository creation for new and imported repos.

Creating new repositories is a very mechanical operation ripe for automation. This bash script will create a new repository in the appropriate directory using the name specified on the command line. It will also set up the default Subversion paths of trunk, branches and tags. It checks for the Collabnet binaries to determine the unique settings needed for that implementation.

#!/bin/sh
 
REPOSITORIES="/var/svn/repositories"
 
# Set variables based on Collabnet vs. other install
if [ -f "/opt/CollabNet_Subversion/bin/svn" ]
then
  BIN_DIR="/opt/CollabNet_Subversion/bin"
  APACHE="csvn:csvn"
else
  BIN_DIR="/usr/bin"
  APACHE="apache:apache"
fi
 
function errormsg
{
  echo "$0: missing operand"
  echo "Usage: $0 RESPOSITORY..."
  echo "Create the RESPOSITORY(s)."
}
 
function is_valid_posix_filename
{
  # Return TRUE if characters in $1 confirms to posix, else FALSE.
  len=`echo "$1"| wc --chars`
  if [ $len -le 255 ]; then
    fs=`echo "$1"| grep '/'`
    if [ -z "$fs" ]; then
      return 1
    fi
  fi
 
  return 0
}  
 
function is_Subversion_repository
{
   # Check whether $1 is a Subversion repository
   if [ -d "$1" ]; then
     if [ -f "$1/format" ]; then
       if [ -f "$1/conf/svnserve.conf" ]; then
         if [ -f "$1/conf/passwd" ]; then
           return 1
         fi
       fi
     fi
   fi
 
   return 0
}
 
# MAIN
 
# Check for user input
if [ -z "$1" ]
then
  errormsg
  exit 1
fi
 
while [ -n "$1" ]
do
  is_valid_posix_filename "$1"
  # Check return value
  if [ $? -ne 0 ]
  then
    is_Subversion_repository "$REPOSITORIES/$1"
    # Check return value
    if [ $? -ne 0 ]
    then
      echo "Subversion repository \"$1\" already exists." 1>&2
    else
      mkdir -p "$REPOSITORIES/$1" &&\
      $BIN_DIR/svnadmin create "$REPOSITORIES/$1" &&\
      $BIN_DIR/svn mkdir "file://$REPOSITORIES/$1/trunk" \
        "file://$REPOSITORIES/$1/tags" \
        "file://$REPOSITORIES/$1/branches" \
        -m 'Creating initial branch structure' \
        --no-auth-cache --non-interactive --quiet &&\
      chown -R $APACHE "$REPOSITORIES/$1" &&\
      echo "New repository \"$1\" has been created."
    fi
  else
    echo "Illegal character (/) or repository name too long (> 255)." 1>&2
  fi
 
  shift
done
 
#END MAIN
 
This bash script automates creation of a repository using a dump file.

#!/bin/sh
 
REPOSITORIES="/var/svn/repositories"
 
# Set variables based on Collabnet vs. other install
if [ -f "/opt/CollabNet_Subversion/bin/svn" ]
then
  BIN_DIR="/opt/CollabNet_Subversion/bin"
  APACHE="csvn:csvn"
else
  BIN_DIR="/usr/bin"
  APACHE="apache:apache"
fi
 
function errormsg
{
  echo "$0: missing operand"
  echo "Usage: $0 RESPOSITORY DUMP..."
  echo "Create the RESPOSITORY."
}
 
function is_valid_posix_filename
{
  # Return TRUE if characters in $1 confirms to posix, else FALSE.
  len=`echo "$1"| wc --chars`
  if [ $len -le 255 ]; then
    fs=`echo "$1"| grep '/'`
    if [ -z "$fs" ]; then
      return 1
    fi
  fi
 
  return 0
}  
 
function is_Subversion_repository
{
   # Check whether $1 is a Subversion repository
   if [ -d "$1" ]; then
     if [ -f "$1/format" ]; then
       if [ -f "$1/conf/svnserve.conf" ]; then
         if [ -f "$1/conf/passwd" ]; then
           return 1
         fi
       fi
     fi
   fi
 
   return 0
}
 
# MAIN
 
# Check for user input
if [ -z "$2" ]
then
  errormsg
  exit 1
fi
 
if [ -f "$2" ]
then
  is_valid_posix_filename "$1"
  # Check return value
  if [ $? -ne 0 ]
  then
    is_Subversion_repository "$REPOSITORIES/$1"
    # Check return value
    if [ $? -ne 0 ]
    then
      echo "Subversion repository \"$1\" already exists." 1>&2
    else
      mkdir -p "$REPOSITORIES/$1" &&\
      $BIN_DIR/svnadmin create "$REPOSITORIES/$1" &&\
      svnadmin load "$REPOSITORIES/$1" < "$2" &&\
      chown -R $APACHE "$REPOSITORIES/$1" &&\
      echo "New repository \"$1\" has been created."
    fi
  else
    echo "Illegal character (/) or repository name too long (> 255)." 1>&2
  fi
else
  echo "$2 is not a valid file." 1>&2
fi
 
#END MAIN
 

No comments:

Post a Comment