Tag: CentOS
以往我們都採用手動覆蓋的方式來升級 Mattermost
今天在無意間找到了可以使用社群編寫好的『腳本』自動升級
以下為實作紀錄
登入伺服器
ssh 192.168.1.230
su
新增社群提供的腳本
vim upgrade_mattermost.sh
內容直接貼上
#!/usr/bin/env 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="/opt/mattermost"
# Backup path
backupdir="/opt"
# Temporary path for download
downloaddir="/tmp"
# Specify the edition you use
edition="Team"
#edition="Enterprise"
# Start with plugins? Set 1 for starting with plugins active
plugins=0
# Set 1 for overriding the database backup question
backupdatabase=0
################################################################################
# Check dependencies
if ((EUID != 0)); then
echo "[-] This script needs to be run as root to work properly. Aborted."
exit 1
fi
if ! type "systemctl" >/dev/null 2>&1 && ! type "service" >/dev/null 2>&1; then
echo "[-] The daemon manager (systemd or sysvinit) 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
test -d ${backup}
test -d ${downloaddir}
test -d ${mattermostdir}
# Ask for database backup
if [ "${backupdatabase}" -eq 0 ]; then
read -r -p "[?] Do you have a current backup of the Mattermost database? [Y/n] " input
case "$input" in
[yY])
echo "[+] Starting the update process of Mattermost..."
;;
*)
echo "[-] Please create a backup and start again"
exit 1
;;
esac
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}"
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 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 "[+] Creating backup of Mattermost..."
cp -ra "${mattermostdir}" "${backupdir}/mattermost-backup-$(date +'%F-%H-%M')/"
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 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
賦予腳本可執行的權限
chmod +x ./upgrade_mattermost.sh
透過腳本自動升級
./upgrade_mattermost.sh 5.32.1
[?] Do you have a current backup of the Mattermost database? [Y/n] y
[+] Starting the update process of Mattermost…
[?] A previous download exists. Do you want to replace it by a new one? [Y/n y
[+] Remove previous download.
[+] Downloading Mattermost Team "5.32.1"…
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 207M 100 207M 0 0 11.2M 0 0:00:18 0:00:18 --:--:-- 12.0M
[+] The Mattermost update package has been downloaded with successful
[+] Extracting Mattermost update package…
[+] Stopping Mattermost service…
[+] Creating backup of Mattermost…
[+] Preparing update…
[+] Renaming plugin folders…
[+] Updating Mattermost…
[+] Cleaning Mattermost temporary files…
[+] Allowing Mattermost to run on port 0-1023…
[+] Starting Mattermost service…
[+] Mattermost updated with successful
Dont forget to reactivate your plugins
mv "/opt/mattermost/plugins~" "/opt/mattermost/plugins"
mv "/opt/mattermost/client/plugins" "/opt/mattermost/client/plugins~"
到此就完成了,未來要再升級可使用此語法如法泡製
./upgrade_mattermost.sh 版本號
參考資料
目前在 GCloud 託管的網站經常會出現不明情況死機
進去使用 TOP 稍微檢查一下,發現記憶體的使用量非常大
使用下面語法檢查 PHP-FPM 每一個行程平均記憶體使用量,發現每個行程約用 170 MB
ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'
使用下面語法檢查共有多少開啟的行程,可以發現有許多開啟中的行程
ps -ylC php-fpm --sort:rss
根據分析我們的網站同時上線人數並沒有很多,所以初步判定是設定上的問題
vim /etc/php-fpm.d/www.conf
pm.max_requests
每個子程序在接受多少請求後自動重啟,可有效防止記憶體溢出,預設值等於0,如果等於0不會自動終止。
網路上部份設置較大52100、102400之類的,建議可以抓在1000左右,基礎服務量提高應先調整線程數為主要重點,雖然重啟會消耗資源跟風險比起來安全更重要。
如果是瞬間流量就是要調整此數值來對應,數字越大瞬間能接受的高峰量越高,調整線程數是每秒一次,超過一秒仍有需求後會另開新線程來輔助。
pm.max_requests = 1000
修改完後重啟服務
systemctl restart php-fpm
修改這個參數後,同時開啟的行程只剩下 9 個,記憶體平均使用量只剩下 100MB
還需要再繼續觀察看看
2020.04.14
同時開啟的行程共有 35 個,每個消耗 123M,但網站線上只有三個人
再次調整設定
pm.max_requests = 1000 pm.max_spare_servers = 35
改成
pm.max_requests = 500 pm.max_spare_servers = 20
看看能否讓線程提早關閉,並減少閒置的線程
經過兩天的測試,同時開啟的行程剩下 15 個,平均每個消耗 88M
於 2020/06/28
經過兩天的測試,同時開啟的行程達到 20 個,平均每個消耗 84M,共消耗 1680 MB
試著再調整看看
pm.max_requests = 400 pm.max_spare_servers = 15
於 2020/07/02
同時開啟的行程達到 15 個,平均每個消耗 51M,共消耗 765MB
剩餘記憶體還有 1042M,在可以接受的範圍內