Enhanced the installer's test mode such that multiple development users
can create independent vagrant VM based clusters on the same bare metal
server. This reduces the number of servers required to do development
and testing of the voltha HA cluster.
Updated the production installer to start a 3 node docker swarm cluster
once installation is completed.
Added an option to the production installer to use a different QEMU/KVM
network to enable testing of the installer using vagrant based VMs on
the same server as the installer. The production installer will use the
default network otherwise.
Further provisioning of the installed environment will be submitted in
subsequent updates as the HA implementation progresses.

Change-Id: I62424e882d4a7f322acb9e26a9ee588c6fa91ca1
diff --git a/install/BootstrapInstaller.sh b/install/BootstrapInstaller.sh
index c140ab7..5aba0cc 100644
--- a/install/BootstrapInstaller.sh
+++ b/install/BootstrapInstaller.sh
@@ -17,6 +17,13 @@
 lCyan='\033[1;36m'
 
 wd=`pwd`
+
+# Check if a specific network was specified on the command line.
+# This is used mostly for testing.
+if [ $# -eq 1 ]; then
+	iVmNetwork=$1
+fi
+
 # Update the XML file with the VM information
 echo -e "${lBlue}Defining the  ${lCyan}$iVmName${lBlue} virtual machine${NC}"
 cat vmTemplate.xml | sed -e "s/{{ VMName }}/$iVmName/g" | sed -e "s/{{ VMNetwork }}/$iVmNetwork/g" > tmp.xml
diff --git a/install/BuildVoltha.sh b/install/BuildVoltha.sh
index 67a6183..8c3af94 100755
--- a/install/BuildVoltha.sh
+++ b/install/BuildVoltha.sh
@@ -1,18 +1,27 @@
 #!/bin/bash
 
-vmName="voltha_voltha"
+uId=`id -u`
+vmName="voltha_voltha${uId}"
 
 # Voltha directory
 cd ..
 
+# Rename voltha for multi-user support
+sed -i -e '/server_name/s/.*/server_name: "voltha'${uId}'"/' settings.vagrant.yaml
+# Build voltha in test mode
+if [ $# -eq 1 -a "$1" == "test" ]; then
+	sed -i -e '/test_mode/s/.*/test_mode: "true"/' settings.vagrant.yaml
+fi
+
 # Destroy the VM if it's running
-vagrant destroy voltha
+vagrant destroy voltha${uId}
 
 # Bring up the VM.
-vagrant up voltha
+vagrant up voltha${uId}
 
 # Get the VM's ip address
 ipAddr=`virsh domifaddr $vmName | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'`
 
+
 # Run all the build commands
-ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .vagrant/machines/voltha/libvirt/private_key vagrant@$ipAddr "cd /cord/incubator/voltha && . env.sh && make fetch && make"
+ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .vagrant/machines/voltha${uId}/libvirt/private_key vagrant@$ipAddr "cd /cord/incubator/voltha && . env.sh && make fetch && make build"
diff --git a/install/CreateInstaller.sh b/install/CreateInstaller.sh
index e8da9a8..15a5cd0 100755
--- a/install/CreateInstaller.sh
+++ b/install/CreateInstaller.sh
@@ -1,7 +1,8 @@
 #!/bin/bash
 
-baseImage="Ubuntu1604LTS"
+
 iVmName="vInstaller"
+baseImage="Ubuntu1604LTS"
 iVmNetwork="vagrant-libvirt"
 installerArchive="installer.tar.bz2"
 installerDirectory="volthaInstaller"
@@ -19,6 +20,7 @@
 lGrey='\033[1;37m'
 lCyan='\033[1;36m'
 
+uId=`id -u`
 wd=`pwd`
 
 # Validate that vagrant is installed.
@@ -46,26 +48,45 @@
 
 # Ensure that the voltha VM is running so that images can be secured
 echo -e "${lBlue}Ensure that the ${lCyan}voltha VM${lBlue} is running${NC}"
-vVM=`virsh list | grep voltha_voltha`
+vVM=`virsh list | grep voltha_voltha${uId}`
 
 if [ -z "$vVM" ]; then
-	./BuildVoltha.sh
+	./BuildVoltha.sh $1
 fi
 
 # Verify if this is intended to be a test environment, if so start 3 VMs
 # to emulate the production installation cluster.
 if [ $# -eq 1 -a "$1" == "test" ]; then
 	echo -e "${lBlue}Testing, create the ${lCyan}ha-serv${lBlue} VMs${NC}"
-	vagrant destroy ha-serv{1,2,3}
-	vagrant up ha-serv{1,2,3}
+	# Update the vagrant settings file
+	sed -i -e '/server_name/s/.*/server_name: "ha-serv'${uId}'-"/' settings.vagrant.yaml
+	sed -i -e '/docker_push_registry/s/.*/docker_push_registry: "vinstall'${uId}':5000"/' ansible/group_vars/all
+	sed -i -e "/vinstall/s/vinstall/vinstall${uId}/" ../ansible/roles/docker/templates/daemon.json
+
+	# Set the insecure registry configuration based on the installer hostname
+	echo -e "${lBlue}Set up the inescure registry hostname ${lCyan}vinstall${uId}${NC}"
+	echo '{' > ansible/roles/voltha/templates/daemon.json
+	echo '"insecure-registries" : ["vinstall'${uId}':5000"]' >> ansible/roles/voltha/templates/daemon.json
+	echo '}' >> ansible/roles/voltha/templates/daemon.json
+
+	vagrant destroy ha-serv${uId}-{1,2,3}
+	vagrant up ha-serv${uId}-{1,2,3}
 	./devSetHostList.sh
+	# Change the installer name
+	iVmName="vInstaller${uId}"
 else
 	rm -fr .test
 	# Clean out the install config file keeping only the commented lines
         # which serve as documentation.
 	sed -i -e '/^#/!d' install.cfg
+	# Set the insecure registry configuration based on the installer hostname
+	echo -e "${lBlue}Set up the inescure registry hostname ${lCyan}vinstall${uId}${NC}"
+	echo '{' > ansible/roles/voltha/templates/daemon.json
+	echo '"insecure-registries" : ["vinstall:5000"]' >> ansible/roles/voltha/templates/daemon.json
+	echo '}' >> ansible/roles/voltha/templates/daemon.json
 fi
 
+
 # Shut down the domain in case it's running.
 echo -e "${lBlue}Shut down the ${lCyan}$iVmName${lBlue} VM if running${NC}"
 ctr=0
@@ -155,6 +176,15 @@
 echo -e "${lBlue}Running the pre-configuration script on the VM${NC}"
 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no vinstall@$ipAddr 
 
+# If we're in test mode, change the hostname of the installer vm
+if [ $# -eq 1 -a "$1" == "test" ]; then
+	echo -e "${lBlue}Test mode, change the installer host name to ${yellow}vinstall${uId}${NC}"
+	ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr \
+		sudo hostnamectl set-hostname vinstall${uId}
+	ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr \
+		sudo service networking restart
+fi
+
 # Install python which is required for ansible
 echo -e "${lBlue}Installing python${NC}"
 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get update 
@@ -177,10 +207,10 @@
 
 # Add the voltha vm's information to the ansible tree
 echo -e "${lBlue}Add the voltha vm and key to the ansible accessible hosts${NC}"
-vIpAddr=`virsh domifaddr voltha_voltha | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'`
+vIpAddr=`virsh domifaddr voltha_voltha${uId} | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'`
 echo "[voltha]" > ansible/hosts/voltha
 echo $vIpAddr >> ansible/hosts/voltha
-echo "ansible_ssh_private_key_file: $wd/../.vagrant/machines/voltha/libvirt/private_key" > ansible/host_vars/$vIpAddr
+echo "ansible_ssh_private_key_file: $wd/../.vagrant/machines/voltha${uId}/libvirt/private_key" > ansible/host_vars/$vIpAddr
 
 
 # Prepare to launch the ansible playbook to configure the installer VM
diff --git a/install/Vagrantfile b/install/Vagrantfile
index dccee81..bc1f9d2 100644
--- a/install/Vagrantfile
+++ b/install/Vagrantfile
@@ -15,6 +15,7 @@
       d.ssh.forward_agent = true
       d.vm.box = settings["box_source"]
       d.vm.hostname = "#{settings['server_name']}#{i}"
+      d.vm.provision :shell, inline: "apt-get -y install python"
       d.vm.provider "libvirt" do |v|
         v.memory = 6144
       end
diff --git a/install/ansible/group_vars/all b/install/ansible/group_vars/all
index 311ec96..e13d45f 100644
--- a/install/ansible/group_vars/all
+++ b/install/ansible/group_vars/all
@@ -8,6 +8,8 @@
 docker_py_version: "1.7.0"
 netifaces_version: "0.10.4"
 target_voltha_home: /home/voltha
+docker_daemon_json: daemon.json
+docker_daemon_json_dest: /etc/docker
 voltha_containers:
   - voltha/nginx
   - voltha/grafana
diff --git a/install/ansible/roles/cluster-host/tasks/cluster-host.yml b/install/ansible/roles/cluster-host/tasks/cluster-host.yml
index 20330c4..b9b2146 100644
--- a/install/ansible/roles/cluster-host/tasks/cluster-host.yml
+++ b/install/ansible/roles/cluster-host/tasks/cluster-host.yml
@@ -48,6 +48,12 @@
   when: target == "cluster"
   tags: [cluster_host]
 
+- name: apt lists are up-to-date
+  copy:
+    src: "/var/lib/apt/lists"
+    dest: "/var/lib/apt"
+  tags: [cluster_host]
+
 - name: Dependent software is installed
   command: dpkg -i "{{ target_voltha_home }}/deb_files/{{ item }}"
   with_items: "{{ deb_files }}"
diff --git a/install/ansible/roles/voltha/tasks/voltha.yml b/install/ansible/roles/voltha/tasks/voltha.yml
index d6884e5..a52b7d9 100644
--- a/install/ansible/roles/voltha/tasks/voltha.yml
+++ b/install/ansible/roles/voltha/tasks/voltha.yml
@@ -76,6 +76,30 @@
   when: target == "cluster"

   tags: [voltha]

 

+# Update the insecure registry to reflect the current installer.

+# The installer name can change depending on whether test mode

+# is being used or not.

+- name: Enable insecure install registry

+  template:

+    src: "{{ docker_daemon_json }}"

+    dest: "{{ docker_daemon_json_dest }}"

+  register: copy_result

+  when: target == "installer"

+  tags: [voltha]

+

+- name: Debain Daemon is reloaded

+  command: systemctl daemon-reload

+  when: copy_result|changed and is_systemd is defined and target == "installer"

+  tags: [voltha]

+

+- name: Debian Docker service is restarted

+  service:

+    name: docker

+    state: restarted

+  when: copy_result|changed or user_result|changed

+  when: target == "installer"

+  tags: [voltha]

+

 - name: Docker images are re-tagged to registry for push

   command: docker tag {{ item }} {{ docker_push_registry }}/{{ item }}

   with_items: "{{ voltha_containers }}"

diff --git a/install/ansible/voltha.yml b/install/ansible/voltha.yml
index f3a1cf4..b9a2c24 100644
--- a/install/ansible/voltha.yml
+++ b/install/ansible/voltha.yml
@@ -9,4 +9,4 @@
     - docker
     - docker-compose
     - voltha
-    - java
+#    - java
diff --git a/install/devSetHostList.sh b/install/devSetHostList.sh
index 924667b..38a050d 100755
--- a/install/devSetHostList.sh
+++ b/install/devSetHostList.sh
@@ -7,12 +7,13 @@
 
 # usage devCopyTiInstaller.sh <ip-address>
 
+uId=`id -u`
 
 sed -i -e '/^#/!d' install.cfg
 rm -fr .test
 mkdir .test
 hosts=""
-for i in `virsh list | awk '{print $2}' | grep ha-serv`
+for i in `virsh list | awk '{print $2}' | grep ha-serv${uId}-`
 do
 	ipAddr=`virsh domifaddr $i | tail -n +3 | head -n 1 | awk '{print $4}' | sed -e 's~/.*~~'`
 	hosts="$hosts $ipAddr"
diff --git a/install/installVoltha.sh b/install/installVoltha.sh
index f5ae9ff..a5632cd 100755
--- a/install/installVoltha.sh
+++ b/install/installVoltha.sh
@@ -43,4 +43,4 @@
 tar xjf $installerArchive
 echo -e "${lBlue}Starting the installer{NC}"
 chmod u+x BootstrapInstaller.sh
-./BootstrapInstaller.sh
+./BootstrapInstaller.sh "$@"
diff --git a/install/vmTemplate.xml b/install/vmTemplate.xml
index faf7288..b08cea6 100644
--- a/install/vmTemplate.xml
+++ b/install/vmTemplate.xml
@@ -63,7 +63,6 @@
       <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
     </controller>
     <interface type='network'>
-      <mac address='52:54:00:ed:19:74'/>
       <source network='{{ VMNetwork }}'/>
       <model type='virtio'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>