kubeasz/tools/easzctl

132 lines
4.6 KiB
Plaintext
Raw Normal View History

#!/bin/bash
#
# This script can be used to manage k8s clusters. (developing)
set -o nounset
#set -o errexit
#set -o xtrace
function usage() {
cat <<EOF
Usage: easzctl COMMAND [args]
Commands:
add-node To add a kube-node(work node) to the k8s cluster
add-master To add a kube-master(master node) to the k8s cluster
add-etcd To add a etcd-node to the etcd cluster
del-etcd To delete a etcd-node from the etcd cluster
clean-node To clean a node, whatever role the node plays
help To display usage information
Use "easzctl help <command>" for more information about a given command.
EOF
}
function process_cmd() {
echo -e "$ACTION : $CMD"
$CMD || { echo -e "+---\033[31mAction failed\033[0m---+ : $CMD"; exit 1; }
echo -e "+---\033[32mAction successed\033[0m---+ : $CMD"
}
function add-node() {
# check new node's address regexp
[[ $1 =~ ^(2(5[0-5]{1}|[0-4][0-9]{1})|[0-1]?[0-9]{1,2})(\.(2(5[0-5]{1}|[0-4][0-9]{1})|[0-1]?[0-9]{1,2})){3}$ ]] || { echo "ERROR: Invalid ip address!"; exit 2; }
# check if the new node already exsited
sed -n '/^\[kube-master/,/^\[harbor/p' $BASEPATH/hosts|grep "^$1" && { echo "ERROR: node $1 already existed!"; exit 2; }
# add a node into 'kube-node' group
sed -i "/\[kube-node/a $1 NEW_NODE=yes" $BASEPATH/hosts
# check if playbook runs successfully
ansible-playbook $BASEPATH/20.addnode.yml -e NODE_TO_ADD=$1 || { sed -i "/$1 NEW_NODE=yes/d" $BASEPATH/hosts; exit 2; }
}
function add-master() {
# check new master's address regexp
[[ $1 =~ ^(2(5[0-5]{1}|[0-4][0-9]{1})|[0-1]?[0-9]{1,2})(\.(2(5[0-5]{1}|[0-4][0-9]{1})|[0-1]?[0-9]{1,2})){3}$ ]] || { echo "ERROR: Invalid ip address!"; exit 2; }
# check if k8s with DPLOY_MODE='multi-master'
grep '^DEPLOY_MODE=multi-master' $BASEPATH/hosts || { echo "ERROR: only k8s with DPLOY_MODE='multi-master' can have master node added!"; exit 2; }
# check if the new master already exsited
sed -n '/^\[kube-master/,/^\[kube-node/p' $BASEPATH/hosts|grep "^$1" && { echo "ERROR: master $1 already existed!"; exit 2; }
# add a node into 'kube-master' group
sed -i "/\[kube-master/a $1 NEW_MASTER=yes" $BASEPATH/hosts
# check if playbook runs successfully
ansible-playbook $BASEPATH/21.addmaster.yml -e NODE_TO_ADD=$1 || { sed -i "/$1 NEW_MASTER=yes/d" $BASEPATH/hosts; exit 2; }
}
function add-etcd() {
# check new node's address regexp
[[ $1 =~ ^(2(5[0-5]{1}|[0-4][0-9]{1})|[0-1]?[0-9]{1,2})(\.(2(5[0-5]{1}|[0-4][0-9]{1})|[0-1]?[0-9]{1,2})){3}$ ]] || { echo "ERROR: Invalid ip address!"; exit 2; }
# check if the new node already exsited
sed -n '/^\[etcd/,/^\[kube-master/p' $BASEPATH/hosts|grep "^$1" && { echo "ERROR: node $1 already existed!"; exit 2; }
# input an unique NODE_NAME of the node in etcd cluster
echo "Please input an UNIQUE name(string) for the new node: "
read NAME
sed -n '/^\[etcd/,/^\[kube-master/p' $BASEPATH/hosts|grep "$NAME" && { echo "ERROR: name [$NAME] already existed!"; exit 2; }
# add a node into 'kube-node' group
sed -i "/\[etcd/a $1 NODE_NAME=$NAME" $BASEPATH/hosts
# check if playbook runs successfully
ansible-playbook $BASEPATH/19.addetcd.yml -e NODE_TO_ADD=$1 || { sed -i "/$1 NODE_NAME=$NAME/d" $BASEPATH/hosts; exit 2; }
}
function del-etcd() {
# check node's address regexp
[[ $1 =~ ^(2(5[0-5]{1}|[0-4][0-9]{1})|[0-1]?[0-9]{1,2})(\.(2(5[0-5]{1}|[0-4][0-9]{1})|[0-1]?[0-9]{1,2})){3}$ ]] || { echo "ERROR: Invalid ip address!"; exit 2; }
#
ansible-playbook $BASEPATH/tools/remove_etcd_node.yml -e ETCD_TO_DEL=$1
}
function clean-node() {
# check node's address regexp
[[ $1 =~ ^(2(5[0-5]{1}|[0-4][0-9]{1})|[0-1]?[0-9]{1,2})(\.(2(5[0-5]{1}|[0-4][0-9]{1})|[0-1]?[0-9]{1,2})){3}$ ]] || { echo "ERROR: Invalid ip address!"; exit 2; }
#
ansible-playbook $BASEPATH/tools/clean_one_node.yml -e NODE_TO_DEL=$1
}
###############################################################
BASEPATH=/etc/ansible
[ "$#" -gt 1 ] || { usage >&2; exit 2; }
case "$1" in
(add-node)
ACTION="+---\033[33mAction: add a k8s work node\033[0m---+"
CMD="add-node $2"
;;
(add-master)
ACTION="+---\033[33mAction: add a k8s master node\033[0m---+"
CMD="add-master $2"
;;
(add-etcd)
ACTION="+---\033[33mAction: add a etcd node\033[0m---+"
CMD="add-etcd $2"
;;
(del-etcd)
ACTION="+---\033[33mAction: delete a etcd node\033[0m---+"
CMD="del-etcd $2"
;;
(clean-node)
ACTION="+---\033[33mAction: clean a node\033[0m---+"
CMD="clean-node $2"
;;
(*)
usage
exit 0
;;
esac
process_cmd