3 flavours & 2 archs
[infrastructure.git] / scripts / git-mirror.sh
index ec6e52d..ff07de2 100755 (executable)
@@ -7,26 +7,32 @@ LOCAL_MIRROR_DIR="/git-mirror"
 QUIET=0
 FAILED=0
 FAILED_CMDS=""
+NOTIFIED_FILE="NOTIFIED_ADMINS"
+RUNNING_FILE=$LOCAL_MIRROR_DIR/RUNNING_MIRROR
 
 function msg () {
-    if [ $QUIET -eq 0 ]
-    then
-        echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx $@"
+    if [ $QUIET -eq 0 ] ; then
+        echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" "$@"
     fi
 }
 
 function error () {
     SUBJECT=$1
     MSG=$2
-    for admin in $ADMINS
-    do
+    CHECK_FILE=$3/$NOTIFIED_FILE
+
+    if [ -f $CHECK_FILE ];  then
+        return
+    fi
+
+    for admin in $ADMINS; do
         echo -e "$MSG" | mail -s "$SUBJECT" $admin
     done
+    touch $CHECK_FILE
 }
 
 function run () {
-    if [ $QUIET -eq 1 ]
-    then
+    if [ $QUIET -eq 1 ]; then
         COMMAND="$1 &> /dev/null"
     else
         COMMAND="$1"
@@ -36,8 +42,7 @@ function run () {
 
     pushd ${REPO} > /dev/null
     eval $COMMAND
-    if [ $? -ne 0 ]
-    then
+    if [ $? -ne 0 ]; then
         FAILED=1
         FAILED_CMDS="$FAILED_CMDS\n$COMMAND" 
     fi
@@ -99,17 +104,14 @@ function mirror () {
         # if there is no remote repository it may be that we only have
         # the repository locally and don't need to mirror
         git ls-remote $MIRROR_REPO &> /dev/null
-        if [ $? -eq 0 ]
-        then
-            if [ -d ${REPO_DIR} ]
-            then
+        if [ $? -eq 0 ]; then
+            if [ -d ${REPO_DIR} ]; then
                msg "pulling from ${NAME}"
                 run "git fetch origin --tags" ${REPO_DIR}
                run "git fetch origin" ${REPO_DIR}
                 merge_all_branches $NAME origin $REPO_DIR
-                if [ $? -ne 0 ]
-                then
-                    error "Can not fetch from ${MASTER_REPO}"
+                if [ $? -ne 0 ]; then
+                    error "git-mirror.sh failed" "Can not fetch from ${MASTER_REPO}" $REPO_DIR
                 fi
             else
                 msg "mirroring ${NAME} for the first time"
@@ -121,21 +123,30 @@ function mirror () {
             run "git fetch local_master --tags" ${REPO_DIR}
             run "git fetch local_master" ${REPO_DIR}
             merge_all_branches $NAME local_master $REPO_DIR
-            if [ $FAILED -ne 0 ]
-            then
+            if [ $FAILED -ne 0 ]; then
                 pushd ${REPO_DIR} > /dev/null
                 STATUS_OUT=$(git status)
                 popd > /dev/null                
-                error "git-mirror.sh failed on ${MIRROR_REPO}" "STATUS:\n$STATUS_OUT \n\n------------\n FAILED COMMANDS:\n$FAILED_CMDS"
+                error "git-mirror.sh failed on ${MIRROR_REPO}" "STATUS:\n$STATUS_OUT \n\n------------\n FAILED COMMANDS:\n$FAILED_CMDS" $REPO_DIR
             else
                 run "git push --tags local_master" ${REPO_DIR}
                 push_all_branches $NAME local_master $REPO_DIR
+
+                # success, remove previous check file if any
+                CHECK_FILE=$REPO_DIR/$NOTIFIED_FILE
+                rm -f $CHECK_FILE
             fi
         fi
     done
 }
 
 
+function failure () {
+    msg "Received signal - cleaning up $RUNNING_FILE and aborting"
+    rm -f $RUNNING_FILE
+    exit 1
+}
+
 while getopts ":hq" opt
 do
   case $opt in
@@ -144,7 +155,7 @@ do
           break
           ;;
       h)
-          echo "USAGE: $0 [-q] REPONAME*"
+          echo "Usage: $0 [-q] REPONAME*"
           exit 1
           ;;
       \?)
@@ -153,6 +164,24 @@ do
   esac
 done
 
+
+# is the stamp older than an hour ? 
+# in minutes
+GRACE=60
+is_old=$(find $RUNNING_FILE -mmin +$GRACE 2> /dev/null)
+if [ -n "$is_old" ] ; then
+    msg "$RUNNING_FILE is older than $GRACE minutes - removing"
+    rm -f $RUNNING_FILE
+fi
+
+if [ -f $RUNNING_FILE ] ; then
+    msg "Found $RUNNING_FILE - another git-mirror seems to be running. Aborting... " 
+    exit 1
+fi
+
+trap failure ERR INT
+
 shift $((OPTIND-1))
+date > $RUNNING_FILE
 mirror $@
-
+rm -f $RUNNING_FILE