-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_mattermost.sh
executable file
·213 lines (172 loc) · 6.88 KB
/
update_mattermost.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/bash
# Immediately exit if a command run from a loop, a pipeline or a compound
# command statement fails or a variable is used unset.
set -e
################################################################################
# Configuration - please adapt it to your environment
################################################################################
# Mattermost path
mattermostdir="/home/$USER/mattermost"
# Backup path
backupdir="/home/$USER/mattermost-backup"
# Temporary path for download
downloaddir="/home/$USER/tmp"
# Specify the edition you use
edition="Team"
#edition="Enterprise"
# Start with plugins? Set 1 for starting with plugins active
plugins=1
################################################################################
# Check dependencies
if ! type "systemctl" >/dev/null 2>&1 && ! type "service" >/dev/null 2>&1 && ! type "supervisorctl" >/dev/null 2>&1; then
echo "[-] The daemon manager (systemd, sysvinit or supervisorctl) is not accessible. Aborted."
exit 1
fi
if ! type "wget" >/dev/null 2>&1 && ! type "curl" >/dev/null 2>&1; then
echo "[-] A download tool like wget or curl is not accessible. Aborted."
exit 1
fi
# Check requirements
if [[ "${edition}" != "Team" ]] && [[ "${edition}" != "Enterprise" ]]; then
echo "[-] The edition must either be \"Team\" or \"Enterprise\". Aborted."
exit 1
fi
# Check config variables
if [ ! -d "$mattermostdir" ]; then
echo "Mattermost directory not found. Please check config"
exit 1
fi
if [ ! -d "$downloaddir" ]; then
echo "Download directory not found. Please check config"
exit 1
fi
if [ ! -d "$backupdir" ]; then
echo "Backup directory not found. Please check config"
exit 1
fi
# Check if Mattermost exists in the path provided above
if [ ! -f "${mattermostdir}/bin/mattermost" ]; then
echo "Mattermost not found please check the path for the Mattermost directory"
exit 1
fi
# Get version from argument
if [ -z "${1}" ]; then
echo "Please specify the version of Mattermost to download"
exit 1
fi
version="${1}"
# Ask to clean backup dir
read -r -p "[?] Do you want to clean backup directory [Y/n] " input
case "$input" in
[yY])
echo "[+] Cleaning Backup directory..."
rm -f "${backupdir}"/mattermost-backup-*.tar.gz
;;
*)
echo "[-] Skipping cleaning"
;;
esac
# Ask for backup
read -r -p "[?] Do you want to backup mattermost first? [Y/n] " input
case "$input" in
[yY])
echo "[+] Creating backup of Mattermost..."
tar -czvf "${backupdir}/mattermost-backup-$(date +'%F-%H-%M').tar.gz" "${mattermostdir}" >/dev/null 2>&1
echo "[+] Starting the update process of Mattermost..."
;;
*)
echo "[-] Skipping backup"
;;
esac
if [[ "${edition}" == "Team" ]]; then
url="https://releases.mattermost.com/${version}/mattermost-team-${version}-linux-amd64.tar.gz"
else
url="https://releases.mattermost.com/${version}/mattermost-${version}-linux-amd64.tar.gz"
fi
# Main
# Get the file
function get_the_file() {
echo "[+] Downloading Mattermost ${edition} \"${version}\"..."
if type "curl" >/dev/null 2>&1; then
if ! curl -LC - "${url}" -o "${downloaddir}/mattermost-upgrade.tar.gz"; then
echo "[-] An issue occurred when downloading the Mattermost update package."
exit 1
fi
else
if ! wget "${url}" -o "${downloaddir}/mattermost-upgrade.tar.gz"; then
echo "[-] An issue occurred when downloading the Mattermost update package."
exit 1
fi
fi
echo "[+] The Mattermost update package has been downloaded with successful"
}
# Check previous download
if [ -e "${downloaddir}/mattermost-upgrade.tar.gz" ]; then
read -r -p "[?] A previous download exists. Do you want to replace it by a new one? [Y/n " input
case "$input" in
[yY])
echo "[+] Remove previous download."
rm -rf "${downloaddir}/mattermost-upgrade.tar.gz"
get_the_file
;;
esac
else
get_the_file
fi
echo "[+] Extracting Mattermost update package..."
mkdir -p "${downloaddir}/mattermost-upgrade"
tar -xf "${downloaddir}/mattermost-upgrade.tar.gz" -C "${downloaddir}/mattermost-upgrade/"
echo "[+] Stopping Mattermost service..."
if type supervisorctl >/dev/null 2>&1; then
supervisorctl stop mattermost >/dev/null 2>&1
elif type systemctl >/dev/null 2>&1; then
systemctl stop mattermost
else
service mattermost stop
fi
if pgrep mattermost > /dev/null; then
echo "[-] Mattermost is still running. Update not possible. Aborting..."
rm -rf "${downloaddir}/mattermost-upgrade"
rm -f "${downloaddir}/mattermost-upgrade.tar.gz"
exit 1
fi
echo "[+] Preparing update..."
USER="$(stat -c '%U' ${mattermostdir}/bin/mattermost)"
GROUP="$(stat -c '%G' ${mattermostdir}/bin/mattermost)"
chown -hR "$USER":"$GROUP" "${downloaddir}/mattermost-upgrade/"
# Clean up Mattermost directory
find "${mattermostdir}" -mindepth 1 -maxdepth 1 -not \( -path "${mattermostdir}/config" -o -path "${mattermostdir}/logs" -o -path "${mattermostdir}/plugins" -o -path "${mattermostdir}/data" -o -path "${mattermostdir}/client" \) -exec rm -rf {} \;
find "${mattermostdir}/client" -mindepth 1 -maxdepth 1 -not \( -path "${mattermostdir}/client/plugins" \) -exec rm -rf {} \;
# Rename plugin directory
if [ "${plugins}" -eq 0 ]; then
echo "[+] Renaming plugin folders..."
if [ -d "${mattermostdir}/plugins/" ]; then
mv "${mattermostdir}/plugins/" "${mattermostdir}/plugins~"
fi
if [ -d "${mattermostdir}/client/plugins/" ]; then
mv "${mattermostdir}/client/plugins/" "${mattermostdir}/client/plugins~"
fi
fi
echo "[+] Updating Mattermost..."
cp -an "${downloaddir}/mattermost-upgrade/mattermost/"* "${mattermostdir}"
echo "[+] Cleaning Mattermost temporary files..."
rm -rf "${downloaddir}/mattermost-upgrade/"
rm -f "${downloaddir}/mattermost-upgrade.gz"
#echo "[+] Allowing Mattermost to run on port 0-1023..."
#setcap cap_net_bind_service=+ep "${mattermostdir}/bin/mattermost"
echo "[+] Starting Mattermost service..."
if type supervisorctl >/dev/null 2>&1; then
supervisorctl start mattermost >/dev/null 2>&1
elif type systemctl >/dev/null 2>&1; then
systemctl start mattermost
else
service mattermost start
fi
echo "[+] Mattermost updated with successful"
if [ "${plugins}" -eq 0 ]; then
echo "*************************************************"
echo "Dont forget to reactivate your plugins"
echo "mv \"${mattermostdir}/plugins~\" \"${mattermostdir}/plugins\""
echo "mv \"${mattermostdir}/client/plugins\" \"${mattermostdir}/client/plugins~\""
echo "*************************************************"
fi