-
Notifications
You must be signed in to change notification settings - Fork 0
/
podman-compose.sh
executable file
·449 lines (382 loc) · 13.7 KB
/
podman-compose.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!/bin/bash
# vim:set tabstop=8 shiftwidth=4 expandtab:
# kate: space-indent on; indent-width 4;
#
# Copyright (c) 2023 Jakob Meng, <jakobmeng@web.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
set -euo pipefail
# Environment variables
DEBUG=${DEBUG:=no}
if [ "$DEBUG" = "yes" ] || [ "$DEBUG" = "true" ]; then
set -x
fi
error() {
echo "ERROR: $*" 1>&2
}
warn() {
echo "WARNING: $*" 1>&2
}
help() {
cmd=$(basename "$0")
cat << ____EOF
Usage: $cmd [OPTIONS] COMMAND
$cmd [ --help ]
Start, stop or remove Podman networks, volumes and containers.
OPTIONS:
-h, --help Print usage
COMMANDS:
up Create and start networks, volumes and containers.
stop Stop containers.
down Stop and remove containers, volumes and networks.
help Print usage
Run '$cmd COMMAND --help' for more information on each command.
____EOF
}
up() {
help() {
cmd=$(basename "$0")
cat << ________EOF
Usage: $cmd up [OPTIONS] [COMMAND] [ARGS...]
Create and start networks, volumes, containers and run COMMAND ARGS in the primary container. If COMMAND is not
specified, then a shell is run.
OPTIONS:
-d, --detach Detached mode.
--distribution DISTRIBUTION Linux distribution used in containers. Options are centos_8, centos_9, debian_10,
debian_11, debian_12, debian_13, fedora_rawhide, ubuntu_1804, ubuntu_2004, ubuntu_2204
and ubuntu_2404.
--infra-image IMAGE Container image that will be used for the infra container.
Defaults to '$infra_image_default'.
--project DIR Directory where Ansible playbooks and the inventory are stored.
Defaults to the parent directory of the directory that contains this script.
--project-name NAME Name to label and find containers, images and volumes with.
Defaults to '$project_name_default'.
-h, --help Print usage.
________EOF
}
cmd_args=()
detach="no"
distribution=""
distribution_default="debian_11"
infra_image=""
infra_image_default="alpine:latest"
project=""
project_name=""
project_name_default="cloudy"
while [ $# -ne 0 ]; do
case "$1" in
"-d"|"--detach")
detach="yes"
;;
"--distribution")
if [ -z "$2" ]; then
error "flag is missing arg: --distribution"
return 255
fi
distribution="$2"
shift
;;
"--infra-image")
if [ -z "$2" ]; then
error "flag is missing arg: --infra-image"
return 255
fi
infra_image="$2"
shift
;;
"-h"|"--help")
help
return 0
;;
"--project")
if [ -z "$2" ]; then
error "flag is missing arg: --project"
return 255
fi
project="$2"
shift
;;
"--project-name")
if [ -z "$2" ]; then
error "flag is missing arg: --project-name"
return 255
fi
project_name="$2"
shift
;;
-*)
error "Unknown flag: $1"
return 255
;;
*)
cmd_args=("$@")
set -- 'FOR_NEXT_SHIFT_ONLY'
;;
esac
shift
done
[ -n "$distribution" ] || distribution=$distribution_default
[ -n "$infra_image" ] || infra_image=$infra_image_default
[ -n "$project_name" ] || project_name=$project_name_default
case "$distribution" in
"centos_8"|"centos_9")
;;
"debian_10"|"debian_11"|"debian_12"|"debian_13")
;;
"fedora_rawhide")
;;
"ubuntu_1804"|"ubuntu_2004"|"ubuntu_2204"|"ubuntu_2204")
;;
*)
error "Unsupported distribution: $distribution"
return 255
;;
esac
# Locate script directory
# NOTE: Symbolic links are followed first in order to always resolve to
# the script's directory even if called through a symbolic link.
cmd_dir=$(dirname "$(readlink -f "$0")")
# Locate project directory
if [ -n "$project" ]; then
project_dir=$(readlink -f "$project")
else
project_dir=$(readlink -f "$cmd_dir/..")
fi
# Podman 3.0.1 does not support podman network exists and does not support regular expressions in filters
if ! podman network ls -q "--filter=name=${project_name}" | grep -q -e "^${project_name}\$"; then
podman network create --driver=bridge \
--subnet=192.168.150.0/24 \
--ip-range=192.168.150.0/24 \
--gateway=192.168.150.1 \
"${project_name}"
fi
for volume in "${project_name}-images" "${project_name}-ssh"; do
# Podman 3.0.1 does not support podman volume exists and does not support regular expressions in filters
if ! podman volume ls -q "--filter=name=$volume" | grep -q -e "^$volume\$"; then
podman volume create "$volume"
fi
done
if podman container exists "${project_name}-infra"; then
if [ -z "$(podman container ls -a -q "--filter=name=^${project_name}-infra\$" --filter=status=running)" ]; then
warn "Stopped container ${project_name}-infra will be (re)started, but not rebuild."\
"Remove container first to force rebuild."
podman start "${project_name}-infra"
fi
else
# Start a container to bring up Podman network
# Podman network has to be started for the following ip route add commands
podman run --detach --stop-signal SIGKILL --network "${project_name}" --name "${project_name}-infra" \
"$infra_image" sleep infinity
fi
if ! gateway=$(ip -json route get 192.168.157.0 | jq -r -e '.[0] | .prefsrc') \
|| [ "$gateway" != "192.168.150.1" ]; then
ip route add 192.168.157.0/24 via 192.168.150.1
fi
if ! gateway=$(ip -json route get 192.168.158.0 | jq -r -e '.[0] | .prefsrc') \
|| [ "$gateway" != "192.168.150.1" ]; then
ip route add 192.168.158.0/24 via 192.168.150.1
fi
if podman container exists "${project_name}"; then
# Container exists
if [ -z "$(podman container ls -a -q "--filter=name=^${project_name}\$" --filter=status=running)" ]
then
warn "Stopped container ${project_name} will be (re)started, but not rebuild."\
"Remove container first to force rebuild."
podman start "${project_name}"
if [ "$detach" != "yes" ]; then
podman attach "${project_name}"
fi
fi
else # Container does not exist
if ! podman image exists "${project_name}:latest"; then
(cd "$cmd_dir" && podman image build -f "Dockerfile.$distribution" -t "${project_name}:latest" .)
fi
podman_args=()
# Disable SELinux separation for the container
podman_args+=(--security-opt label=disable)
# privileged is required for libvirtd to be able to write to /proc/sys/net/ipv6/conf/*/disable_ipv6
podman_args+=(--privileged)
# libvirt tcp transport
podman_args+=(--publish '127.0.0.1:16509:16509/tcp')
# VNC connections
podman_args+=(--publish '127.0.0.1:5900-5999:5900-5999/tcp')
# Persist storage volumes of libvirt domains
podman_args+=(-v "${project_name}-images:/home/cloudy/.local/share/libvirt/images/")
# Persist SSH credentials and configuration for accessing virtual machines
podman_args+=(-v "${project_name}-ssh:/home/cloudy/.ssh/")
# Or instead:
# Grant access to SSH credentials and configuration on Ansible controller for accessing virtual machines
#podman_args+=(-v "$HOME/.ssh/:/home/cloudy/.ssh/:ro")
# Grant access to user's playbooks, inventories and Ansible configuration
podman_args+=(-v "$project_dir/:/home/cloudy/project/:ro")
# For development only
podman_args+=(-v "$cmd_dir/entrypoint.sh:/usr/local/bin/entrypoint.sh:ro")
# Bind mount collection into container which allows derived collections to pin this collection and allows
# developers of this collection to test changes to this collection, skipping the collection installation from
# Ansible Galaxy.
collection_dir=$(readlink -f "$cmd_dir/..")
if [ -e "$collection_dir/galaxy.yml" ] \
&& grep -q -e '^name: cloudy$' "$collection_dir/galaxy.yml" \
&& grep -q -e '^namespace: jm1$' "$collection_dir/galaxy.yml"
then
podman_args+=(-v "$collection_dir/:/usr/share/ansible/collections/ansible_collections/jm1/cloudy/:ro")
fi
if [ "$detach" = "yes" ]; then
podman_args+=(--detach)
fi
podman run \
--init \
--interactive \
--tty \
--name "${project_name}" \
-e "DEBUG=${DEBUG:=no}" \
-e "DEBUG_SHELL=${DEBUG_SHELL:=no}" \
--network "${project_name}" \
--device '/dev/kvm:/dev/kvm' \
--sysctl "net.ipv4.conf.eth0.proxy_arp=1" \
"${podman_args[@]}" \
"${project_name}:latest" "${cmd_args[@]}"
fi
}
stop() {
help() {
cmd=$(basename "$0")
cat << ________EOF
Usage: $cmd stop [OPTIONS]
Stop containers.
OPTIONS:
--project-name NAME Name to label and find containers, images and volumes with.
Defaults to '$project_name_default'.
-h, --help Print usage
________EOF
}
project_name=""
project_name_default="cloudy"
while [ $# -ne 0 ]; do
case "$1" in
"-h"|"--help")
help
return 0
;;
"--project-name")
if [ -z "$2" ]; then
error "flag is missing arg: --project-name"
return 255
fi
project_name="$2"
shift
;;
-*)
error "Unknown flag: $1"
return 255
;;
*)
error "Unknown command: $1"
return 255
;;
esac
shift
done
[ -n "$project_name" ] || project_name=$project_name_default
podman stop --ignore "${project_name}"
ip route del 192.168.158.0/24 via 192.168.150.1 || true
ip route del 192.168.157.0/24 via 192.168.150.1 || true
podman stop --ignore "${project_name}-infra"
}
down() {
help() {
cmd=$(basename "$0")
cat << ________EOF
Usage: $cmd down [OPTIONS]
Stop and remove containers, volumes and networks.
OPTIONS:
--project-name NAME Name to label and find containers, images and volumes with.
Defaults to '$project_name_default'.
-h, --help Print usage
________EOF
}
project_name=""
project_name_default="cloudy"
while [ $# -ne 0 ]; do
case "$1" in
"-h"|"--help")
help
return 0
;;
"--project-name")
if [ -z "$2" ]; then
error "flag is missing arg: --project-name"
return 255
fi
project_name="$2"
shift
;;
-*)
error "Unknown flag: $1"
return 255
;;
*)
error "Unknown command: $1"
return 255
;;
esac
shift
done
[ -n "$project_name" ] || project_name=$project_name_default
podman stop --ignore "${project_name}"
podman rm --force --ignore "${project_name}"
ip route del 192.168.158.0/24 via 192.168.150.1 || true
ip route del 192.168.157.0/24 via 192.168.150.1 || true
podman rm --force --ignore "${project_name}-infra"
podman image rm --force "${project_name}:latest" || true
podman volume rm --force "${project_name}-images" || true
podman volume rm --force "${project_name}-ssh" || true
podman network rm --force "${project_name}" || true
}
if [ $# -eq 0 ]; then
help
exit 1
fi
if [ "$(id -u)" -ne 0 ]; then
error "Please run as root"
exit 125
fi
for cmd in ip podman; do
if ! command -v "$cmd" >/dev/null; then
error "$cmd not found"
exit 255
fi
done
while [ $# -ne 0 ]; do
case "$1" in
"up"|"stop"|"down"|"help")
("$@")
exit
;;
"-h"|"--help")
help
exit 0
;;
-*)
error "Unknown flag: $1"
exit 1
;;
*)
error "Unknown command: $1"
exit 1
;;
esac
shift
done