Blame SOURCES/bz1476009-fence_azure_arm-new-fence-agent.patch

4822a5
diff -uNr a/configure.ac b/configure.ac
4822a5
--- a/configure.ac	2017-10-05 13:09:31.369561411 +0200
4822a5
+++ b/configure.ac	2017-10-05 13:16:02.860680521 +0200
4822a5
@@ -268,6 +268,7 @@
4822a5
 		 fence/agents/amt/Makefile
4822a5
 		 fence/agents/amt_ws/Makefile
4822a5
 		 fence/agents/aws/Makefile
4822a5
+		 fence/agents/azure_arm/Makefile
4822a5
 		 fence/agents/bladecenter/Makefile
4822a5
 		 fence/agents/brocade/Makefile
4822a5
 		 fence/agents/cisco_mds/Makefile
4822a5
diff -uNr a/fence/agents/azure_arm/fence_azure_arm.py b/fence/agents/azure_arm/fence_azure_arm.py
4822a5
--- a/fence/agents/azure_arm/fence_azure_arm.py	1970-01-01 01:00:00.000000000 +0100
4822a5
+++ b/fence/agents/azure_arm/fence_azure_arm.py	2017-10-05 13:14:46.755434886 +0200
4822a5
@@ -0,0 +1,149 @@
4822a5
+#!/usr/bin/python -tt
4822a5
+
4822a5
+import sys, re, pexpect
4822a5
+import logging
4822a5
+import atexit
4822a5
+sys.path.append("/usr/share/fence")
4822a5
+from fencing import *
4822a5
+from fencing import fail, fail_usage, EC_TIMED_OUT, run_delay
4822a5
+
4822a5
+#BEGIN_VERSION_GENERATION
4822a5
+RELEASE_VERSION="4.0.25.34-695e-dirty"
4822a5
+BUILD_DATE="(built Wed Jun 28 08:13:44 UTC 2017)"
4822a5
+REDHAT_COPYRIGHT="Copyright (C) Red Hat, Inc. 2004-2010 All rights reserved."
4822a5
+#END_VERSION_GENERATION
4822a5
+
4822a5
+def get_nodes_list(compute_client, options):
4822a5
+    result = {}
4822a5
+    if compute_client:
4822a5
+        rgName = options["--resourceGroup"]
4822a5
+        vms = compute_client.virtual_machines.list(rgName)
4822a5
+        try:
4822a5
+            for vm in vms:
4822a5
+                result[vm.name] = ("", None)
4822a5
+        except Exception as e:
4822a5
+            fail_usage("Failed: %s" % e)
4822a5
+
4822a5
+    return result
4822a5
+
4822a5
+def get_power_status(compute_client, options):
4822a5
+    logging.info("getting power status for VM " + options["--plug"])
4822a5
+
4822a5
+    if compute_client:
4822a5
+        rgName = options["--resourceGroup"]
4822a5
+        vmName = options["--plug"]
4822a5
+
4822a5
+        powerState = "unknown"
4822a5
+        try:
4822a5
+            vmStatus = compute_client.virtual_machines.get(rgName, vmName, "instanceView")
4822a5
+        except Exception as e:
4822a5
+            fail_usage("Failed: %s" % e)
4822a5
+        for status in vmStatus.instance_view.statuses:
4822a5
+            if status.code.startswith("PowerState"):
4822a5
+                powerState = status.code
4822a5
+                break
4822a5
+
4822a5
+        logging.info("Found power state of VM: " + powerState)
4822a5
+        if powerState == "PowerState/running":
4822a5
+            return "on"
4822a5
+
4822a5
+    return "off"
4822a5
+
4822a5
+def set_power_status(compute_client, options):
4822a5
+    logging.info("setting power status for VM " + options["--plug"] + " to " + options["--action"])
4822a5
+
4822a5
+    if compute_client:
4822a5
+        rgName = options["--resourceGroup"]
4822a5
+        vmName = options["--plug"]
4822a5
+
4822a5
+        if (options["--action"]=="off"):
4822a5
+            logging.info("Deallocating " + vmName + "in resource group " + rgName)
4822a5
+            compute_client.virtual_machines.deallocate(rgName, vmName)
4822a5
+        elif (options["--action"]=="on"):
4822a5
+            logging.info("Starting " + vmName + "in resource group " + rgName)
4822a5
+            compute_client.virtual_machines.start(rgName, vmName)
4822a5
+
4822a5
+
4822a5
+def define_new_opts():
4822a5
+    all_opt["resourceGroup"] = {
4822a5
+        "getopt" : ":",
4822a5
+        "longopt" : "resourceGroup",
4822a5
+        "help" : "--resourceGroup=[name]         Name of the resource group",
4822a5
+        "shortdesc" : "Name of resource group.",
4822a5
+        "required" : "1",
4822a5
+        "order" : 2
4822a5
+    }
4822a5
+    all_opt["tenantId"] = {
4822a5
+        "getopt" : ":",
4822a5
+        "longopt" : "tenantId",
4822a5
+        "help" : "--tenantId=[name]              Id of the Azure Active Directory tenant",
4822a5
+        "shortdesc" : "Id of Azure Active Directory tenant.",
4822a5
+        "required" : "1",
4822a5
+        "order" : 3
4822a5
+    }
4822a5
+    all_opt["subscriptionId"] = {
4822a5
+        "getopt" : ":",
4822a5
+        "longopt" : "subscriptionId",
4822a5
+        "help" : "--subscriptionId=[name]        Id of the Azure subscription",
4822a5
+        "shortdesc" : "Id of the Azure subscription.",
4822a5
+        "required" : "1",
4822a5
+        "order" : 4
4822a5
+    }
4822a5
+
4822a5
+# Main agent method
4822a5
+def main():
4822a5
+    compute_client = None
4822a5
+
4822a5
+    device_opt = ["resourceGroup", "login", "passwd", "tenantId", "subscriptionId","port"]
4822a5
+
4822a5
+    atexit.register(atexit_handler)
4822a5
+
4822a5
+    define_new_opts()
4822a5
+
4822a5
+    all_opt["power_timeout"]["default"] = "150"
4822a5
+
4822a5
+    all_opt["login"]["help"] = "-l, --username=[appid]         Application ID"
4822a5
+    all_opt["passwd"]["help"] = "-p, --password=[authkey]       Authentication key"
4822a5
+
4822a5
+    options = check_input(device_opt, process_input(device_opt))
4822a5
+
4822a5
+    docs = {}
4822a5
+    docs["shortdesc"] = "Fence agent for Azure Resource Manager"
4822a5
+    docs["longdesc"] = "Used to deallocate virtual machines and to report power state of virtual machines running in Azure. It uses Azure SDK for Python to connect to Azure.\
4822a5
+\n.P\n\
4822a5
+For instructions to setup credentials see: https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal\
4822a5
+\n.P\n\
4822a5
+Username and password are application ID and authentication key from \"App registrations\"."
4822a5
+    docs["vendorurl"] = "http://www.microsoft.com"
4822a5
+    show_docs(options, docs)
4822a5
+
4822a5
+    run_delay(options)
4822a5
+
4822a5
+    try:
4822a5
+        from azure.common.credentials import ServicePrincipalCredentials
4822a5
+        from azure.mgmt.compute import ComputeManagementClient
4822a5
+
4822a5
+        tenantid = options["--tenantId"]
4822a5
+        servicePrincipal = options["--username"]
4822a5
+        spPassword = options["--password"]
4822a5
+        subscriptionId = options["--subscriptionId"]
4822a5
+        credentials = ServicePrincipalCredentials(
4822a5
+            client_id = servicePrincipal,
4822a5
+            secret = spPassword,
4822a5
+            tenant = tenantid
4822a5
+        )
4822a5
+        compute_client = ComputeManagementClient(
4822a5
+            credentials,
4822a5
+            subscriptionId
4822a5
+        )
4822a5
+    except ImportError:
4822a5
+        fail_usage("Azure Resource Manager Python SDK not found or not accessible")
4822a5
+    except Exception as e:
4822a5
+        fail_usage("Failed: %s" % re.sub("^, ", "", str(e)))
4822a5
+
4822a5
+    # Operate the fencing device
4822a5
+    result = fence_action(compute_client, options, set_power_status, get_power_status, get_nodes_list)
4822a5
+    sys.exit(result)
4822a5
+
4822a5
+if __name__ == "__main__":
4822a5
+    main()
4822a5
diff -uNr a/fence/agents/azure_arm/Makefile.am b/fence/agents/azure_arm/Makefile.am
4822a5
--- a/fence/agents/azure_arm/Makefile.am	1970-01-01 01:00:00.000000000 +0100
4822a5
+++ b/fence/agents/azure_arm/Makefile.am	2017-10-05 13:55:41.206064062 +0200
4822a5
@@ -0,0 +1,17 @@
4822a5
+MAINTAINERCLEANFILES	= Makefile.in
4822a5
+
4822a5
+TARGET			= fence_azure_arm
4822a5
+
4822a5
+SRC			= $(TARGET).py
4822a5
+
4822a5
+EXTRA_DIST		= $(SRC)
4822a5
+
4822a5
+sbin_SCRIPTS		= $(TARGET)
4822a5
+
4822a5
+man_MANS		= $(TARGET).8
4822a5
+
4822a5
+FENCE_TEST_ARGS		= -l test -p test -n 1
4822a5
+
4822a5
+include $(top_srcdir)/make/fencebuild.mk
4822a5
+include $(top_srcdir)/make/fenceman.mk
4822a5
+include $(top_srcdir)/make/agentpycheck.mk
4822a5
diff -uNr a/tests/data/metadata/fence_azure_arm.xml b/tests/data/metadata/fence_azure_arm.xml
4822a5
--- a/tests/data/metadata/fence_azure_arm.xml	1970-01-01 01:00:00.000000000 +0100
4822a5
+++ b/tests/data/metadata/fence_azure_arm.xml	2017-10-05 13:18:35.373168796 +0200
4822a5
@@ -0,0 +1,142 @@
4822a5
+
4822a5
+<resource-agent name="fence_azure_arm" shortdesc="Fence agent for Azure Resource Manager" >
4822a5
+<longdesc>Used to deallocate virtual machines and to report power state of virtual machines running in Azure. It uses Azure SDK for Python to connect to Azure.
4822a5
+.P
4822a5
+For instructions to setup credentials see: https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal
4822a5
+.P
4822a5
+Username and password are application ID and authentication key from "App registrations".</longdesc>
4822a5
+<vendor-url>http://www.microsoft.com</vendor-url>
4822a5
+<parameters>
4822a5
+	<parameter name="passwd" unique="0" required="0" deprecated="1">
4822a5
+		<getopt mixed="-p, --password=[authkey]" />
4822a5
+		<content type="string"  />
4822a5
+		<shortdesc lang="en">Login password or passphrase</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="action" unique="0" required="1">
4822a5
+		<getopt mixed="-o, --action=[action]" />
4822a5
+		<content type="string" default="reboot"  />
4822a5
+		<shortdesc lang="en">Fencing Action</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="passwd_script" unique="0" required="0" deprecated="1">
4822a5
+		<getopt mixed="-S, --password-script=[script]" />
4822a5
+		<content type="string"  />
4822a5
+		<shortdesc lang="en">Script to retrieve password</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="login" unique="0" required="1" deprecated="1">
4822a5
+		<getopt mixed="-l, --username=[appid]" />
4822a5
+		<content type="string"  />
4822a5
+		<shortdesc lang="en">Login Name</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="port" unique="0" required="1" deprecated="1">
4822a5
+		<getopt mixed="-n, --plug=[id]" />
4822a5
+		<content type="string"  />
4822a5
+		<shortdesc lang="en">Physical plug number, name of virtual machine or UUID</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="username" unique="0" required="1" obsoletes="login">
4822a5
+		<getopt mixed="-l, --username=[appid]" />
4822a5
+		<content type="string"  />
4822a5
+		<shortdesc lang="en">Login Name</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="password" unique="0" required="0" obsoletes="passwd">
4822a5
+		<getopt mixed="-p, --password=[authkey]" />
4822a5
+		<content type="string"  />
4822a5
+		<shortdesc lang="en">Login password or passphrase</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="password_script" unique="0" required="0" obsoletes="passwd_script">
4822a5
+		<getopt mixed="-S, --password-script=[script]" />
4822a5
+		<content type="string"  />
4822a5
+		<shortdesc lang="en">Script to retrieve password</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="plug" unique="0" required="1" obsoletes="port">
4822a5
+		<getopt mixed="-n, --plug=[id]" />
4822a5
+		<content type="string"  />
4822a5
+		<shortdesc lang="en">Physical plug number, name of virtual machine or UUID</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="resourceGroup" unique="0" required="1">
4822a5
+		<getopt mixed="--resourceGroup=[name]" />
4822a5
+		<content type="string"  />
4822a5
+		<shortdesc lang="en">Name of resource group.</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="tenantId" unique="0" required="1">
4822a5
+		<getopt mixed="--tenantId=[name]" />
4822a5
+		<content type="string"  />
4822a5
+		<shortdesc lang="en">Id of Azure Active Directory tenant.</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="subscriptionId" unique="0" required="1">
4822a5
+		<getopt mixed="--subscriptionId=[name]" />
4822a5
+		<content type="string"  />
4822a5
+		<shortdesc lang="en">Id of the Azure subscription.</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="verbose" unique="0" required="0">
4822a5
+		<getopt mixed="-v, --verbose" />
4822a5
+		<content type="boolean"  />
4822a5
+		<shortdesc lang="en">Verbose mode</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="debug" unique="0" required="0" deprecated="1">
4822a5
+		<getopt mixed="-D, --debug-file=[debugfile]" />
4822a5
+		<content type="string"  />
4822a5
+		<shortdesc lang="en">Write debug information to given file</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="debug_file" unique="0" required="0" obsoletes="debug">
4822a5
+		<getopt mixed="-D, --debug-file=[debugfile]" />
4822a5
+		<content type="string"  />
4822a5
+		<shortdesc lang="en">Write debug information to given file</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="version" unique="0" required="0">
4822a5
+		<getopt mixed="-V, --version" />
4822a5
+		<content type="boolean"  />
4822a5
+		<shortdesc lang="en">Display version information and exit</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="help" unique="0" required="0">
4822a5
+		<getopt mixed="-h, --help" />
4822a5
+		<content type="boolean"  />
4822a5
+		<shortdesc lang="en">Display help and exit</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="separator" unique="0" required="0">
4822a5
+		<getopt mixed="-C, --separator=[char]" />
4822a5
+		<content type="string" default=","  />
4822a5
+		<shortdesc lang="en">Separator for CSV created by operation list</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="delay" unique="0" required="0">
4822a5
+		<getopt mixed="--delay=[seconds]" />
4822a5
+		<content type="second" default="0"  />
4822a5
+		<shortdesc lang="en">Wait X seconds before fencing is started</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="shell_timeout" unique="0" required="0">
4822a5
+		<getopt mixed="--shell-timeout=[seconds]" />
4822a5
+		<content type="second" default="3"  />
4822a5
+		<shortdesc lang="en">Wait X seconds for cmd prompt after issuing command</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="power_wait" unique="0" required="0">
4822a5
+		<getopt mixed="--power-wait=[seconds]" />
4822a5
+		<content type="second" default="0"  />
4822a5
+		<shortdesc lang="en">Wait X seconds after issuing ON/OFF</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="power_timeout" unique="0" required="0">
4822a5
+		<getopt mixed="--power-timeout=[seconds]" />
4822a5
+		<content type="second" default="150"  />
4822a5
+		<shortdesc lang="en">Test X seconds for status change after ON/OFF</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="login_timeout" unique="0" required="0">
4822a5
+		<getopt mixed="--login-timeout=[seconds]" />
4822a5
+		<content type="second" default="5"  />
4822a5
+		<shortdesc lang="en">Wait X seconds for cmd prompt after login</shortdesc>
4822a5
+	</parameter>
4822a5
+	<parameter name="retry_on" unique="0" required="0">
4822a5
+		<getopt mixed="--retry-on=[attempts]" />
4822a5
+		<content type="integer" default="1"  />
4822a5
+		<shortdesc lang="en">Count of attempts to retry power on</shortdesc>
4822a5
+	</parameter>
4822a5
+</parameters>
4822a5
+<actions>
4822a5
+	<action name="on" automatic="0"/>
4822a5
+	<action name="off" />
4822a5
+	<action name="reboot" />
4822a5
+	<action name="status" />
4822a5
+	<action name="list" />
4822a5
+	<action name="list-status" />
4822a5
+	<action name="monitor" />
4822a5
+	<action name="metadata" />
4822a5
+	<action name="validate-all" />
4822a5
+</actions>
4822a5
+</resource-agent>