Make build_chroot compatible with Squeeze
[ganeti-local] / daemons / ganeti-cleaner.in
1 #!/bin/bash
2 #
3
4 # Copyright (C) 2009, 2010, 2011, 2012 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 -u
22
23 @SHELL_ENV_INIT@
24
25 # Overridden by unittest
26 : ${CHECK_CERT_EXPIRED:=$PKGLIBDIR/check-cert-expired}
27
28 usage() {
29     echo "Usage: $0 node|master" 2>&1
30     exit $1
31 }
32
33 if [[ "$#" -ne 1 ]]; then
34   usage 1
35 fi
36
37 case "$1" in
38   node)
39     readonly CLEANER_LOG_DIR=$LOG_DIR/cleaner
40     ;;
41   master)
42     readonly CLEANER_LOG_DIR=$LOG_DIR/master-cleaner
43     ;;
44   --help-completion)
45     echo "choices=node,master 1 1"
46     exit 0
47     ;;
48   --help)
49     usage 0
50     ;;
51   *)
52     usage 1
53     ;;
54 esac
55
56 readonly CRYPTO_DIR=$RUN_DIR/crypto
57 readonly QUEUE_ARCHIVE_DIR=$DATA_DIR/queue/archive
58
59 in_cluster() {
60   [[ -e $DATA_DIR/ssconf_master_node ]]
61 }
62
63 cleanup_node() {
64   # Return if directory for crypto keys doesn't exist
65   [[ -d $CRYPTO_DIR ]] || return 0
66
67   find $CRYPTO_DIR -mindepth 1 -maxdepth 1 -type d | \
68   while read dir; do
69     if $CHECK_CERT_EXPIRED $dir/cert; then
70       rm -vf $dir/{cert,key}
71       rmdir -v --ignore-fail-on-non-empty $dir
72     fi
73   done
74 }
75
76 cleanup_watcher() {
77   # Return if machine is not part of a cluster
78   in_cluster || return 0
79
80   # Remove old watcher files
81   find $DATA_DIR -maxdepth 1 -type f -mtime +$REMOVE_AFTER \
82     \( -name 'watcher.*-*-*-*.data' -or \
83        -name 'watcher.*-*-*-*.instance-status' \) -print0 | \
84   xargs -r0 rm -vf
85 }
86
87 cleanup_master() {
88   # Return if machine is not part of a cluster
89   in_cluster || return 0
90
91   # Return if queue archive directory doesn't exist
92   [[ -d $QUEUE_ARCHIVE_DIR ]] || return 0
93
94   # Remove old jobs
95   find $QUEUE_ARCHIVE_DIR -mindepth 2 -type f -mtime +$REMOVE_AFTER -print0 | \
96   xargs -r0 rm -vf
97 }
98
99 # Define how many days archived jobs should be left alone
100 REMOVE_AFTER=21
101
102 # Define how many log files to keep around (usually one per day)
103 KEEP_LOGS=50
104
105 # Log file for this run
106 LOG_FILE=$CLEANER_LOG_DIR/cleaner-$(date +'%Y-%m-%dT%H_%M').$$.log
107
108 # Create log directory
109 mkdir -p $CLEANER_LOG_DIR
110
111 # Redirect all output to log file
112 exec >>$LOG_FILE 2>&1
113
114 echo "Cleaner started at $(date)"
115
116 # Remove old cleaner log files
117 find $CLEANER_LOG_DIR -maxdepth 1 -type f | sort | head -n -$KEEP_LOGS | \
118 xargs -r rm -vf
119
120 case "$1" in
121   node)
122     cleanup_node
123     cleanup_watcher
124     ;;
125   master)
126     cleanup_master
127     ;;
128 esac
129
130 exit 0