salt-states/pod/files/pod.sh.jinja

159 lines
4.7 KiB
Plaintext
Raw Normal View History

2023-06-27 07:16:37 +00:00
#!/usr/bin/env bash
function pull_image(){
{%- for container, cargs in pillar['pods'][pod]['containers'].items() %}
if ! podman image exists {{ cargs['image'] }}:{{ cargs['tag'] }}; then
podman pull {{ cargs['image'] }}:{{ cargs['tag'] }}
fi
{%- endfor %}
}
function create_pod() {
if ! podman pod exists {{ pod }};then
podman pod create \
--name {{ pod }} \
--infra-name {{ pod }}-infra \
{%- 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 %}
fi
{%- for container, cargs in pillar['pods'][pod]['containers'].items() %}
if ! podman container exists {{ pod }}-{{ container }};then
podman container create \
--name {{ pod }}-{{ container }} \
--pod {{ pod }} \
{%- if cargs['podman_options'] is defined %}
{%- for option, value in cargs['podman_options'].items() %}
--{{ option }} {{ value }} \
{%- endfor %}
{%- endif %}
{%- if cargs['volumes'] is defined %}
{%- for volume, mount in cargs['volumes'].items() %}
-v {{ volume }}:{{ mount }} \
{%- endfor %}
{%- endif %}
{%- if cargs['env'] is defined %}
{%- for key, value in cargs['env'].items() %}
-e {{ key }}={{ value }} \
{%- endfor %}
{%- endif %}
{%- if cargs['devices'] is defined %}
{%- for key, value in cargs['devices'].items() %}
--device {{ key }}:{{ value}} \
{%- endfor %}
{%- endif %}
{{ cargs['image'] }}:{{ cargs['tag'] }}{%- if cargs['run'] is defined %} \
{{ cargs['run'] }}
{%- endif %}
fi
{%- endfor %}
}
function generate_systemd_unit_file() {
cd /etc/systemd/system
podman generate systemd --files --name {{ pod }} --pod-prefix="" --container-prefix=""
}
function check_update() {
ImageUpdate=0
{%- for container, cargs in pillar['pods'][pod]['containers'].items() %}
podman pull {{ cargs['image'] }}:{{ cargs['tag'] }}
if [[ "$(podman image inspect {{ cargs['image'] }}:{{ cargs['tag'] }} --format "{% raw %}{{.Id}}{% endraw %}")" == "$(podman inspect {{ pod }}-{{ container }} --format "{% raw %}{{ .Image }}{% endraw %}")" ]];then
echo "No image updates available for {{ pod }}-{{ container }}"
else
echo "Image update available for {{ pod }}-{{ container }}"
ImageUpdate=1
fi
{%- endfor %}
return $ImageUpdate
}
function update() {
systemctl stop {{ pod }}
podman pod rm {{ pod }}
create_pod
generate_systemd_unit_file
systemctl daemon-reload
systemctl enable --now {{ pod }}.service
}
function printHelp(){
cat << EOF
Usage ${0##*/} [options..]
-h,-?, --help Show help and exit
-p, --pull pull container images for all containers in pod {{ pod }}
-c, --create create {{ pod }} pod
-s, --start start and enables {{ pod }} pod
-S, --stop stop {{ pod }} pod
-i, --is-running check to see if pod 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_pod
shift
;;
-s|--start)
systemctl --user enable --now {{ pod }}.service
shift
;;
-S|--stop)
systemctl --user stop {{ pod }}.service
shift
;;
-i|--is-running)
systemctl --user is-active {{ pod }}.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