blob: 465f5d9994246b707924aced870f1a75dce3ec84 [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
23# Create the key directory
24mkdir .keys
25
26# Create the host list
27echo "[cluster]" > ansible/hosts/cluster
28
29# Silence SSH and avoid prompts
30rm -f ~/.ssh/config
31echo "Host *" > ~/.ssh/config
32echo " StrictHostKeyChecking no" >> ~/.ssh/config
33echo " UserKnownHostsFile /dev/null" >> ~/.ssh/config
34
35sudo cp ~/.ssh/config /root/.ssh/config
36
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040037for i in $hosts
38do
39 # Generate the key for the host
40 echo -e "${lBlue}Generating the key-pair for communication with host ${yellow}$i${NC}"
41 ssh-keygen -f ./$i -t rsa -N ''
42 mv $i .keys
43
44 # Generate the pre-configuration script
45 echo -e "${lBlue}Creating the pre-configuration script${NC}"
46 cat <<HERE > bash_login.sh
47#!/bin/bash
48 echo "voltha ALL=(ALL) NOPASSWD:ALL" > tmp
49 sudo chown root.root tmp
50 sudo mv tmp /etc/sudoers.d/voltha
51 sudo mkdir /home/voltha
52 mkdir voltha_ssh
53 ssh-keygen -f ~/voltha_ssh/id_rsa -t rsa -N ''
54 sudo mv voltha_ssh /home/voltha/.ssh
55HERE
56 echo "sudo cat <<HERE > /home/voltha/.ssh/authorized_keys" >> bash_login.sh
57 cat $i.pub >> bash_login.sh
58 echo "HERE" >> bash_login.sh
59 echo "chmod 400 /home/voltha/.ssh/authorized_keys" >> bash_login.sh
60 echo "sudo useradd -b /home -d /home/voltha voltha -s /bin/bash" >> bash_login.sh
61 echo "sudo chown -R voltha.voltha /home/voltha" >> bash_login.sh
62 echo "echo 'voltha:voltha' | sudo chpasswd" >> bash_login.sh
63 echo "rm .bash_login" >> bash_login.sh
64 echo "logout" >> bash_login.sh
65 rm $i.pub
66 # Copy the pre-config file to the VM
67 echo -e "${lBlue}Transfering pre-configuration script to ${yellow}$i${NC}"
68 if [ -d ".test" ]; then
69 echo -e "${red}Test mode set!!${lBlue} Using pre-populated ssh key for ${yellow}$i${NC}"
Sergio Slobodrianc5477712017-06-07 11:56:56 -040070 scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .test/$i bash_login.sh $iUser@$i:.bash_login
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040071 else
Sergio Slobodrianc5477712017-06-07 11:56:56 -040072 scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bash_login.sh $iUser@$i:.bash_login
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040073 fi
74 rm bash_login.sh
75
76 # Run the pre-config file on the VM
77 echo -e "${lBlue}Running the pre-configuration script on ${yellow}$i${NC}"
78 if [ -d ".test" ]; then
79 echo -e "${red}Test mode set!!${lBlue} Using pre-populated ssh key for ${yellow}$i${NC}"
Sergio Slobodrianc5477712017-06-07 11:56:56 -040080 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .test/$i $iUser@$i
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040081 else
Sergio Slobodrianc5477712017-06-07 11:56:56 -040082 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $iUser@$i
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040083 fi
84
85 # Configure ansible and ssh for silent operation
86 echo -e "${lBlue}Configuring ansible${NC}"
87 echo $i >> ansible/hosts/cluster
88 echo "ansible_ssh_private_key_file: $wd/.keys/$i" > ansible/host_vars/$i
89
90 # Create the tunnel to the registry to allow pulls from localhost
91 echo -e "${lBlue}Creating a secure shell tunnel to the registry for ${yellow}$i${NC}"
92 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i -f voltha@$i -R 5000:localhost:5000 -N
93
94done
95# Add the dependent software list to the cluster variables
96echo -e "${lBlue}Setting up dependent software${NC}"
97echo "deb_files:" >> ansible/group_vars/all
98for i in deb_files/*.deb
99do
100echo " - `basename $i`" >> ansible/group_vars/all
101done
102
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400103# Make sure the ssh keys propagate to all hosts allowing passwordless logins between them
104echo -e "${lBlue}Propagating ssh keys${NC}"
105cp -r .keys ansible/roles/cluster-host/files/.keys
106
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400107# Running ansible
108echo -e "${lBlue}Running ansible${NC}"
109cp ansible/ansible.cfg .ansible.cfg
110sudo ansible-playbook ansible/voltha.yml -i ansible/hosts/cluster
111
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400112# Now initialize the the docker swarm cluster with managers.
113# The first server needs to be the primary swarm manager
114# the other nodes are backup mangers that join the swarm.
115# In the future, worker nodes will likely be added.
116
117echo "[swarm-master]" > ansible/hosts/swarm-master
118echo "[swarm-master-backup]" > ansible/hosts/swarm-master-backup
119
120ctr=1
121for i in $hosts
122do
123 if [ $ctr -eq 1 ]; then
124 echo $i >> ansible/hosts/swarm-master
125 echo "swarm_master_addr: \"$i\"" >> ansible/group_vars/all
126 ctr=0
127 else
128 echo $i >> ansible/hosts/swarm-master-backup
129 fi
130done
131sudo ansible-playbook ansible/swarm-master.yml -i ansible/hosts/swarm-master
132sudo ansible-playbook ansible/swarm-master-backup.yml -i ansible/hosts/swarm-master-backup
133