File: cron-apt

package info (click to toggle)
cron-apt 0.0.6woody1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 212 kB
  • ctags: 10
  • sloc: sh: 114; makefile: 70
file content (150 lines) | stat: -rwxr-xr-x 3,989 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
# DocumentId: $Id: cron-apt,v 1.14 2002/04/24 20:15:24 ola Exp $
# Changes:
#	2002-04-07 Ola Lundqvist <opal@debian.org>
#		Added skipping of comment lines in action files.
#		Added exra config.d directory.
#		Rewrote the mail and log part to make it more flexible.
#		Christian Perrier <Christian.Perrier@onera.fr> was the
#		one that needed this flexibility.
#		Also added skipping of backup files (*~).
#	2002-03-05 Ola Lundqvist <opal@debian.org>
#		Made it complete so it can be released.
#	2002-03-03 Ola Lundqvist <opal@debian.org>
#		Wrote the beginning.
#	2002-04-24 Ola Lundqvist <opal@debian.org>
#		Added a default path. Thanks to Donovan Baarda
#		<abo@minkirri.apana.org.au> for pointing it out.

export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

############################# arguments #######################################

if [ "$1" = "--help" ] ; then
    echo "USAGE: cron-apt [configfile]"
    exit 0
fi
CONFIG=$1
if [ -z "$CONFIG" ] ; then
    CONFIG=/etc/cron-apt/config
fi

############################## defaults #######################################
ACTIONDIR=/etc/cron-apt/action.d
ACTIONCONFDIR=/etc/cron-apt/config.d
ERROR=/var/log/cron-apt/error
TEMP=/var/log/cron-apt/temp
MAIL=/var/log/cron-apt/mail
LOG=/var/log/cron-apt/log
MAILTO=root
# error, always
MAILON=error
# error, never
EXITON=error
# verbose, error
DEBUG=verbose
# general options
OPTIONS=-q

# Read configuration.
if [ -f $CONFIG ] ; then
    . $CONFIG
else
    echo "The config file $CONFIG does not exist, using defaults."
fi

############################## functions ######################################

# Send the mail to the system administrator. Will only be sent if there is
# something to send. No arguments needed.
mailit() {
    if [ -f $MAIL ] ; then
	SUBJECT="CRON-APT completed on $(uname -n)"
	if [ -f $ERROR ] ; then
	    SUBJECT="CRON-APT error on $(uname -n)"
	fi
	mail -s "$SUBJECT" "$MAILTO" < $MAIL
	rm -f $MAIL
    fi
}

# Create file with RUN information. Line data should be added later.
# Arguments:
# 1: File to create
# 2: Action command (filename) (i.e $ACTIONF)
createinfo() {
    FILE="$1"
    ACTT="CRON-APT ACTION: $ACTIONF"
    ACT="$ACTT, $(date)"
    RUNT="CRON-APT RUN:"
    RUN="$RUNT $(date)"
    touch $FILE
    if ! grep "$RUNT" $FILE > /dev/null 2>&1 ; then
	echo "$RUN" >> $FILE
    fi
    if ! grep "$ACTT" $FILE > /dev/null 2>&1 ; then
	echo "$ACT" >> $FILE
    fi
}

############################### check #########################################

if [ -e $ERROR ] ; then
    echo "Error in last update. Fix the problem and remove $ERROR."
    exit
fi

############################### script ########################################
# Always run mailit before exit.

for ACTIONF in $(ls $ACTIONDIR) ; do
    if echo "$ACTIONF" | grep "~" ; then
	# Temporary file, skipping.
	:
    elif [ -f "$ACTIONDIR/$ACTIONF" ] ; then
	if [ -f "$ACTIONCONFDIR/$ACTIONF" ] ; then
	    . "$ACTIONCONFDIR/$ACTIONF"
	fi
	STATUS=$(cat "$ACTIONDIR/$ACTIONF" | \
	    sed -e "s/#.*$//;" | \
	    grep -v "^[[:space:]]*$" | {
		while read -e LINE ; do
		    echo "CRON-APT LINE: $LINE, $(date)" > $TEMP
		    if ! apt-get $OPTIONS $LINE >> $TEMP 2>&1 ; then
			# An error has occured.
			createinfo $ERROR $ACTIONF
			cat $TEMP >> $ERROR
			if [ "$MAILON" = "error" -o "$MAILON" = "always" ] ; then
			    createinfo $MAIL $ACTIONF
			    cat $TEMP >> $MAIL
			fi
			if [ "$DEBUG" = "error" -o "$DEBUG" = "verbose" ] ; then
			    createinfo $LOG $ACTIONF
			    cat $TEMP >> $LOG
			fi
			if [ "$EXITON" = "error" ] ; then
			    echo exit
			    exit
			fi
		    else
			# No error has occured.
			if [ "$MAILON" = "always" ] ; then
			    createinfo $MAIL $ACTIONF
			    cat $TEMP >> $MAIL
			fi
			if [ "$DEBUG" = "verbose" ] ; then
			    createinfo $LOG $ACTIONF
			    cat $TEMP >> $LOG
			fi
		    fi
		done
		exit
		})
	if echo $STATUS | grep exit > /dev/null 2>&1 ; then
	    mailit
	    exit
	fi
    fi
done

mailit