#!/bin/bash
#####
<<Install
id -u antietcd &>/dev/null || useradd -r -s /sbin/nologin antietcd
if [ $(df / | grep /dev &>/dev/null) ] 
then
[ -d /var/lib/antietcd ] || mkdir -p /var/lib/antietcd && chown antietcd:antietcd /var/lib/antietcd
else
[ -d /var/lib/antietcd ] || zfs create -o setuid=off -o exec=off -o devices=off -o mountpoint=/var/lib/antietcd -o recordsize=4K -o refquota=10G $(zfs get -H -o value name / | cut -d/ -f1)/antietcd && chown antietcd:antietcd /var/lib/antietcd
fi
Install


eth_out=$(ip route get 8.8.8.8 | sed -n 's/.*dev \([^\ ]*\).*/\1/p')
eth_ip=$(ip route get 8.8.8.8 | sed -n 's/.*src \([^\ ]*\).*/\1/p')
[ "$eth_ip" == '194.50.153.38' ] && eth_ip='10.0.254.1'
#eth_ip=$(getent hosts $(hostname) | awk '{print $1}')

case $(hostname) in
    felis)
        node=node1
        ;;
    xoma)
        node=node2
        ;;
    muromec)
        node=node3
        ip="--ip 10.0.254.1"
        ;;
esac

tee <<EOF > /etc/systemd/system/antietcd.service
[Unit]
Description=AntiEtcd Cluster Node
After=network-online.target
Wants=network-online.target

[Service]
User=antietcd
Group=antietcd
ExecStart=antietcd \
 --node_id $node \
 --port 2377 \
 --cluster node1=http://172.16.11.5:2377,node2=http://172.16.111.10:2377,node3=http://10.0.254.1:2377 \
 --cluster_key lksd93nf823bf82knjflkksd9en0fvu \
 --data /var/lib/antietcd/antietcd.gz \
 $ip
Restart=on-failure
RestartSec=3s
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

EOF

systemctl daemon-reload
systemctl enable antietcd
systemctl restart antietcd
journalctl -ef -u antietcd