Unit tests for hv_xen._GetCommand
[ganeti-local] / daemons / ganeti-cleaner.in
index ec5d8f7..75cf5f0 100644 (file)
@@ -1,7 +1,7 @@
 #!/bin/bash
 #
 
-# Copyright (C) 2009, 2010, 2011 Google Inc.
+# Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301, USA.
 
-set -e
+set -e -u
 
-in_cluster() {
-  [[ -e $DATA_DIR/ssconf_master_node ]]
-}
+@SHELL_ENV_INIT@
 
-cleanup_master() {
-  # Return if machine is not part of a cluster
-  in_cluster || return 0
+# Overridden by unittest
+: ${CHECK_CERT_EXPIRED:=$PKGLIBDIR/check-cert-expired}
 
-  # Return if queue archive directory doesn't exist
-  [[ -d $QUEUE_ARCHIVE_DIR ]] || return 0
+usage() {
+    echo "Usage: $0 node|master" 2>&1
+    exit $1
+}
 
-  # Remove old jobs
-  find $QUEUE_ARCHIVE_DIR -mindepth 2 -type f -mtime +$REMOVE_AFTER -print0 | \
-  xargs -r0 rm -vf
+if [[ "$#" -ne 1 ]]; then
+  usage 1
+fi
+
+case "$1" in
+  node)
+    readonly CLEANER_LOG_DIR=$LOG_DIR/cleaner
+    ;;
+  master)
+    readonly CLEANER_LOG_DIR=$LOG_DIR/master-cleaner
+    ;;
+  --help-completion)
+    echo "choices=node,master 1 1"
+    exit 0
+    ;;
+  --help)
+    usage 0
+    ;;
+  *)
+    usage 1
+    ;;
+esac
+
+readonly CRYPTO_DIR=$RUN_DIR/crypto
+readonly QUEUE_ARCHIVE_DIR=$DATA_DIR/queue/archive
+
+in_cluster() {
+  [[ -e $DATA_DIR/ssconf_master_node ]]
 }
 
 cleanup_node() {
@@ -55,18 +79,22 @@ cleanup_watcher() {
 
   # Remove old watcher files
   find $DATA_DIR -maxdepth 1 -type f -mtime +$REMOVE_AFTER \
-    -name 'watcher.*-*-*-*.data' -print0 | \
+    \( -name 'watcher.*-*-*-*.data' -or \
+       -name 'watcher.*-*-*-*.instance-status' \) -print0 | \
   xargs -r0 rm -vf
 }
 
-# Overridden by unittest
-: ${LOCALSTATEDIR:=@LOCALSTATEDIR@}
-: ${CHECK_CERT_EXPIRED:=@PKGLIBDIR@/check-cert-expired}
+cleanup_master() {
+  # Return if machine is not part of a cluster
+  in_cluster || return 0
 
-DATA_DIR=$LOCALSTATEDIR/lib/ganeti
-CLEANER_LOG_DIR=$LOCALSTATEDIR/log/ganeti/cleaner
-QUEUE_ARCHIVE_DIR=$DATA_DIR/queue/archive
-CRYPTO_DIR=$LOCALSTATEDIR/run/ganeti/crypto
+  # Return if queue archive directory doesn't exist
+  [[ -d $QUEUE_ARCHIVE_DIR ]] || return 0
+
+  # Remove old jobs
+  find $QUEUE_ARCHIVE_DIR -mindepth 2 -type f -mtime +$REMOVE_AFTER -print0 | \
+  xargs -r0 rm -vf
+}
 
 # Define how many days archived jobs should be left alone
 REMOVE_AFTER=21
@@ -89,8 +117,14 @@ echo "Cleaner started at $(date)"
 find $CLEANER_LOG_DIR -maxdepth 1 -type f | sort | head -n -$KEEP_LOGS | \
 xargs -r rm -vf
 
-cleanup_master
-cleanup_node
-cleanup_watcher
+case "$1" in
+  node)
+    cleanup_node
+    cleanup_watcher
+    ;;
+  master)
+    cleanup_master
+    ;;
+esac
 
 exit 0