blob: 24f603fb5a02c3ff496695e47132609e5d89cccd [file] [log] [blame]
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -04001#!/bin/bash
Zack Williams41513bf2018-07-07 20:08:35 -07002# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040015
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040016lBlue='\033[1;34m'
17green='\033[0;32m'
18orange='\033[0;33m'
19NC='\033[0m'
20red='\033[0;31m'
21yellow='\033[1;33m'
22dGrey='\033[1;30m'
23lGrey='\033[1;37m'
24lCyan='\033[1;36m'
25wd=`pwd`
26
27
28# Clean up any prior executions
29rm -fr .keys
30rm -f ansible/hosts/cluster
31rm -f ansible/host_vars/*
32
33# Source the configuration information
34. install.cfg
35
Sergio Slobodrian37f4a0e2017-06-14 07:50:01 -040036if [ -z "$hosts" ]; then
37 echo -e "${red}No hosts specifed!!${NC}"
38 echo -e "${red}Did you forget to update the config file ${yellow}installer.cfg${red}?${NC}"
39 exit
40fi
41
Sergio Slobodrianb92e5132017-09-13 13:04:05 -040042if [ "$iUser" == "voltha" ]; then
43 echo -e "${yellow}voltha ${red}can't be used as be install user!!!${NC}"
44 echo -e "${red}Please delete the ${yellow}voltha ${red}user on the targets and create a different installation user${NC}"
45 exit
46fi
47
Sergio Slobodrian8725ea82017-08-27 23:47:41 -040048# Configure barrier file sizes but only if a value was provided in the config file
49
50if [ -v logLimit ]; then
Stephane Barbariebcea2f42018-03-02 18:46:32 -050051 sed -i -e "/logger_volume_size/s/.*/logger_volume_size: ${logLimit}/" ansible/group_vars/all
Sergio Slobodrian8725ea82017-08-27 23:47:41 -040052fi
53if [ -v regLimit ]; then
Stephane Barbariebcea2f42018-03-02 18:46:32 -050054 sed -i -e "/registry_volume_size/s/.*/registry_volume_size: ${regLimit}/" ansible/group_vars/all
Sergio Slobodrian8725ea82017-08-27 23:47:41 -040055fi
56if [ -v consulLimit ]; then
Stephane Barbariebcea2f42018-03-02 18:46:32 -050057 sed -i -e "/consul_volume_size/s/.*/consul_volume_size: ${consulLimit}/" ansible/group_vars/all
Sergio Slobodrian8725ea82017-08-27 23:47:41 -040058fi
59
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040060# Create the key directory
61mkdir .keys
62
63# Create the host list
64echo "[cluster]" > ansible/hosts/cluster
65
66# Silence SSH and avoid prompts
67rm -f ~/.ssh/config
68echo "Host *" > ~/.ssh/config
69echo " StrictHostKeyChecking no" >> ~/.ssh/config
70echo " UserKnownHostsFile /dev/null" >> ~/.ssh/config
71
72sudo cp ~/.ssh/config /root/.ssh/config
73
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040074for i in $hosts
75do
Stephane Barbariebcea2f42018-03-02 18:46:32 -050076 # Generate the key for the host
77 echo -e "${lBlue}Generating the key-pair for communication with host ${yellow}$i${NC}"
78 ssh-keygen -f ./$i -t rsa -N ''
79 mv $i .keys
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040080
Stephane Barbariebcea2f42018-03-02 18:46:32 -050081 # Generate the pre-configuration script
82 echo -e "${lBlue}Creating the pre-configuration script${NC}"
83 head -n +1 BashLoginTarget.sh > bash_login.sh
84 echo "" >> bash_login.sh
85 echo -n 'key="' >> bash_login.sh
86 sed -i -e 's/$/"/' $i.pub
87 cat $i.pub >> bash_login.sh
88 tail -n +2 BashLoginTarget.sh | grep -v "{{ key }}" >> bash_login.sh
89 rm $i.pub
Sergio Slobodrianb92e5132017-09-13 13:04:05 -040090
Stephane Barbariebcea2f42018-03-02 18:46:32 -050091 # Copy the pre-config file to the VM
92 echo -e "${lBlue}Transfering pre-configuration script to ${yellow}$i${NC}"
93 if [ -d ".test" ]; then
94 echo -e "${red}Test mode set!!${lBlue} Using pre-populated ssh key for ${yellow}$i${NC}"
95 scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .test/$i bash_login.sh $iUser@$i:.bash_login
96 else
97 scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bash_login.sh $iUser@$i:.bash_login
98 fi
99 rm bash_login.sh
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400100
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500101 # Run the pre-config file on the VM
102 echo -e "${lBlue}Running the pre-configuration script on ${yellow}$i${NC}"
103 if [ -d ".test" ]; then
104 echo -e "${red}Test mode set!!${lBlue} Using pre-populated ssh key for ${yellow}$i${NC}"
105 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .test/$i $iUser@$i
106 else
107 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $iUser@$i
108 fi
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400109
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500110 # Configure ansible and ssh for silent operation
111 echo -e "${lBlue}Configuring ansible${NC}"
112 echo $i >> ansible/hosts/cluster
113 echo "ansible_ssh_private_key_file: $wd/.keys/$i" > ansible/host_vars/$i
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400114
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500115 # Create the tunnel to the registry to allow pulls from localhost
116 echo -e "${lBlue}Creating a secure shell tunnel to the registry for ${yellow}$i${NC}"
117 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i -f voltha@$i -R 5000:localhost:5000 -N
118
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400119done
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400120
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400121# Add the dependent software list to the cluster variables
122echo -e "${lBlue}Setting up dependent software${NC}"
123# Delete any grub updates since the boot disk is almost
124# guaranteed not to be the same device as the installer.
125mkdir grub_updates
126sudo mv deb_files/*grub* grub_updates
127# Sort the packages in dependency order to get rid of scary non-errors
128# that are issued by ansible.
129#echo -e "${lBlue}Dependency sorting dependent software${NC}"
130#./sort_packages.sh
131#echo "deb_files:" >> ansible/group_vars/all
132#for i in `cat sortedDebs.txt`
133#do
134#echo " - $i" >> ansible/group_vars/all
135#done
136
137# Make sure the ssh keys propagate to all hosts allowing passwordless logins between them
138echo -e "${lBlue}Propagating ssh keys${NC}"
Sergio Slobodrian37f4a0e2017-06-14 07:50:01 -0400139cp -r .keys ansible/roles/cluster-host/files
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400140
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400141# Install python on all the 3 servers since python is required for
142for i in $hosts
143do
144 echo -e "${lBlue}Installing ${lCyan}Python${lBlue}${NC}"
145 scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i -r python-deb voltha@$i:.
146 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i "sudo dpkg -i /home/voltha/python-deb/*minimal*"
147 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i sudo dpkg -i -R /home/voltha/python-deb
148 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i rm -fr python-deb
149
150done
Sergio Slobodrian9d9c8442017-07-25 07:55:42 -0400151
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500152if [ "$cluster_framework" == "kubernetes" ]; then
Sergio Slobodrian9d9c8442017-07-25 07:55:42 -0400153
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400154 echo -e "${green}Deploying kubernetes${NC}"
Sergio Slobodrian9d9c8442017-07-25 07:55:42 -0400155
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400156 # Remove previously created inventory if it exists
Stephane Barbarie78d9fa62018-04-19 14:11:05 -0400157 cp -rfp kubespray/inventory/sample kubespray/inventory/voltha
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400158
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400159 # Adjust kubespray configuration
Sergio Slobodrian37f4a0e2017-06-14 07:50:01 -0400160
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400161 # Destination OS
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500162 sed -i -e "/bootstrap_os: none/s/.*/bootstrap_os: ubuntu/" \
163 kubespray/inventory/voltha/group_vars/all.yml
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400164
165 # Subnet used for deployed k8s services
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500166 sed -i -e "/kube_service_addresses: 10.233.0.0\/18/s/.*/kube_service_addresses: $cluster_service_subnet/" \
167 kubespray/inventory/voltha/group_vars/k8s-cluster.yml
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400168
169 # Subnet used for deployed k8s pods
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500170 sed -i -e "/kube_pods_subnet: 10.233.64.0\/18/s/.*/kube_pods_subnet: $cluster_pod_subnet/" \
171 kubespray/inventory/voltha/group_vars/k8s-cluster.yml
Sergio Slobodrian37f4a0e2017-06-14 07:50:01 -0400172
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400173 # Prevent any downloads from kubespray
174 sed -i -e "s/skip_downloads: false/skip_downloads: true/" \
175 kubespray/cluster.yml
176 sed -i -e "s/- { role: docker, tags: docker }/#&/" \
177 kubespray/cluster.yml
178 sed -i -e "s/skip_downloads: false/skip_downloads: true/" \
179 kubespray/roles/download/defaults/main.yml
180 sed -i -e "s/when: ansible_os_family == \"Debian\"/& and skip_downloads == \"false\" /" \
181 kubespray/roles/kubernetes/preinstall/tasks/main.yml
182 sed -i -e "s/or is_atomic)/& and skip_downloads == \"false\" /" \
183 kubespray/roles/kubernetes/preinstall/tasks/main.yml
184
Stephane Barbarie78d9fa62018-04-19 14:11:05 -0400185 # Configure failover parameters
186 sed -i -e "s/kube_controller_node_monitor_grace_period: .*/kube_controller_node_monitor_grace_period: 20s/" \
187 kubespray/roles/kubernetes/master/defaults/main.yml
188 sed -i -e "s/kube_controller_pod_eviction_timeout: .*/kube_controller_pod_eviction_timeout: 30s/" \
189 kubespray/roles/kubernetes/master/defaults/main.yml
190
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400191 # Construct node inventory
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500192 CONFIG_FILE=kubespray/inventory/voltha/hosts.ini python3 \
193 kubespray/contrib/inventory_builder/inventory.py $hosts
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400194
Stephane Barbarie78d9fa62018-04-19 14:11:05 -0400195 # The inventory builder configures 2 masters.
196 # Due to non-stable behaviours, force the use of a single master
197 cat kubespray/inventory/voltha/hosts.ini \
198 | sed -e ':begin;$!N;s/\(\[kube-master\]\)\n/\1/;tbegin;P;D' \
199 | sed -e '/\[kube-master\].*/,/\[kube-node\]/{//!d}' \
200 | sed -e 's/\(\[kube-master\]\)\(.*\)/\1\n\2\n/' \
201 > kubespray/inventory/voltha/hosts.ini.tmp
202
203 mv kubespray/inventory/voltha/hosts.ini.tmp kubespray/inventory/voltha/hosts.ini
204
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400205 ordered_nodes=`CONFIG_FILE=kubespray/inventory/voltha/hosts.ini python3 \
206 kubespray/contrib/inventory_builder/inventory.py print_ips`
207
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500208 echo "[k8s-master]" > ansible/hosts/k8s-master
209
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400210 mkdir -p kubespray/inventory/voltha/host_vars
211
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500212 ctr=1
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400213 for i in $ordered_nodes
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500214 do
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400215 echo -e "${lBlue}Adding SSH keys to kubespray ansible${NC}"
216 echo "ansible_ssh_private_key_file: $wd/.keys/$i" > kubespray/inventory/voltha/host_vars/node$ctr
217
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400218 if [ $ctr -eq 1 ]; then
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500219 echo $i >> ansible/hosts/k8s-master
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400220 fi
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400221 ctr=$((ctr + 1))
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500222 done
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400223
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400224 # Prepare Voltha
225 # ... Prepares environment and copies all required container images
226 # ... including the ones needed by kubespray
227 cp ansible/ansible.cfg .ansible.cfg
228 ansible-playbook -v ansible/voltha-k8s.yml -i ansible/hosts/cluster -e 'config_voltha=true'
229
230 # Deploy kubernetes
231 ANSIBLE_CONFIG=kubespray/ansible.cfg ansible-playbook -v -b \
232 --become-method=sudo --become-user root -u voltha \
233 -i kubespray/inventory/voltha/hosts.ini kubespray/cluster.yml
234
Stephane Barbarie78d9fa62018-04-19 14:11:05 -0400235 # Now all 3 servers need to be rebooted because of software installs.
236 # Reboot them and wait patiently until they all come back.
237 # Note this destroys the registry tunnel wich is no longer needed.
238 hList=""
239 for i in $hosts
240 do
241 echo -e "${lBlue}Rebooting cluster hosts${NC}"
242 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i sudo telinit 6
243 hList="$i $hList"
244 done
245
246 # Give the hosts time to shut down so that pings stop working or the
247 # script just falls through the next loop and the rest fails.
248 echo -e "${lBlue}Waiting for shutdown${NC}"
249 sleep 5
250
251
252 while [ ! -z "$hList" ];
253 do
254 # Attempt to ping the VMs on the list one by one.
255 echo -e "${lBlue}Waiting for hosts to reboot ${yellow}$hList${NC}"
256 for i in $hList
257 do
258 ping -q -c 1 $i > /dev/null 2>&1
259 ret=$?
260 if [ $ret -eq 0 ]; then
261 ipExpr=`echo $i | sed -e "s/\./[.]/g"`
262 hList=`echo $hList | sed -e "s/$ipExpr//" | sed -e "s/^ //" | sed -e "s/ $//"`
263 fi
264 done
265
266 done
267
268 # Wait for kubernetes to settle after reboot
269 k8sIsUp="no"
270 while [ "$k8sIsUp" == "no" ];
271 do
272 # Attempt to ping the VMs on the list one by one.
273 echo -e "${lBlue}Waiting for kubernetes to settle${NC}"
274 for i in $hosts
275 do
276 nc -vz $i 6443 > /dev/null 2>&1
277 ret=$?
278 if [ $ret -eq 0 ]; then
279 k8sIsUp="yes"
280 break
281 fi
282 sleep 1
283 done
284 done
285 echo -e "${lBlue}Kubernetes is up and running${NC}"
286
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400287 # Deploy Voltha
288 ansible-playbook -v ansible/voltha-k8s.yml -i ansible/hosts/k8s-master -e 'deploy_voltha=true'
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500289
290else
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500291 # Legacy swarm instructions
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500292
293 # Create the daemon.json file for the swarm
294 echo "{" > daemon.json
295 echo -n ' "insecure-registries" : [' >> daemon.json
296 first=""
297 for i in .keys/*
298 do
299 if [ -z "$first" ]; then
300 echo -n '"'`basename $i`':5001"' >> daemon.json
301 first="not"
302 else
303 echo -n ' , "'`basename $i`':5001"' >> daemon.json
304 fi
305 done
306 echo "]" >> daemon.json
307 echo "}" >> daemon.json
308 unset first
309
310 # Running ansible
311 echo -e "${lBlue}Running ansible${NC}"
312 cp ansible/ansible.cfg .ansible.cfg
313 ansible-playbook ansible/voltha.yml -i ansible/hosts/cluster
314
315 # Now all 3 servers need to be rebooted because of software installs.
316 # Reboot them and wait patiently until they all come back.
317 # Note this destroys the registry tunnel wich is no longer needed.
318 hList=""
319 for i in $hosts
320 do
321 echo -e "${lBlue}Rebooting cluster hosts${NC}"
322 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i sudo telinit 6
323 hList="$i $hList"
324 done
325
326 # Give the hosts time to shut down so that pings stop working or the
327 # script just falls through the next loop and the rest fails.
328 echo -e "${lBlue}Waiting for shutdown${NC}"
329 sleep 5
330
331
332 while [ ! -z "$hList" ];
333 do
334 # Attempt to ping the VMs on the list one by one.
335 echo -e "${lBlue}Waiting for hosts to reboot ${yellow}$hList${NC}"
336 for i in $hList
337 do
338 ping -q -c 1 $i > /dev/null 2>&1
339 ret=$?
340 if [ $ret -eq 0 ]; then
341 ipExpr=`echo $i | sed -e "s/\./[.]/g"`
342 hList=`echo $hList | sed -e "s/$ipExpr//" | sed -e "s/^ //" | sed -e "s/ $//"`
343 fi
344 done
345
346 done
347
348 # Now initialize the the docker swarm cluster with managers.
349 # The first server needs to be the primary swarm manager
350 # the other nodes are backup mangers that join the swarm.
351 # In the future, worker nodes will likely be added.
352
353 echo "[swarm-master]" > ansible/hosts/swarm-master
354 echo "[swarm-master-backup]" > ansible/hosts/swarm-master-backup
355
356 ctr=1
357 for i in $hosts
358 do
359 if [ $ctr -eq 1 ]; then
360 echo $i >> ansible/hosts/swarm-master
361 echo "swarm_master_addr: \"$i\"" >> ansible/group_vars/all
362 ctr=0
363 else
364 echo $i >> ansible/hosts/swarm-master-backup
365 fi
366 done
367 ansible-playbook ansible/swarm.yml -i ansible/hosts/swarm-master
368 ansible-playbook ansible/swarm.yml -i ansible/hosts/swarm-master-backup
369 ansible-playbook ansible/voltha.yml -i ansible/hosts/swarm-master
370
371fi