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

4
# Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
Klaus Aehlig's avatar
Klaus Aehlig committed
5
# All rights reserved.
6
#
Klaus Aehlig's avatar
Klaus Aehlig committed
7 8 9
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
10
#
Klaus Aehlig's avatar
Klaus Aehlig committed
11 12
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
13
#
Klaus Aehlig's avatar
Klaus Aehlig committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29

30 31 32 33 34 35 36
set -e -u

@SHELL_ENV_INIT@

# Overridden by unittest
: ${CHECK_CERT_EXPIRED:=$PKGLIBDIR/check-cert-expired}

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
usage() {
    echo "Usage: $0 node|master" 2>&1
    exit $1
}

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

65
readonly CRYPTO_DIR=$RUN_DIR/crypto
66
readonly QUEUE_ARCHIVE_DIR=$DATA_DIR/queue/archive
67

68 69 70 71
in_cluster() {
  [[ -e $DATA_DIR/ssconf_master_node ]]
}

72 73 74 75 76 77 78 79 80 81 82 83 84
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
}

85 86 87 88 89 90
cleanup_watcher() {
  # Return if machine is not part of a cluster
  in_cluster || return 0

  # Remove old watcher files
  find $DATA_DIR -maxdepth 1 -type f -mtime +$REMOVE_AFTER \
91 92
    \( -name 'watcher.*-*-*-*.data' -or \
       -name 'watcher.*-*-*-*.instance-status' \) -print0 | \
93 94 95
  xargs -r0 rm -vf
}

96 97 98 99 100 101 102 103 104 105 106 107
cleanup_master() {
  # Return if machine is not part of a cluster
  in_cluster || 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
}

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

111 112 113 114 115 116 117 118 119 120 121 122 123
# 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)"
124

125 126 127
# Switch to a working directory accessible to the cleaner
cd $CLEANER_LOG_DIR

128 129 130
# Remove old cleaner log files
find $CLEANER_LOG_DIR -maxdepth 1 -type f | sort | head -n -$KEEP_LOGS | \
xargs -r rm -vf
131

132 133 134 135 136 137 138 139 140
case "$1" in
  node)
    cleanup_node
    cleanup_watcher
    ;;
  master)
    cleanup_master
    ;;
esac
141 142

exit 0