#!/bin/bash
[ -z "$1" ] && echo "No arguments for etcdctl " && exit 1
{% set str_cli_port = "" ~ taskserv.cli_port %}
etcdctl \
--endpoints {% if taskserv.adv_listen_clients is starting_with("$servers") -%}
  {%- for srv in defs.servers %} 
    {%- set srv_index = loop.index -%}
    {%- for task in srv.taskservs -%}
      {%- if task.name != "etcd" -%}{% continue %}{% endif %}
      {%- if srv_index > 1 -%},{%- endif -%}
      {%- if taskserv.adv_listen_clients is containing("$network_private_ip") -%}
      {{taskserv.etcd_protocol}}://{{ taskserv.adv_listen_clients | replace(from="$servers:$network_private_ip",to=srv.network_private_ip) | replace(from="$cli_port", to=str_cli_port)}}
      {%- elif taskserv.adv_listen_clients is containing("$network_public_ip") -%}
      {{taskserv.etcd_protocol}}://{{ taskserv.adv_listen_clients | replace(from="$servers:$network_public_ip",to=settings[loop.index0].ip_addresses.pub) | replace(from="$cli_port", to=str_cli_port)}}
      {%- else -%}
      {{taskserv.etcd_protocol}}://{{ taskserv.adv_listen_clients | replace(from="$servers",to=srv.hostname) | replace(from="$cli_port", to=str_cli_port)}}
      {%- endif -%}
    {%- endfor -%}
  {%- endfor -%}
{%- else -%}
  {{taskserv.adv_listen_clients}}
{%- endif %} \
{% if taskserv.ssl_mode != "" -%}
--cacert  /etc/ssl/etcd/ca.crt  \
--cert  /etc/ssl/etcd/{{taskserv.cluster_name}}.crt  \
--key  /etc/ssl/etcd/{{taskserv.cluster_name}}.key  \
{%- endif %}
$*