Statistics
| Branch: | Tag: | Revision:

root / daemons / ganeti-cleaner.in @ a30b473c

History | View | Annotate | Download (2.2 kB)

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