cord-in-a-box enhancements to support more than one compute node

Change-Id: Ic8fa77ead0ab474af7758015c8732d04acdbd76f
diff --git a/Vagrantfile b/Vagrantfile
index ac96263..ffa826e 100644
--- a/Vagrantfile
+++ b/Vagrantfile
@@ -9,6 +9,14 @@
     config.vm.synced_folder "..", "/cord"
   end
 
+  #By default, this variable is set to 2, such that Vagrantfile allows creation
+  #of compute nodes up to 2. If the number of compute nodes to be supported more 
+  #than 2, set the environment variable NUM_COMPUTE_NODES to the desired value
+  #before executing this Vagrantfile.
+  num_compute_nodes = (ENV['NUM_COMPUTE_NODES'] || 2).to_i
+  compute_ip_base = "10.6.1."
+  compute_ips = num_compute_nodes.times.collect { |n| compute_ip_base + "#{n+2}" }
+
   config.vm.define "corddev" do |d|
     d.ssh.forward_agent = true
     d.vm.box = "ubuntu/trusty64"
@@ -91,30 +99,34 @@
     end
   end
 
-  # Libvirt compute node
-  # Not able to merge with virtualbox config for compute nodes above
-  # Issue is that here no box and no private network are specified
-  config.vm.define "compute_node" do |c|
-    c.vm.synced_folder '.', '/vagrant', disabled: true
-    c.vm.communicator = "none"
-    c.vm.hostname = "computenode"
-    c.vm.network "public_network",
-      adapter: 1,
-      auto_config: false,
-      dev: "mgmtbr",
-      mode: "bridge",
-      type: "bridge"
-    c.vm.network "private_network",
-      adapter: 2,
-      ip: "10.6.1.2"
-    c.vm.provider :libvirt do |domain|
-      domain.memory = 8192
-      domain.cpus = 4
-      domain.machine_virtual_size = 100
-      domain.storage :file, :size => '100G', :type => 'qcow2'
-      domain.boot 'network'
-      domain.boot 'hd'
-      domain.nested = true
+  num_compute_nodes.times do |n|
+    # Libvirt compute node
+    # Not able to merge with virtualbox config for compute nodes above
+    # Issue is that here no box and no private network are specified
+    config.vm.define "compute_node-#{n+1}" do |c|
+      compute_ip = compute_ips[n]
+      compute_index = n+1
+      c.vm.synced_folder '.', '/vagrant', disabled: true
+      c.vm.communicator = "none"
+      c.vm.hostname = "computenode-#{compute_index}"
+      c.vm.network "public_network",
+        adapter: 1,
+        auto_config: false,
+        dev: "mgmtbr",
+        mode: "bridge",
+        type: "bridge"
+      c.vm.network "private_network",
+        adapter: 2,
+        ip: "#{compute_ip}"
+      c.vm.provider :libvirt do |domain|
+        domain.memory = 8192
+        domain.cpus = 4
+        domain.machine_virtual_size = 100
+        domain.storage :file, :size => '100G', :type => 'qcow2'
+        domain.boot 'network'
+        domain.boot 'hd'
+        domain.nested = true
+      end
     end
   end
 
diff --git a/scripts/cord-in-a-box.sh b/scripts/cord-in-a-box.sh
index 706662c..5eecc1c 100755
--- a/scripts/cord-in-a-box.sh
+++ b/scripts/cord-in-a-box.sh
@@ -181,19 +181,27 @@
   sudo su maas -c 'cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys'
 }
 
+FIRST_COMPUTE_NODE=true
+
 function add_compute_node() {
+  echo add_compute_node: $1 $2
+    
   cd $CORDDIR/build
-  sudo su $USER -c 'vagrant up compute_node --provider libvirt'
+  sudo su $USER -c "vagrant up $1 --provider libvirt"
 
-  # Change MAC address of bridge to match cord-pod service profile
-  # This change won't survive a reboot
-  BRIDGE=$( route -n | grep 10.6.1.0 | awk '{print $8}' )
-  sudo ifconfig $BRIDGE hw ether 02:42:0a:06:01:01
+  if [ "$FIRST_COMPUTE_NODE" == true ]; then
+    # Change MAC address of bridge to match cord-pod service profile
+    # This change won't survive a reboot
+    BRIDGE=$( route -n | grep 10.6.1.0 | awk '{print $8}' )
+    sudo ifconfig $BRIDGE hw ether 02:42:0a:06:01:01
 
-  # Add gateway IP addresses to $BRIDGE for vsg and exampleservice tests
-  # This change won't survive a reboot
-  sudo ip address add 10.6.1.129 dev $BRIDGE
-  sudo ip address add 10.6.1.193 dev $BRIDGE
+    # Add gateway IP addresses to $BRIDGE for vsg and exampleservice tests
+    # This change won't survive a reboot
+    sudo ip address add 10.6.1.129 dev $BRIDGE
+    sudo ip address add 10.6.1.193 dev $BRIDGE
+
+    FIRST_COMPUTE_NODE=false
+  fi
 
   # Sign into MAAS
   KEY=$(sudo maas-region-admin apikey --username=cord)
@@ -209,7 +217,7 @@
   # Add remote power state
   maas cord node update $NODEID power_type="virsh" \
     power_parameters_power_address="qemu+ssh://maas@localhost/system" \
-    power_parameters_power_id="build_compute_node"
+    power_parameters_power_id="$2"
 
   STATUS=$(sudo /usr/local/bin/get-node-prov-state |jq ".[] | select(.id == \"$NODEID\").status")
   until [ "$STATUS" == "2" ]; do
@@ -247,8 +255,11 @@
 SETUP_ONLY=0
 DIAGNOSTICS=0
 CLEANUP=0
+#By default, cord-in-a-box creates 1 compute node. If more than one compute is
+#needed, use -n option
+NUM_COMPUTE_NODES=1
 
-while getopts "b:cdhst" opt; do
+while getopts "b:cdhn:st" opt; do
   case ${opt} in
     b ) GERRIT_BRANCHES+=("$OPTARG")
       ;;
@@ -263,10 +274,13 @@
       echo "    $0 -c             cleanup from previous test"
       echo "    $0 -d             run diagnostic collector"
       echo "    $0 -h             display this help message"
+      echo "    $0 -n             number of compute nodes to setup. currently max 2 nodes can be supported"
       echo "    $0 -s             run initial setup phase only (don't start building CORD)"
       echo "    $0 -t             do install, bring up cord-pod configuration, run E2E test"
       exit 0
       ;;
+    n ) NUM_COMPUTE_NODES=$OPTARG
+      ;;
     s ) SETUP_ONLY=1
       ;;
     t ) RUN_TEST=1
@@ -298,7 +312,21 @@
 
 install_head_node
 set_up_maas_user
-add_compute_node
+
+#Limiting the maximum number of compute nodes that can be supported to 2. If 
+#more than 2 compute nodes are needed, this script need to be enhanced to set
+#the environment variable NUM_COMPUTE_NODES before invoking the Vagrant commands
+if [[ $NUM_COMPUTE_NODES -gt 2 ]]
+then
+   echo "currently max only two compute nodes can be supported..."
+   NUM_COMPUTE_NODES=2
+fi
+
+for i in `seq 1 $NUM_COMPUTE_NODES`;
+do
+   echo adding the compute node: compute-node-$i
+   add_compute_node compute_node-$i build_compute_node-$i
+done
 
 if [[ $RUN_TEST -eq 1 ]]
 then