Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 3 | lBlue='\033[1;34m' |
| 4 | green='\033[0;32m' |
| 5 | orange='\033[0;33m' |
| 6 | NC='\033[0m' |
| 7 | red='\033[0;31m' |
| 8 | yellow='\033[1;33m' |
| 9 | dGrey='\033[1;30m' |
| 10 | lGrey='\033[1;37m' |
| 11 | lCyan='\033[1;36m' |
| 12 | wd=`pwd` |
| 13 | |
| 14 | |
| 15 | # Clean up any prior executions |
| 16 | rm -fr .keys |
| 17 | rm -f ansible/hosts/cluster |
| 18 | rm -f ansible/host_vars/* |
| 19 | |
| 20 | # Source the configuration information |
| 21 | . install.cfg |
| 22 | |
| 23 | # Create the key directory |
| 24 | mkdir .keys |
| 25 | |
| 26 | # Create the host list |
| 27 | echo "[cluster]" > ansible/hosts/cluster |
| 28 | |
| 29 | # Silence SSH and avoid prompts |
| 30 | rm -f ~/.ssh/config |
| 31 | echo "Host *" > ~/.ssh/config |
| 32 | echo " StrictHostKeyChecking no" >> ~/.ssh/config |
| 33 | echo " UserKnownHostsFile /dev/null" >> ~/.ssh/config |
| 34 | |
| 35 | sudo cp ~/.ssh/config /root/.ssh/config |
| 36 | |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 37 | for i in $hosts |
| 38 | do |
| 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 |
| 55 | HERE |
| 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 Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 70 | scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .test/$i bash_login.sh $iUser@$i:.bash_login |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 71 | else |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 72 | scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bash_login.sh $iUser@$i:.bash_login |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 73 | 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 Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 80 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .test/$i $iUser@$i |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 81 | else |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 82 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $iUser@$i |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 83 | 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 | |
| 94 | done |
| 95 | # Add the dependent software list to the cluster variables |
| 96 | echo -e "${lBlue}Setting up dependent software${NC}" |
| 97 | echo "deb_files:" >> ansible/group_vars/all |
| 98 | for i in deb_files/*.deb |
| 99 | do |
| 100 | echo " - `basename $i`" >> ansible/group_vars/all |
| 101 | done |
| 102 | |
Sergio Slobodrian | d24189e | 2017-06-10 23:27:15 -0400 | [diff] [blame] | 103 | # Make sure the ssh keys propagate to all hosts allowing passwordless logins between them |
| 104 | echo -e "${lBlue}Propagating ssh keys${NC}" |
| 105 | cp -r .keys ansible/roles/cluster-host/files/.keys |
| 106 | |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 107 | # Running ansible |
| 108 | echo -e "${lBlue}Running ansible${NC}" |
| 109 | cp ansible/ansible.cfg .ansible.cfg |
| 110 | sudo ansible-playbook ansible/voltha.yml -i ansible/hosts/cluster |
| 111 | |
Sergio Slobodrian | d24189e | 2017-06-10 23:27:15 -0400 | [diff] [blame] | 112 | # 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 | |
| 117 | echo "[swarm-master]" > ansible/hosts/swarm-master |
| 118 | echo "[swarm-master-backup]" > ansible/hosts/swarm-master-backup |
| 119 | |
| 120 | ctr=1 |
| 121 | for i in $hosts |
| 122 | do |
| 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 |
| 130 | done |
| 131 | sudo ansible-playbook ansible/swarm-master.yml -i ansible/hosts/swarm-master |
| 132 | sudo ansible-playbook ansible/swarm-master-backup.yml -i ansible/hosts/swarm-master-backup |
| 133 | |