Patrole devstack plugin

Adds devstack plugin for Patrole in order to correctly deploy
Patrole.

This commit:
  - Adds the plugin.sh for orchestrating Patrole installation
    via devstack
  - The settings file for declaring global variables; allow
    RBAC_TEST_ROLE to be overriden by global variable
    RBAC_TEST_ROLE (i.e. export RBAC_TEST_ROLE=Member
    from shell will override the rbac role at run time)
  - Removes pre/post_test_hook since that logic is now
    handled by updated infra jobs [0] and by
    the devstack patrole plugin.

[0] https://review.openstack.org/#/c/468939/3/jenkins/jobs/patrole.yaml

Change-Id: I38c02cbcfea9334c9c0c10096e383efa9a9fc474
Implements: blueprint patrole-devstack-plugin
diff --git a/contrib/post_test_hook.sh b/contrib/post_test_hook.sh
deleted file mode 100644
index a0353ca..0000000
--- a/contrib/post_test_hook.sh
+++ /dev/null
@@ -1,83 +0,0 @@
-#!/bin/bash -xe
-#
-# Licensed under the Apache License, Version 2.0 (the "License"); you may
-# not use this file except in compliance with the License. You may obtain
-# a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-# License for the specific language governing permissions and limitations
-# under the License.
-
-# This script is executed inside post_test_hook function in devstack gate.
-# First argument ($1) expects 'rbac-role' as value for setting appropriate
-# tempest rbac option 'rbac_test_role'.
-
-# Allow tempest.conf to be modified by Jenkins.
-sudo chown -R jenkins:stack $BASE/new/tempest
-sudo chown -R jenkins:stack $BASE/data/tempest
-
-TEMPEST_CONFIG=$BASE/new/tempest/etc/tempest.conf
-TEMPEST_COMMAND="sudo -H -u tempest tox"
-
-DEVSTACK_GATE_TEMPEST_REGEX="(?!.*\[.*\bslow\b.*\])(^patrole_tempest_plugin\.tests\.api)"
-DEVSTACK_MULTINODE_GATE_TEMPEST_REGEX="(?=.*\[.*\bslow\b.*\])(^patrole_tempest_plugin\.tests\.api)"
-
-# Import devstack function 'iniset'.
-source $BASE/new/devstack/functions
-
-# First argument is expected to contain value equal either to 'admin' or
-# 'member' (both lower-case).
-RBAC_ROLE=$1
-
-if [[ "$RBAC_ROLE" == "member" ]]; then
-    RBAC_ROLE="Member"
-fi
-
-# Second argument is expected to contain value indicating whether the
-# environment is "multinode" or not (empty string).
-TYPE=$2
-
-function set_uuid_tokens() {
-    # Use uuid tokens for faster test runs
-    KEYSTONE_CONF=/etc/keystone/keystone.conf
-    iniset $KEYSTONE_CONF token provider uuid
-    sudo service apache2 restart
-}
-
-function setup_config() {
-    # Set enable_rbac=True under [rbac] section in tempest.conf
-    iniset $TEMPEST_CONFIG rbac enable_rbac True
-    # Set rbac_test_role=$RBAC_ROLE under [rbac] section in tempest.conf
-    iniset $TEMPEST_CONFIG rbac rbac_test_role $RBAC_ROLE
-    # Set strict_policy_check=False under [rbac] section in tempest.conf
-    iniset $TEMPEST_CONFIG rbac strict_policy_check False
-    # Set additional, necessary CONF values
-    iniset $TEMPEST_CONFIG auth tempest_roles Member
-}
-
-function run_tests() {
-    # Give permissions back to Tempest.
-    sudo chown -R tempest:stack $BASE/new/tempest
-    sudo chown -R tempest:stack $BASE/data/tempest
-
-    set -o errexit
-
-    # cd into Tempest directory before executing tox.
-    cd $BASE/new/tempest
-
-    if [[ "$TYPE" == "multinode" ]]; then
-        $TEMPEST_COMMAND -eall-plugin -- $DEVSTACK_MULTINODE_GATE_TEMPEST_REGEX --concurrency=$TEMPEST_CONCURRENCY
-    else
-        $TEMPEST_COMMAND -eall-plugin -- $DEVSTACK_GATE_TEMPEST_REGEX --concurrency=$TEMPEST_CONCURRENCY
-    fi
-
-    sudo -H -u tempest .tox/all-plugin/bin/tempest list-plugins
-}
-
-set_uuid_tokens
-setup_config
-run_tests
diff --git a/contrib/pre_test_hook.sh b/contrib/pre_test_hook.sh
deleted file mode 100755
index bbe05c7..0000000
--- a/contrib/pre_test_hook.sh
+++ /dev/null
@@ -1,18 +0,0 @@
-#!/bin/bash -xe
-#
-# Licensed under the Apache License, Version 2.0 (the "License"); you may
-# not use this file except in compliance with the License. You may obtain
-# a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-# License for the specific language governing permissions and limitations
-# under the License.
-
-# This script is executed inside pre_test_hook function in devstack gate.
-# Installs patrole tempest plugin manually.
-PATROLE_DIR=$BASE/new/patrole
-sudo pip install -e $PATROLE_DIR
diff --git a/devstack/plugin.sh b/devstack/plugin.sh
new file mode 100644
index 0000000..1066136
--- /dev/null
+++ b/devstack/plugin.sh
@@ -0,0 +1,35 @@
+#!/usr/bin/env bash
+# Plugin file for Patrole Tempest plugin
+# --------------------------------------
+
+# Dependencies:
+# ``functions`` file
+# ``DEST`` must be defined
+
+# Save trace setting
+XTRACE=$(set +o | grep xtrace)
+set -o xtrace
+
+function install_patrole_tempest_plugin() {
+    if is_service_enabled tempest; then
+        setup_package $PATROLE_DIR -e
+
+        if [[ "$RBAC_TEST_ROLE" == "member" ]]; then
+            RBAC_TEST_ROLE="Member"
+        fi
+
+        iniset $TEMPEST_CONFIG rbac enable_rbac True
+        iniset $TEMPEST_CONFIG rbac rbac_test_role $RBAC_TEST_ROLE
+        iniset $TEMPEST_CONFIG rbac strict_policy_check False
+    fi
+}
+
+if is_service_enabled tempest; then
+    if [[ "$1" == "stack" && "$2" == "test-config" ]]; then
+        echo_summary "Installing Patrole Tempest plugin"
+        install_patrole_tempest_plugin
+    fi
+fi
+
+# Restore xtrace
+$XTRACE
diff --git a/devstack/settings b/devstack/settings
new file mode 100644
index 0000000..670e878
--- /dev/null
+++ b/devstack/settings
@@ -0,0 +1,8 @@
+# Settings needed for the Patrole Tempest plugin
+# ----------------------------------------------
+
+PATROLE_DIR=$DEST/patrole
+TEMPEST_DIR=$DEST/tempest
+TEMPEST_CONFIG_DIR=${TEMPEST_CONFIG_DIR:-$TEMPEST_DIR/etc}
+TEMPEST_CONFIG=$TEMPEST_CONFIG_DIR/tempest.conf
+RBAC_TEST_ROLE=${RBAC_TEST_ROLE:-admin}
diff --git a/releasenotes/notes/patrole-devstack-plugin-551c9af3325723c9.yaml b/releasenotes/notes/patrole-devstack-plugin-551c9af3325723c9.yaml
new file mode 100644
index 0000000..a0950bd
--- /dev/null
+++ b/releasenotes/notes/patrole-devstack-plugin-551c9af3325723c9.yaml
@@ -0,0 +1,6 @@
+---
+features:
+  - |
+    Add Patrole DevStack plugin, allowing Patrole to be installed using
+    DevStack by adding "enable_plugin patrole" to "local" section of
+    local.conf.