f68c2fb4329e1bd1d1b01bd35cd0841ceeecc533
[iec.git] / src / foundation / scripts / startup.sh
1 #!/bin/bash
2 #Install the k8s-master & k8s-worker node from Mgnt node
3 #
4 set -e
5
6 #
7 # Displays the help menu.
8 #
9 display_help () {
10   echo "Usage:"
11   echo " "
12   echo "This script can help you to deploy a simple iec testing"
13   echo "environments."
14   echo "Firstly, the master node and worker node information must"
15   echo "be added into config file which will be used for deployment."
16   echo ""
17   echo "Secondly, there should be an user on each node which will be"
18   echo "used to install the corresponding software on master and"
19   echo "worker nodes. At the same time, this user should be enable to"
20   echo "run the sudo command without input password on the hosts."
21   echo " "
22   echo "Example usages:"
23   echo "   ./startup.sh"
24 }
25
26
27
28 #
29 # Deploy k8s with calico.
30 #
31 deploy_k8s () {
32   set -o xtrace
33
34   INSTALL_SOFTWARE="sudo apt-get update && sudo apt-get install -y git &&\
35            sudo rm -rf ~/.kube ~/iec &&\
36            git clone ${REPO_URL} &&\
37            cd iec/src/foundation/scripts/ && source k8s_common.sh"
38
39   #Automatic deploy the K8s environments on Master node
40   SETUP_MASTER="cd iec/src/foundation/scripts/ && source k8s_master.sh ${K8S_MASTER_IP}"
41   sshpass -p ${K8S_MASTERPW} ssh ${HOST_USER}@${K8S_MASTER_IP} ${INSTALL_SOFTWARE}
42   sshpass -p ${K8S_MASTERPW} ssh ${HOST_USER}@${K8S_MASTER_IP} ${SETUP_MASTER} | tee ${LOG_FILE}
43
44   KUBEADM_JOIN_CMD=$(grep "kubeadm join " ./${LOG_FILE})
45
46
47   #Automatic deploy the K8s environments on each worker-node
48   SETUP_WORKER="cd iec/src/foundation/scripts/ && source k8s_worker.sh"
49
50   for worker in "${K8S_WORKER_GROUP[@]}"
51   do
52     ip_addr="$(cut -d',' -f1 <<<${worker})"
53     passwd="$(cut -d',' -f2 <<<${worker})"
54     echo "Install & Deploy on ${ip_addr}. password:${passwd}"
55
56     sshpass -p ${passwd} ssh ${HOST_USER}@${ip_addr} ${INSTALL_SOFTWARE}
57     sshpass -p ${passwd} ssh ${HOST_USER}@${ip_addr} "echo \"sudo ${KUBEADM_JOIN_CMD}\" >> ./iec/src/foundation/scripts/k8s_worker.sh"
58     sshpass -p ${passwd} ssh ${HOST_USER}@${ip_addr} ${SETUP_WORKER}
59
60   done
61
62
63   #Deploy etcd & CNI from master node
64   #There may be more options in future. e.g: Calico, Contiv-vpp, Ovn-k8s ...
65   SETUP_CNI="cd iec/src/foundation/scripts && source setup-cni.sh"
66   sshpass -p ${K8S_MASTERPW} ssh ${HOST_USER}@${K8S_MASTER_IP} ${SETUP_CNI}
67 }
68
69 #
70 # Check the K8s environments
71 #
72 check_k8s_status(){
73   set -o xtrace
74
75   VERIFY_K8S="cd iec/src/foundation/scripts/ && source nginx.sh"
76   sshpass -p ${K8S_MASTERPW} ssh ${HOST_USER}@${K8S_MASTER_IP} ${VERIFY_K8S}
77
78   sleep 30
79 }
80
81
82 #
83 # Init
84 #
85 if [ $1 == "--help" ] || [ $1 == "-h" ];
86 then
87   display_help
88   exit 0
89 fi
90
91
92 # Read the configuration file
93 source config
94
95 echo "The number of K8s-Workers:${#K8S_WORKER_GROUP[@]}"
96
97 rm -f "${LOG_FILE}"
98
99 deploy_k8s
100
101 check_k8s_status