From 6d16e66f91fd3f42b741c5d9fb33bd0e2001ad82 Mon Sep 17 00:00:00 2001
From: david_williams <david_williams>
Date: Sat, 12 Feb 2011 19:56:12 +0000
Subject: [PATCH] improve daily cleanup scripts

---
 releng.control/cleanupArtifacts.sh    |  8 +--
 releng.control/removeArtifactDirIf.sh |  5 ++
 releng.control/removeIf.sh            |  5 ++
 releng.control/removeUtils.shsource   | 95 +++++++++++++++++++++++++++
 4 files changed, 109 insertions(+), 4 deletions(-)
 create mode 100644 releng.control/removeArtifactDirIf.sh
 create mode 100644 releng.control/removeIf.sh
 create mode 100644 releng.control/removeUtils.shsource

diff --git a/releng.control/cleanupArtifacts.sh b/releng.control/cleanupArtifacts.sh
index f2318d513..d5b711034 100644
--- a/releng.control/cleanupArtifacts.sh
+++ b/releng.control/cleanupArtifacts.sh
@@ -186,9 +186,9 @@ removeOldDirectoriesAndFiles ${RECOMMENDED_TMP_DIR} 13;
 # autodownload so when if/when we want it, we have to get our selves, and copy into prereqs directory.
 # And one of those, I think oagis_release? has some write protected files that prevents their deletion, 
 # once unzipped, so we have tweeked the permissions. 
-echo "INFO: Checking to remove old pre-req directories and files not accessed for 1 month."
-removeOldDirectoriesAndFiles ${LOCAL_PREREQS_CACHE} 30;
+echo "INFO: Checking to remove old pre-req directories and files not accessed for 3 months."
+removeOldDirectoriesAndFiles ${LOCAL_PREREQS_CACHE} 90;
 
-echo "INFO: Checking to remove old pre-req directories and files not accessed for 1 month."
-removeOldDirectories ${BASE_BUILDERS} 30;
+echo "INFO: Checking to remove old basebuilders not accessed for 3 months."
+removeOldDirectories ${BASE_BUILDERS} 90;
 
diff --git a/releng.control/removeArtifactDirIf.sh b/releng.control/removeArtifactDirIf.sh
new file mode 100644
index 000000000..046437f95
--- /dev/null
+++ b/releng.control/removeArtifactDirIf.sh
@@ -0,0 +1,5 @@
+#!/usr/bin/env bash
+
+source removeUtils.shsource
+
+return removeArtifactsDirIf "$*"
diff --git a/releng.control/removeIf.sh b/releng.control/removeIf.sh
new file mode 100644
index 000000000..4d83fc43d
--- /dev/null
+++ b/releng.control/removeIf.sh
@@ -0,0 +1,5 @@
+#!/usr/bin/env bash
+
+source removeUtils.shsource
+
+return removeIf "$*"
diff --git a/releng.control/removeUtils.shsource b/releng.control/removeUtils.shsource
new file mode 100644
index 000000000..508718ac6
--- /dev/null
+++ b/releng.control/removeUtils.shsource
@@ -0,0 +1,95 @@
+#!/usr/bin/env bash
+
+# simple function that can be called via 'find' that removes and displays name of removed directory
+function removeIf () 
+{
+       # echo "arg: $1";
+       
+       if [ -z $1 ]
+         then
+           echo "ERROR: No argument. This function requires a directory as an argument. " ;
+           return 1;
+         fi
+       foundDirectory=$1
+       
+       if [ ! -d $foundDirectory ] 
+       then
+         echo "ERROR: " "${foundDirectory}" ", is not a directory. This function requires a directory as an argument. "
+         return 2;
+       fi 
+       
+       # should already be in foundDirectory, if execDir used (and execDir, in a 'find' is recommended, as more secure)
+       cd $foundDirectory
+       
+       rm -fr $foundDirectory
+       rc=$?
+       if [ rc == 0 ]
+       then 
+         echo "               removed: $foundDirectory";
+       else
+         echo "WARNING: rc: " $rc " could not remove " $foundDirectory;
+       fi 
+       
+       return rc;
+}
+
+# function that can remove a directory (e.g. via find) but checks to make sure not to remove the last one (or, last 'saveAtLeast')
+function removeArtifactsIf ()
+{
+       # echo "arg: $1";
+       
+       if [ -z $1 ]
+         then
+           echo "ERROR: No argument. This function requires a directory as an argument. " ;
+           return 1;
+         fi
+       foundDirectory=$1
+       nSave=$2
+       
+       if [ -z $nSave ]
+       then
+          nSave=1;
+       fi   
+       
+       if [ ! -d $foundDirectory ] 
+       then
+         echo "ERROR: " "${foundDirectory}" ", is not a directory. This function requires a directory as an argument. "
+         return 2;
+       fi 
+       
+       # should already be in foundDirectory, if execDir used (and execDir, in a 'find' is recommended, as more secure)
+       cd $foundDirectory
+       
+       # move up one so we can examine syblings
+       cd ..
+       currentDirectory=`pwd`
+       echo "               current working directory: $currentDirectory";
+       ndirs=`ls -lA | wc -l`
+       ndirs=$(($ndirs - 1)); # don't count the "totals" line
+       # echo "NDirs: $ndirs"
+       
+       # if only one left, do not remove it, no matter how old
+       # or, as improved ... if less than or equal to nSave is left, do not remove
+       if [ $ndirs -le $nSave ]
+       then
+           return 0;
+       fi
+       # To have no directories is unexpected, since otherwise this method should not have been called. 
+       # So, this check is just a safety check.
+       if [ $ndirs -lt 1 ]
+       then
+           exit 101;
+       fi
+       
+       rm -fr $foundDirectory
+       rc=$?
+       if [ rc == 0 ]
+       then 
+         echo "               removed: $foundDirectory";
+       else
+         echo "WARNING: rc: " $rc ". could not remove " $foundDirectory;
+       fi 
+       
+       return rc;
+
+}
-- 
GitLab