Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | baseImage="Ubuntu1604LTS" |
| 4 | iVmName="Ubuntu1604LTS-1" |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 5 | shutdownTimeout=5 |
| 6 | ipTimeout=10 |
| 7 | |
| 8 | lBlue='\033[1;34m' |
| 9 | green='\033[0;32m' |
| 10 | orange='\033[0;33m' |
| 11 | NC='\033[0m' |
| 12 | red='\033[0;31m' |
| 13 | yellow='\033[1;33m' |
| 14 | dGrey='\033[1;30m' |
| 15 | lGrey='\033[1;37m' |
| 16 | lCyan='\033[1;36m' |
| 17 | wd=`pwd` |
| 18 | |
| 19 | |
| 20 | # Clean up any prior executions |
| 21 | rm -fr .keys |
| 22 | rm -f ansible/hosts/cluster |
| 23 | rm -f ansible/host_vars/* |
| 24 | |
| 25 | # Source the configuration information |
| 26 | . install.cfg |
| 27 | |
| 28 | # Create the key directory |
| 29 | mkdir .keys |
| 30 | |
| 31 | # Create the host list |
| 32 | echo "[cluster]" > ansible/hosts/cluster |
| 33 | |
| 34 | # Silence SSH and avoid prompts |
| 35 | rm -f ~/.ssh/config |
| 36 | echo "Host *" > ~/.ssh/config |
| 37 | echo " StrictHostKeyChecking no" >> ~/.ssh/config |
| 38 | echo " UserKnownHostsFile /dev/null" >> ~/.ssh/config |
| 39 | |
| 40 | sudo cp ~/.ssh/config /root/.ssh/config |
| 41 | |
| 42 | |
| 43 | for i in $hosts |
| 44 | do |
| 45 | # Generate the key for the host |
| 46 | echo -e "${lBlue}Generating the key-pair for communication with host ${yellow}$i${NC}" |
| 47 | ssh-keygen -f ./$i -t rsa -N '' |
| 48 | mv $i .keys |
| 49 | |
| 50 | # Generate the pre-configuration script |
| 51 | echo -e "${lBlue}Creating the pre-configuration script${NC}" |
| 52 | cat <<HERE > bash_login.sh |
| 53 | #!/bin/bash |
| 54 | echo "voltha ALL=(ALL) NOPASSWD:ALL" > tmp |
| 55 | sudo chown root.root tmp |
| 56 | sudo mv tmp /etc/sudoers.d/voltha |
| 57 | sudo mkdir /home/voltha |
| 58 | mkdir voltha_ssh |
| 59 | ssh-keygen -f ~/voltha_ssh/id_rsa -t rsa -N '' |
| 60 | sudo mv voltha_ssh /home/voltha/.ssh |
| 61 | HERE |
| 62 | echo "sudo cat <<HERE > /home/voltha/.ssh/authorized_keys" >> bash_login.sh |
| 63 | cat $i.pub >> bash_login.sh |
| 64 | echo "HERE" >> bash_login.sh |
| 65 | echo "chmod 400 /home/voltha/.ssh/authorized_keys" >> bash_login.sh |
| 66 | echo "sudo useradd -b /home -d /home/voltha voltha -s /bin/bash" >> bash_login.sh |
| 67 | echo "sudo chown -R voltha.voltha /home/voltha" >> bash_login.sh |
| 68 | echo "echo 'voltha:voltha' | sudo chpasswd" >> bash_login.sh |
| 69 | echo "rm .bash_login" >> bash_login.sh |
| 70 | echo "logout" >> bash_login.sh |
| 71 | rm $i.pub |
| 72 | # Copy the pre-config file to the VM |
| 73 | echo -e "${lBlue}Transfering pre-configuration script to ${yellow}$i${NC}" |
| 74 | if [ -d ".test" ]; then |
| 75 | 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] | 76 | 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] | 77 | else |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 78 | 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] | 79 | fi |
| 80 | rm bash_login.sh |
| 81 | |
| 82 | # Run the pre-config file on the VM |
| 83 | echo -e "${lBlue}Running the pre-configuration script on ${yellow}$i${NC}" |
| 84 | if [ -d ".test" ]; then |
| 85 | 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] | 86 | 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] | 87 | else |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 88 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $iUser@$i |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 89 | fi |
| 90 | |
| 91 | # Configure ansible and ssh for silent operation |
| 92 | echo -e "${lBlue}Configuring ansible${NC}" |
| 93 | echo $i >> ansible/hosts/cluster |
| 94 | echo "ansible_ssh_private_key_file: $wd/.keys/$i" > ansible/host_vars/$i |
| 95 | |
| 96 | # Create the tunnel to the registry to allow pulls from localhost |
| 97 | echo -e "${lBlue}Creating a secure shell tunnel to the registry for ${yellow}$i${NC}" |
| 98 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i -f voltha@$i -R 5000:localhost:5000 -N |
| 99 | |
| 100 | done |
| 101 | # Add the dependent software list to the cluster variables |
| 102 | echo -e "${lBlue}Setting up dependent software${NC}" |
| 103 | echo "deb_files:" >> ansible/group_vars/all |
| 104 | for i in deb_files/*.deb |
| 105 | do |
| 106 | echo " - `basename $i`" >> ansible/group_vars/all |
| 107 | done |
| 108 | |
| 109 | # Running ansible |
| 110 | echo -e "${lBlue}Running ansible${NC}" |
| 111 | cp ansible/ansible.cfg .ansible.cfg |
| 112 | sudo ansible-playbook ansible/voltha.yml -i ansible/hosts/cluster |
| 113 | |