blob: e8da9a89fae44f49b5163c50a1026bcba8915a67 [file] [log] [blame]
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -04001#!/bin/bash
2
3baseImage="Ubuntu1604LTS"
Sergio Slobodrianc5477712017-06-07 11:56:56 -04004iVmName="vInstaller"
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -04005iVmNetwork="vagrant-libvirt"
Sergio Slobodrianc5477712017-06-07 11:56:56 -04006installerArchive="installer.tar.bz2"
7installerDirectory="volthaInstaller"
8installerPart="installer.part"
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -04009shutdownTimeout=5
10ipTimeout=10
11
12lBlue='\033[1;34m'
13green='\033[0;32m'
14orange='\033[0;33m'
15NC='\033[0m'
16red='\033[0;31m'
17yellow='\033[1;33m'
18dGrey='\033[1;30m'
19lGrey='\033[1;37m'
20lCyan='\033[1;36m'
21
22wd=`pwd`
23
24# Validate that vagrant is installed.
25echo -e "${lBlue}Ensure that ${lCyan}vagrant${lBlue} is installed${NC}"
26vInst=`which vagrant`
27
28if [ -z "$vInst" ]; then
29 wget https://releases.hashicorp.com/vagrant/1.9.5/vagrant_1.9.5_x86_64.deb
30 sudo dpkg -i vagrant_1.8.5_x86_64.deb
31 rm vagrant_1.8.5_x86_64.deb
32fi
33unset vInst
34
35# Validate that ansible is installed
36echo -e "${lBlue}Ensure that ${lCyan}ansible${lBlue} is installed${NC}"
37aInst=`which ansible`
38
39if [ -z "$aInst" ]; then
Sergio Slobodrianc5477712017-06-07 11:56:56 -040040 sudo apt-get install -y software-properties-common
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040041 sudo apt-add-repository ppa:ansible/ansible
42 sudo apt-get update
Sergio Slobodrianc5477712017-06-07 11:56:56 -040043 sudo apt-get install -y ansible
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040044fi
45unset vInst
46
47# Ensure that the voltha VM is running so that images can be secured
48echo -e "${lBlue}Ensure that the ${lCyan}voltha VM${lBlue} is running${NC}"
49vVM=`virsh list | grep voltha_voltha`
50
51if [ -z "$vVM" ]; then
52 ./BuildVoltha.sh
53fi
54
55# Verify if this is intended to be a test environment, if so start 3 VMs
56# to emulate the production installation cluster.
57if [ $# -eq 1 -a "$1" == "test" ]; then
58 echo -e "${lBlue}Testing, create the ${lCyan}ha-serv${lBlue} VMs${NC}"
59 vagrant destroy ha-serv{1,2,3}
60 vagrant up ha-serv{1,2,3}
61 ./devSetHostList.sh
62else
63 rm -fr .test
Sergio Slobodrianc5477712017-06-07 11:56:56 -040064 # Clean out the install config file keeping only the commented lines
65 # which serve as documentation.
66 sed -i -e '/^#/!d' install.cfg
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040067fi
68
69# Shut down the domain in case it's running.
70echo -e "${lBlue}Shut down the ${lCyan}$iVmName${lBlue} VM if running${NC}"
71ctr=0
72vStat=`virsh list | grep $iVmName`
Sergio Slobodrianc5477712017-06-07 11:56:56 -040073virsh shutdown $iVmName
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040074while [ ! -z "$vStat" ];
75do
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040076 echo "Waiting for $iVmName to shut down"
77 sleep 2
78 vStat=`virsh list | grep $iVmName`
79 ctr=`expr $ctr + 1`
80 if [ $ctr -eq $shutdownTimeout ]; then
81 echo -e "${red}Tired of waiting, forcing the VM off${NC}"
82 virsh destroy $iVmName
83 vStat=`virsh list | grep $iVmName`
84 fi
85done
86
87
88# Delete the VM and ignore any errors should they occur
89echo -e "${lBlue}Undefining the ${lCyan}$iVmName${lBlue} domain${NC}"
90virsh undefine $iVmName
91
92# Remove the associated volume
93echo -e "${lBlue}Removing the ${lCyan}$iVmName.qcow2${lBlue} volume${NC}"
94virsh vol-delete "${iVmName}.qcow2" default
95
96# Clone the base vanilla ubuntu install
97echo -e "${lBlue}Cloning the ${lCyan}$baseImage.qcow2${lBlue} to ${lCyan}$iVmName.qcow2${NC}"
98virsh vol-clone "${baseImage}.qcow2" "${iVmName}.qcow2" default
99
100# Create the xml file and define the VM for virsh
101echo -e "${lBlue}Defining the ${lCyan}$iVmName${lBlue} virtual machine${NC}"
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400102cat vmTemplate.xml | sed -e "s/{{ VMName }}/$iVmName/g" | sed -e "s/{{ VMNetwork }}/$iVmNetwork/g" > tmp.xml
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400103
104virsh define tmp.xml
105
106rm tmp.xml
107
108# Start the VMm, if it's already running just ignore the error
109echo -e "${lBlue}Starting the ${lCyan}$iVmName${lBlue} virtual machine${NC}"
110virsh start $iVmName > /dev/null 2>&1
111
112# Generate a keypair for communicating with the VM
113echo -e "${lBlue}Generating the key-pair for communication with the VM${NC}"
114ssh-keygen -f ./key -t rsa -N ''
115
116mv key key.pem
117
118# Clone BashLogin.sh and add the public key to it for later use.
119echo -e "${lBlue}Creating the pre-configuration script${NC}"
120cp BashLogin.sh bash_login.sh
121echo "cat <<HERE > .ssh/authorized_keys" >> bash_login.sh
122cat key.pub >> bash_login.sh
123echo "HERE" >> bash_login.sh
124echo "chmod 400 .ssh/authorized_keys" >> bash_login.sh
125echo "rm .bash_login" >> bash_login.sh
126echo "logout" >> bash_login.sh
127rm key.pub
128
129
130
131# Get the VM's IP address
132ctr=0
133ipAddr=""
134while [ -z "$ipAddr" ];
135do
136 echo -e "${lBlue}Waiting for the VM's IP address${NC}"
137 ipAddr=`virsh domifaddr $iVmName | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'`
138 sleep 3
139 if [ $ctr -eq $ipTimeout ]; then
140 echo -e "${red}Tired of waiting, please adjust the ipTimeout if the VM is slow to start${NC}"
141 exit
142 fi
143 ctr=`expr $ctr + 1`
144done
145
146echo -e "${lBlue}The IP address is: ${lCyan}$ipAddr${NC}"
147
148# Copy the pre-config file to the VM
149echo -e "${lBlue}Transfering pre-configuration script to the VM${NC}"
150scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bash_login.sh vinstall@$ipAddr:.bash_login
151
152rm bash_login.sh
153
154# Run the pre-config file on the VM
155echo -e "${lBlue}Running the pre-configuration script on the VM${NC}"
156ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no vinstall@$ipAddr
157
158# Install python which is required for ansible
159echo -e "${lBlue}Installing python${NC}"
160ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get update
161ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get -y install python
162
163# Make sure the VM is up-to-date
164echo -e "${lBlue}Ensure that the VM is up-to-date${NC}"
165ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get update
166ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get -y upgrade
167
168
169
170# Copy the apt repository to the VM because it's way too slow using ansible
171#echo -e "${red}NOT COPYING${lBlue} the apt-repository to the VM, ${red}TESTING ONLY REMOVE FOR PRODUCTION${NC}"
172#echo -e "${lBlue}Copy the apt-repository to the VM${NC}"
173#scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem -r apt-mirror vinstall@$ipAddr:apt-mirror
174
175# Create the docker.cfg file in the ansible tree using the VMs IP address
176echo 'DOCKER_OPTS="$DOCKER_OPTS --insecure-registry '$ipAddr':5000 -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --registry-mirror=http://'$ipAddr':5001"' > ansible/roles/docker/templates/docker.cfg
177
178# Add the voltha vm's information to the ansible tree
179echo -e "${lBlue}Add the voltha vm and key to the ansible accessible hosts${NC}"
180vIpAddr=`virsh domifaddr voltha_voltha | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'`
181echo "[voltha]" > ansible/hosts/voltha
182echo $vIpAddr >> ansible/hosts/voltha
183echo "ansible_ssh_private_key_file: $wd/../.vagrant/machines/voltha/libvirt/private_key" > ansible/host_vars/$vIpAddr
184
185
186# Prepare to launch the ansible playbook to configure the installer VM
187echo -e "${lBlue}Prepare to launch the ansible playbook to configure the VM${NC}"
188echo "[installer]" > ansible/hosts/installer
189echo "$ipAddr" >> ansible/hosts/installer
190echo "ansible_ssh_private_key_file: $wd/key.pem" > ansible/host_vars/$ipAddr
191
192# Launch the ansible playbook
193echo -e "${lBlue}Launching the ansible playbook${NC}"
194ansible-playbook ansible/volthainstall.yml -i ansible/hosts/installer
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400195if [ $? -ne 0 ]; then
196 echo -e "${red}PLAYBOOK FAILED, Exiting${NC}"
197 exit
198fi
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400199ansible-playbook ansible/volthainstall.yml -i ansible/hosts/voltha
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400200if [ $? -ne 0 ]; then
201 echo -e "${red}PLAYBOOK FAILED, Exiting${NC}"
202 exit
203fi
204
205if [ $# -eq 1 -a "$1" == "test" ]; then
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400206 echo -e "${lBlue}Testing, the install image ${red}WILL NOT${lBlue} be built${NC}"
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400207else
208 echo -e "${lBlue}Building, the install image (this can take a while)${NC}"
209 # Create a temporary directory for all the installer files
210 mkdir tmp_installer
211 cp vmTemplate.xml tmp_installer
212 # Shut down the installer vm
213 ctr=0
214 vStat=`virsh list | grep $iVmName`
215 virsh shutdown $iVmName
216 while [ ! -z "$vStat" ];
217 do
218 echo "Waiting for $iVmName to shut down"
219 sleep 2
220 vStat=`virsh list | grep $iVmName`
221 ctr=`expr $ctr + 1`
222 if [ $ctr -eq $shutdownTimeout ]; then
223 echo -e "${red}Tired of waiting, forcing the VM off${NC}"
224 virsh destroy $iVmName
225 vStat=`virsh list | grep $iVmName`
226 fi
227 done
228 # Copy the install bootstrap script to the installer directory
229 cp BootstrapInstaller.sh tmp_installer
230 # Copy the private key to access the VM
231 cp key.pem tmp_installer
232 pushd tmp_installer > /dev/null 2>&1
233 # Copy the vm image to the installer directory
234 virsh vol-dumpxml $iVmName.qcow2 default | sed -e 's/<key.*key>//' | sed -e '/^[ ]*$/d' > ${iVmName}_volume.xml
235 virsh pool-create-as installer --type dir --target `pwd`
236 virsh vol-create-from installer ${iVmName}_volume.xml $iVmName.qcow2 --inputpool default
237 virsh pool-destroy installer
238 # The image is copied in as root. It needs to have ownership changed
239 # this will result in a password prompt.
240 sudo chown `whoami`.`whoami` $iVmName.qcow2
241 # Now create the installer tar file
242 tar cjf ../$installerArchive .
243 popd > /dev/null 2>&1
244 # Clean up
245 rm -fr tmp_installer
246 # Final location for the installer
247 rm -fr $installerDirectory
248 mkdir $installerDirectory
249 cp installVoltha.sh $installerDirectory
250 # Check the image size and determine if it needs to be split.
251 # To be safe, split the image into chunks smaller than 2G so that
252 # it will fit on a FAT32 volume.
253 fSize=`ls -l $installerArchive | awk '{print $5'}`
254 if [ $fSize -gt 2000000000 ]; then
255 echo -e "${lBlue}Installer file too large, breaking into parts${NC}"
256 # The file is too large, breaking it up into parts
257 sPos=0
258 fnn="00"
259 while dd if=$installerArchive of=${installerDirectory}/${installerPart}$fnn \
260 bs=1900MB count=1 skip=$sPos > /dev/null 2>&1
261 do
262 sPos=`expr $sPos + 1`
263 if [ ! -s ${installerDirectory}/${installerPart}$fnn ]; then
264 rm -f ${installerDirectory}/${installerPart}$fnn
265 break
266 fi
267 if [ $sPos -lt 10 ]; then
268 fnn="0$sPos"
269 else
270 fnn="$sPos"
271 fi
272 done
273 else
274 cp $installerArchive $installerDirectory
275 fi
276 # Clean up
277 rm $installerArchive
278 echo -e "${lBlue}The install image is built and can be found in ${yellow}$installerDirectory${NC}"
279 echo -e "${lBlue}Copy all the files in ${yellow}$installerDirectory${lBlue} to the traasnport media${NC}"
280fi