Upgrade ovn-kubernetes CNI to latest release
[iec.git] / src / foundation / scripts / startup.sh
1 #!/bin/bash
2 # shellcheck disable=SC1073,SC1072,SC1039,SC2059,SC2046
3 #Install the k8s-master & k8s-worker node from Mgnt node
4 #
5 set -e
6
7 #
8 # Displays the help menu.
9 #
10 display_help () {
11   echo "Usage:"
12   echo " "
13   echo "This script can help you to deploy a simple iec testing"
14   echo "environments."
15   echo "Firstly, the master node and worker node information must"
16   echo "be added into config file which will be used for deployment."
17   echo ""
18   echo "Secondly, there should be an user on each node which will be"
19   echo "used to install the corresponding software on master and"
20   echo "worker nodes. At the same time, this user should be enable to"
21   echo "run the sudo command without input password on the hosts."
22   echo ""
23   echo "In the end, some optional parameters could be directly passed"
24   echo "to startup.sh by shell for easy customizing your own IEC"
25   echo "environments:"
26   echo "-k|--kube:      ---- The version of k8s"
27   echo "-c|--cni-ver:   ---- Kubernetes-cni version"
28   echo "-C|--cni:       ---- CNI type: calico/flannel"
29   echo ""
30   echo "Example usages:"
31   echo "   ./startup.sh #Deploy with default parameters"
32   echo "    #Deploy the flannel with 1.15.2 K8s"
33   echo "   ./startup.sh -C flannel -k 1.15.2 -c 0.7.5"
34   exit
35 }
36
37 #
38 # Setup system configuration before invoke setup-cni.sh
39 #
40 k8s_worker_preconfigure() {
41 case ${CNI_TYPE} in
42   contivpp)
43     if [ -n "${DEV_NAME[$ip_addr]}" ]
44     then
45       CONTIVPP_CONFIG="cd iec/src/foundation/scripts/cni/contivpp && sudo ./contiv-update-config.sh ${DEV_NAME[$ip_addr]}"
46       sshpass -p ${passwd} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${ip_addr} $CONTIVPP_CONFIG
47     fi
48     ;;
49   danm)
50     DANM_CONFIG="cd iec/src/foundation/scripts/cni/danm && sudo ./danm_install.sh"
51     sshpass -p ${passwd} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${ip_addr} $DANM_CONFIG
52     ;;
53   cilium)
54     CILIUM_CONFIG="cd iec/src/foundation/scripts/cni/cilium && sudo ./cilium_install.sh"
55     sshpass -p ${passwd} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${ip_addr} $CILIUM_CONFIG
56     ;;
57   ovn-kubernetes)
58     OVN_KUBERNETES_PRECONFIG="cd iec/src/foundation/scripts/cni/ovn-kubernetes && ./clean_old_ovs.sh"
59     sshpass -p ${passwd} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${ip_addr} $OVN_KUBERNETES_PRECONFIG
60     ;;
61   *)
62     ;;
63 esac
64 }
65
66 #
67 # Deploy k8s.
68 #
69 deploy_k8s () {
70   set -o xtrace
71
72   INSTALL_SOFTWARE="sudo apt-get update && sudo apt-get install -y git &&\
73            sudo rm -rf ~/.kube ~/iec &&\
74            git clone ${REPO_URL} &&\
75            cd iec/src/foundation/scripts/ && source k8s_common.sh $KUBE_VERSION $CNI_VERSION"
76
77   #Automatic deploy the K8s environments on Master node
78   SETUP_MASTER="cd iec/src/foundation/scripts/ && source k8s_master.sh ${K8S_MASTER_IP} ${POD_NETWORK_CIDR} ${SVC_CIDR}"
79   sshpass -p ${K8S_MASTERPW} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${K8S_MASTER_IP} ${INSTALL_SOFTWARE}
80   sshpass -p ${K8S_MASTERPW} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${K8S_MASTER_IP} ${SETUP_MASTER} | tee ${LOG_FILE}
81
82   TOKEN=$(grep "\--token " ./${LOG_FILE})
83   TOKEN_ID=$(echo ${TOKEN#*"token "}|cut -f1 -d' ')
84
85   SH256=$(grep "\--discovery-token-ca-cert-hash " ./${LOG_FILE})
86   TOKEN_CA_SH256=${SH256#*"sha256:"}
87
88   KUBEADM_JOIN_CMD="kubeadm join ${K8S_MASTER_IP}:6443 --token ${TOKEN_ID} --discovery-token-ca-cert-hash sha256:${TOKEN_CA_SH256}"
89
90   #Automatic deploy the K8s environments on each worker-node
91   SETUP_WORKER="cd iec/src/foundation/scripts/ && source k8s_worker.sh"
92
93   for worker in "${K8S_WORKER_GROUP[@]}"
94   do
95     ip_addr="$(cut -d',' -f1 <<<${worker})"
96     passwd="$(cut -d',' -f2 <<<${worker})"
97     echo "Install & Deploy on ${ip_addr}. password:${passwd}"
98
99     sshpass -p ${passwd} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${ip_addr} ${INSTALL_SOFTWARE}
100     sshpass -p ${passwd} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${ip_addr} "echo \"sudo ${KUBEADM_JOIN_CMD}\" >> ./iec/src/foundation/scripts/k8s_worker.sh"
101     sleep 2
102     k8s_worker_preconfigure
103
104     sshpass -p ${passwd} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${ip_addr} ${SETUP_WORKER}
105     sshpass -p ${passwd} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${ip_addr} "sudo swapon -a"
106   done
107
108
109   #Deploy etcd & CNI from master node
110   SETUP_CNI="cd iec/src/foundation/scripts && source setup-cni.sh $CNI_TYPE $POD_NETWORK_CIDR $K8S_MASTER_IP $SVC_CIDR $CLUSTER_IP"
111   sshpass -p ${K8S_MASTERPW} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${K8S_MASTER_IP} ${SETUP_CNI}
112   SETUP_HELM="cd iec/src/foundation/scripts && source helm.sh"
113   sshpass -p ${K8S_MASTERPW} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${K8S_MASTER_IP} ${SETUP_HELM}
114
115 }
116
117 #
118 # Check the K8s environments
119 #
120 check_k8s_status(){
121   set -o xtrace
122
123   VERIFY_K8S="cd iec/src/foundation/scripts/ && source nginx.sh"
124   sshpass -p ${K8S_MASTERPW} ssh -o StrictHostKeyChecking=no ${HOST_USER}@${K8S_MASTER_IP} ${VERIFY_K8S}
125 }
126
127 #
128 # print environments
129 #
130 printOption(){
131   echo "K8S_MASTER_IP:${K8S_MASTER_IP}"
132   echo "HOST_USER:${HOST_USER}"
133   echo "REPO_URL:${REPO_URL}"
134
135   echo "The number of K8s-Workers:${#K8S_WORKER_GROUP[@]}"
136   for worker in "${K8S_WORKER_GROUP[@]}"
137   do
138     ip_addr="$(cut -d',' -f1 <<<${worker})"
139     passwd="$(cut -d',' -f2 <<<${worker})"
140     echo "Install & Deploy on ${ip_addr}. password:${passwd}"
141   done
142
143   echo "KUBE_VERSION: ${KUBE_VERSION}"
144   echo "CNI_TPYE: ${CNI_TYPE}"
145   echo "CLUSTER_IP: ${CLUSTER_IP}"
146   echo "POD_NETWORK_CIDR: ${POD_NETWORK_CIDR}"
147 }
148
149 # Read the configuration file
150 source config
151
152 rm -f "${LOG_FILE}"
153
154 ARGS=`getopt -a -o C:k:c:h -l cni:,kube:,cni-ver:,help -- "$@"`
155 eval set -- "${ARGS}"
156 while true
157 do
158         case "$1" in
159         -C|--cni)
160                 CNI_TYPE="$2"
161                 echo "CNI_TYPE=$2"
162                 shift
163                 ;;
164         -k|--kube)
165                 echo "This is config kube version:$2"
166                 KUBE_VERSION="$2"
167                 shift
168                 ;;
169         -c|--cni-ver)
170                 echo "This is config cni version:$2"
171                 CNI_VERSION="$2"
172                 shift
173                 ;;
174         -h|--help)
175                 echo "this is help case"
176                 display_help
177                 ;;
178         --)
179                 printOption
180                 shift
181                 break
182                 ;;
183         esac
184 shift
185 done
186
187 deploy_k8s
188
189 sleep 20
190
191 check_k8s_status