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