blob: f3a5aa35c0a33f1f2893ea6f4e38b2666aa8adc6 [file] [log] [blame]
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -04001#!/bin/bash
2
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -04003lBlue='\033[1;34m'
4green='\033[0;32m'
5orange='\033[0;33m'
6NC='\033[0m'
7red='\033[0;31m'
8yellow='\033[1;33m'
9dGrey='\033[1;30m'
10lGrey='\033[1;37m'
11lCyan='\033[1;36m'
12wd=`pwd`
13
14
15# Clean up any prior executions
16rm -fr .keys
17rm -f ansible/hosts/cluster
18rm -f ansible/host_vars/*
19
20# Source the configuration information
21. install.cfg
22
Sergio Slobodrian37f4a0e2017-06-14 07:50:01 -040023if [ -z "$hosts" ]; then
24 echo -e "${red}No hosts specifed!!${NC}"
25 echo -e "${red}Did you forget to update the config file ${yellow}installer.cfg${red}?${NC}"
26 exit
27fi
28
Sergio Slobodrianb92e5132017-09-13 13:04:05 -040029if [ "$iUser" == "voltha" ]; then
30 echo -e "${yellow}voltha ${red}can't be used as be install user!!!${NC}"
31 echo -e "${red}Please delete the ${yellow}voltha ${red}user on the targets and create a different installation user${NC}"
32 exit
33fi
34
Sergio Slobodrian8725ea82017-08-27 23:47:41 -040035# Configure barrier file sizes but only if a value was provided in the config file
36
37if [ -v logLimit ]; then
Stephane Barbariebcea2f42018-03-02 18:46:32 -050038 sed -i -e "/logger_volume_size/s/.*/logger_volume_size: ${logLimit}/" ansible/group_vars/all
Sergio Slobodrian8725ea82017-08-27 23:47:41 -040039fi
40if [ -v regLimit ]; then
Stephane Barbariebcea2f42018-03-02 18:46:32 -050041 sed -i -e "/registry_volume_size/s/.*/registry_volume_size: ${regLimit}/" ansible/group_vars/all
Sergio Slobodrian8725ea82017-08-27 23:47:41 -040042fi
43if [ -v consulLimit ]; then
Stephane Barbariebcea2f42018-03-02 18:46:32 -050044 sed -i -e "/consul_volume_size/s/.*/consul_volume_size: ${consulLimit}/" ansible/group_vars/all
Sergio Slobodrian8725ea82017-08-27 23:47:41 -040045fi
46
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040047# Create the key directory
48mkdir .keys
49
50# Create the host list
51echo "[cluster]" > ansible/hosts/cluster
52
53# Silence SSH and avoid prompts
54rm -f ~/.ssh/config
55echo "Host *" > ~/.ssh/config
56echo " StrictHostKeyChecking no" >> ~/.ssh/config
57echo " UserKnownHostsFile /dev/null" >> ~/.ssh/config
58
59sudo cp ~/.ssh/config /root/.ssh/config
60
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040061for i in $hosts
62do
Stephane Barbariebcea2f42018-03-02 18:46:32 -050063 # Generate the key for the host
64 echo -e "${lBlue}Generating the key-pair for communication with host ${yellow}$i${NC}"
65 ssh-keygen -f ./$i -t rsa -N ''
66 mv $i .keys
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040067
Stephane Barbariebcea2f42018-03-02 18:46:32 -050068 # Generate the pre-configuration script
69 echo -e "${lBlue}Creating the pre-configuration script${NC}"
70 head -n +1 BashLoginTarget.sh > bash_login.sh
71 echo "" >> bash_login.sh
72 echo -n 'key="' >> bash_login.sh
73 sed -i -e 's/$/"/' $i.pub
74 cat $i.pub >> bash_login.sh
75 tail -n +2 BashLoginTarget.sh | grep -v "{{ key }}" >> bash_login.sh
76 rm $i.pub
Sergio Slobodrianb92e5132017-09-13 13:04:05 -040077
Stephane Barbariebcea2f42018-03-02 18:46:32 -050078 # Copy the pre-config file to the VM
79 echo -e "${lBlue}Transfering pre-configuration script to ${yellow}$i${NC}"
80 if [ -d ".test" ]; then
81 echo -e "${red}Test mode set!!${lBlue} Using pre-populated ssh key for ${yellow}$i${NC}"
82 scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .test/$i bash_login.sh $iUser@$i:.bash_login
83 else
84 scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bash_login.sh $iUser@$i:.bash_login
85 fi
86 rm bash_login.sh
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040087
Stephane Barbariebcea2f42018-03-02 18:46:32 -050088 # Run the pre-config file on the VM
89 echo -e "${lBlue}Running the pre-configuration script on ${yellow}$i${NC}"
90 if [ -d ".test" ]; then
91 echo -e "${red}Test mode set!!${lBlue} Using pre-populated ssh key for ${yellow}$i${NC}"
92 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .test/$i $iUser@$i
93 else
94 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $iUser@$i
95 fi
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040096
Stephane Barbariebcea2f42018-03-02 18:46:32 -050097 # Configure ansible and ssh for silent operation
98 echo -e "${lBlue}Configuring ansible${NC}"
99 echo $i >> ansible/hosts/cluster
100 echo "ansible_ssh_private_key_file: $wd/.keys/$i" > ansible/host_vars/$i
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400101
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500102 # Create the tunnel to the registry to allow pulls from localhost
103 echo -e "${lBlue}Creating a secure shell tunnel to the registry for ${yellow}$i${NC}"
104 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i -f voltha@$i -R 5000:localhost:5000 -N
105
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400106done
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400107
Sergio Slobodrian37f4a0e2017-06-14 07:50:01 -0400108cp -r .keys ansible/roles/cluster-host/files
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400109
Sergio Slobodrian9d9c8442017-07-25 07:55:42 -0400110
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500111if [ "$cluster_framework" == "kubernetes" ]; then
Sergio Slobodrian9d9c8442017-07-25 07:55:42 -0400112
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500113 echo -e "${green}Deploying kubernetes${NC}"
Sergio Slobodrian9d9c8442017-07-25 07:55:42 -0400114
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500115 rm -rf kubespray
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400116
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500117 git clone https://github.com/kubernetes-incubator/kubespray.git
Sergio Slobodrian37f4a0e2017-06-14 07:50:01 -0400118
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500119 cp -rfp kubespray/inventory/sample kubespray/inventory/voltha
Sergio Slobodrian37f4a0e2017-06-14 07:50:01 -0400120
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500121 # TODO: Replace with variables taken from config file
122 # bootstrap_os: ubuntu
123 # network_plugin: weave
Sergio Slobodrian37f4a0e2017-06-14 07:50:01 -0400124
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500125 sed -i -e "/bootstrap_os: none/s/.*/bootstrap_os: ubuntu/" \
126 kubespray/inventory/voltha/group_vars/all.yml
127 sed -i -e "/kube_network_plugin: calico/s/.*/kube_network_plugin: weave/" \
128 kubespray/inventory/voltha/group_vars/k8s-cluster.yml
129 sed -i -e "/kube_service_addresses: 10.233.0.0\/18/s/.*/kube_service_addresses: $cluster_service_subnet/" \
130 kubespray/inventory/voltha/group_vars/k8s-cluster.yml
131 sed -i -e "/kube_pods_subnet: 10.233.64.0\/18/s/.*/kube_pods_subnet: $cluster_pod_subnet/" \
132 kubespray/inventory/voltha/group_vars/k8s-cluster.yml
133 sed -i -e "s/docker_options: \"/&--insecure-registry=$cluster_registry /" \
134 kubespray/inventory/voltha/group_vars/k8s-cluster.yml
Sergio Slobodrian37f4a0e2017-06-14 07:50:01 -0400135
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500136 CONFIG_FILE=kubespray/inventory/voltha/hosts.ini python3 \
137 kubespray/contrib/inventory_builder/inventory.py $hosts
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400138
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500139 # The inventory defines
140 sed -i -e '/\[kube-master\]/a\
141 node3
142 ' kubespray/inventory/voltha/hosts.ini
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400143
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500144 ansible-playbook -u root -i kubespray/inventory/voltha/hosts.ini kubespray/cluster.yml
145
146 echo "[k8s-master]" > ansible/hosts/k8s-master
147
148 ctr=1
149 for i in $hosts
150 do
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400151 if [ $ctr -eq 1 ]; then
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500152 echo $i >> ansible/hosts/k8s-master
153 ctr=0
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400154 fi
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500155 done
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400156
Stephane Barbariebcea2f42018-03-02 18:46:32 -0500157 ansible-playbook ansible/voltha-k8s.yml -i ansible/hosts/cluster -e 'config_voltha=true'
158 ansible-playbook ansible/voltha-k8s.yml -i ansible/hosts/k8s-master -e 'deploy_voltha=true'
159
160else
161 #
162 # Legacy swarm instructions
163 #
164
165 # Add the dependent software list to the cluster variables
166 echo -e "${lBlue}Setting up dependent software${NC}"
167 # Delete any grub updates since the boot disk is almost
168 # guaranteed not to be the same device as the installer.
169 mkdir grub_updates
170 sudo mv deb_files/*grub* grub_updates
171 # Sort the packages in dependency order to get rid of scary non-errors
172 # that are issued by ansible.
173 #echo -e "${lBlue}Dependency sorting dependent software${NC}"
174 #./sort_packages.sh
175 #echo "deb_files:" >> ansible/group_vars/all
176 #for i in `cat sortedDebs.txt`
177 #do
178 #echo " - $i" >> ansible/group_vars/all
179 #done
180
181 # Make sure the ssh keys propagate to all hosts allowing passwordless logins between them
182 echo -e "${lBlue}Propagating ssh keys${NC}"
183 cp -r .keys ansible/roles/cluster-host/files
184
185 # Install python on all the 3 servers since python is required for
186 for i in $hosts
187 do
188 echo -e "${lBlue}Installing ${lCyan}Python${lBlue}${NC}"
189 scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i -r python-deb voltha@$i:.
190 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i "sudo dpkg -i /home/voltha/python-deb/*minimal*"
191 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i sudo dpkg -i -R /home/voltha/python-deb
192 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i rm -fr python-deb
193
194 done
195
196 # Create the daemon.json file for the swarm
197 echo "{" > daemon.json
198 echo -n ' "insecure-registries" : [' >> daemon.json
199 first=""
200 for i in .keys/*
201 do
202 if [ -z "$first" ]; then
203 echo -n '"'`basename $i`':5001"' >> daemon.json
204 first="not"
205 else
206 echo -n ' , "'`basename $i`':5001"' >> daemon.json
207 fi
208 done
209 echo "]" >> daemon.json
210 echo "}" >> daemon.json
211 unset first
212
213 # Running ansible
214 echo -e "${lBlue}Running ansible${NC}"
215 cp ansible/ansible.cfg .ansible.cfg
216 ansible-playbook ansible/voltha.yml -i ansible/hosts/cluster
217
218 # Now all 3 servers need to be rebooted because of software installs.
219 # Reboot them and wait patiently until they all come back.
220 # Note this destroys the registry tunnel wich is no longer needed.
221 hList=""
222 for i in $hosts
223 do
224 echo -e "${lBlue}Rebooting cluster hosts${NC}"
225 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i sudo telinit 6
226 hList="$i $hList"
227 done
228
229 # Give the hosts time to shut down so that pings stop working or the
230 # script just falls through the next loop and the rest fails.
231 echo -e "${lBlue}Waiting for shutdown${NC}"
232 sleep 5
233
234
235 while [ ! -z "$hList" ];
236 do
237 # Attempt to ping the VMs on the list one by one.
238 echo -e "${lBlue}Waiting for hosts to reboot ${yellow}$hList${NC}"
239 for i in $hList
240 do
241 ping -q -c 1 $i > /dev/null 2>&1
242 ret=$?
243 if [ $ret -eq 0 ]; then
244 ipExpr=`echo $i | sed -e "s/\./[.]/g"`
245 hList=`echo $hList | sed -e "s/$ipExpr//" | sed -e "s/^ //" | sed -e "s/ $//"`
246 fi
247 done
248
249 done
250
251 # Now initialize the the docker swarm cluster with managers.
252 # The first server needs to be the primary swarm manager
253 # the other nodes are backup mangers that join the swarm.
254 # In the future, worker nodes will likely be added.
255
256 echo "[swarm-master]" > ansible/hosts/swarm-master
257 echo "[swarm-master-backup]" > ansible/hosts/swarm-master-backup
258
259 ctr=1
260 for i in $hosts
261 do
262 if [ $ctr -eq 1 ]; then
263 echo $i >> ansible/hosts/swarm-master
264 echo "swarm_master_addr: \"$i\"" >> ansible/group_vars/all
265 ctr=0
266 else
267 echo $i >> ansible/hosts/swarm-master-backup
268 fi
269 done
270 ansible-playbook ansible/swarm.yml -i ansible/hosts/swarm-master
271 ansible-playbook ansible/swarm.yml -i ansible/hosts/swarm-master-backup
272 ansible-playbook ansible/voltha.yml -i ansible/hosts/swarm-master
273
274fi