File 0002-fence_azure_arm-add-fencing-agent-for-Azure-Resource-Manager.patch of Package fence-agents.4605

commit 5dbf45e6ef73e2e0e2385ada8e82693d5c8c3a64
Author: Sebastian Dusch <sedusch@microsoft.com>
Date:   Mon Nov 7 15:34:30 2016 +0100

    fence_azure_arm: add fencing agent for Azure Resource Manager

diff --git a/fence/agents/azure_arm/Makefile.am b/fence/agents/azure_arm/Makefile.am
new file mode 100644
index 0000000..e61ed89
--- /dev/null
+++ b/fence/agents/azure_arm/Makefile.am
@@ -0,0 +1,17 @@
+MAINTAINERCLEANFILES= Makefile.in
+
+TARGET= fence_azure_arm
+
+SRC= $(TARGET).py
+
+EXTRA_DIST= $(SRC)
+
+sbin_SCRIPTS= $(TARGET)
+
+man_MANS= fence_azure_arm.8
+
+FENCE_TEST_ARGS         = -l test -p test -a test -n 1
+
+include $(top_srcdir)/make/fencebuild.mk
+include $(top_srcdir)/make/fenceman.mk
+
diff --git a/fence/agents/azure_arm/fence_azure_arm.py b/fence/agents/azure_arm/fence_azure_arm.py
new file mode 100644
index 0000000..a3211a2
--- /dev/null
+++ b/fence/agents/azure_arm/fence_azure_arm.py
@@ -0,0 +1,134 @@
+#!/usr/bin/python -tt
+
+import sys, re, pexpect
+import logging
+import atexit
+sys.path.append("/usr/share/fence")
+from fencing import *
+from fencing import fail, fail_usage, EC_TIMED_OUT, run_delay
+
+#BEGIN_VERSION_GENERATION
+RELEASE_VERSION="4.0.24.6-7e576"
+BUILD_DATE="(built Thu Nov 3 15:43:03 STD 2016)"
+REDHAT_COPYRIGHT="Copyright (C) Red Hat, Inc. 2004-2010 All rights reserved."
+#END_VERSION_GENERATION
+
+compute_client = None
+
+def get_nodes_list(conn, options):
+    result = {}
+    if compute_client:
+        rgName = options["--resourceGroup"]
+        vms = compute_client.virtual_machines.list(rgName)
+        for vm in vms:
+            result[vm.name] = ("", None)
+
+    return result
+
+def get_power_status(conn, options):
+    logging.info("getting power status for VM " + options["--plug"])
+
+    if compute_client:
+        rgName = options["--resourceGroup"]
+        vmName = options["--plug"]
+
+        powerState = "unknown"
+        vmStatus = compute_client.virtual_machines.get(rgName, vmName, "instanceView")
+        for status in vmStatus.instance_view.statuses:
+            if status.code.startswith("PowerState"):
+                powerState = status.code
+                break
+
+        logging.info("Found power state of VM: " + powerState)
+        if powerState == "PowerState/running":
+            return "on"
+
+    return "off"
+
+def set_power_status(conn, options):
+    logging.info("setting power status for VM " + options["--plug"] + " to " + options["--action"])
+
+    if compute_client:
+        rgName = options["--resourceGroup"]
+        vmName = options["--plug"]
+
+        if (options["--action"]=="off"):
+            logging.info("Deallocating " + vmName + "in resource group " + rgName)
+            compute_client.virtual_machines.deallocate(rgName, vmName)
+        elif (options["--action"]=="reboot"):
+            logging.info("Restarting " + vmName + "in resource group " + rgName)
+            compute_client.virtual_machines.restart(rgName, vmName)
+
+        sys.exit(0)
+
+    sys.exit(1)
+
+# Main agent method
+def main():
+    global compute_client
+
+    device_opt = ["resourceGroup", "login", "passwd", "tenantId", "subscriptionId","port"]
+
+    atexit.register(atexit_handler)
+
+    all_opt["resourceGroup"] = {
+        "getopt" : "rg:",
+        "longopt" : "resourceGroup",
+        "help" : "-rg, --resourceGroup=[name]       Name of the resource group",
+        "shortdesc" : "Name of resource group.",
+        "required" : "1",
+        "order" : 2
+    }
+    all_opt["tenantId"] = {
+        "getopt" : "tid:",
+        "longopt" : "tenantId",
+        "help" : "-tid, --tenantId=[name]       Id of the Azure Active Directory tenant",
+        "shortdesc" : "Id of Azure Active Directory tenant.",
+        "required" : "1",
+        "order" : 3
+    }
+    all_opt["subscriptionId"] = {
+        "getopt" : "sid:",
+        "longopt" : "subscriptionId",
+        "help" : "-sid, --subscriptionId=[name]       Id of the Azure subscription",
+        "shortdesc" : "Id of the Azure subscription.",
+        "required" : "1",
+        "order" : 4
+    }
+
+    options = check_input(device_opt, process_input(device_opt))
+
+    docs = {}
+    docs["shortdesc"] = "Fence agent for Azure Resource Manager"
+    docs["longdesc"] = "Used to deallocate virtual machines and to report power state of virtual machines running in Azure"
+    docs["vendorurl"] = "http://www.microsoft.com"
+    show_docs(options, docs)
+
+    run_delay(options)
+
+    try:
+        from azure.common.credentials import ServicePrincipalCredentials
+        from azure.mgmt.compute import ComputeManagementClient
+
+        tenantid = options["--tenantId"]
+        servicePrincipal = options["--username"]
+        spPassword = options["--password"]
+        subscriptionId = options["--subscriptionId"]
+        credentials = ServicePrincipalCredentials(
+            client_id = servicePrincipal,
+            secret = spPassword,
+            tenant = tenantid
+        )
+        compute_client = ComputeManagementClient(
+            credentials,
+            subscriptionId
+        )
+    except ImportError:
+        fail_usage("Azure Resource Manager Pyhton SDK not found or not accessible")
+
+    # Operate the fencing device
+    result = fence_action(None, options, set_power_status, get_power_status, get_nodes_list)
+    sys.exit(result)
+
+if __name__ == "__main__":
+    main()
diff --git a/tests/data/metadata/fence_azure_arm.xml b/tests/data/metadata/fence_azure_arm.xml
new file mode 100644
index 0000000..1bafc3e
--- /dev/null
+++ b/tests/data/metadata/fence_azure_arm.xml
@@ -0,0 +1,113 @@
+<?xml version="1.0" ?>
+<resource-agent name="fence_azure_arm" shortdesc="Fence agent for Azure Resource Manager" >
+<longdesc>Used to deallocate virtual machines and to report power state of virtual machines running in Azure</longdesc>
+<vendor-url>http://www.microsoft.com</vendor-url>
+<parameters>
+	<parameter name="action" unique="0" required="1">
+		<getopt mixed="-o, --action=[action]" />
+		<content type="string" default="reboot"  />
+		<shortdesc lang="en">Fencing action</shortdesc>
+	</parameter>
+	<parameter name="login" unique="0" required="1">
+		<getopt mixed="-l, --username=[name]" />
+		<content type="string"  />
+		<shortdesc lang="en">Login name</shortdesc>
+	</parameter>
+	<parameter name="passwd" unique="0" required="0">
+		<getopt mixed="-p, --password=[password]" />
+		<content type="string"  />
+		<shortdesc lang="en">Login password or passphrase</shortdesc>
+	</parameter>
+	<parameter name="passwd_script" unique="0" required="0">
+		<getopt mixed="-S, --password-script=[script]" />
+		<content type="string"  />
+		<shortdesc lang="en">Script to run to retrieve password</shortdesc>
+	</parameter>
+	<parameter name="port" unique="0" required="1">
+		<getopt mixed="-n, --plug=[id]" />
+		<content type="string"  />
+		<shortdesc lang="en">Physical plug number on device, UUID or identification of machine</shortdesc>
+	</parameter>
+	<parameter name="resourceGroup" unique="0" required="1">
+		<getopt mixed="-rg, --resourceGroup=[name]" />
+		<content type="string"  />
+		<shortdesc lang="en">Name of resource group.</shortdesc>
+	</parameter>
+	<parameter name="tenantId" unique="0" required="1">
+		<getopt mixed="-tid, --tenantId=[name]" />
+		<content type="string"  />
+		<shortdesc lang="en">Id of Azure Active Directory tenant.</shortdesc>
+	</parameter>
+	<parameter name="subscriptionId" unique="0" required="1">
+		<getopt mixed="-sid, --subscriptionId=[name]" />
+		<content type="string"  />
+		<shortdesc lang="en">Id of the Azure subscription.</shortdesc>
+	</parameter>
+	<parameter name="verbose" unique="0" required="0">
+		<getopt mixed="-v, --verbose" />
+		<content type="boolean"  />
+		<shortdesc lang="en">Verbose mode</shortdesc>
+	</parameter>
+	<parameter name="debug" unique="0" required="0">
+		<getopt mixed="-D, --debug-file=[debugfile]" />
+		<content type="string"  />
+		<shortdesc lang="en">Write debug information to given file</shortdesc>
+	</parameter>
+	<parameter name="version" unique="0" required="0">
+		<getopt mixed="-V, --version" />
+		<content type="boolean"  />
+		<shortdesc lang="en">Display version information and exit</shortdesc>
+	</parameter>
+	<parameter name="help" unique="0" required="0">
+		<getopt mixed="-h, --help" />
+		<content type="boolean"  />
+		<shortdesc lang="en">Display help and exit</shortdesc>
+	</parameter>
+	<parameter name="separator" unique="0" required="0">
+		<getopt mixed="-C, --separator=[char]" />
+		<content type="string" default=","  />
+		<shortdesc lang="en">Separator for CSV created by 'list' operation</shortdesc>
+	</parameter>
+	<parameter name="delay" unique="0" required="0">
+		<getopt mixed="--delay=[seconds]" />
+		<content type="second" default="0"  />
+		<shortdesc lang="en">Wait X seconds before fencing is started</shortdesc>
+	</parameter>
+	<parameter name="login_timeout" unique="0" required="0">
+		<getopt mixed="--login-timeout=[seconds]" />
+		<content type="second" default="5"  />
+		<shortdesc lang="en">Wait X seconds for cmd prompt after login</shortdesc>
+	</parameter>
+	<parameter name="power_timeout" unique="0" required="0">
+		<getopt mixed="--power-timeout=[seconds]" />
+		<content type="second" default="20"  />
+		<shortdesc lang="en">Test X seconds for status change after ON/OFF</shortdesc>
+	</parameter>
+	<parameter name="power_wait" unique="0" required="0">
+		<getopt mixed="--power-wait=[seconds]" />
+		<content type="second" default="0"  />
+		<shortdesc lang="en">Wait X seconds after issuing ON/OFF</shortdesc>
+	</parameter>
+	<parameter name="shell_timeout" unique="0" required="0">
+		<getopt mixed="--shell-timeout=[seconds]" />
+		<content type="second" default="3"  />
+		<shortdesc lang="en">Wait X seconds for cmd prompt after issuing command</shortdesc>
+	</parameter>
+	<parameter name="retry_on" unique="0" required="0">
+		<getopt mixed="--retry-on=[attempts]" />
+		<content type="integer" default="1"  />
+		<shortdesc lang="en">Count of attempts to retry power on</shortdesc>
+	</parameter>
+</parameters>
+<actions>
+	<action name="on" automatic="0"/>
+	<action name="off" />
+	<action name="reboot" />
+	<action name="status" />
+	<action name="list" />
+	<action name="list-status" />
+	<action name="monitor" />
+	<action name="metadata" />
+	<action name="validate-all" />
+</actions>
+</resource-agent>
openSUSE Build Service is sponsored by