blob: 8816db6b75d1407a1d9610ed0f41421237ec1ba6 [file] [log] [blame]
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -04001#!/bin/bash
2
Sergio Slobodrian7c483622017-06-13 15:51:34 -04003
Sergio Slobodrianc5477712017-06-07 11:56:56 -04004iVmName="vInstaller"
Sergio Slobodrian7c483622017-06-13 15:51:34 -04005baseImage="Ubuntu1604LTS"
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -04006iVmNetwork="vagrant-libvirt"
Sergio Slobodrianc5477712017-06-07 11:56:56 -04007installerArchive="installer.tar.bz2"
8installerDirectory="volthaInstaller"
9installerPart="installer.part"
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040010shutdownTimeout=5
11ipTimeout=10
12
13lBlue='\033[1;34m'
14green='\033[0;32m'
15orange='\033[0;33m'
16NC='\033[0m'
17red='\033[0;31m'
18yellow='\033[1;33m'
19dGrey='\033[1;30m'
20lGrey='\033[1;37m'
21lCyan='\033[1;36m'
22
Sergio Slobodrian7c483622017-06-13 15:51:34 -040023uId=`id -u`
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040024wd=`pwd`
25
26# Validate that vagrant is installed.
27echo -e "${lBlue}Ensure that ${lCyan}vagrant${lBlue} is installed${NC}"
28vInst=`which vagrant`
29
30if [ -z "$vInst" ]; then
31 wget https://releases.hashicorp.com/vagrant/1.9.5/vagrant_1.9.5_x86_64.deb
32 sudo dpkg -i vagrant_1.8.5_x86_64.deb
33 rm vagrant_1.8.5_x86_64.deb
34fi
35unset vInst
36
37# Validate that ansible is installed
38echo -e "${lBlue}Ensure that ${lCyan}ansible${lBlue} is installed${NC}"
39aInst=`which ansible`
40
41if [ -z "$aInst" ]; then
Sergio Slobodrianc5477712017-06-07 11:56:56 -040042 sudo apt-get install -y software-properties-common
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040043 sudo apt-add-repository ppa:ansible/ansible
44 sudo apt-get update
Sergio Slobodrianc5477712017-06-07 11:56:56 -040045 sudo apt-get install -y ansible
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040046fi
47unset vInst
48
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040049# Verify if this is intended to be a test environment, if so start 3 VMs
50# to emulate the production installation cluster.
51if [ $# -eq 1 -a "$1" == "test" ]; then
52 echo -e "${lBlue}Testing, create the ${lCyan}ha-serv${lBlue} VMs${NC}"
Sergio Slobodrian7c483622017-06-13 15:51:34 -040053 # Update the vagrant settings file
54 sed -i -e '/server_name/s/.*/server_name: "ha-serv'${uId}'-"/' settings.vagrant.yaml
55 sed -i -e '/docker_push_registry/s/.*/docker_push_registry: "vinstall'${uId}':5000"/' ansible/group_vars/all
56 sed -i -e "/vinstall/s/vinstall/vinstall${uId}/" ../ansible/roles/docker/templates/daemon.json
57
58 # Set the insecure registry configuration based on the installer hostname
59 echo -e "${lBlue}Set up the inescure registry hostname ${lCyan}vinstall${uId}${NC}"
60 echo '{' > ansible/roles/voltha/templates/daemon.json
61 echo '"insecure-registries" : ["vinstall'${uId}':5000"]' >> ansible/roles/voltha/templates/daemon.json
62 echo '}' >> ansible/roles/voltha/templates/daemon.json
63
64 vagrant destroy ha-serv${uId}-{1,2,3}
65 vagrant up ha-serv${uId}-{1,2,3}
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040066 ./devSetHostList.sh
Sergio Slobodrian7c483622017-06-13 15:51:34 -040067 # Change the installer name
68 iVmName="vInstaller${uId}"
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040069else
70 rm -fr .test
Sergio Slobodrianc5477712017-06-07 11:56:56 -040071 # Clean out the install config file keeping only the commented lines
72 # which serve as documentation.
73 sed -i -e '/^#/!d' install.cfg
Sergio Slobodrian7c483622017-06-13 15:51:34 -040074 # Set the insecure registry configuration based on the installer hostname
75 echo -e "${lBlue}Set up the inescure registry hostname ${lCyan}vinstall${uId}${NC}"
76 echo '{' > ansible/roles/voltha/templates/daemon.json
77 echo '"insecure-registries" : ["vinstall:5000"]' >> ansible/roles/voltha/templates/daemon.json
78 echo '}' >> ansible/roles/voltha/templates/daemon.json
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040079fi
80
Sergio Slobodrian7c483622017-06-13 15:51:34 -040081
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040082# Shut down the domain in case it's running.
83echo -e "${lBlue}Shut down the ${lCyan}$iVmName${lBlue} VM if running${NC}"
84ctr=0
85vStat=`virsh list | grep $iVmName`
Sergio Slobodrianc5477712017-06-07 11:56:56 -040086virsh shutdown $iVmName
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040087while [ ! -z "$vStat" ];
88do
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040089 echo "Waiting for $iVmName to shut down"
90 sleep 2
91 vStat=`virsh list | grep $iVmName`
92 ctr=`expr $ctr + 1`
93 if [ $ctr -eq $shutdownTimeout ]; then
94 echo -e "${red}Tired of waiting, forcing the VM off${NC}"
95 virsh destroy $iVmName
96 vStat=`virsh list | grep $iVmName`
97 fi
98done
99
100
101# Delete the VM and ignore any errors should they occur
102echo -e "${lBlue}Undefining the ${lCyan}$iVmName${lBlue} domain${NC}"
103virsh undefine $iVmName
104
105# Remove the associated volume
106echo -e "${lBlue}Removing the ${lCyan}$iVmName.qcow2${lBlue} volume${NC}"
107virsh vol-delete "${iVmName}.qcow2" default
108
109# Clone the base vanilla ubuntu install
110echo -e "${lBlue}Cloning the ${lCyan}$baseImage.qcow2${lBlue} to ${lCyan}$iVmName.qcow2${NC}"
111virsh vol-clone "${baseImage}.qcow2" "${iVmName}.qcow2" default
112
113# Create the xml file and define the VM for virsh
114echo -e "${lBlue}Defining the ${lCyan}$iVmName${lBlue} virtual machine${NC}"
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400115cat vmTemplate.xml | sed -e "s/{{ VMName }}/$iVmName/g" | sed -e "s/{{ VMNetwork }}/$iVmNetwork/g" > tmp.xml
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400116
117virsh define tmp.xml
118
119rm tmp.xml
120
121# Start the VMm, if it's already running just ignore the error
122echo -e "${lBlue}Starting the ${lCyan}$iVmName${lBlue} virtual machine${NC}"
123virsh start $iVmName > /dev/null 2>&1
124
125# Generate a keypair for communicating with the VM
126echo -e "${lBlue}Generating the key-pair for communication with the VM${NC}"
127ssh-keygen -f ./key -t rsa -N ''
128
129mv key key.pem
130
131# Clone BashLogin.sh and add the public key to it for later use.
132echo -e "${lBlue}Creating the pre-configuration script${NC}"
133cp BashLogin.sh bash_login.sh
134echo "cat <<HERE > .ssh/authorized_keys" >> bash_login.sh
135cat key.pub >> bash_login.sh
136echo "HERE" >> bash_login.sh
137echo "chmod 400 .ssh/authorized_keys" >> bash_login.sh
138echo "rm .bash_login" >> bash_login.sh
139echo "logout" >> bash_login.sh
140rm key.pub
141
142
143
144# Get the VM's IP address
145ctr=0
146ipAddr=""
147while [ -z "$ipAddr" ];
148do
149 echo -e "${lBlue}Waiting for the VM's IP address${NC}"
150 ipAddr=`virsh domifaddr $iVmName | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'`
151 sleep 3
152 if [ $ctr -eq $ipTimeout ]; then
153 echo -e "${red}Tired of waiting, please adjust the ipTimeout if the VM is slow to start${NC}"
154 exit
155 fi
156 ctr=`expr $ctr + 1`
157done
158
159echo -e "${lBlue}The IP address is: ${lCyan}$ipAddr${NC}"
160
161# Copy the pre-config file to the VM
162echo -e "${lBlue}Transfering pre-configuration script to the VM${NC}"
163scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bash_login.sh vinstall@$ipAddr:.bash_login
164
165rm bash_login.sh
166
167# Run the pre-config file on the VM
168echo -e "${lBlue}Running the pre-configuration script on the VM${NC}"
169ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no vinstall@$ipAddr
170
Sergio Slobodrian7c483622017-06-13 15:51:34 -0400171# If we're in test mode, change the hostname of the installer vm
172if [ $# -eq 1 -a "$1" == "test" ]; then
173 echo -e "${lBlue}Test mode, change the installer host name to ${yellow}vinstall${uId}${NC}"
174 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr \
175 sudo hostnamectl set-hostname vinstall${uId}
176 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr \
177 sudo service networking restart
178fi
179
Sergio Slobodrian36e16552017-06-19 11:00:45 -0400180# Ensure that the voltha VM is running so that images can be secured
181echo -e "${lBlue}Ensure that the ${lCyan}voltha VM${lBlue} is running${NC}"
182vVM=`virsh list | grep voltha_voltha${uId}`
183
184if [ -z "$vVM" ]; then
185 if [ $# -eq 1 -a "$1" == "test" ]; then
186 ./BuildVoltha.sh $1
187 else
188 # Default to installer mode
189 ./BuildVoltha.sh install
190 fi
191fi
192
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400193# Install python which is required for ansible
194echo -e "${lBlue}Installing python${NC}"
195ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get update
196ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get -y install python
197
198# Make sure the VM is up-to-date
199echo -e "${lBlue}Ensure that the VM is up-to-date${NC}"
200ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get update
201ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get -y upgrade
202
203
204
205# Copy the apt repository to the VM because it's way too slow using ansible
206#echo -e "${red}NOT COPYING${lBlue} the apt-repository to the VM, ${red}TESTING ONLY REMOVE FOR PRODUCTION${NC}"
207#echo -e "${lBlue}Copy the apt-repository to the VM${NC}"
208#scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem -r apt-mirror vinstall@$ipAddr:apt-mirror
209
210# Create the docker.cfg file in the ansible tree using the VMs IP address
211echo '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
212
213# Add the voltha vm's information to the ansible tree
214echo -e "${lBlue}Add the voltha vm and key to the ansible accessible hosts${NC}"
Sergio Slobodrian7c483622017-06-13 15:51:34 -0400215vIpAddr=`virsh domifaddr voltha_voltha${uId} | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'`
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400216echo "[voltha]" > ansible/hosts/voltha
217echo $vIpAddr >> ansible/hosts/voltha
Sergio Slobodrian7c483622017-06-13 15:51:34 -0400218echo "ansible_ssh_private_key_file: $wd/../.vagrant/machines/voltha${uId}/libvirt/private_key" > ansible/host_vars/$vIpAddr
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400219
220
221# Prepare to launch the ansible playbook to configure the installer VM
222echo -e "${lBlue}Prepare to launch the ansible playbook to configure the VM${NC}"
223echo "[installer]" > ansible/hosts/installer
224echo "$ipAddr" >> ansible/hosts/installer
225echo "ansible_ssh_private_key_file: $wd/key.pem" > ansible/host_vars/$ipAddr
226
227# Launch the ansible playbook
228echo -e "${lBlue}Launching the ansible playbook${NC}"
229ansible-playbook ansible/volthainstall.yml -i ansible/hosts/installer
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400230if [ $? -ne 0 ]; then
231 echo -e "${red}PLAYBOOK FAILED, Exiting${NC}"
232 exit
233fi
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400234ansible-playbook ansible/volthainstall.yml -i ansible/hosts/voltha
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400235if [ $? -ne 0 ]; then
236 echo -e "${red}PLAYBOOK FAILED, Exiting${NC}"
237 exit
238fi
239
240if [ $# -eq 1 -a "$1" == "test" ]; then
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400241 echo -e "${lBlue}Testing, the install image ${red}WILL NOT${lBlue} be built${NC}"
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400242else
243 echo -e "${lBlue}Building, the install image (this can take a while)${NC}"
244 # Create a temporary directory for all the installer files
245 mkdir tmp_installer
246 cp vmTemplate.xml tmp_installer
247 # Shut down the installer vm
248 ctr=0
249 vStat=`virsh list | grep $iVmName`
250 virsh shutdown $iVmName
251 while [ ! -z "$vStat" ];
252 do
253 echo "Waiting for $iVmName to shut down"
254 sleep 2
255 vStat=`virsh list | grep $iVmName`
256 ctr=`expr $ctr + 1`
257 if [ $ctr -eq $shutdownTimeout ]; then
258 echo -e "${red}Tired of waiting, forcing the VM off${NC}"
259 virsh destroy $iVmName
260 vStat=`virsh list | grep $iVmName`
261 fi
262 done
263 # Copy the install bootstrap script to the installer directory
264 cp BootstrapInstaller.sh tmp_installer
265 # Copy the private key to access the VM
266 cp key.pem tmp_installer
267 pushd tmp_installer > /dev/null 2>&1
268 # Copy the vm image to the installer directory
269 virsh vol-dumpxml $iVmName.qcow2 default | sed -e 's/<key.*key>//' | sed -e '/^[ ]*$/d' > ${iVmName}_volume.xml
270 virsh pool-create-as installer --type dir --target `pwd`
271 virsh vol-create-from installer ${iVmName}_volume.xml $iVmName.qcow2 --inputpool default
272 virsh pool-destroy installer
273 # The image is copied in as root. It needs to have ownership changed
274 # this will result in a password prompt.
275 sudo chown `whoami`.`whoami` $iVmName.qcow2
276 # Now create the installer tar file
277 tar cjf ../$installerArchive .
278 popd > /dev/null 2>&1
279 # Clean up
280 rm -fr tmp_installer
281 # Final location for the installer
282 rm -fr $installerDirectory
283 mkdir $installerDirectory
Sergio Slobodrian36e16552017-06-19 11:00:45 -0400284 cp deployInstaller.sh $installerDirectory
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400285 # Check the image size and determine if it needs to be split.
286 # To be safe, split the image into chunks smaller than 2G so that
287 # it will fit on a FAT32 volume.
288 fSize=`ls -l $installerArchive | awk '{print $5'}`
289 if [ $fSize -gt 2000000000 ]; then
290 echo -e "${lBlue}Installer file too large, breaking into parts${NC}"
291 # The file is too large, breaking it up into parts
292 sPos=0
293 fnn="00"
294 while dd if=$installerArchive of=${installerDirectory}/${installerPart}$fnn \
295 bs=1900MB count=1 skip=$sPos > /dev/null 2>&1
296 do
297 sPos=`expr $sPos + 1`
298 if [ ! -s ${installerDirectory}/${installerPart}$fnn ]; then
299 rm -f ${installerDirectory}/${installerPart}$fnn
300 break
301 fi
302 if [ $sPos -lt 10 ]; then
303 fnn="0$sPos"
304 else
305 fnn="$sPos"
306 fi
307 done
308 else
309 cp $installerArchive $installerDirectory
310 fi
311 # Clean up
312 rm $installerArchive
313 echo -e "${lBlue}The install image is built and can be found in ${yellow}$installerDirectory${NC}"
314 echo -e "${lBlue}Copy all the files in ${yellow}$installerDirectory${lBlue} to the traasnport media${NC}"
315fi