145 lines
4.2 KiB
Django/Jinja
145 lines
4.2 KiB
Django/Jinja
#!/usr/bin/env bash
|
|
|
|
function pull_image(){
|
|
if ! podman image exists {{ args['image'] }}:{{ args['tag'] }}; then
|
|
podman pull {{ args['image'] }}:{{ args['tag'] }}
|
|
fi
|
|
|
|
}
|
|
|
|
function create_container() {
|
|
if ! podman container exists {{ container }};then
|
|
podman container create \
|
|
--name {{ container }} \
|
|
{%- if args['podman_options'] is defined %}
|
|
{%- for option, value in args['podman_options'].items() %}
|
|
--{{ option }} {{ value }} \
|
|
{%- endfor %}
|
|
{%- endif %}
|
|
{%- if args['volumes'] is defined %}
|
|
{%- for volume, mount in args['volumes'].items() %}
|
|
-v {{ volume }}:{{ mount }} \
|
|
{%- endfor %}
|
|
{%- endif %}
|
|
{%- if args['ports'] is defined %}
|
|
{%- for ports in args['ports'] %}
|
|
-p {{ ports['host'] }}:{{ ports['container'] }}{% if ports['protocol'] is defined %}/{{ ports['protocol'] }}{% endif %} \
|
|
{%- endfor %}
|
|
{%- endif %}
|
|
{%- if args['env'] is defined %}
|
|
{%- for key, value in args['env'].items() %}
|
|
-e {{ key }}={{ value }} \
|
|
{%- endfor %}
|
|
{%- endif %}
|
|
{%- if args['devices'] is defined %}
|
|
{%- for key, value in args['devices'].items() %}
|
|
--device {{ key }}:{{ value}} \
|
|
{%- endfor %}
|
|
{%- endif %}
|
|
{{ args['image'] }}:{{ args['tag'] }}{%- if args['run'] is defined %} \
|
|
{{ args['run'] }}
|
|
{%- endif %}
|
|
fi
|
|
|
|
}
|
|
|
|
function generate_systemd_unit_file() {
|
|
podman generate systemd --name {{ container }} > /etc/systemd/system/{{ container }}.service
|
|
}
|
|
|
|
function check_update() {
|
|
podman pull {{ args['image'] }}:{{ args['tag'] }}
|
|
if [[ "$(podman image inspect {{ args['image'] }}:{{ args['tag'] }} --format "{% raw %}{{.Id}}{% endraw %}")" == "$(podman inspect {{ container }} --format "{% raw %}{{ .Image }}{% endraw %}")" ]];then
|
|
echo "No image updates available"
|
|
return 0
|
|
else
|
|
echo "Image update available"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
function update() {
|
|
systemctl stop {{ container }}
|
|
podman container rm {{ container }}
|
|
create_container
|
|
generate_systemd_unit_file
|
|
systemctl daemon-reload
|
|
systemctl enable --now {{ container }}.service
|
|
}
|
|
|
|
|
|
|
|
function printHelp(){
|
|
cat << EOF
|
|
Usage ${0##*/} [options..]
|
|
-h,-?, --help Show help and exit
|
|
-p, --pull pull container image ({{ container }}:{{ args['tag'] }})
|
|
-v, --volumes create container volumes
|
|
-c, --create create {{ container }} containers
|
|
-s, --start start and enables {{ container }} container
|
|
-S, --stop stop {{ container }} container
|
|
-i, --is-running check to see if container service is running
|
|
-u, --check-update check if there are image updates avaiable
|
|
--update perform image update if it exists
|
|
-g, --generate-systemd generate user systemd service unit file
|
|
EOF
|
|
}
|
|
|
|
|
|
while :; do
|
|
case $1 in
|
|
-h|-\?|--help)
|
|
printHelp
|
|
exit
|
|
;;
|
|
-p|--pull)
|
|
pull_image
|
|
shift
|
|
;;
|
|
-v|--volumes)
|
|
create_volumes
|
|
shift
|
|
;;
|
|
-c|--create)
|
|
create_container
|
|
shift
|
|
;;
|
|
-s|--start)
|
|
systemctl --user enable --now {{ container }}.service
|
|
shift
|
|
;;
|
|
-S|--stop)
|
|
systemctl --user stop {{ container }}.service
|
|
shift
|
|
;;
|
|
-i|--is-running)
|
|
systemctl --user is-active {{ container }}.service
|
|
exit $?
|
|
shift
|
|
;;
|
|
-g|--generate-systemd)
|
|
generate_systemd_unit_file
|
|
shift
|
|
;;
|
|
-u|--check-update)
|
|
check_update
|
|
shift
|
|
;;
|
|
--update)
|
|
update
|
|
shift
|
|
;;
|
|
--) #End of all options
|
|
shift
|
|
break
|
|
;;
|
|
-?*)
|
|
printf "'%s' is not a valid option\n" "$1" >&2
|
|
exit 1
|
|
;;
|
|
*) #Break out of case, no more options
|
|
break
|
|
esac
|
|
shift
|
|
done
|