changes for centos updated to iotgateway blueprint
[eliot.git] / blueprints / iotgateway / scripts / setup.sh
1 #!/bin/bash -ex
2 ##############################################################################
3 # Copyright (c) 2019 Huawei Tech and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 ########################################################################################
12 #                                                                                      #
13 # The script is to setup the ELIOT Manager and ELIOT nodes.                            #
14 # It installs Docker in both ELIOT Manager and ELIOT node.                             #
15 # It installs Kubernetes. In the ELIOT Manager kubeadm, kubelet, kubectl is installed. #
16 # In ELIOT Edge Node it will install kubeadn, kubelet.                                 #
17 # Script is tested in Ubuntu 16.04 version.                                            #
18 # sshpass needs to be installed before executing this script.                          #
19 ########################################################################################
20
21 # constants
22
23 OSPLATFORM=$(awk -F= '/^NAME/{print $2}' /etc/os-release)
24 ELIOT_REPO="https://gerrit.akraino.org/r/eliot"
25
26 show_help()
27 {
28   echo "The script helps in setting up the ELIOT Toplogy Infrastrucutre"
29   echo "The setup installs Docker, K8S Master and K8S worker nodes in  "
30   echo "ELIOT Manager and ELIOT Workder Nodes respectively "
31   echo "After completion of script execution execute command: "
32   echo "kubectl get nodes to check whether the connection between "
33   echo "ELIOT Manager and ELIOT Nodes are established"
34   echo ""
35   echo "Nodelist file should have the details of Worker Nodes in the format of:"
36   echo "EliotNodeUserName|EliotNodeIP|EliotNodePasswor"
37   echo "Each line should have detail of one ELIOT Node only"
38 }
39
40 # Setting up ELIOT Manager Node.
41 # Installing Docker, K8S and Initializing K8S Master
42 setup_k8smaster()
43 {
44   #set -o xtrace
45   sudo rm -rf ~/.kube
46   source common.sh | tee eliotcommon.log
47   source k8smaster.sh | tee kubeadm.log
48   # Setup ELIOT Node
49   setup_k8sworkers
50 }
51
52 setup_k8sworkers()
53 {
54   set -o xtrace
55
56   # Install Docker on ELIOT Node
57   SETUP_WORKER_COMMON="sudo rm -rf ~/eliot &&\
58                        git clone ${ELIOT_REPO} &&\
59                        cd eliot/blueprints/iotgateway/scripts/ && source common.sh"
60   #SETUP_WORKER_COMMON="cd eliot/scripts/ && source common.sh"
61   SETUP_WORKER="cd eliot/blueprints/iotgateway/scripts/ && source k8sworker.sh"
62
63   KUBEADM_TOKEN=$(kubeadm token create --print-join-command)
64   KUBEADM_JOIN="sudo ${KUBEADM_TOKEN}"
65
66  # Read all the Worker Node details from nodelist file.
67  while read line
68  do
69      nodeinfo="${line}"
70      nodeusr=$(echo ${nodeinfo} | cut -d"|" -f1)
71      nodeip=$(echo ${nodeinfo} | cut -d"|" -f2)
72      nodepaswd=$(echo ${nodeinfo} | cut -d"|" -f3)
73      sshpass -p ${nodepaswd} ssh ${nodeusr}@${nodeip} ${SETUP_WORKER_COMMON} < /dev/null
74      sshpass -p ${nodepaswd} ssh ${nodeusr}@${nodeip} ${SETUP_WORKER} < /dev/null
75      sshpass -p ${nodepaswd} ssh ${nodeusr}@${nodeip} ${KUBEADM_JOIN} < /dev/null
76  done < nodelist > /dev/null 2>&1
77
78 }
79
80 setup_k8smaster_centos()
81 {
82   set -o xtrace
83   sudo rm -rf ~/.kube
84   source common_centos.sh | tee eliotcommon_centos.log
85   source k8smaster_centos.sh | tee kubeadm_centos.log
86
87   # Setup ELIOT Node
88   setup_k8sworkers_centos
89
90   kubectl apply -f cni/calico/v38/calico.yaml
91
92 }
93
94
95 setup_k8sworkers_centos()
96 {
97   set -o xtrace
98   # Install Docker on ELIOT Node
99
100   SETUP_WORKER_COMMON_CENTOS="sudo rm -rf ~/eliot &&\
101                               git clone ${ELIOT_REPO} &&\
102                               cd eliot/blueprints/iotgateway/scripts/ && source common_centos.sh"
103
104   # SETUP_WORKER_COMMON_CENTOS="cd /root/eliot/scripts/ && source common_centos.sh"
105
106   KUBEADM_TOKEN=$(sudo kubeadm token create --print-join-command)
107   KUBEADM_JOIN_CENTOS="sudo ${KUBEADM_TOKEN}"
108   # Read all the Worker Node details from nodelist file.
109   while read line
110   do
111       nodeinfo="${line}" < /dev/null 2>&1
112       nodeusr=$(echo ${nodeinfo} | cut -d"|" -f1) < /dev/null
113       nodeip=$(echo ${nodeinfo} | cut -d"|" -f2)  < /dev/null
114       nodepaswd=$(echo ${nodeinfo} | cut -d"|" -f3) < /dev/null
115       sudo sshpass -p ${nodepaswd} ssh ${nodeusr}@${nodeip} ${SETUP_WORKER_COMMON_CENTOS} < /dev/null
116       sudo sshpass -p ${nodepaswd} ssh ${nodeusr}@${nodeip} ${KUBEADM_JOIN_CENTOS} < /dev/null
117   done < nodelist > /dev/null 2>&1
118
119 }
120
121 # verify kubernetes setup by deploying nginx server.
122
123 verify_k8s_status(){
124   set -o xtrace
125   source verifyk8s.sh | tee verifyk8s.log
126 }
127
128
129 install_edgex(){
130  set -o xtrace
131  cd edgex && source edgexonk8s.sh
132 }
133
134 # verify installation of edgex platform 
135 verify_edgex()
136 {
137  set -o xtrace
138  source verifyedgex.sh | tee verifyedgex.log
139
140 }
141
142
143 install_cadvisor_edge(){
144  set -o xtrace
145  SETUP_CADVISOR_ATEDGE="cd eliot/blueprints/iotgateway/scripts/ && source cadvisorsetup.sh"
146  while read line
147  do
148      nodeinfo="${line}"
149      nodeusr=$(echo ${nodeinfo} | cut -d"|" -f1)
150      nodeip=$(echo ${nodeinfo} | cut -d"|" -f2)
151      nodepaswd=$(echo ${nodeinfo} | cut -d"|" -f3)
152      sshpass -p ${nodepaswd} ssh ${nodeusr}@${nodeip} ${SETUP_CADVISOR_ATEDGE} < /dev/null
153  done < nodelist > /dev/null 2>&1
154  echo "CADVISOR Installed in all the ELIOT IOT-GATEWAY Nodes"
155 }
156
157 install_prometheus(){
158  set -o xtrace
159  source prometheus.sh | tee install_prometheus.log
160  echo "Prometheus deployed successfully on ELIOT Manager Node  and integrated with CAdvisor running on IOT-Gateway Nodes "
161 }
162
163 # Start
164 #
165
166 if [ $1 == "--help" ] || [ $1 == "-h" ];
167 then
168   show_help
169   exit 0
170 fi
171
172 if [[ $OSPLATFORM = *CentOS* ]]; then
173    setup_k8smaster_centos
174 else
175    setup_k8smaster
176 fi
177
178 sleep 20
179 verify_k8s_status
180 install_cadvisor_edge
181 sleep 10
182 install_prometheus
183 sleep 5
184 sudo docker ps | grep prometheus
185
186 install_edgex
187 sleep 20
188 verify_edgex
189
190 # Removing the taint from master node
191 kubectl taint nodes --all node-role.kubernetes.io/master- || true
192
193 echo "ELIOT IOT-Gateway Platform is Successfully Deployed !!!"
194