Poll resource tracker for ironic cpus as well as count

When ironic nodes are enrolled, their resources are not available
to the nova scheduler until after a round of ironic and nova periodic
tasks have run  In addition to waiting for ironic nodes to show up in
the resource tracker, also wait for associated CPU resources.  In
the worst case, this means waiting for 3 total rounds of periodic
tasks.

Change-Id: Idbbc43bf74ff5fff3d50f3494148454bb51e378f
Closes-bug: #1398128
diff --git a/lib/ironic b/lib/ironic
index e79842c..abf4700 100644
--- a/lib/ironic
+++ b/lib/ironic
@@ -501,18 +501,20 @@
 }
 
 function wait_for_nova_resources {
-    # After nodes have been enrolled, we need to wait for n-cpu's periodic
-    # task populate the resource tracker with available nodes.  Wait for 2
-    # minutes before timing out.
-    local expected_count=$1
-    echo_summary "Waiting 2 minutes for Nova resource tracker to pick up $expected_count Ironic nodes"
+    # After nodes have been enrolled, we need to wait for both ironic and
+    # nova's periodic tasks to populate the resource tracker with available
+    # nodes and resources. Wait up to 2 minutes for a given resource before
+    # timing out.
+    local resource=$1
+    local expected_count=$2
+    echo_summary "Waiting 2 minutes for Nova resource tracker to pick up $resource >= $expected_count"
     for i in $(seq 1 120); do
-        if [ $(nova hypervisor-stats | grep " count " | get_field 2) -ge $expected_count ]; then
+        if [ $(nova hypervisor-stats | grep " $resource " | get_field 2) -ge $expected_count ]; then
             return 0
         fi
         sleep 1
     done
-    die $LINENO "Nova hypervisor-stats did not register at least $expected_count nodes"
+    die $LINENO "Timed out waiting for Nova hypervisor-stats $resource >= $expected_count"
 }
 
 function enroll_nodes {
@@ -551,6 +553,7 @@
     fi
 
     local total_nodes=0
+    local total_cpus=0
     while read hardware_info; do
         if ! is_ironic_hardware; then
             local mac_address=$hardware_info
@@ -582,6 +585,7 @@
         ironic port-create --address $mac_address --node_uuid $node_id
 
         total_nodes=$((total_nodes+1))
+        total_cpus=$((total_cpus+$ironic_node_cpu))
     done < $ironic_hwinfo_file
 
     # create the nova flavor
@@ -598,7 +602,8 @@
     nova flavor-key baremetal set "cpu_arch"="x86_64" "baremetal:deploy_kernel_id"="$IRONIC_DEPLOY_KERNEL_ID" "baremetal:deploy_ramdisk_id"="$IRONIC_DEPLOY_RAMDISK_ID"
 
     if [ "$VIRT_DRIVER" == "ironic" ]; then
-        wait_for_nova_resources $total_nodes
+        wait_for_nova_resources "count" $total_nodes
+        wait_for_nova_resources "vcpus" $total_cpus
     fi
 }