# This file is managed by ansible, don't make changes here - they will be
# overwritten.
[Unit]
Description=alertmanager
{% if container_binary == 'docker' %}
After=docker.service network-online.target local-fs.target time-sync.target
Requires=docker.service
{% else %}
After=network-online.target local-fs.target time-sync.target
{% endif %}
Wants=network-online.target local-fs.target time-sync.target

[Service]
WorkingDirectory={{ alertmanager_data_dir }}
EnvironmentFile=-/etc/environment
{% if container_binary == 'podman' %}
ExecStartPre=-/usr/bin/rm -f /%t/%n-pid /%t/%n-cid
ExecStartPre=-/usr/bin/{{ container_binary }} rm --storage alertmanager
{% endif %}
ExecStartPre=-/usr/bin/{{ container_binary }} rm -f alertmanager
ExecStart=/usr/bin/{{ container_binary }} run --rm --name=alertmanager \
{% if container_binary == 'podman' %}
  -d --log-driver journald --conmon-pidfile /%t/%n-pid --cidfile /%t/%n-cid \
{% endif %}
  --pids-limit={{ 0 if container_binary == 'podman' else -1 }} \
  -v "{{ alertmanager_conf_dir }}:/etc/alertmanager:Z" \
  -v "{{ alertmanager_data_dir }}:/alertmanager:Z" \
  --net=host \
  --cpu-period={{ alertmanager_container_cpu_period }} \
  --cpu-quota={{ alertmanager_container_cpu_period * alertmanager_container_cpu_cores }} \
  --memory={{ alertmanager_container_memory }}GB \
  --memory-swap={{ alertmanager_container_memory * 2 }}GB \
  {{ alertmanager_container_image }} \
  --config.file=/etc/alertmanager/alertmanager.yml \
  --cluster.listen-address={{ grafana_server_addr }}:{{ alertmanager_cluster_port }} \
{% for peer in grafana_server_addrs|difference(grafana_server_addr) %}
  --cluster.peer={{ peer }}:{{ alertmanager_cluster_port }} \
{% endfor %}
  --storage.path=/alertmanager \
  --web.external-url=http://{{ ansible_facts['fqdn'] }}:{{ alertmanager_port }}/ \
  --web.listen-address={{ grafana_server_addr }}:{{ alertmanager_port }}
{% if container_binary == 'podman' %}
ExecStop=/usr/bin/sh -c "/usr/bin/{{ container_binary }} rm -f `cat /%t/%n-cid`"
{% else %}
ExecStop=/usr/bin/{{ container_binary }} stop alertmanager
{% endif %}
KillMode=none
Restart=always
RestartSec=10s
TimeoutStartSec=120
TimeoutStopSec=15
{% if container_binary == 'podman' %}
Type=forking
PIDFile=/%t/%n-pid
{% endif %}

[Install]
WantedBy=multi-user.target
