#!/bin/bash
# Info: Script to install/create/delete/update etcd from file settings 
# Author: JesusPerezLorenzo  
# Release: 1.0 
# Date: 12-11-2024

USAGE="install-etcd.sh install | update | remvoe"
[ "$1" == "-h"  ] && echo "$USAGE"  && exit 1

[ -r "env-etcd" ] && . ./env-etcd

ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')"
CMD_TSK=${1:-install}

#[ -z "$ETCD_VERSION" ] && echo "No ETCD_VERSION found " && exit
HOSTNAME=$(hostname)
export LC_CTYPE=C.UTF-8 
export LANG=C.UTF-8

[ ! -d "/etc/etcd" ] && sudo mkdir  /etc/etcd

_init() {
  [ -z "$ETCD_VERSION" ] || [ -z "$ARCH" ] && exit 1
  local curr_vers
  local has_etcd
  has_etcd=$(type etcd 2>/dev/null)
  [ -n "$has_etcd" ] && curr_vers="v"$(etcd -version 2>/dev/null | grep etcd | cut -f2 -d":" | sed  's/ //g')
  [ "$curr_vers" == "$ETCD_VERSION" ] && return
  # choose either URL
  GOOGLE_URL=https://storage.googleapis.com/etcd
  GITHUB_URL=https://github.com/etcd-io/etcd/releases/download
  case "$SOURCE_URL" in 
    google) DOWNLOAD_URL=${GOOGLE_URL} ;;
    github) DOWNLOAD_URL=${GITHUB_URL} ;;
  esac
  rm -f "/tmp/etcd-${ETCD_VERSION}-${ARCH}.tar.gz"
  [ -d "/tmp/etcd-download" ] && rm -rf /tmp/etcd-download
  mkdir -p /tmp/etcd-download

  if ! curl -fsSL "${DOWNLOAD_URL}/${ETCD_VERSION}/etcd-${ETCD_VERSION}-linux-${ARCH}.tar.gz" -o "/tmp/etcd-${ETCD_VERSION}-${ARCH}.tar.gz" ; then 
    echo "Error downloading etcd-${ETCD_VERSION}-${ARCH}.tar.gz"
    exit 1
  fi
  if ! tar xzf "/tmp/etcd-${ETCD_VERSION}-${ARCH}.tar.gz" -C /tmp/etcd-download --strip-components=1 ; then
    echo "Error extracting etcd-${ETCD_VERSION}-${ARCH}.tar.gz"
    exit 1
  fi
  rm -f "/tmp/etcd-${ETCD_VERSION}-${ARCH}.tar.gz"

  chmod +x /tmp/etcd-download/etcd
  chmod +x /tmp/etcd-download/etcdctl 

  sudo mv /tmp/etcd-download/etcd /usr/local/bin
  sudo mv /tmp/etcd-download/etcdctl /usr/local/bin
  sudo mv /tmp/etcd-download/etcdutl /usr/local/bin
  sudo mv /tmp/etcd-download /etc/etcd/"${ETCD_VERSION}"
  
  # start a local etcd server
  # /tmp/etcd-download/etcd
  # write,read to etcd
  # /tmp/etcd-download/etcdctl --endpoints=localhost:2379 put foo bar
  # /tmp/etcd-download/etcdctl --endpoints=localhost:2379 get foo
}
_config_etcd() {
  [ ! -d "/etc/etcd" ] && sudo mkdir /etc/etcd

  has_user=$(sudo grep etcd /etc/passwd)
  [ -z "$has_user" ] && sudo useradd -d /home/etcd -m etcd

  [ ! -d "/etc/ssl/etcd" ] && sudo mkdir -p /etc/ssl/etcd
  sudo cp certs/*  /etc/ssl/etcd
  sudo chown -R etcd:etcd /etc/ssl/etcd

  [ ! -d "${ETCD_DATA}" ] && sudo mkdir -p "${ETCD_DATA}"
  sudo chown -R etcd:etcd "${ETCD_DATA}"
  sudo chmod 700 "${ETCD_DATA}"

  #[ -r "etcd-sysusers.conf" ] && sudo cp etcd-sysusers.conf /usr/lib/sysusers.d
  #[ -r "etcd-tmpfile.conf" ] && sudo cp etcd-tmpfiles.conf /usr/lib/tmpfiles.d

  sudo cp etcdctl.sh /etc/etcd/etcdctl.sh
  sed 's/, / /g' < etcdctl.sh | sudo tee /etc/etcd/etcdctl.sh &>/dev/null
  sudo chmod +x /etc/etcd/etcdctl.sh

  sudo cp cert-show.sh /etc/etcd/cert-show.sh
  # sudo cp setup.sh /etc/etcd/etcd_setup.sh

  sudo cp env-etcd /etc/etcd/env
  # [ ! -r "/etc/etcd/config.yaml" ] && 
  sed 's/,"/"/g' < etcd.yaml | sudo tee /etc/etcd/config.yaml &>/dev/null

  sudo cp etcd.service /lib/systemd/system/etcd.service
  #[ ! -L "/etc/systemd/system/etcd.service" ] && sudo ln -s /lib/systemd/system/etcd.service /etc/systemd/system
  sudo timeout -k 10 20 systemctl daemon-reload  >/dev/null 2>&1

  sudo timeout -k 10 20 systemctl enable --now etcd >/dev/null 2>&1
  # sudo timeout -k 10 20 systemctl restart etcd  >/dev/null 2>&1

  # This command sets the cluster to existing for the next start
  #sudo sed -i s"/initial-cluster-state: 'new'/initial-cluster-state: 'existing'/"g  /etc/etcd/config.yaml 
  #sudo sed -i s"/ETCD_INITIAL_CLUSTER_STATE=\"new\"/ETCD_INITIAL_CLUSTER_STATE=\"existing\"/"g  /etc/etcd/env 

}
_stop_resolved() {
  sudo timeout -k 10 20 systemctl stop etcd >/dev/null 2>&1
  sudo timeout -k 10 20 systemctl disable etcd >/dev/null 2>&1
  }
_remove_etcd() {
  sudo timeout -k 10 20 systemctl stop etcd >/dev/null 2>&1
  sudo timeout -k 10 20 systemctl disable etcd >/dev/null 2>&1
}
_start_etcd() {
  sudo timeout -k 10 20 systemctl enable etcd >/dev/null 2>&1
  sudo timeout -k 10 20 systemctl start etcd >/dev/null 2>&1
}
_restart_etcd() {
  sudo timeout -k 10 20 systemctl restart etcd >/dev/null 2>&1
}
if [ "$CMD_TSK" == "install" ] ; then
  if ! _init ; then
    echo "error etcd init"
    exit 1
  fi
#  _make_certs
  _config_etcd
  exit 0
fi
if [ "$CMD_TSK" == "config" ] ; then
  if ! _config_etcd ; then
    echo "error etcd config"
    exit 1
  fi
  exit
fi
if [ "$CMD_TSK" == "remove" ] ; then
  _remove_etcd
  exit
fi
if [ "$CMD_TSK"  == "update" ] ; then 
  _restart_etcd && exit 0
fi
if ! _stop_resolved ; then 
  echo "error etcd stop"
  exit 1
fi
if ! _start_etcd ; then 
  echo "error etcd start"
  exit 1
fi
