-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquadlet-sync
More file actions
executable file
·114 lines (98 loc) · 3.51 KB
/
Copy pathquadlet-sync
File metadata and controls
executable file
·114 lines (98 loc) · 3.51 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
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
#!/usr/bin/bash
# Script for simple git-ops
set -e
commit_state() {
# Save current state
if [ "$CURRENT_COMMIT" != "$LAST_COMMIT" ]; then
echo -n "$CURRENT_COMMIT" > "$LAST_COMMIT_FILE"
fi
}
WORKDIR="$HOME/.quadlet-sync"
mkdir -p "$WORKDIR" "$HOME/.config/containers"
if [ "$GIT_URL" == "" ] || [ "$GIT_SUBDIR" == "" ]; then
echo "GIT_URL or GIT_SUBDIR not set!" && exit 1
fi
WORKDIR_GIT="$WORKDIR/git"
WORKDIR_GIT_SUBDIR="$WORKDIR_GIT/$GIT_SUBDIR"
WORKDIR_GIT_SUBDIR_QUADLETS="$WORKDIR_GIT_SUBDIR/quadlets"
TARGET_QUADLETS_DIR="$HOME/.config/containers/systemd"
LAST_COMMIT_FILE="$WORKDIR/last_commit"
touch "$LAST_COMMIT_FILE"
# Clone or pull
if [ ! -d "$WORKDIR_GIT" ]; then
echo "Git workdir doesn't exist, cloning..."
git clone "$GIT_URL" "$WORKDIR_GIT"
else
if [ "$GIT_URL" != "$(git -C $WORKDIR_GIT remote get-url origin)" ]; then
echo "GIT_URL changed! Please check the cached git dir remote!" && exit 1
fi
#echo "Git workdir exist, pulling latest changes..."
git -C "$WORKDIR_GIT" pull > /dev/null
fi
# Check if subdir for this podman host exists
if [ ! -d "$WORKDIR_GIT_SUBDIR" ]; then
echo "GIT_SUBDIR=$GIT_SUBDIR not found in git repo!" && exit 1
fi
if [ ! -d "$WORKDIR_GIT_SUBDIR_QUADLETS" ]; then
echo "Quadlets dir not found in git repo subdir!" && exit 1
fi
# Symlink git dir to systemd path
if [ ! -d "$TARGET_QUADLETS_DIR" ]; then
ln -s "$WORKDIR_GIT_SUBDIR_QUADLETS" "$TARGET_QUADLETS_DIR"
else
if [ ! -L "$TARGET_QUADLETS_DIR" ]; then
echo "Target quadlets dir is not symlink!" && exit 1
fi
fi
# Check changed files since the last check
LAST_COMMIT=$(cat "$LAST_COMMIT_FILE")
CURRENT_COMMIT=$(git -C "$WORKDIR_GIT" rev-parse HEAD)
if [ "$LAST_COMMIT" != "" ]; then
CHANGED_FILES=$(git -C "$WORKDIR_GIT" diff --name-only $LAST_COMMIT $CURRENT_COMMIT | grep "^$GIT_SUBDIR/" || true)
else
CHANGED_FILES=$(git -C "$WORKDIR_GIT" ls-files | grep "^$GIT_SUBDIR/" || true)
fi
CADDY_RELOAD=""
CONTAINERS=""
if [ "$CHANGED_FILES" != "" ]; then
echo "Following files changed:"
for line in $CHANGED_FILES; do
echo " $line"
if [[ "$line" == *".container" ]]; then
CONTAINERS="$CONTAINERS $(basename $line)"
fi
if [[ "$line" == *"caddy.container" ]]; then
CADDY_RELOAD="${CADDY_RELOAD}no"
fi
if [[ "$line" == *"/Caddyfile" ]]; then
CADDY_RELOAD="${CADDY_RELOAD}yes"
fi
done
fi
if [ "$CONTAINERS" != "" ]; then
# Some container relevant to current host changed - reload, restart and stop what's needed
systemctl --user daemon-reload
# (Re)Start added/changed containers
NAME_FILTER=""
for container in $CONTAINERS; do
NAME_FILTER="$NAME_FILTER -f 'name=$container'"
done
UNITS=$(eval "podman quadlet list $NAME_FILTER --format '{{.UnitName}}'" | grep -v "UNIT" || true)
for unit in $UNITS; do
systemctl --user restart $unit
echo "Restarted $unit"
done
# Stop deleted containers (possibly other non-container deleted services but there shouldn't be any)
DELETED_UNITS=$(systemctl --user list-units --type=service --state=not-found --legend=false --plain | awk '{print $1};')
for unit in $DELETED_UNITS; do
systemctl --user stop $unit
echo "Stopped $unit"
done
else
echo "No container files changed since the last pull."
fi
# Reload caddy if Caddyfile changed but container didn't
if [ "$CADDY_RELOAD" == "yes" ]; then
podman exec -w /etc/caddy caddy caddy reload
fi
commit_state