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

155 lines
4.6 KiB
Plaintext
Raw Permalink 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
}
{% if pillar['containers'][container]['volumes'] is defined %}
2022-11-02 10:18:20 +00:00
function create_volumes() {
{% for volume, mounts in args['volumes'].items() -%}
if ! podman volume exists {{ container }}-{{ volume }}; then
podman volume create {{ container }}-{{ volume }}
fi
{% endfor %}
}
{% endif %}
2022-11-02 10:18:20 +00:00
function create_container() {
if ! podman container exists {{ container }};then
podman container create \
--name {{ container }} \
2022-11-15 10:50:10 +00:00
{%- if args['podman_options'] is defined %}
{%- for option, value in args['podman_options'].items() %}
--{{ option }} {{ value }} \
{%- endfor %}
{%- endif %}
{%- if args['volumes'] is defined %}
2022-11-02 10:18:20 +00:00
{%- for volume, mount in args['volumes'].items() %}
-v {{ container }}-{{ volume }}:{{ mount }} \
{%- endfor %}
{%- endif %}
2022-11-15 10:50:10 +00:00
{%- 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 %}
2023-03-28 13:57:56 +00:00
{{ args['image'] }}:{{ args['tag'] }}{%- if args['run'] is defined %} \
{{ args['run'] }}
{%- endif %}
2022-11-02 10:18:20 +00:00
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"
2022-11-15 10:50:10 +00:00
return 0
2022-11-02 10:18:20 +00:00
else
echo "Image update available"
2022-11-15 10:50:10 +00:00
return 1
2022-11-02 10:18:20 +00:00
fi
}
function update() {
systemctl --user stop {{ container }}
podman container rm {{ container }}
create_container
generate_systemd_unit_file
systemctl --user daemon-reload
systemctl --user enable --now {{ container }}.service
2022-11-02 10:18:20 +00:00
}
2022-11-15 10:50:10 +00:00
2022-11-02 10:18:20 +00:00
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
2022-11-02 10:18:20 +00:00
-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
;;
2022-11-15 10:50:10 +00:00
-s|--start)
systemctl --user enable --now {{ container }}.service
shift
;;
-S|--stop)
systemctl --user stop {{ container }}.service
shift
;;
-i|--is-running)
2022-11-02 10:18:20 +00:00
systemctl --user is-active {{ container }}.service
2022-11-15 10:50:10 +00:00
exit $?
shift
;;
2022-11-02 10:18:20 +00:00
-g|--generate-systemd)
2022-11-15 10:50:10 +00:00
generate_systemd_unit_file
shift
;;
-u|--check-update)
check_update
shift
;;
--update)
update
shift
;;
2022-11-02 10:18:20 +00:00
--) #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