#!/bin/bash ############################################################## # File Name: etcd_linux.sh # Version: V1.0 # Author: sanshi # Organization: https://www.9133w.cn # Created Time : 2020-06-29 08:59:58 # Description: ############################################################## # set -ue #GITHUB_URL=https://github.com/etcd-io/etcd/releases/download LOCAL_URL=https://9133w.cn/sources DOWNLOAD_URL=${LOCAL_URL} local_ip=`hostname -I | awk '{print $1}'` tmp_file=`mktemp /tmp/XXXX` tar_tmp="/tmp" install_dir="$HOME/etcd" #depends rpm -q epel-release &> /dev/null || sudo yum install -y epel-release command -v sshpass || sudo yum install -y sshpass command -v pssh || sudo yum install -y pssh if [ ! -f $HOME/.ssh/id_rsa.pub ] then echo -e "\n\n" | ssh-keygen -t rsa -N"" -q fi echo "simple install[简单安装]?" cat << EOF [1] yes [2] no EOF read -p "Your choice:[选项(1/2)]: " choic if [[ "$choic" -eq 1 ]] then sudo yum install -y etcd && \ sudo systemctl start etcd && \ sudo systemctl enable etcd cat << EOF etcd installed finish. conf_path: /etc/etcd data_path: /var/lib/etcd EOF exit 0 fi echo "Install single instance or cluster[单机版还是集群版]?" cat << EOF [1] single(单机) [2] cluster(集群) EOF read -p "Your choice:[选项(1/2)]: " coc # check running ? [[ "$coc" -eq 1 ]] && ps -ef |grep etcd |grep -Ev "grep|$0" && echo 'etcd running now!' && exit 0 # download [[ -d "$install_dir" ]] && rm -rf "$install_dir" [ -f $tar_tmp/etcd-v3.tgz ] || \ wget -T 300 --tries=300 ${DOWNLOAD_URL}/etcd-v3.tgz -O $tar_tmp/etcd-v3.tgz tar xf $tar_tmp/etcd-v3.tgz -C $HOME/ # install [[ ! -d $install_dir ]] && echo "etcd dir not exists" && exit 1 pushd $install_dir if [[ "$coc" -eq 2 ]] then let num=1 while [[ "$num" -ge 1 ]] do read -p "Input cluster number ip or q|Q to finish: " remote_ip sleep 1 if [[ "$remote_ip" =~ q|Q ]] then let num=0 break fi read -p "Input cluster number user: " remote_user sleep 1 read -p "Input cluster user password: " -s remote_pass read -p "Input cluster login sshd port: " remote_port echo "node$num:$remote_ip:$remote_user:$remote_port" >> $tmp_file && ((num++)) echo $num if pssh -H $remote_ip:$remote_port -l $remote_user "echo" &> /dev/null then : else sshpass -p$remote_pass ssh-copy-id -o StrictHostKeyChecking=no -o Port=$remote_port $remote_user@$remote_ip [ $? -ne 0 ] && echo "local and remote access each other not ok" && exit 2 fi if ! pssh -H $remote_ip:$remote_port -l $remote_user "ls $install_dir" then scp -P $remote_port -r /usr/local/etcd $remote_user@$remote_ip:$HOME/ [ $? -ne 0 ] && \ echo "copy etcd files failure to remote $remote_ip" && exit 3 || \ echo "copy etcd source files to remote $remote_ip finish" fi done read -p "Input data dir of etcd: " data_dir data_dir=${data_dir:-$install_dir} init_c="`awk -F: '{print $1"=http://"$2":2380"}' $tmp_file | sed ':t;N;s/\n/,/;b t'`" | true init_c=${init_c:-0} if [[ "$init_c" != "0" ]] then init_cluster="node0=http://$local_ip:2380,$init_c" while read line do node_name=`echo $line | awk -F: '{print $1}'` node_ip=`echo $line | awk -F: '{print $2}'` node_user=`echo $line | awk -F: '{print $3}'` node_ssh_port=`echo $line | awk -F: '{print $4}'` pssh -H $node_ip:$node_ssh_port -l $node_user "pushd $install_dir; \ nohup ./etcd --name $node_name \ --data-dir ${data_dir} \ --listen-client-urls http://0.0.0.0:2379 \ --advertise-client-urls http://$node_ip:2379 \ --listen-peer-urls http://0.0.0.0:2380 \ --initial-advertise-peer-urls http://$node_ip:2380 \ --initial-cluster-token etcd-prod \ --initial-cluster-state new \ --initial-cluster $init_cluster >> ./status.log 2>&1 &;popd" sleep 1 pssh -H $node_ip:$node_ssh_port -l $node_user "/usr/sbin/ss -tnl | grep -q 2379" && \ echo "$node_name etcd start success, exec path: $install_dir" || \ echo "$node_name etcd start failure" done < $tmp_file else init_cluster="node0=http://$local_ip:2380" fi nohup ./etcd --name node0 \ --data-dir ${data_dir} \ --listen-client-urls http://0.0.0.0:2379 \ --advertise-client-urls http://$local_ip:2379 \ --listen-peer-urls http://0.0.0.0:2380 \ --initial-advertise-peer-urls http://$local_ip:2380 \ --initial-cluster-token etcd-prod \ --initial-cluster-state new \ --initial-cluster $init_cluster >> ./status.log 2>&1 & sleep 1 && /usr/sbin/ss -tnl | grep -q 2379 && \ echo "start node0 of etcd cluster finish" && \ sudo ln -s $install_dir/etcdctl /usr/local/bin/etcdctl && \ sudo ln -s $install_dir/etcd /usr/local/bin/etcd || \ echo "start node0 of etcd cluster failure" elif [[ "$coc" -eq 1 ]] then read -p "Input data dir of etcd: " data_dir data_dir=${data_dir:-$install_dir} nohup ./etcd --name node0 \ --data-dir ${data_dir} \ --listen-client-urls http://0.0.0.0:2379 \ --advertise-client-urls http://0.0.0.0:2379 \ --listen-peer-urls http://0.0.0.0:2380 \ --initial-advertise-peer-urls http://0.0.0.0:2380 \ --initial-cluster node0=http://0.0.0.0:2380 >> ./etcd.log 2>&1 & sleep 1 && /usr/sbin/ss -tnl | grep -q 2379 && \ echo "start node0 of etcd finish" && \ sudo ln -s $install_dir/etcdctl /usr/local/bin/etcdctl && \ sudo ln -s $install_dir/etcd /usr/local/bin/etcd || \ echo "start node0 of etcd failure" else echo "invalid choice" exit 1 fi popd rm -f $tar_tmp/etcd-v3.tgz rm -f $tmp_file exit 0