#!/bin/sh

# start etcd cluster
# xiaoshengxu@sohu-inc.com
# 2016-03-01

# Usage:   sudo sh start_etcd.sh <number>
# Require: etcd_node_list.conf
# Example: sudo sh start_etcd.sh 1
# Tips:    1) number begin with 1
#          2) calibrate system time for all nodes: ntpdate ntp.sohu.com

# STEP 01: check arguments
node_id=$1
if [ "$1" == "" ]; then
  echo "[ERROR] Usage: sudo sh start_etcd.sh <number>"
  exit 1
fi
tmp=`echo $node_id | sed 's/[0-9]//g'`
if [ -n "${tmp}" ]; then
  echo "[ERROR] Argument must be integer"
  exit 1
fi
if [ $node_id -lt 1 ]; then
  echo "[ERROR] number $node_id is less than 1"
  exit 1
fi

# STEP 02: assign append arguments
current_path=$(pwd)
data_dir=$current_path/etcd-data
conf_file=$current_path/etcd_node_list.conf
cluster_token='k8s-etcd-cluster'
name_prefix='k8sEtcd'
peer_port=4010
client_port=4012
mkdir $data_dir
chmod +x $current_path/etcd

# STEP 03: get all node ips
node_ips=()
node_len=0
clusters=
while IFS='' read -r line || [[ -n "$line" ]]; do
  node_ips[$node_len]=$line
  node_len=$((node_len+1))
  if [ $node_len -eq 1 ]; then
    clusters=$name_prefix$node_len=http://$line:$peer_port
  else
    clusters=$clusters,$name_prefix$node_len=http://$line:$peer_port
  fi
done < $conf_file
if [ $node_id -gt $node_len ]; then
  echo "[ERROR] number $node_id is out of range"
  exit 1
fi

# STEP 04: compare selected node ip with local ips
local_ips=(`ip addr show | grep inet | grep -v inet6 | grep brd | awk '{print $2}' | cut -f1 -d '/'`)
exist_ip=0
for i in ${local_ips[@]}
do
  if [ "$i" == "${node_ips[$((node_id-1))]}" ]; then
    exist_ip=1
    break
  fi
done
if [ $exist_ip -eq 0 ]; then
  echo "[ERROR] IP ${node_ips[$((node_id-1))]} is not node IP"
  exit 1
fi

# STEP 05: install etcd
echo "# configure file for etcd.service
# -name
ETCD_NAME='-name $name_prefix$node_id'
# -initial-advertise-peer-urls
INITIAL_ADVERTISE_PEER_URLS='-initial-advertise-peer-urls http://${node_ips[$((node_id-1))]}:$peer_port'
# -listen-peer-urls
LISTEN_PEER_URLS='-listen-peer-urls http://0.0.0.0:$peer_port'
# -advertise-client-urls
ADVERTISE_CLIENT_URLS='-advertise-client-urls http://${node_ips[$((node_id-1))]}:$client_port'
# -listen-client-urls
LISTEN_CLIENT_URLS='-listen-client-urls http://0.0.0.0:$client_port'
# -initial-cluster-token
INITIAL_CLUSTER='-initial-cluster-token $cluster_token'
# -initial-cluster
INITIAL_CLUSTER='-initial-cluster $clusters'
# -initial-cluster-state
INITIAL_CLUSTER_STATE='-initial-cluster-state new'
# other parameters
ETCD_OPTS='--data-dir $data_dir'
" > /etc/sysconfig/etcd
echo "[Unit]
Description=ETCD
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/etcd
ExecStart=$current_path/etcd \$ETCD_NAME \\
          \$INITIAL_ADVERTISE_PEER_URLS \\
          \$LISTEN_PEER_URLS \\
          \$ADVERTISE_CLIENT_URLS \\
          \$LISTEN_CLIENT_URLS \\
          \$INITIAL_CLUSTER_TOKEN \\
          \$INITIAL_CLUSTER \\
          \$INITIAL_CLUSTER_STATE \\
          \$ETCD_OPTS
Restart=always
" > /lib/systemd/system/etcd.service

# STEP 06: start etcd
systemctl stop etcd
systemctl daemon-reload
systemctl start etcd
systemctl status -l etcd
