-
Notifications
You must be signed in to change notification settings - Fork 8
/
pic-update.sh
executable file
·66 lines (59 loc) · 2.26 KB
/
pic-update.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
#!/usr/bin/env bash
set -e
set -o pipefail
force_update=${1:-false}
# Constants
PIC_PATH="/opt/Plex-Image-Cleanup"
PIC_VENV_PATH="/opt/.venv/pmm-image"
PIC_SERVICE_NAME="pmm-image"
PIC_UPSTREAM_GIT_REMOTE="origin"
PIC_VERSION_FILE="$PIC_PATH/VERSION"
PIC_REQUIREMENTS_FILE="$PIC_PATH/requirements.txt"
CURRENT_UID=$(id -u)
# Check if Plex-Image-Cleanup is installed and the current user owns it
check_pic_installation() {
if [ -d "$PIC_PATH" ]; then
local pic_repo_owner
pic_repo_owner=$(stat -c '%u' "$PIC_PATH")
if [ "$pic_repo_owner" != "$CURRENT_UID" ]; then
echo "You do not own the Plex-Image-Cleanup repo. Please run this script as the user that owns the repo [$pic_repo_owner]."
exit 1
fi
else
echo "Plex-Image-Cleanup folder does not exist. Please install Plex-Image-Cleanup before running this script."
exit 1
fi
}
# Update Plex-Image-Cleanup if necessary
update_pic() {
current_branch=$(git -C "$PIC_PATH" rev-parse --abbrev-ref HEAD)
echo "Current Branch: $current_branch. Checking for updates..."
git -C "$PIC_PATH" fetch
if [ "$(git -C "$PIC_PATH" rev-parse HEAD)" = "$(git -C "$PIC_PATH" rev-parse @'{u}')" ] && [ "$force_update" != true ]; then
current_version=$(cat "$PIC_VERSION_FILE")
echo "=== Already up to date $current_version on $current_branch ==="
exit 0
fi
git -C "$PIC_PATH" reset --hard "$PIC_UPSTREAM_GIT_REMOTE/$current_branch"
}
# Update venv if necessary
update_venv() {
current_requirements=$(sha1sum "$PIC_REQUIREMENTS_FILE" | awk '{print $1}')
new_requirements=$(sha1sum "$PIC_REQUIREMENTS_FILE" | awk '{print $1}')
if [ "$current_requirements" != "$new_requirements" ] || [ "$force_update" = true ]; then
echo "=== Requirements changed, updating venv ==="
"$PIC_VENV_PATH/bin/python3" "$PIC_VENV_PATH/bin/pip" install -r "$PIC_REQUIREMENTS_FILE"
fi
}
# Restart the Plex-Image-Cleanup service
restart_service() {
echo "=== Restarting Plex-Image-Cleanup Service ==="
sudo systemctl restart "$PIC_SERVICE_NAME"
new_version=$(cat "$PIC_VERSION_FILE")
echo "=== Updated to $new_version on $current_branch ==="
}
# Main script execution
check_pic_installation
update_pic
update_venv
restart_service