-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpodman.sh
More file actions
executable file
·275 lines (212 loc) · 6.73 KB
/
podman.sh
File metadata and controls
executable file
·275 lines (212 loc) · 6.73 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/bin/bash
function main {
# loop args
if [[ $# -ne 0 ]] ; then
for var in "$@" ; do
eval $var
done
exit 1
fi
# menu
while true; do
read -n 1 -p "
server tools
===================
1) Podman Install - Arch
2) Podman Install - Debian/Armbian/DietPi
r) Remove All Containers
b) Backup podman folder
u) Update containers
x) Update containers (root)
p) Pipe Service
*) Any key to exit
:" ans;
reset
case $ans in
1) fn_install_arch ;;
2) fn_install_debian ;;
r) fn_remove_all ;;
b) fn_backup ;;
u) fn_update ;;
x) fn_update_root ;;
p) fn_pipe ;;
*) $SHELL ;;
esac
done
}
function fn_update_root {
echo "Pulling latest images for all running containers..."
echo
# 2. Capture the root container list into a variable first
# This prevents 'sudo' from getting stuck in a pipe
local container_data
container_data=$(sudo podman ps --format "{{.Names}}|{{.Image}}")
if [[ -z "$container_data" ]]; then
echo "No running root containers found."
return 0
fi
echo "Containers found. Starting updates..."
echo "--------------------------------------"
# 3. Loop through the captured list
for entry in $container_data; do
# Split name and image by the '|' character
local cname="${entry%|*}"
local img="${entry#*|}"
# Skip if it's an infra container
if sudo podman inspect "$cname" --format '{{.IsInfra}}' 2>/dev/null | grep -q 'true'; then
continue
fi
echo "Updating Root Container: $cname"
# Pull latest image
sudo podman pull --quiet "$img"
# Handle systemd service
local unit_base="${cname#systemd-}"
echo "Stopping system service: $unit_base"
sudo systemctl stop "$unit_base"
# FORCE remove the container (-f is critical here)
echo "Removing container: $cname"
sudo podman rm -f "$cname"
# Restart service
echo "Starting system service: $unit_base"
sudo systemctl start "$unit_base"
echo "Finished $unit_base"
echo "--------------------------------------"
done
echo "All root containers processed."
}
function fn_pipe {
# pipe
mkdir $HOME/Containers/pipe
mkfifo $HOME/Containers/pipe/pipe_in
mkfifo $HOME/Containers/pipe/pipe_out
# create script
sudo tee $HOME/Containers/pipe/start_pipe.sh > /dev/null << EOL
#!/bin/bash
while true; do eval "\$(cat pipe_in)" > pipe_out; done
EOL
# mount as /pipe in docker
sudo tee $HOME/Containers/pipe/run.sh > /dev/null << EOL
#!/bin/bash
# Get the directory this script is in
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Paths relative to the script directory
PIPE_IN="$SCRIPT_DIR/pipe_in"
PIPE_OUT="$SCRIPT_DIR/pipe_out"
# Show script directory
echo "$SCRIPT_DIR"
# Echo arguments
echo "$@"
# Send to pipe_in
echo "$@" > "$PIPE_IN"
# Read from pipe_out
cat "$PIPE_OUT"
EOL
sudo chmod +x $HOME/Containers/pipe/start_pipe.sh
sudo chmod +x $HOME/Containers/pipe/run.sh
# create service
sudo tee /etc/systemd/system/pipe.service > /dev/null << EOL
[Unit]
Description=container pipe
After=sound.target
[Service]
ExecStart=$HOME/Containers/pipe/start_pipe.sh
WorkingDirectory=$HOME/Containers/pipe/
StandardOutput=inherit
StandardError=inherit
Restart=always
User=$USER
Environment="PULSE_RUNTIME_PATH=/run/user/1000/pulse/"
[Install]
WantedBy=default.target
EOL
sudo systemctl reset-failed pipe
sudo systemctl enable pipe
sudo systemctl start --now pipe
systemctl status pipe.service
}
function fn_update {
echo "Pulling latest images for all running containers..."
echo
# Get all running container names and their images
while read -r cname img; do
# Skip containers with no image (e.g., infra containers)
if [[ -z "$img" ]]; then
echo "Skipping container '$cname' (no image; likely infra)"
echo
continue
fi
# Skip if it's an infra container
if podman inspect "$cname" --format '{{.IsInfra}}' 2>/dev/null | grep -q 'true'; then
echo "Skipping infra container '$cname'"
echo
continue
fi
echo "Processing container: $cname with image: $img"
# Pull latest image
podman pull --quiet "$img"
# Stop the container's systemd service by removing systemd- prefix if present
unit_base="${cname#systemd-}"
echo "Stopping systemd service: $unit_base"
systemctl --user stop "$unit_base"
# Force remove the container
echo "Removing container: $cname"
podman rm -f "$cname"
# Start the systemd service to recreate the container from updated image
echo "Starting systemd service: $unit_base"
systemctl --user start "$unit_base"
echo "Updated and restarted $unit_base"
echo
done < <(podman ps --format "{{.Names}} {{.Image}}")
echo "All running containers have been force updated."
}
function fn_backup {
echo "backup docker folder..."
hostname=$(hostname)
archive=$HOME/Backups/podman-${hostname}.tar.gz
backup=$HOME/Containers
#echo "listing containers"
#containers=$(docker container list -qa)
#echo $containers
mkdir $HOME/Backups
echo "stop containers"
podman pause --all
echo "create backup..."
echo ${archive}
sudo tar -czvf ${archive} ${backup} > /dev/null
echo "restart containers"
podman unpause --all
echo "done!"
}
function fn_install_debian {
# dietpit doesnt have dbus and other depends for user access, which we need
./util.sh -i podman dbus-user-session uidmap catatonit passt
# enable logind and bus access
sudo systemctl unmask systemd-logind.service
sudo systemctl unmask dbus.service
sudo systemctl daemon-reload
sudo loginctl enable-linger $USER
# systemd env variables
USER_ID=$(id -u)
mkdir -p $HOME/.config/environment.d/
tee $HOME/.config/environment.d/60-bus-fix.conf > /dev/null << EOL
XDG_RUNTIME_DIR=/run/user/$USER_ID
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$USER_ID/bus
EOL
sudo systemctl start podman --now
echo "If your using dietpi with dropbear, use dietpi-software to change to openssh!"
echo "complete!"
#echo "reboot now and run again"
}
function fn_install_arch {
./util.sh -i podman crun
# podlet: need rust to compile, until a bin version is released
#rustup default stable
#./util.sh -i podlet # yay
sudo systemctl start podman --now
}
function fn_remove_all {
podman rm --all
podman ps --all
}
# pass all args
main "$@"