refactor lib/installer 13/27913/2
authorRyota MIBU <r-mibu@cq.jp.nec.com>
Thu, 2 Feb 2017 15:03:01 +0000 (15:03 +0000)
committerRyota MIBU <r-mibu@cq.jp.nec.com>
Thu, 2 Feb 2017 16:22:03 +0000 (16:22 +0000)
- make sure expected parameters and functions are set

- remove all installer dependent codes from run.sh
  except non-used function which will be removed later on

Change-Id: I4628173ef0891d893146976c7553ed6a95329957
Signed-off-by: Ryota MIBU <r-mibu@cq.jp.nec.com>
tests/functions-common
tests/lib/installer
tests/lib/installers/apex
tests/lib/installers/fuel
tests/lib/installers/local
tests/run.sh

index 7928f20..53a620b 100644 (file)
@@ -73,6 +73,20 @@ function die_if_not_set {
     $xtrace
 }
 
+# Check the function is defined
+# die_if_not_defined $LINENO function-name "message"
+function die_if_not_defined {
+    local xtrace
+    xtrace=$(set +o | grep xtrace)
+    set +o xtrace
+    local line=$1; shift
+    local func_name=$1; shift
+    if ! declare -f "$func_name" > /dev/null; then
+        die $line "$*"
+    fi
+    $xtrace
+}
+
 # Wait until the condition is met.
 # wait_until condition timeout interval
 function wait_until {
@@ -90,3 +104,17 @@ function wait_until {
         fi
     done
 }
+
+# Print IP address of the first vNIC owned by specified VM via virsh
+# get_first_vnic_ip vm_name
+function get_first_vnic_ip {
+    local vm_name=$1
+
+    _vnic_mac=$(sudo virsh domiflist $vm_name | \
+        sed -n -e 's/^.*\([0-9a-f]\{2\}\(:[0-9a-f]\{2\}\)\{5\}\).*$/\1/p' | \
+        head -1)
+    die_if_not_set $LINENO _vnic_mac
+    _vnic_ip=$(arp -e | grep $_vnic_mac | awk '{print $1}')
+    die_if_not_set $LINENO _vnic_ip
+    echo $_vnic_ip
+}
index bdee914..13953d2 100644 (file)
@@ -1,8 +1,7 @@
 #!/bin/bash
 
 INSTALLER_TYPE=${INSTALLER_TYPE:-local}
-INSTALLER_IP=${INSTALLER_IP:-none}
-ssh_opts_cpu="$ssh_opts"
+ssh_opts="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
 
 function is_installer_supported {
     local installer="$1"
@@ -14,22 +13,27 @@ function is_installer {
     [[ $installer == $INSTALLER_TYPE ]]
 }
 
-function setup_installer {
-    if ! is_set INSTALLER_IP; then
-        get_installer_ip
-    fi
-
-    installer_get_ssh_keys
-    installer_apply_patches
-}
-
-function cleanup_installer {
-    cleanup_installer_$INSTALLER_TYPE
+function validate_installer_lib {
+    local xtrace
+    xtrace=$(set +o | grep xtrace)
+    set +o xtrace
+    for p in COMPUTE_USER ssh_opts_cpu
+    do
+        die_if_not_set $LINENO $p \
+            "Parameter $p for $INSTALLER_TYPE is missing."
+    done
+    for f in setup_installer get_compute_ip_from_hostname cleanup_installer
+    do
+        die_if_not_defined $LINENO $f \
+            "Mandatory function ${f}() for $INSTALLER_TYPE is missing."
+    done
+    $xtrace
 }
 
-
 if ! is_installer_supported $INSTALLER_TYPE; then
     die $LINENO "INSTALLER_TYPE=$INSTALLER_TYPE is not supported."
 fi
 
 source $TOP_DIR/lib/installers/$INSTALLER_TYPE
+
+validate_installer_lib
index 45a5dcd..e353d25 100644 (file)
@@ -1,24 +1,47 @@
 #!/bin/bash
 
+COMPUTE_USER=${COMPUTE_USER:-heat-admin}
+ssh_opts_cpu="$ssh_opts -i instack_key"
+
 function get_installer_ip {
-    local instack_mac=$(sudo virsh domiflist instack | awk '/default/{print $5}')
-    INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk '{print $1}')
-    die_if_not_set $LINENO INSTALLER_IP "No installer IP"
+    is_set INSTALLER_IP && return
+    INSTALLER_IP=$(get_first_vnic_ip instack)
 }
 
 function installer_get_ssh_keys {
-    sudo scp $ssh_opts root@"$INSTALLER_IP":/home/stack/.ssh/id_rsa instack_key
+    sudo scp $ssh_opts "root@$INSTALLER_IP:/home/stack/.ssh/id_rsa" instack_key
     sudo chown $(whoami):$(whoami) instack_key
     chmod 400 instack_key
-    ssh_opts_cpu+=" -i instack_key"
 }
 
-function installer_apply_patches {
-    # Noop
-    return
+function get_controller_ips {
+    is_set CONTROLLER_IPS && return
+    get_installer_ip
+    CONTROLLER_IPS=$(sudo ssh $ssh_opts $INSTALLER_IP \
+                     "source stackrc
+                      nova list | grep ' overcloud-controller-[0-9] ' | \
+                      sed -e 's/^.*ctlplane=//' -e 's/ *|\$//'")
+    die_if_not_set $LINENO CONTROLLER_IPS "No controller IPs"
+}
+
+function setup_installer {
+    get_installer_ip
+    installer_get_ssh_keys
+    get_controller_ips
+}
+
+function get_compute_ip_from_hostname {
+    local compute_host=$1
+
+    compute_host_in_undercloud=${compute_host%%.*}
+    COMPUTE_IP=$(sudo ssh $ssh_opts $INSTALLER_IP \
+                 "source stackrc;
+                  nova show $compute_host_in_undercloud  | \
+                  awk '/ ctlplane network /{print \$5}'")
+    die_if_not_set $LINENO COMPUTE_IP "Could get IP address of $compute_host."
 }
 
-function cleanup_installer_apex {
+function cleanup_installer {
     # Noop
     return
 }
index 9ff9e72..da0de34 100644 (file)
@@ -4,18 +4,16 @@ if [[ "congress " == "$INSPECTOR_TYPE" ]]; then
     die $LINENO "fuel does not support congress yet..."
 fi
 
+COMPUTE_USER=${COMPUTE_USER:-root}
+ssh_opts_cpu="$ssh_opts -i instack_key"
+
 function get_installer_ip {
-    ssh_opts_cpu="$ssh_opts -i instack_key"
     is_set INSTALLER_IP && return
-    local instack_mac
-    instack_mac=$(sudo virsh domiflist fuel-master | awk '/fuel1/{print $5}')
-    INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk '{print $1}')
-    die_if_not_set $LINENO INSTALLER_IP "No installer IP"
+    INSTALLER_IP=$(get_first_vnic_ip fuel-master)
 }
 
 function get_controller_ips {
     is_set CONTROLLER_IPS && return
-    get_installer_ip
     CONTROLLER_IPS=$(ssh $ssh_opts_cpu root@$INSTALLER_IP \
                      "fuel node | grep controller | cut -d '|' -f 5|xargs")
     die_if_not_set $LINENO CONTROLLER_IPS "No controller IPs"
@@ -33,13 +31,7 @@ function installer_get_ssh_keys {
 }
 
 function installer_apply_patches {
-    if ! openstack flavor show $VM_FLAVOR ; then
-        openstack flavor create --ram 512 --disk 1 $VM_FLAVOR \
-            && touch created_doctor_flavor
-    fi
-
     # TODO(r-mibu): fix the followings in upstream (fuel)
-    get_controller_ips
     for node in $CONTROLLER_IPS;do
         echo "check controller configuration for doctor ($node)"
         ssh $ssh_opts_cpu "root@$node" '
@@ -86,11 +78,28 @@ function installer_apply_patches {
     done
 }
 
-function cleanup_installer_fuel {
-    if [[ -e created_doctor_flavor ]] && openstack flavor show $VM_FLAVOR ; then
-        openstack flavor delete $VM_FLAVOR && rm -f created_doctor_flavor
+function setup_installer {
+    get_installer_ip
+    installer_get_ssh_keys
+    get_controller_ips
+    installer_apply_patches
+    if ! openstack flavor show $VM_FLAVOR ; then
+        openstack flavor create --ram 512 --disk 1 $VM_FLAVOR \
+            && touch created_doctor_flavor
     fi
+}
+
+function get_compute_ip_from_hostname {
+    local compute_host=$1
+
+    compute_host_in_undercloud=${compute_host%%.*}
+    node_id=$(echo $compute_host_in_undercloud | cut -d "-" -f 2)
+    COMPUTE_IP=$(sshpass -p r00tme ssh 2>/dev/null $ssh_opts root@${INSTALLER_IP} \
+         "fuel node|awk -F '|' -v id=$node_id '{if (\$1 == id) print \$5}' |xargs")
+    die_if_not_set $LINENO COMPUTE_IP "Could get IP address of $compute_host."
+}
 
+function installer_revert_patches {
     # TODO(r-mibu): fix the followings in upstream (fuel)
     get_controller_ips
     for node in $CONTROLLER_IPS;do
@@ -132,3 +141,10 @@ function cleanup_installer_fuel {
             ' >> installer_apply_patches_$node.log 2>&1
     done
 }
+
+function cleanup_installer {
+    if [[ -e created_doctor_flavor ]] && openstack flavor show $VM_FLAVOR ; then
+        openstack flavor delete $VM_FLAVOR && rm -f created_doctor_flavor
+    fi
+    installer_revert_patches
+}
index e7aed14..50c3686 100644 (file)
@@ -1,9 +1,7 @@
 #!/bin/bash
 
-function get_installer_ip {
-    # Noop
-    return
-}
+COMPUTE_USER=${COMPUTE_USER:-$(whoami)}
+ssh_opts_cpu="$ssh_opts"
 
 function installer_get_ssh_keys {
     echo "INSTALLER_TYPE set to 'local'. Assuming SSH keys already exchanged with $COMPUTE_HOST"
@@ -15,7 +13,24 @@ function installer_apply_patches {
     return
 }
 
-function cleanup_installer_local {
+function setup_installer {
+    installer_get_ssh_keys
+    installer_apply_patches
+}
+
+function get_compute_ip_from_hostname {
+    local compute_host=$1
+
+    if is_set COMPUTE_IP; then
+        echo "Using pre-configured COMPUTE_IP=$COMPUTE_IP ..."
+        return
+    fi
+    COMPUTE_IP=$(getent hosts "$compute_host" | awk '{ print $1 }')
+    die_if_not_set $LINENO COMPUTE_IP \
+        "Could not resolve $compute_host. Either manually set COMPUTE_IP or enable DNS resolution."
+}
+
+function cleanup_installer {
     # Noop
     return
 }
index 227c6ea..50f0164 100755 (executable)
@@ -31,7 +31,6 @@ PROFILER_TYPE=${PROFILER_TYPE:-none}
 
 TOP_DIR=$(cd $(dirname "$0") && pwd)
 
-ssh_opts="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
 as_doctor_user="--os-username $DOCTOR_USER --os-password $DOCTOR_PW
                 --os-tenant-name $DOCTOR_PROJECT"
 
@@ -45,23 +44,8 @@ get_compute_host_info() {
     compute_host_in_undercloud=${COMPUTE_HOST%%.*}
     die_if_not_set $LINENO COMPUTE_HOST "Failed to get compute hostname"
 
-    if is_installer apex; then
-        COMPUTE_USER=${COMPUTE_USER:-heat-admin}
-        COMPUTE_IP=$(sudo ssh $ssh_opts $INSTALLER_IP \
-             "source stackrc; \
-             nova show $compute_host_in_undercloud \
-             | awk '/ ctlplane network /{print \$5}'")
-    elif is_installer fuel; then
-        COMPUTE_USER=${COMPUTE_USER:-root}
-        node_id=$(echo $compute_host_in_undercloud | cut -d "-" -f 2)
-        COMPUTE_IP=$(sshpass -p r00tme ssh 2>/dev/null $ssh_opts root@${INSTALLER_IP} \
-             "fuel node|awk -F '|' -v id=$node_id '{if (\$1 == id) print \$5}' |xargs")
-    elif is_installer local; then
-        COMPUTE_USER=${COMPUTE_USER:-$(whoami)}
-        COMPUTE_IP=$(getent hosts "$COMPUTE_HOST" | awk '{ print $1 }')
-    fi
+    get_compute_ip_from_hostname $COMPUTE_HOST
 
-    die_if_not_set $LINENO COMPUTE_IP "Could not resolve $COMPUTE_HOST. Either manually set COMPUTE_IP or enable DNS resolution."
     echo "COMPUTE_HOST=$COMPUTE_HOST"
     echo "COMPUTE_IP=$COMPUTE_IP"
 
@@ -78,7 +62,9 @@ get_compute_host_info() {
     fi
 }
 
-get_consumer_ip() {
+# TODO(r-mibu): update this function to support consumer instance
+#               and migrate this function into installer lib
+get_consumer_ip___to_be_removed() {
     local get_consumer_command="ip route get $COMPUTE_IP | awk '/ src /{print \$NF}'"
     if is_installer apex; then
         CONSUMER_IP=$(sudo ssh $ssh_opts root@$INSTALLER_IP \
@@ -173,16 +159,6 @@ start_consumer() {
     # avoid some network problems dpends on infra and installers.
     # This tunnel will be terminated by stop_consumer() or after 10 mins passed.
     if ! is_installer local; then
-        if is_installer apex; then
-            CONTROLLER_IPS=$(sudo ssh $ssh_opts $INSTALLER_IP \
-                             "source stackrc; \
-                             nova list | grep ' overcloud-controller-[0-9] ' \
-                             | sed -e 's/^.*ctlplane=//' -e 's/ *|\$//'")
-        elif is_installer fuel; then
-            get_controller_ips
-        fi
-
-        die_if_not_set $LINENO CONTROLLER_IPS "Could not get CONTROLLER_IPS."
         for ip in $CONTROLLER_IPS
         do
             forward_rule="-R $CONSUMER_PORT:localhost:$CONSUMER_PORT"
@@ -330,7 +306,9 @@ cleanup() {
     # TODO: We need to make sure the target compute host is back to IP
     #       reachable. wait_ping() will be added by tojuvone .
     sleep 110
-    scp $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP:disable_network.log" .
+    if is_set COMPUTE_IP; then
+        scp $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP:disable_network.log" .
+    fi
 
     openstack $as_doctor_user server list | grep -q " $VM_NAME " && openstack $as_doctor_user server delete "$VM_NAME"
     sleep 1