Advertisement
flipje

configs-to-subversion-script

Aug 2nd, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.18 KB | None | 0 0
  1. # dit script zet alle /configs die naar de nfs share gezonden worden in subversion,
  2. # zodat we altijd een backlog hebben welke wijzigingen er op de machines gedaan zijn.
  3. # flip hess 2012
  4.  
  5. # Global variables:
  6.  
  7. PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
  8. SCRIPT_PATH="${0}"
  9. ARGS="${#}"
  10. REPO_DIR="/data/subversion/machines"
  11. SOURCE_DIR="/data/config/machines"
  12. LOG="/var/log/svn-update.log"
  13.  
  14.  
  15. # Functions:
  16.  
  17.   # exit function
  18.   function die()
  19.   {
  20.     echo -e "Error in ${SCRIPT_PATH}:\n${1}"
  21.     exit 1
  22.   }
  23.  
  24.   # Shows usage function.
  25.   function fShowUsage()
  26.   {
  27.     echo "Usage: ${SCRIPT_PATH}"
  28.     exit 0
  29.   }
  30.  
  31.   # check for.......
  32.   function fCheck()
  33.   {
  34.     # script depends on:
  35.     [ -x /usr/bin/svn ] || die "This script depends on /usr/bin/svn"
  36.  
  37.     # user must be root:
  38.     [ "$(whoami)" = root ] || die "User must be root!"
  39.  
  40.     # check for arguments:
  41.     [ "${ARGS}" = 0 ] || fShowUsage
  42.  
  43.     # check for directories
  44.     [ -d "${SOURCE_DIR}" ]  || die "Failed to find ${SOURCE_DIR} directory!"
  45.     [ -d "${REPO_DIR}" ] || die "Failed to find ${REPO_DIR} directory!"
  46.  
  47.     # remove pem's from source
  48.     find "${SOURCE_DIR}" -type l -name '*\.pem' -delete || die "Failed to delete pem symlinks from config"
  49.     find "${SOURCE_DIR}" -type f -name '*\.pem' -delete || die "Failed to delete pem symlinks from config"
  50.  
  51.     return 0
  52.   }
  53.  
  54.   # The main function.
  55.   function fMain()
  56.   {
  57.    # check if
  58.    for HOST in $( ls "${SOURCE_DIR}" );
  59.    do
  60.      # check if repo is present
  61.      if [ -d "${REPO_DIR}/${HOST}" ] ; then
  62.  
  63.         # if so run update
  64.         cd "${SOURCE_DIR}/${HOST}" || { echo "Failed to cd to \"${SOURCE_DIR}/${HOST}\""; continue; }
  65.  
  66.         # force add all files
  67.         svn add --force ./* >> ${LOG} || { echo "Failed to add \"${SOURCE_DIR}/${HOST}\" to repository"; continue; }
  68.  
  69.         # remove from svn everything that has been locally removed
  70.         svn status | grep "^\!" | sed 's/! *//' | xargs -I% svn rm % || { echo "Failed to remove deleted dirs for \"${SOURCE_DIR}/${HOST}\" from revision"; continue; }
  71.  
  72.         # commit
  73.         svn commit -m "$(date)" >> ${LOG} || { echo "Failed to commit to \"${REPO_DIR}/${HOST}\""; continue; }
  74.      else
  75.         # if no create repo and import
  76.         svnadmin create "${REPO_DIR}/${HOST}" || { echo "Failed to create svn repository \"${REPO_DIR}/${HOST}\""; continue; }
  77.  
  78.         # go to dir
  79.         cd "${SOURCE_DIR}/${HOST}" || { echo "Failed to cd to \"${SOURCE_DIR}/${HOST}\""; continue; }
  80.  
  81.         # checkout new repository
  82.         svn co file://${REPO_DIR}/${HOST} "${SOURCE_DIR}/${HOST}"  >> ${LOG} || { echo "Failed to checkout \"file://${REPO_DIR}/${HOST}\""; continue; }
  83.  
  84.         # add files
  85.         svn add ./* >> ${LOG} || die "Failed to add \"${SOURCE_DIR}/${HOST}\" to repository"; continue; }
  86.  
  87.         # commit to repository
  88.         svn commit -m "initial commit" >> ${LOG} || { echo "Failed commity changes to repository \"file://${REPO_DIR}/${HOST}\""; continue; }
  89.      fi  
  90.    done
  91.  
  92.    # klaar
  93.     return 0
  94.   }
  95.  
  96.  # check environment:
  97.   fCheck
  98.  
  99.  # Start the program:
  100.   fMain "${@}"
  101.  
  102.  # Exit with previous return code:
  103.   exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement