-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathqbm-update.sh
More file actions
executable file
·67 lines (60 loc) · 2.29 KB
/
qbm-update.sh
File metadata and controls
executable file
·67 lines (60 loc) · 2.29 KB
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
#!/usr/bin/env bash
set -e
set -o pipefail
force_update=${1:-false}
# Constants
QBM_PATH="/opt/qbit_manage"
QBM_VENV_PATH="/opt/.venv/qbm-venv"
QBM_SERVICE_NAME="qbmanage"
QBM_UPSTREAM_GIT_REMOTE="origin"
QBM_VERSION_FILE="$QBM_PATH/VERSION"
QBM_REQUIREMENTS_FILE="$QBM_PATH/pyproject.toml"
CURRENT_UID=$(id -un)
# Check if QBM is installed and if the current user owns it
check_qbm_installation() {
if [ -d "$QBM_PATH" ]; then
qbm_repo_owner=$(stat --format='%U' "$QBM_PATH")
qbm_repo_group=$(stat --format='%G' "$QBM_PATH")
if [ "$qbm_repo_owner" != "$CURRENT_UID" ]; then
echo "You do not own the QbitManage repo. Please run this script as the user that owns the repo [$qbm_repo_owner]."
echo "use 'sudo -u $qbm_repo_owner -g $qbm_repo_group qbm-update'"
exit 1
fi
else
echo "QbitManage folder does not exist. Please install QbitManage before running this script."
exit 1
fi
}
# Update QBM if necessary
update_qbm() {
current_branch=$(git -C "$QBM_PATH" rev-parse --abbrev-ref HEAD)
echo "Current Branch: $current_branch. Checking for updates..."
git -C "$QBM_PATH" fetch
if [ "$(git -C "$QBM_PATH" rev-parse HEAD)" = "$(git -C "$QBM_PATH" rev-parse @'{u}')" ] && [ "$force_update" != true ]; then
current_version=$(cat "$QBM_VERSION_FILE")
echo "=== Already up to date $current_version on $current_branch ==="
exit 0
fi
current_requirements=$(sha1sum "$QBM_REQUIREMENTS_FILE" | awk '{print $1}')
git -C "$QBM_PATH" reset --hard "$QBM_UPSTREAM_GIT_REMOTE/$current_branch"
}
# Update virtual environment if requirements have changed
update_venv() {
new_requirements=$(sha1sum "$QBM_REQUIREMENTS_FILE" | awk '{print $1}')
if [ "$current_requirements" != "$new_requirements" ] || [ "$force_update" = true ]; then
echo "=== Requirements changed, updating venv ==="
"$QBM_VENV_PATH/bin/python" -m pip install --upgrade "$QBM_PATH"
fi
}
# Restart the QBM service
restart_service() {
echo "=== Restarting QBM Service ==="
sudo systemctl restart "$QBM_SERVICE_NAME"
new_version=$(cat "$QBM_VERSION_FILE")
echo "=== Updated to $new_version on $current_branch"
}
# Main script execution
check_qbm_installation
update_qbm
update_venv
restart_service