This repository has been archived on 2023-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
salt-states_old/podman/files/container.sh.jinja

149 lines
4.0 KiB
Plaintext
Raw Normal View History

2022-11-02 10:18:20 +00:00
#!/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_volumes() {
{% for volume, mounts in args['volumes'].items() -%}
if ! podman volume exists {{ container }}-{{ volume }}; then
podman volume create {{ container }}-{{ volume }}
fi
{% endfor %}
}
function create_container() {
if ! podman container exists {{ container }};then
podman container create \
--name {{ container }} \
{%- if args['volumes'] is defined %}
{%- for volume, mount in args['volumes'].items() %}
-v {{ container }}-{{ 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'] }}
fi
}
function generate_systemd_unit_file() {
podman generate systemd --name {{ container }} > $HOME/.config/systemd/user/{{ 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() {
if ! check_update; then
systemctl --user stop {{ container }}
podman container rm {{ container }}
create_container
generate_systemd_unit_file
systemctl --user daemon-reload
systemctl --user enable --now {{ container }}.service
fi
}
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
-U, --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
;;
-U|--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