Merge branch 'stable-2.6-esi' into stable-2.6-ippool-hotplug-esi
[ganeti-local] / daemons / ganeti-cleaner.in
1 #!/bin/bash
2 #
3
4 # Copyright (C) 2009, 2010, 2011 Google Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21 set -e
22
23 in_cluster() {
24   [[ -e $DATA_DIR/ssconf_master_node ]]
25 }
26
27 cleanup_master() {
28   # Return if machine is not part of a cluster
29   in_cluster || return 0
30
31   # Return if queue archive directory doesn't exist
32   [[ -d $QUEUE_ARCHIVE_DIR ]] || return 0
33
34   # Remove old jobs
35   find $QUEUE_ARCHIVE_DIR -mindepth 2 -type f -mtime +$REMOVE_AFTER -print0 | \
36   xargs -r0 rm -vf
37 }
38
39 cleanup_node() {
40   # Return if directory for crypto keys doesn't exist
41   [[ -d $CRYPTO_DIR ]] || return 0
42
43   find $CRYPTO_DIR -mindepth 1 -maxdepth 1 -type d | \
44   while read dir; do
45     if $CHECK_CERT_EXPIRED $dir/cert; then
46       rm -vf $dir/{cert,key}
47       rmdir -v --ignore-fail-on-non-empty $dir
48     fi
49   done
50 }
51
52 cleanup_watcher() {
53   # Return if machine is not part of a cluster
54   in_cluster || return 0
55
56   # Remove old watcher files
57   find $DATA_DIR -maxdepth 1 -type f -mtime +$REMOVE_AFTER \
58     \( -name 'watcher.*-*-*-*.data' -or \
59        -name 'watcher.*-*-*-*.instance-status' \) -print0 | \
60   xargs -r0 rm -vf
61 }
62
63 # Overridden by unittest
64 : ${LOCALSTATEDIR:=@LOCALSTATEDIR@}
65 : ${CHECK_CERT_EXPIRED:=@PKGLIBDIR@/check-cert-expired}
66
67 DATA_DIR=$LOCALSTATEDIR/lib/ganeti
68 CLEANER_LOG_DIR=$LOCALSTATEDIR/log/ganeti/cleaner
69 QUEUE_ARCHIVE_DIR=$DATA_DIR/queue/archive
70 CRYPTO_DIR=$LOCALSTATEDIR/run/ganeti/crypto
71
72 # Define how many days archived jobs should be left alone
73 REMOVE_AFTER=21
74
75 # Define how many log files to keep around (usually one per day)
76 KEEP_LOGS=50
77
78 # Log file for this run
79 LOG_FILE=$CLEANER_LOG_DIR/cleaner-$(date +'%Y-%m-%dT%H_%M').$$.log
80
81 # Create log directory
82 mkdir -p $CLEANER_LOG_DIR
83
84 # Redirect all output to log file
85 exec >>$LOG_FILE 2>&1
86
87 echo "Cleaner started at $(date)"
88
89 # Remove old cleaner log files
90 find $CLEANER_LOG_DIR -maxdepth 1 -type f | sort | head -n -$KEEP_LOGS | \
91 xargs -r rm -vf
92
93 cleanup_master
94 cleanup_node
95 cleanup_watcher
96
97 exit 0