Refactor inspectors support code 13/25313/5
authorCarlos Goncalves <carlos.goncalves@neclab.eu>
Wed, 30 Nov 2016 13:08:20 +0000 (13:08 +0000)
committerCarlos Goncalves <carlos.goncalves@neclab.eu>
Wed, 14 Dec 2016 16:12:13 +0000 (16:12 +0000)
JIRA: DOCTOR-71

Change-Id: I0913d4d0390325cc0cc715572b7525a6bbb795d3
Signed-off-by: Carlos Goncalves <carlos.goncalves@neclab.eu>
tests/lib/inspector [new file with mode: 0644]
tests/lib/inspectors/congress [new file with mode: 0644]
tests/lib/inspectors/sample [new file with mode: 0644]
tests/run.sh

diff --git a/tests/lib/inspector b/tests/lib/inspector
new file mode 100644 (file)
index 0000000..2fb7c40
--- /dev/null
@@ -0,0 +1,30 @@
+#!/bin/bash
+
+INSPECTOR_TYPE=${INSPECTOR_TYPE:-sample}
+
+function is_inspector_supported {
+    local inspector="$1"
+    [[ -f $TOP_DIR/lib/inspectors/$inspector ]]
+}
+
+function is_inspector {
+    local inspector="$1"
+    [[ $inspector == $INSPECTOR_TYPE ]]
+}
+
+function start_inspector {
+    if ! is_inspector_supported $INSPECTOR_TYPE; then
+        die $LINENO"INSPECTOR_TYPE=$INSPECTOR_TYPE is not supported."
+    fi
+
+    source $TOP_DIR/lib/inspectors/$INSPECTOR_TYPE
+    start_inspector_$INSPECTOR_TYPE
+}
+
+function stop_inspector {
+    stop_inspector_$INSPECTOR_TYPE
+}
+
+function cleanup_inspector {
+    cleanup_inspector_$INSPECTOR_TYPE
+}
diff --git a/tests/lib/inspectors/congress b/tests/lib/inspectors/congress
new file mode 100644 (file)
index 0000000..0482525
--- /dev/null
@@ -0,0 +1,69 @@
+#!/bin/bash
+
+function _congress_add_rule {
+    name=$1
+    policy=$2
+    rule=$3
+
+    if ! openstack congress policy rule list $policy | grep -q -e "// Name: $name$" ; then
+        openstack congress policy rule create --name $name $policy "$rule"
+    fi
+}
+
+function _congress_del_rule {
+    name=$1
+    policy=$2
+
+    if openstack congress policy rule list $policy | grep -q -e "^// Name: $name$" ; then
+        openstack congress policy rule delete $policy $name
+    fi
+}
+
+function _congress_add_rules {
+    _congress_add_rule host_down classification \
+        'host_down(host) :-
+            doctor:events(hostname=host, type="compute.host.down", status="down")'
+
+    _congress_add_rule active_instance_in_host classification \
+        'active_instance_in_host(vmid, host) :-
+            nova:servers(id=vmid, host_name=host, status="ACTIVE")'
+
+    _congress_add_rule host_force_down classification \
+        'execute[nova:services.force_down(host, "nova-compute", "True")] :-
+            host_down(host)'
+
+    _congress_add_rule error_vm_states classification \
+        'execute[nova:servers.reset_state(vmid, "error")] :-
+            host_down(host),
+            active_instance_in_host(vmid, host)'
+}
+
+function start_inspector_congress {
+    nova_api_min_version="2.11"
+    nova_api_version=$(openstack congress datasource list | \
+                       grep nova | grep -Po "(?<='api_version': ')[^']*")
+    [[ -z $nova_api_version ]] && nova_api_version="2.0"
+    if [[ "$nova_api_version" < "$nova_api_min_version" ]]; then
+        echo "ERROR: Congress Nova datasource API version < $nova_api_min_version ($nova_api_version)"
+        exit 1
+    fi
+    openstack congress driver list | grep -q " doctor "
+    openstack congress datasource list | grep -q " doctor " || {
+        openstack congress datasource create doctor doctor
+    }
+    _congress_add_rules
+
+}
+
+function stop_inspector_congress {
+    _congress_del_rule host_force_down classification
+    _congress_del_rule error_vm_states classification
+    _congress_del_rule active_instance_in_host classification
+    _congress_del_rule host_down classification
+
+}
+
+function cleanup_inspector_congress {
+    # Noop
+    return
+}
diff --git a/tests/lib/inspectors/sample b/tests/lib/inspectors/sample
new file mode 100644 (file)
index 0000000..cd21a00
--- /dev/null
@@ -0,0 +1,16 @@
+#!/bin/bash
+
+function start_inspector_sample {
+    pgrep -f "python inspector.py" && return 0
+    python inspector.py "$INSPECTOR_PORT" > inspector.log 2>&1 &
+}
+
+function stop_inspector_sample {
+    pgrep -f "python inspector.py" || return 0
+    kill $(pgrep -f "python inspector.py")
+}
+
+function cleanup_inspector_sample {
+    # Noop
+    return
+}
index f1087ba..eefa79b 100755 (executable)
@@ -26,18 +26,12 @@ DOCTOR_PROJECT=doctor
 #TODO: change back to `_member_` when JIRA DOCTOR-55 is done
 DOCTOR_ROLE=admin
 
-SUPPORTED_INSPECTOR_TYPES="sample congress"
-INSPECTOR_TYPE=${INSPECTOR_TYPE:-sample}
 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"
 
-if [[ ! "$SUPPORTED_INSPECTOR_TYPES" =~ "$INSPECTOR_TYPE" ]] ; then
-    echo "ERROR: INSPECTOR_TYPE=$INSPECTOR_TYPE is not supported."
-    exit 1
-fi
 
 get_compute_host_info() {
     # get computer host info which VM boot in
@@ -166,78 +160,6 @@ stop_monitor() {
     sudo kill $(pgrep -f "python monitor.py")
 }
 
-congress_add_rule() {
-    name=$1
-    policy=$2
-    rule=$3
-
-    if ! openstack congress policy rule list $policy | grep -q -e "// Name: $name$" ; then
-        openstack congress policy rule create --name $name $policy "$rule"
-    fi
-}
-
-congress_del_rule() {
-    name=$1
-    policy=$2
-
-    if openstack congress policy rule list $policy | grep -q -e "^// Name: $name$" ; then
-        openstack congress policy rule delete $policy $name
-    fi
-}
-
-congress_setup_rules() {
-    congress_add_rule host_down classification \
-        'host_down(host) :-
-            doctor:events(hostname=host, type="compute.host.down", status="down")'
-
-    congress_add_rule active_instance_in_host classification \
-        'active_instance_in_host(vmid, host) :-
-            nova:servers(id=vmid, host_name=host, status="ACTIVE")'
-
-    congress_add_rule host_force_down classification \
-        'execute[nova:services.force_down(host, "nova-compute", "True")] :-
-            host_down(host)'
-
-    congress_add_rule error_vm_states classification \
-        'execute[nova:servers.reset_state(vmid, "error")] :-
-            host_down(host),
-            active_instance_in_host(vmid, host)'
-}
-
-start_inspector() {
-    if [[ "$INSPECTOR_TYPE" == "sample" ]] ; then
-        pgrep -f "python inspector.py" && return 0
-        python inspector.py "$INSPECTOR_PORT" > inspector.log 2>&1 &
-    elif [[ "$INSPECTOR_TYPE" == "congress" ]] ; then
-        nova_api_min_version="2.11"
-        nova_api_version=$(openstack congress datasource list | \
-                           grep nova | grep -Po "(?<='api_version': ')[^']*")
-        if ! is_set nova_api_version; then
-            nova_api_version="2.0"
-        fi
-        if [[ "$nova_api_version" < "$nova_api_min_version" ]]; then
-            die $LINENO "Congress Nova datasource API version < $nova_api_min_version ($nova_api_version)"
-        fi
-        openstack congress driver list | grep -q " doctor "
-        openstack congress datasource list | grep -q " doctor " || {
-            openstack congress datasource create doctor doctor
-        }
-        congress_setup_rules
-    fi
-}
-
-stop_inspector() {
-    if [[ "$INSPECTOR_TYPE" == "sample" ]] ; then
-        pgrep -f "python inspector.py" || return 0
-        kill $(pgrep -f "python inspector.py")
-    elif [[ "$INSPECTOR_TYPE" == "congress" ]] ; then
-        congress_del_rule host_force_down classification
-        congress_del_rule error_vm_states classification
-        congress_del_rule active_instance_in_host classification
-        congress_del_rule host_down classification
-    fi
-}
-
 start_consumer() {
     pgrep -f "python consumer.py" && return 0
     python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
@@ -380,6 +302,7 @@ cleanup() {
     openstack user delete "$DOCTOR_USER"
 
     cleanup_installer
+    cleanup_inspector
 }
 
 
@@ -389,6 +312,7 @@ trap cleanup EXIT
 
 source $TOP_DIR/functions-common
 source $TOP_DIR/lib/installer
+source $TOP_DIR/lib/inspector
 
 setup_installer