Blame SOURCES/bz1296201-fence_amt_ws-new-fence-agent.patch

035a21
diff -uNr a/configure.ac b/configure.ac
035a21
--- a/configure.ac	2017-10-05 14:14:39.688675727 +0200
035a21
+++ b/configure.ac	2017-10-05 14:15:17.964291884 +0200
035a21
@@ -265,7 +265,8 @@
035a21
 		 fence/agents/alom/Makefile
035a21
 		 fence/agents/apc/Makefile
035a21
 		 fence/agents/apc_snmp/Makefile
035a21
-		 fence/agents/amt/Makefile 
035a21
+		 fence/agents/amt/Makefile
035a21
+		 fence/agents/amt_ws/Makefile
035a21
 		 fence/agents/bladecenter/Makefile
035a21
 		 fence/agents/brocade/Makefile
035a21
 		 fence/agents/cisco_mds/Makefile
035a21
diff -uNr a/fence/agents/amt_ws/fence_amt_ws.py b/fence/agents/amt_ws/fence_amt_ws.py
035a21
--- a/fence/agents/amt_ws/fence_amt_ws.py	1970-01-01 01:00:00.000000000 +0100
035a21
+++ b/fence/agents/amt_ws/fence_amt_ws.py	2017-10-05 14:15:17.965291874 +0200
035a21
@@ -0,0 +1,243 @@
035a21
+#!/usr/bin/python -tt
035a21
+
035a21
+#
035a21
+# Fence agent for Intel AMT (WS) based on code from the openstack/ironic project:
035a21
+# https://github.com/openstack/ironic/blob/master/ironic/drivers/modules/amt/power.py
035a21
+#
035a21
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
035a21
+# not use this file except in compliance with the License. You may obtain
035a21
+# a copy of the License at
035a21
+#
035a21
+#      http://www.apache.org/licenses/LICENSE-2.0
035a21
+#
035a21
+# Unless required by applicable law or agreed to in writing, software
035a21
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
035a21
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
035a21
+# License for the specific language governing permissions and limitations
035a21
+# under the License.
035a21
+#
035a21
+
035a21
+import sys
035a21
+import atexit
035a21
+import logging
035a21
+sys.path.append("@FENCEAGENTSLIBDIR@")
035a21
+from fencing import *
035a21
+from fencing import run_delay, fail_usage, fail, EC_STATUS
035a21
+
035a21
+import pywsman
035a21
+from xml.etree import ElementTree
035a21
+
035a21
+
035a21
+#BEGIN_VERSION_GENERATION
035a21
+RELEASE_VERSION="Fence agent for Intel AMT (WS)"
035a21
+REDHAT_COPYRIGHT=""
035a21
+BUILD_DATE=""
035a21
+#END_VERSION_GENERATION
035a21
+
035a21
+POWER_ON='2'
035a21
+POWER_OFF='8'
035a21
+POWER_CYCLE='10'
035a21
+
035a21
+RET_SUCCESS = '0'
035a21
+
035a21
+CIM_PowerManagementService           = ('http://schemas.dmtf.org/wbem/wscim/1/'
035a21
+                                        'cim-schema/2/CIM_PowerManagementService')
035a21
+CIM_ComputerSystem                   = ('http://schemas.dmtf.org/wbem/wscim/'
035a21
+                                        '1/cim-schema/2/CIM_ComputerSystem')
035a21
+CIM_AssociatedPowerManagementService = ('http://schemas.dmtf.org/wbem/wscim/'
035a21
+                                        '1/cim-schema/2/'
035a21
+                                        'CIM_AssociatedPowerManagementService')
035a21
+
035a21
+CIM_BootConfigSetting                = ('http://schemas.dmtf.org/wbem/wscim/'
035a21
+                                        '1/cim-schema/2/CIM_BootConfigSetting')
035a21
+CIM_BootSourceSetting                = ('http://schemas.dmtf.org/wbem/wscim/'
035a21
+                                        '1/cim-schema/2/CIM_BootSourceSetting')
035a21
+
035a21
+
035a21
+def xml_find(doc, namespace, item):
035a21
+    if doc is None:
035a21
+        return
035a21
+    tree = ElementTree.fromstring(doc.root().string())
035a21
+    query = ('.//{%(namespace)s}%(item)s' % {'namespace': namespace,
035a21
+                                             'item': item})
035a21
+    return tree.find(query)
035a21
+
035a21
+def _generate_power_action_input(action):
035a21
+    method_input = "RequestPowerStateChange_INPUT"
035a21
+    address = 'http://schemas.xmlsoap.org/ws/2004/08/addressing'
035a21
+    anonymous = ('http://schemas.xmlsoap.org/ws/2004/08/addressing/'
035a21
+                 'role/anonymous')
035a21
+    wsman = 'http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd'
035a21
+    namespace = CIM_PowerManagementService
035a21
+
035a21
+    doc = pywsman.XmlDoc(method_input)
035a21
+    root = doc.root()
035a21
+    root.set_ns(namespace)
035a21
+    root.add(namespace, 'PowerState', action)
035a21
+
035a21
+    child = root.add(namespace, 'ManagedElement', None)
035a21
+    child.add(address, 'Address', anonymous)
035a21
+
035a21
+    grand_child = child.add(address, 'ReferenceParameters', None)
035a21
+    grand_child.add(wsman, 'ResourceURI', CIM_ComputerSystem)
035a21
+
035a21
+    g_grand_child = grand_child.add(wsman, 'SelectorSet', None)
035a21
+    g_g_grand_child = g_grand_child.add(wsman, 'Selector', 'ManagedSystem')
035a21
+    g_g_grand_child.attr_add(wsman, 'Name', 'Name')
035a21
+    return doc
035a21
+
035a21
+def get_power_status(_, options):
035a21
+    client = pywsman.Client(options["--ip"], int(options["--ipport"]), \
035a21
+                            '/wsman', 'http', 'admin', options["--password"])
035a21
+    namespace = CIM_AssociatedPowerManagementService
035a21
+    client_options = pywsman.ClientOptions()
035a21
+    doc = client.get(client_options, namespace)
035a21
+    _SOAP_ENVELOPE = 'http://www.w3.org/2003/05/soap-envelope'
035a21
+    item = 'Fault'
035a21
+    fault = xml_find(doc, _SOAP_ENVELOPE, item)
035a21
+    if fault is not None:
035a21
+        logging.error("Failed to get power state for: %s port:%s", \
035a21
+                      options["--ip"], options["--ipport"])
035a21
+        fail(EC_STATUS)
035a21
+
035a21
+    item = "PowerState"
035a21
+    try: power_state = xml_find(doc, namespace, item).text
035a21
+    except AttributeError:
035a21
+        logging.error("Failed to get power state for: %s port:%s", \
035a21
+                      options["--ip"], options["--ipport"])
035a21
+        fail(EC_STATUS)
035a21
+    if power_state == POWER_ON:
035a21
+        return "on"
035a21
+    elif power_state == POWER_OFF:
035a21
+        return "off"
035a21
+    else:
035a21
+        fail(EC_STATUS)
035a21
+
035a21
+def set_power_status(_, options):
035a21
+    client = pywsman.Client(options["--ip"], int(options["--ipport"]), \
035a21
+                            '/wsman', 'http', 'admin', options["--password"])
035a21
+
035a21
+    method = 'RequestPowerStateChange'
035a21
+    client_options = pywsman.ClientOptions()
035a21
+    client_options.add_selector('Name', 'Intel(r) AMT Power Management Service')
035a21
+
035a21
+    if options["--action"] == "on":
035a21
+        target_state = POWER_ON
035a21
+    elif options["--action"] == "off":
035a21
+        target_state = POWER_OFF
035a21
+    elif options["--action"] == "reboot":
035a21
+        target_state = POWER_CYCLE
035a21
+    if options["--action"] in ["on", "off", "reboot"] \
035a21
+       and options.has_key("--boot-option"):
035a21
+        set_boot_order(_, client, options)
035a21
+
035a21
+    doc = _generate_power_action_input(target_state)
035a21
+    client_doc = client.invoke(client_options, CIM_PowerManagementService, \
035a21
+                               method, doc)
035a21
+    item = "ReturnValue"
035a21
+    return_value = xml_find(client_doc, CIM_PowerManagementService, item).text
035a21
+    if return_value != RET_SUCCESS:
035a21
+        logging.error("Failed to set power state: %s for: %s", \
035a21
+                      options["--action"], options["--ip"])
035a21
+        fail(EC_STATUS)
035a21
+
035a21
+def set_boot_order(_, client, options):
035a21
+    method_input = "ChangeBootOrder_INPUT"
035a21
+    address = 'http://schemas.xmlsoap.org/ws/2004/08/addressing'
035a21
+    anonymous = ('http://schemas.xmlsoap.org/ws/2004/08/addressing/'
035a21
+                 'role/anonymous')
035a21
+    wsman = 'http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd'
035a21
+    namespace = CIM_BootConfigSetting
035a21
+
035a21
+    if options["--boot-option"] == "pxe":
035a21
+        device = "Intel(r) AMT: Force PXE Boot"
035a21
+    elif options["--boot-option"] in ["hd", "hdsafe"]:
035a21
+        device = "Intel(r) AMT: Force Hard-drive Boot"
035a21
+    elif options["--boot-option"] == "cd":
035a21
+        device = "Intel(r) AMT: Force CD/DVD Boot"
035a21
+    elif options["--boot-option"] == "diag":
035a21
+        device = "Intel(r) AMT: Force Diagnostic Boot"
035a21
+    else:
035a21
+        logging.error('Boot device: %s not supported.', \
035a21
+                      options["--boot-option"])
035a21
+        return
035a21
+
035a21
+    method = 'ChangeBootOrder'
035a21
+    client_options = pywsman.ClientOptions()
035a21
+    client_options.add_selector('InstanceID', \
035a21
+                                'Intel(r) AMT: Boot Configuration 0')
035a21
+
035a21
+    doc = pywsman.XmlDoc(method_input)
035a21
+    root = doc.root()
035a21
+    root.set_ns(namespace)
035a21
+
035a21
+    child = root.add(namespace, 'Source', None)
035a21
+    child.add(address, 'Address', anonymous)
035a21
+
035a21
+    grand_child = child.add(address, 'ReferenceParameters', None)
035a21
+    grand_child.add(wsman, 'ResourceURI', CIM_BootSourceSetting)
035a21
+
035a21
+    g_grand_child = grand_child.add(wsman, 'SelectorSet', None)
035a21
+    g_g_grand_child = g_grand_child.add(wsman, 'Selector', device)
035a21
+    g_g_grand_child.attr_add(wsman, 'Name', 'InstanceID')
035a21
+    if options["--boot-option"] == "hdsafe":
035a21
+        g_g_grand_child = g_grand_child.add(wsman, 'Selector', 'True')
035a21
+        g_g_grand_child.attr_add(wsman, 'Name', 'UseSafeMode')
035a21
+
035a21
+    client_doc = client.invoke(client_options, CIM_BootConfigSetting, \
035a21
+                               method, doc)
035a21
+    item = "ReturnValue"
035a21
+    return_value = xml_find(client_doc, CIM_BootConfigSetting, item).text
035a21
+    if return_value != RET_SUCCESS:
035a21
+        logging.error("Failed to set boot device to: %s for: %s", \
035a21
+                      options["--boot-option"], options["--ip"])
035a21
+        fail(EC_STATUS)
035a21
+
035a21
+def reboot_cycle(_, options):
035a21
+    status = set_power_status(_, options)
035a21
+    return not bool(status)
035a21
+
035a21
+def define_new_opts():
035a21
+    all_opt["boot_option"] = {
035a21
+        "getopt" : "b:",
035a21
+        "longopt" : "boot-option",
035a21
+        "help" : "-b, --boot-option=[option]     "
035a21
+                "Change the default boot behavior of the\n"
035a21
+                "                                  machine."
035a21
+                " (pxe|hd|hdsafe|cd|diag)",
035a21
+        "required" : "0",
035a21
+        "shortdesc" : "Change the default boot behavior of the machine.",
035a21
+        "choices" : ["pxe", "hd", "hdsafe", "cd", "diag"],
035a21
+        "order" : 1
035a21
+    }
035a21
+
035a21
+def main():
035a21
+    atexit.register(atexit_handler)
035a21
+
035a21
+    device_opt = ["ipaddr", "no_login", "passwd", "boot_option", "no_port",
035a21
+                  "method"]
035a21
+
035a21
+    define_new_opts()
035a21
+
035a21
+    all_opt["ipport"]["default"] = "16992"
035a21
+
035a21
+    options = check_input(device_opt, process_input(device_opt))
035a21
+
035a21
+    docs = {}
035a21
+    docs["shortdesc"] = "Fence agent for AMT (WS)"
035a21
+    docs["longdesc"] = "fence_amt_ws is an I/O Fencing agent \
035a21
+which can be used with Intel AMT (WS). This agent requires \
035a21
+the pywsman Python library which is included in OpenWSMAN. \
035a21
+(http://openwsman.github.io/)."
035a21
+    docs["vendorurl"] = "http://www.intel.com/"
035a21
+    show_docs(options, docs)
035a21
+
035a21
+    run_delay(options)
035a21
+
035a21
+    result = fence_action(None, options, set_power_status, get_power_status, \
035a21
+                          None, reboot_cycle)
035a21
+
035a21
+    sys.exit(result)
035a21
+
035a21
+if __name__ == "__main__":
035a21
+    main()
035a21
diff -uNr a/fence/agents/amt_ws/Makefile.am b/fence/agents/amt_ws/Makefile.am
035a21
--- a/fence/agents/amt_ws/Makefile.am	1970-01-01 01:00:00.000000000 +0100
035a21
+++ b/fence/agents/amt_ws/Makefile.am	2017-10-05 14:15:17.965291874 +0200
035a21
@@ -0,0 +1,17 @@
035a21
+MAINTAINERCLEANFILES	= Makefile.in
035a21
+
035a21
+TARGET			= fence_amt_ws
035a21
+
035a21
+SRC			= $(TARGET).py
035a21
+
035a21
+EXTRA_DIST		= $(SRC)
035a21
+
035a21
+sbin_SCRIPTS		= $(TARGET)
035a21
+
035a21
+man_MANS		= $(TARGET).8
035a21
+
035a21
+FENCE_TEST_ARGS         = -p test -a test
035a21
+
035a21
+include $(top_srcdir)/make/fencebuild.mk
035a21
+include $(top_srcdir)/make/fenceman.mk
035a21
+include $(top_srcdir)/make/agentpycheck.mk
035a21
diff -uNr a/tests/data/metadata/fence_amt_ws.xml b/tests/data/metadata/fence_amt_ws.xml
035a21
--- a/tests/data/metadata/fence_amt_ws.xml	1970-01-01 01:00:00.000000000 +0100
035a21
+++ b/tests/data/metadata/fence_amt_ws.xml	2017-10-05 14:10:49.145987710 +0200
035a21
@@ -0,0 +1,155 @@
035a21
+
035a21
+<resource-agent name="fence_amt_ws" shortdesc="Fence agent for AMT (WS)" >
035a21
+<longdesc>fence_amt_ws is an I/O Fencing agent which can be used with Intel AMT (WS). This agent requires the pywsman Python library which is included in OpenWSMAN. (http://openwsman.github.io/).</longdesc>
035a21
+<vendor-url>http://www.intel.com/</vendor-url>
035a21
+<parameters>
035a21
+	<parameter name="ipport" unique="0" required="0">
035a21
+		<getopt mixed="-u, --ipport=[port]" />
035a21
+		<content type="integer" default="16992"  />
035a21
+		<shortdesc lang="en">TCP/UDP port to use for connection with device</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="port" unique="0" required="0" deprecated="1">
035a21
+		<getopt mixed="-n, --plug=[ip]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">IP address or hostname of fencing device (together with --port-as-ip)</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="inet6_only" unique="0" required="0">
035a21
+		<getopt mixed="-6, --inet6-only" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">Forces agent to use IPv6 addresses only</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="ipaddr" unique="0" required="0" deprecated="1">
035a21
+		<getopt mixed="-a, --ip=[ip]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">IP Address or Hostname</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="inet4_only" unique="0" required="0">
035a21
+		<getopt mixed="-4, --inet4-only" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">Forces agent to use IPv4 addresses only</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="method" unique="0" required="0">
035a21
+		<getopt mixed="-m, --method=[method]" />
035a21
+		<content type="select" default="onoff"  >
035a21
+			<option value="onoff" />
035a21
+			<option value="cycle" />
035a21
+		</content>
035a21
+		<shortdesc lang="en">Method to fence (onoff|cycle)</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="passwd_script" unique="0" required="0" deprecated="1">
035a21
+		<getopt mixed="-S, --password-script=[script]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Script to retrieve password</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="passwd" unique="0" required="0" deprecated="1">
035a21
+		<getopt mixed="-p, --password=[password]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Login password or passphrase</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="boot_option" unique="0" required="0">
035a21
+		<getopt mixed="-b, --boot-option=[option]" />
035a21
+		<content type="select"  >
035a21
+			<option value="pxe" />
035a21
+			<option value="hd" />
035a21
+			<option value="hdsafe" />
035a21
+			<option value="cd" />
035a21
+			<option value="diag" />
035a21
+		</content>
035a21
+		<shortdesc lang="en">Change the default boot behavior of the machine.</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="action" unique="0" required="1">
035a21
+		<getopt mixed="-o, --action=[action]" />
035a21
+		<content type="string" default="reboot"  />
035a21
+		<shortdesc lang="en">Fencing Action</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="plug" unique="0" required="0" obsoletes="port">
035a21
+		<getopt mixed="-n, --plug=[ip]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">IP address or hostname of fencing device (together with --port-as-ip)</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="ip" unique="0" required="0" obsoletes="ipaddr">
035a21
+		<getopt mixed="-a, --ip=[ip]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">IP Address or Hostname</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="password" unique="0" required="0" obsoletes="passwd">
035a21
+		<getopt mixed="-p, --password=[password]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Login password or passphrase</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="password_script" unique="0" required="0" obsoletes="passwd_script">
035a21
+		<getopt mixed="-S, --password-script=[script]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Script to retrieve password</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="verbose" unique="0" required="0">
035a21
+		<getopt mixed="-v, --verbose" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">Verbose mode</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="debug" unique="0" required="0" deprecated="1">
035a21
+		<getopt mixed="-D, --debug-file=[debugfile]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Write debug information to given file</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="debug_file" unique="0" required="0" obsoletes="debug">
035a21
+		<getopt mixed="-D, --debug-file=[debugfile]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Write debug information to given file</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="version" unique="0" required="0">
035a21
+		<getopt mixed="-V, --version" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">Display version information and exit</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="help" unique="0" required="0">
035a21
+		<getopt mixed="-h, --help" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">Display help and exit</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="power_wait" unique="0" required="0">
035a21
+		<getopt mixed="--power-wait=[seconds]" />
035a21
+		<content type="second" default="0"  />
035a21
+		<shortdesc lang="en">Wait X seconds after issuing ON/OFF</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="login_timeout" unique="0" required="0">
035a21
+		<getopt mixed="--login-timeout=[seconds]" />
035a21
+		<content type="second" default="5"  />
035a21
+		<shortdesc lang="en">Wait X seconds for cmd prompt after login</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="power_timeout" unique="0" required="0">
035a21
+		<getopt mixed="--power-timeout=[seconds]" />
035a21
+		<content type="second" default="20"  />
035a21
+		<shortdesc lang="en">Test X seconds for status change after ON/OFF</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="delay" unique="0" required="0">
035a21
+		<getopt mixed="--delay=[seconds]" />
035a21
+		<content type="second" default="0"  />
035a21
+		<shortdesc lang="en">Wait X seconds before fencing is started</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="shell_timeout" unique="0" required="0">
035a21
+		<getopt mixed="--shell-timeout=[seconds]" />
035a21
+		<content type="second" default="3"  />
035a21
+		<shortdesc lang="en">Wait X seconds for cmd prompt after issuing command</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="port_as_ip" unique="0" required="0">
035a21
+		<getopt mixed="--port-as-ip" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">Make "port/plug" to be an alias to IP address</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="retry_on" unique="0" required="0">
035a21
+		<getopt mixed="--retry-on=[attempts]" />
035a21
+		<content type="integer" default="1"  />
035a21
+		<shortdesc lang="en">Count of attempts to retry power on</shortdesc>
035a21
+	</parameter>
035a21
+</parameters>
035a21
+<actions>
035a21
+	<action name="on" automatic="0"/>
035a21
+	<action name="off" />
035a21
+	<action name="reboot" />
035a21
+	<action name="status" />
035a21
+	<action name="monitor" />
035a21
+	<action name="metadata" />
035a21
+	<action name="validate-all" />
035a21
+</actions>
035a21
+</resource-agent>