ganeti-cleaner.in 2.21 KB
Newer Older
1
2
3
#!/bin/bash
#

4
# Copyright (C) 2009, 2010 Google Inc.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#
# 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
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

set -e

23
24
25
26
27
28
29
30
31
32
33
34
cleanup_master() {
  # Return if machine is not part of a cluster
  [[ -e $DATA_DIR/ssconf_master_node ]] || return 0

  # 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
}

35
36
37
38
39
40
41
42
43
44
45
46
47
cleanup_node() {
  # Return if directory for crypto keys doesn't exist
  [[ -d $CRYPTO_DIR ]] || return 0

  find $CRYPTO_DIR -mindepth 1 -maxdepth 1 -type d | \
  while read dir; do
    if $CHECK_CERT_EXPIRED $dir/cert; then
      rm -vf $dir/{cert,key}
      rmdir -v --ignore-fail-on-non-empty $dir
    fi
  done
}

48
49
50
51
52
53
# Overridden by unittest
: ${LOCALSTATEDIR:=@LOCALSTATEDIR@}
: ${CHECK_CERT_EXPIRED:=@PKGLIBDIR@/check-cert-expired}

DATA_DIR=$LOCALSTATEDIR/lib/ganeti
CLEANER_LOG_DIR=$LOCALSTATEDIR/log/ganeti/cleaner
54
QUEUE_ARCHIVE_DIR=$DATA_DIR/queue/archive
55
CRYPTO_DIR=$LOCALSTATEDIR/run/ganeti/crypto
56
57
58
59

# Define how many days archived jobs should be left alone
REMOVE_AFTER=21

60
61
62
63
64
65
66
67
68
69
70
71
72
# Define how many log files to keep around (usually one per day)
KEEP_LOGS=50

# Log file for this run
LOG_FILE=$CLEANER_LOG_DIR/cleaner-$(date +'%Y-%m-%dT%H_%M').$$.log

# Create log directory
mkdir -p $CLEANER_LOG_DIR

# Redirect all output to log file
exec >>$LOG_FILE 2>&1

echo "Cleaner started at $(date)"
73

74
75
76
# Remove old cleaner log files
find $CLEANER_LOG_DIR -maxdepth 1 -type f | sort | head -n -$KEEP_LOGS | \
xargs -r rm -vf
77

78
cleanup_master
79
cleanup_node
80
81

exit 0