diff -uNr a/configure.ac b/configure.ac --- a/configure.ac 2017-10-05 13:09:31.369561411 +0200 +++ b/configure.ac 2017-10-05 13:16:02.860680521 +0200 @@ -268,6 +268,7 @@ fence/agents/amt/Makefile fence/agents/amt_ws/Makefile fence/agents/aws/Makefile + fence/agents/azure_arm/Makefile fence/agents/bladecenter/Makefile fence/agents/brocade/Makefile fence/agents/cisco_mds/Makefile diff -uNr a/fence/agents/azure_arm/fence_azure_arm.py b/fence/agents/azure_arm/fence_azure_arm.py --- a/fence/agents/azure_arm/fence_azure_arm.py 1970-01-01 01:00:00.000000000 +0100 +++ b/fence/agents/azure_arm/fence_azure_arm.py 2017-10-05 13:14:46.755434886 +0200 @@ -0,0 +1,149 @@ +#!/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.25.34-695e-dirty" +BUILD_DATE="(built Wed Jun 28 08:13:44 UTC 2017)" +REDHAT_COPYRIGHT="Copyright (C) Red Hat, Inc. 2004-2010 All rights reserved." +#END_VERSION_GENERATION + +def get_nodes_list(compute_client, options): + result = {} + if compute_client: + rgName = options["--resourceGroup"] + vms = compute_client.virtual_machines.list(rgName) + try: + for vm in vms: + result[vm.name] = ("", None) + except Exception as e: + fail_usage("Failed: %s" % e) + + return result + +def get_power_status(compute_client, options): + logging.info("getting power status for VM " + options["--plug"]) + + if compute_client: + rgName = options["--resourceGroup"] + vmName = options["--plug"] + + powerState = "unknown" + try: + vmStatus = compute_client.virtual_machines.get(rgName, vmName, "instanceView") + except Exception as e: + fail_usage("Failed: %s" % e) + 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(compute_client, 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"]=="on"): + logging.info("Starting " + vmName + "in resource group " + rgName) + compute_client.virtual_machines.start(rgName, vmName) + + +def define_new_opts(): + all_opt["resourceGroup"] = { + "getopt" : ":", + "longopt" : "resourceGroup", + "help" : "--resourceGroup=[name] Name of the resource group", + "shortdesc" : "Name of resource group.", + "required" : "1", + "order" : 2 + } + all_opt["tenantId"] = { + "getopt" : ":", + "longopt" : "tenantId", + "help" : "--tenantId=[name] Id of the Azure Active Directory tenant", + "shortdesc" : "Id of Azure Active Directory tenant.", + "required" : "1", + "order" : 3 + } + all_opt["subscriptionId"] = { + "getopt" : ":", + "longopt" : "subscriptionId", + "help" : "--subscriptionId=[name] Id of the Azure subscription", + "shortdesc" : "Id of the Azure subscription.", + "required" : "1", + "order" : 4 + } + +# Main agent method +def main(): + compute_client = None + + device_opt = ["resourceGroup", "login", "passwd", "tenantId", "subscriptionId","port"] + + atexit.register(atexit_handler) + + define_new_opts() + + all_opt["power_timeout"]["default"] = "150" + + all_opt["login"]["help"] = "-l, --username=[appid] Application ID" + all_opt["passwd"]["help"] = "-p, --password=[authkey] Authentication key" + + 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. It uses Azure SDK for Python to connect to Azure.\ +\n.P\n\ +For instructions to setup credentials see: https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal\ +\n.P\n\ +Username and password are application ID and authentication key from \"App registrations\"." + 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 Python SDK not found or not accessible") + except Exception as e: + fail_usage("Failed: %s" % re.sub("^, ", "", str(e))) + + # Operate the fencing device + result = fence_action(compute_client, options, set_power_status, get_power_status, get_nodes_list) + sys.exit(result) + +if __name__ == "__main__": + main() diff -uNr a/fence/agents/azure_arm/Makefile.am b/fence/agents/azure_arm/Makefile.am --- a/fence/agents/azure_arm/Makefile.am 1970-01-01 01:00:00.000000000 +0100 +++ b/fence/agents/azure_arm/Makefile.am 2017-10-05 13:55:41.206064062 +0200 @@ -0,0 +1,17 @@ +MAINTAINERCLEANFILES = Makefile.in + +TARGET = fence_azure_arm + +SRC = $(TARGET).py + +EXTRA_DIST = $(SRC) + +sbin_SCRIPTS = $(TARGET) + +man_MANS = $(TARGET).8 + +FENCE_TEST_ARGS = -l test -p test -n 1 + +include $(top_srcdir)/make/fencebuild.mk +include $(top_srcdir)/make/fenceman.mk +include $(top_srcdir)/make/agentpycheck.mk diff -uNr a/tests/data/metadata/fence_azure_arm.xml b/tests/data/metadata/fence_azure_arm.xml --- a/tests/data/metadata/fence_azure_arm.xml 1970-01-01 01:00:00.000000000 +0100 +++ b/tests/data/metadata/fence_azure_arm.xml 2017-10-05 13:18:35.373168796 +0200 @@ -0,0 +1,142 @@ + + +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. +.P +For instructions to setup credentials see: https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal +.P +Username and password are application ID and authentication key from "App registrations". +http://www.microsoft.com + + + + + Login password or passphrase + + + + + Fencing Action + + + + + Script to retrieve password + + + + + Login Name + + + + + Physical plug number, name of virtual machine or UUID + + + + + Login Name + + + + + Login password or passphrase + + + + + Script to retrieve password + + + + + Physical plug number, name of virtual machine or UUID + + + + + Name of resource group. + + + + + Id of Azure Active Directory tenant. + + + + + Id of the Azure subscription. + + + + + Verbose mode + + + + + Write debug information to given file + + + + + Write debug information to given file + + + + + Display version information and exit + + + + + Display help and exit + + + + + Separator for CSV created by operation list + + + + + Wait X seconds before fencing is started + + + + + Wait X seconds for cmd prompt after issuing command + + + + + Wait X seconds after issuing ON/OFF + + + + + Test X seconds for status change after ON/OFF + + + + + Wait X seconds for cmd prompt after login + + + + + Count of attempts to retry power on + + + + + + + + + + + + + +