-
Notifications
You must be signed in to change notification settings - Fork 5
/
updater4mc.sh
executable file
·165 lines (143 loc) · 4.82 KB
/
updater4mc.sh
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
#
# Mailcleaner Updater - Updater for MailCleaner Antispam
# Copyright (C) 2017 Florian Billebault <florian.billebault@gmail.com>
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
#
# This script allow to update MailCleaner from an external source
#
# Usage:
# ./updater4mc.sh [--noreboot]
#
CONFFILE=/etc/mailcleaner.conf
STATUSFILE=/var/mailcleaner/spool/mailcleaner/updater4mc.status
if [ ! -f "$CONFFILE" ]; then
echo "Not a valid MailCleaner Installation: no conf file"
echo "Not a valid MailCleaner Installation: no conf file" > $STATUSFILE
exit 1
fi
HOSTID=`grep 'HOSTID' $CONFFILE | cut -d ' ' -f3`
if [ "$HOSTID" = "" ]; then
HOSTID=1
fi
SRCDIR=`grep 'SRCDIR' $CONFFILE | cut -d ' ' -f3`
if [ "$SRCDIR" = "" ]; then
SRCDIR="/opt/mailcleaner"
fi
VARDIR=`grep 'VARDIR' $CONFFILE | cut -d ' ' -f3`
if [ "$VARDIR" = "" ]; then
VARDIR="/opt/mailcleaner"
fi
ISMASTER=$(grep 'ISMASTER' ${CONFFILE} | cut -d ' ' -f3)
HTTPPROXY=`grep -e '^HTTPPROXY' $CONFFILE | cut -d ' ' -f3`
export http_proxy=$HTTPPROXY
function realpath()
{
local __rpath=$1
shift
local f=$@
if [ -d "$f" ]; then
base=""
dir="$f"
else
base="/$(basename "$f")"
dir=$(dirname "$f")
fi
dir=$(cd "$dir" && /bin/pwd)
eval $__rpath="'$dir'"
}
realpath rpath "$0"
# Enabling copy output and error to logs
exec > >(tee -ai "${VARDIR}/log/mailcleaner/updater4mc.log")
exec 2>&1
echo "Running" > $STATUSFILE
echo "$(date +%F_%T) Launching Updater4MC"
cd "$rpath"
git fetch && BRANCH=`git status | grep 'On branch' | sed -r 's/On branch //'`
if [ "$BRANCH" != 'master' ]; then
echo "Abandoning update because Git tree at '$rpath' is not on 'master' branch."
exit 1
fi
git reset --hard @{u} && STATUS=`git pull`
if [ "$STATUS" != 'Already up-to-date.' ]; then
echo "Abandoning update because Git tree at '$rpath' is blocking changes"
exit 1
fi
cd "${SRCDIR}"
SRCSTART=$(git log --no-merges --decorate=no -n 1 | head -n 1 | cut -d' ' -f2)
${SRCDIR}/lib/updates/gitupdate.sh
BRANCH=`git status | grep 'On branch' | sed -r 's/On branch //'`
if [ "$BRANCH" != 'master' ]; then
echo "Abandoning update because Git tree at '$SRCDIR' is not on 'master' branch."
exit 1
fi
# Clean up files that have been removed
for i in $(git status | grep 'deleted by us:' | cut -d: -f 2); do
git rm $i
done
STATUS=`git pull`
# Unless there were no changes
if [ "$STATUS" != 'Already up-to-date.' ]; then
STATUS=`git status | head -n 2 | tail -n 1`
# Unless those changes were successfully applied
if [ "$STATUS" != "Your branch is up-to-date with 'origin/master'." ]; then
echo "Abandoning update because Git tree at '$SRCDIR' is blocking changes"
exit 1
fi
fi
SRCEND=$(git log --no-merges --decorate=no -n 1 | head -n 1 | cut -d' ' -f2)
[ ! -d "${VARDIR}/spool/updater" ] && mkdir "${VARDIR}/spool/updater"
for updtlib in $(find $rpath"/libs" -type f -name "*.lib" |sort |uniq)
do
echo -n "Importing library: $updtlib ..."
. "$updtlib"
echo "Done."
done
UPDATED=0
VERSION=0
for updtfile in $(find $rpath"/updates/" -type f -name "*.update" |sort |uniq)
do
if [ ! -e "${VARDIR}/spool/updater/$(basename -s'.update' ${updtfile})" ]; then
echo "Executing update: $updtfile ..."
. "$updtfile"
retcode=$?
if [ $retcode -eq 0 ]; then
touch "${VARDIR}/spool/updater/$(basename -s'.update' ${updtfile})"
UPDATED=1
VERSION=`echo $updtfile | sed 's/^.*\/\([0-9]*\)_[^\/]*$/\1/'`
elif [ $retcode -ne 1 ]; then
echo -e "\tError during ${updtfile} update. Please join logfile to your post on MailCleaner Community forum."
echo "Failed to complete update $updtfile" > $STATUSFILE
exit 1
fi
echo "End of update."
else
VERSION=`echo $updtfile | sed 's/^.*\/\([0-9]*\)_[^\/]*$/\1/'`
echo "Already updated: $updtfile ..."
fi
done
if [[ $SRCSTART != $SRCEND ]] || [[ $UPDATED == 1 ]]; then
. "${rpath}/updates/tolaunch.always" $1
fi
echo $(echo $VERSION)-$(echo ${SRCEND} | cut -b-7) > ${SRCDIR}/etc/mailcleaner/version.def
echo
echo "$(date +%F_%T) End of Updater4MC:"
echo ">> All updates done ! Follow forum announces or relaunch this script regularly."
echo ">> Logfile present here: ${VARDIR}/log/mailcleaner/updater4mc.log"
echo
rm $STATUSFILE
${SRCDIR}/scripts/cron/service_checks.pl
exit 0