]> Frank Brehm's Git Trees - profitbricks/jenkins-build-scripts.git/commitdiff
implement garbage collection of liveboot images, except for updating cidb after remov...
authorHolger Levsen <holger@layer-acht.org>
Wed, 14 Nov 2012 18:02:12 +0000 (19:02 +0100)
committerHolger Levsen <holger@layer-acht.org>
Wed, 14 Nov 2012 18:02:12 +0000 (19:02 +0100)
keep_liveboot.py
liveboot_garbage_collection.sh

index b24a00a02405634241aec245bd0ac4efbf401284..e9ff5a91bf2541ff8fd00a15300af571e577be7a 100755 (executable)
@@ -16,15 +16,22 @@ from cidb import *
 
 logger = logger_init()
 
-def keep_liveboot(name):
+def liveboot_to_keep(name):
     con = db_connect()
     cur = con.cursor()
     try:
-        cur.execute("SELECT keep_image FROM liveboot WHERE build_name = '%s'" % (name))
+        cur.execute("SELECT keep_image FROM liveboot WHERE build_name = '%s' and keep_image = true" % (name))
     except psycopg2.DatabaseError as e:
         logger.debug("SELECT keep_image FROM liveboot WHERE build_name = '%s' and keep_image = true" % (name))
         return False
     if cur.fetchone():
         return True
 
+if __name__ == '__main__':
+    if len(sys.argv) != 2:
+        print("usage: %s $liveboot_name")
+        sys.exit(1)
+    name = sys.argv[1]
+    if not liveboot_to_keep(name):
+       sys.exit(1)
 
index 0c514ee4438e511de9b48e308c0bec376df5fb18..17a838e6ede23b9de0999c13cd217788e3d4ab4b 100755 (executable)
@@ -3,19 +3,48 @@
 # copyright 2012 Holger Levsen <holger@layer-acht.org>
 # GPL2 licenced
 
-set -x
-export
+#set -x
+#export
 
-# some settings
+# no configuration below this block
 AMOUNT_TO_KEEP=15
-KEPT=0
+LIVEBOOT_IMAGE_PATH="/srv/mirror/liveboot"
 
-for DIRECTORY in $(ls -1r) ; do
-       echo -n $DIRECTORY
-       if ! ./keep_liveboot.py "$DIRECTORY" && [ $KEPT -le $AMOUNT_TO_KEEP ] ; then
-               echo we can delete this one
-       else
-               echo keep it
+echo
+echo "liveboot garbage collection"
+echo "---------------------------"
+echo "- first goes through all liveboot images and keeps those with the 'keep'-flag set"
+echo "- second, it will go through all liveboot images (started with the latest) and keeps images until $AMOUNT_TO_KEEP liveboot images are kept. All the others will be deleted."
+echo
+
+KEPT=0
+FIRST_PASS=$(ls -1r $LIVEBOOT_IMAGE_PATH)
+SECOND_PASS=""
+for DIRECTORY in $FIRST_PASS ; do
+       if ./keep_liveboot.py "$DIRECTORY" ; then 
+               echo -n "$DIRECTORY: "
+               echo keep flag set, keeping it.
+       else 
+               SECOND_PASS="$SECOND_PASS $DIRECTORY"
+       fi
+done
+for DIRECTORY in $SECOND_PASS ; do
+       echo -n "$DIRECTORY: "
+       if  [ $KEPT -le $AMOUNT_TO_KEEP ] ; then
+               echo keep it, there is enough space left...
                let "KEPT=KEPT+1"
+       else
+               echo "deleted."
+               echo rm -rf "$LIVEBOOT_IMAGE_PATH/$DIRECTORY"
+               echo FIXME: still need to remove the liveboot from cidb...
        fi
 done
+echo
+echo
+
+echo "Existing liveboot images after garbage collection:"
+ls -1r $LIVEBOOT_IMAGE_PATH
+echo 
+echo "Free diskspace on sagunt:"
+df -h $LIVEBOOT_IMAGE_PATH
+echo "The liveboot images are copied to other servers which might not have as much space available."