Blame SOURCES/bz2092921-fence_ibm_powervs-proxy-private-api-servers.patch

149097
From 98fec5c6d55369ad681abc0cde0d8677835957ab Mon Sep 17 00:00:00 2001
149097
From: Arnold Beilmann <arnoldbeilmann@macbook-pro.speedport.ip>
149097
Date: Thu, 5 May 2022 15:26:22 +0200
149097
Subject: [PATCH 1/2] modified for PowerVS
149097
149097
---
149097
 agents/ibm_powervs/fence_ibm_powervs.py | 108 +++++++++++++++++++-----
149097
 1 file changed, 89 insertions(+), 19 deletions(-)
149097
149097
diff --git a/agents/ibm_powervs/fence_ibm_powervs.py b/agents/ibm_powervs/fence_ibm_powervs.py
149097
index 6649771ea..727009220 100755
149097
--- a/agents/ibm_powervs/fence_ibm_powervs.py
149097
+++ b/agents/ibm_powervs/fence_ibm_powervs.py
149097
@@ -1,10 +1,11 @@
149097
-#!@PYTHON@ -tt
149097
+#!/usr/libexec/platform-python -tt
149097
 
149097
 import sys
149097
 import pycurl, io, json
149097
 import logging
149097
 import atexit
149097
-sys.path.append("@FENCEAGENTSLIBDIR@")
149097
+import time
149097
+sys.path.append("/usr/share/fence")
149097
 from fencing import *
149097
 from fencing import fail, run_delay, EC_LOGIN_DENIED, EC_STATUS
149097
 
149097
@@ -14,16 +15,30 @@
149097
 	 "ERROR": "unknown"
149097
 }
149097
 
149097
+def get_token(conn, options):
149097
+
149097
+        try:
149097
+                command = "identity/token"
149097
+                action = "grant_type=urn%3Aibm%3Aparams%3Aoauth%3Agrant-type%3Aapikey&apikey={}".format(options["--token"])
149097
+                res = send_command(conn, command, "POST", action, printResult=False)
149097
+        except Exception as e:
149097
+                logging.debug("Failed: {}".format(e))
149097
+                return "TOKEN_IS_MISSING_OR_WRONG"
149097
+
149097
+        #if "--verbose" in options:
149097
+        #        logging.debug(json.dumps(res, indent=2))
149097
+
149097
+        return res["access_token"]
149097
+
149097
 def get_list(conn, options):
149097
 	outlets = {}
149097
-
149097
+	
149097
 	try:
149097
 		command = "cloud-instances/{}/pvm-instances".format(options["--instance"])
149097
 		res = send_command(conn, command)
149097
 	except Exception as e:
149097
 		logging.debug("Failed: {}".format(e))
149097
 		return outlets
149097
-
149097
 	for r in res["pvmInstances"]:
149097
 		if "--verbose" in options:
149097
 			logging.debug(json.dumps(r, indent=2))
149097
@@ -32,6 +47,7 @@ def get_list(conn, options):
149097
 	return outlets
149097
 
149097
 def get_power_status(conn, options):
149097
+
149097
 	try:
149097
 		command = "cloud-instances/{}/pvm-instances/{}".format(
149097
 				options["--instance"], options["--plug"])
149097
@@ -40,10 +56,11 @@ def get_power_status(conn, options):
149097
 	except KeyError as e:
149097
 		logging.debug("Failed: Unable to get status for {}".format(e))
149097
 		fail(EC_STATUS)
149097
-
149097
+	
149097
 	return result
149097
 
149097
 def set_power_status(conn, options):
149097
+
149097
 	action = {
149097
 		"on" :  '{"action" : "start"}',
149097
 		"off" : '{"action" : "immediate-shutdown"}',
149097
@@ -56,35 +73,63 @@ def set_power_status(conn, options):
149097
 		logging.debug("Failed: Unable to set power to {} for {}".format(options["--action"], e))
149097
 		fail(EC_STATUS)
149097
 
149097
-def connect(opt):
149097
+def connect(opt, token):
149097
 	conn = pycurl.Curl()
149097
 
149097
 	## setup correct URL
149097
-	conn.base_url = "https://" + opt["--region"] + ".power-iaas.cloud.ibm.com/pcloud/v1/"
149097
+	conn.base_url = "https://private." + opt["--region"] + ".power-iaas.cloud.ibm.com/pcloud/v1/"
149097
+	if opt["--api-type"] == "public":
149097
+		conn.base_url = "https://" + opt["--region"] + ".power-iaas.cloud.ibm.com/pcloud/v1/"
149097
 
149097
 	if opt["--verbose-level"] > 1:
149097
-		conn.setopt(pycurl.VERBOSE, 1)
149097
+		conn.setopt(pycurl.VERBOSE, 0)
149097
 
149097
+	conn.setopt(pycurl.CONNECTTIMEOUT,int(opt["--shell-timeout"]))
149097
 	conn.setopt(pycurl.TIMEOUT, int(opt["--shell-timeout"]))
149097
 	conn.setopt(pycurl.SSL_VERIFYPEER, 1)
149097
 	conn.setopt(pycurl.SSL_VERIFYHOST, 2)
149097
+	conn.setopt(pycurl.PROXY, "{}".format(opt["--proxy"]))
149097
 
149097
 	# set auth token for later requests
149097
 	conn.setopt(pycurl.HTTPHEADER, [
149097
 		"Content-Type: application/json",
149097
-		"Authorization: Bearer {}".format(opt["--token"]),
149097
+		"Authorization: Bearer {}".format(token),
149097
 		"CRN: {}".format(opt["--crn"]),
149097
 		"User-Agent: curl",
149097
 	])
149097
+
149097
+	return conn
149097
+
149097
+def auth_connect(opt):
149097
+	conn = pycurl.Curl()
149097
+
149097
+	# setup correct URL
149097
+	conn.base_url = "https://iam.cloud.ibm.com/"
149097
+
149097
+	if opt["--verbose-level"] > 1:
149097
+		conn.setopt(pycurl.VERBOSE, 1)
149097
+
149097
+	conn.setopt(pycurl.CONNECTTIMEOUT,int(opt["--shell-timeout"]))
149097
+	conn.setopt(pycurl.TIMEOUT, int(opt["--shell-timeout"]))
149097
+	conn.setopt(pycurl.SSL_VERIFYPEER, 1)
149097
+	conn.setopt(pycurl.SSL_VERIFYHOST, 2)
149097
+	conn.setopt(pycurl.PROXY, "{}".format(opt["--proxy"]))
149097
+
149097
+	# set auth token for later requests
149097
+	conn.setopt(pycurl.HTTPHEADER, [
149097
+		"Content-type: application/x-www-form-urlencoded",
149097
+		"Accept: application/json",
149097
+		"User-Agent: curl",
149097
+	])
149097
 
149097
 	return conn
149097
 
149097
 def disconnect(conn):
149097
 	conn.close()
149097
 
149097
-def send_command(conn, command, method="GET", action=None):
149097
+def send_command(conn, command, method="GET", action=None, printResult=True):
149097
 	url = conn.base_url + command
149097
-
149097
+	
149097
 	conn.setopt(pycurl.URL, url.encode("ascii"))
149097
 
149097
 	web_buffer = io.BytesIO()
149097
@@ -99,8 +144,10 @@ def send_command(conn, command, method="GET", action=None):
149097
 	conn.setopt(pycurl.WRITEFUNCTION, web_buffer.write)
149097
 
149097
 	try:
149097
+		time.sleep(3)
149097
 		conn.perform()
149097
 	except Exception as e:
149097
+		logging.error("ADD_DEBUG: {}".format(e))
149097
 		raise(e)
149097
 
149097
 	rc = conn.getinfo(pycurl.HTTP_CODE)
149097
@@ -110,8 +157,7 @@ def send_command(conn, command, method="GET", action=None):
149097
 
149097
 	if rc != 200:
149097
 		if len(result) > 0:
149097
-			raise Exception("{}: {}".format(rc,
149097
-					result["value"]["messages"][0]["default_message"]))
149097
+			raise Exception("{}: {}".format(rc,result))
149097
 		else:
149097
 			raise Exception("Remote returned {} for request to {}".format(rc, url))
149097
 
149097
@@ -121,7 +167,8 @@ def send_command(conn, command, method="GET", action=None):
149097
 	logging.debug("url: {}".format(url))
149097
 	logging.debug("method: {}".format(method))
149097
 	logging.debug("response code: {}".format(rc))
149097
-	logging.debug("result: {}\n".format(result))
149097
+	if printResult:
149097
+		logging.debug("result: {}\n".format(result))
149097
 
149097
 	return result
149097
 
149097
@@ -129,9 +176,9 @@ def define_new_opts():
149097
 	all_opt["token"] = {
149097
 		"getopt" : ":",
149097
 		"longopt" : "token",
149097
-		"help" : "--token=[token]                Bearer Token",
149097
+		"help" : "--token=[token]                API Token",
149097
 		"required" : "1",
149097
-		"shortdesc" : "Bearer Token",
149097
+		"shortdesc" : "API Token",
149097
 		"order" : 0
149097
 	}
149097
 	all_opt["crn"] = {
149097
@@ -158,6 +205,22 @@ def define_new_opts():
149097
 		"shortdesc" : "Region",
149097
 		"order" : 0
149097
 	}
149097
+	all_opt["api-type"] = {
149097
+                "getopt" : ":",
149097
+                "longopt" : "api-type",
149097
+                "help" : "--api-type=[private|public]          API-type: 'private' (default) or 'public'",
149097
+                "required" : "0",
149097
+                "shortdesc" : "API-type (private|public)",
149097
+                "order" : 0
149097
+        }
149097
+	all_opt["proxy"] = {
149097
+                "getopt" : ":",
149097
+                "longopt" : "proxy",
149097
+                "help" : "--proxy=[http://<URL>:<PORT>]          Proxy: 'http://<URL>:<PORT>'",
149097
+                "required" : "0",
149097
+                "shortdesc" : "Network proxy",
149097
+                "order" : 0
149097
+        }
149097
 
149097
 
149097
 def main():
149097
@@ -166,6 +229,8 @@ def main():
149097
 		"crn",
149097
 		"instance",
149097
 		"region",
149097
+		"api-type",
149097
+		"proxy",
149097
 		"port",
149097
 		"no_password",
149097
 	]
149097
@@ -173,9 +238,11 @@ def main():
149097
 	atexit.register(atexit_handler)
149097
 	define_new_opts()
149097
 
149097
-	all_opt["shell_timeout"]["default"] = "15"
149097
+	all_opt["shell_timeout"]["default"] = "500"
149097
 	all_opt["power_timeout"]["default"] = "30"
149097
 	all_opt["power_wait"]["default"] = "1"
149097
+	all_opt["api-type"]["default"] = "private"
149097
+	all_opt["proxy"]["default"] = ""
149097
 
149097
 	options = check_input(device_opt, process_input(device_opt))
149097
 
149097
@@ -190,8 +257,11 @@ def main():
149097
 	## Fence operations
149097
 	####
149097
 	run_delay(options)
149097
-
149097
-	conn = connect(options)
149097
+	
149097
+	auth_conn = auth_connect(options)
149097
+	token = get_token(auth_conn, options)
149097
+	disconnect(auth_conn)
149097
+	conn = connect(options, token)
149097
 	atexit.register(disconnect, conn)
149097
 
149097
 	result = fence_action(conn, options, set_power_status, get_power_status, get_list)
149097
149097
From fbe9a539ac8f40686a8027b7e768d9f7b799e485 Mon Sep 17 00:00:00 2001
149097
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
149097
Date: Fri, 6 May 2022 11:22:47 +0200
149097
Subject: [PATCH 2/2] fence_ibm_powervs: cleanup and fixes
149097
149097
---
149097
 agents/ibm_powervs/fence_ibm_powervs.py   | 37 ++++++++++-------------
149097
 tests/data/metadata/fence_ibm_powervs.xml | 19 ++++++++++--
149097
 2 files changed, 33 insertions(+), 23 deletions(-)
149097
149097
diff --git a/agents/ibm_powervs/fence_ibm_powervs.py b/agents/ibm_powervs/fence_ibm_powervs.py
149097
index 727009220..819ab8896 100755
149097
--- a/agents/ibm_powervs/fence_ibm_powervs.py
149097
+++ b/agents/ibm_powervs/fence_ibm_powervs.py
149097
@@ -1,11 +1,11 @@
149097
-#!/usr/libexec/platform-python -tt
149097
+#!@PYTHON@ -tt
149097
 
149097
 import sys
149097
 import pycurl, io, json
149097
 import logging
149097
 import atexit
149097
 import time
149097
-sys.path.append("/usr/share/fence")
149097
+sys.path.append("@FENCEAGENTSLIBDIR@")
149097
 from fencing import *
149097
 from fencing import fail, run_delay, EC_LOGIN_DENIED, EC_STATUS
149097
 
149097
@@ -16,7 +16,6 @@
149097
 }
149097
 
149097
 def get_token(conn, options):
149097
-
149097
         try:
149097
                 command = "identity/token"
149097
                 action = "grant_type=urn%3Aibm%3Aparams%3Aoauth%3Agrant-type%3Aapikey&apikey={}".format(options["--token"])
149097
@@ -25,20 +24,18 @@ def get_token(conn, options):
149097
                 logging.debug("Failed: {}".format(e))
149097
                 return "TOKEN_IS_MISSING_OR_WRONG"
149097
 
149097
-        #if "--verbose" in options:
149097
-        #        logging.debug(json.dumps(res, indent=2))
149097
-
149097
         return res["access_token"]
149097
 
149097
 def get_list(conn, options):
149097
 	outlets = {}
149097
-	
149097
+
149097
 	try:
149097
 		command = "cloud-instances/{}/pvm-instances".format(options["--instance"])
149097
 		res = send_command(conn, command)
149097
 	except Exception as e:
149097
 		logging.debug("Failed: {}".format(e))
149097
 		return outlets
149097
+
149097
 	for r in res["pvmInstances"]:
149097
 		if "--verbose" in options:
149097
 			logging.debug(json.dumps(r, indent=2))
149097
@@ -47,7 +44,6 @@ def get_list(conn, options):
149097
 	return outlets
149097
 
149097
 def get_power_status(conn, options):
149097
-
149097
 	try:
149097
 		command = "cloud-instances/{}/pvm-instances/{}".format(
149097
 				options["--instance"], options["--plug"])
149097
@@ -56,11 +52,10 @@ def get_power_status(conn, options):
149097
 	except KeyError as e:
149097
 		logging.debug("Failed: Unable to get status for {}".format(e))
149097
 		fail(EC_STATUS)
149097
-	
149097
+
149097
 	return result
149097
 
149097
 def set_power_status(conn, options):
149097
-
149097
 	action = {
149097
 		"on" :  '{"action" : "start"}',
149097
 		"off" : '{"action" : "immediate-shutdown"}',
149097
@@ -77,11 +72,11 @@ def connect(opt, token):
149097
 	conn = pycurl.Curl()
149097
 
149097
 	## setup correct URL
149097
-	conn.base_url = "https://private." + opt["--region"] + ".power-iaas.cloud.ibm.com/pcloud/v1/"
149097
-	if opt["--api-type"] == "public":
149097
-		conn.base_url = "https://" + opt["--region"] + ".power-iaas.cloud.ibm.com/pcloud/v1/"
149097
+	conn.base_url = "https://" + opt["--region"] + ".power-iaas.cloud.ibm.com/pcloud/v1/"
149097
+	if opt["--api-type"] == "private":
149097
+		conn.base_url = "https://private." + opt["--region"] + ".power-iaas.cloud.ibm.com/pcloud/v1/"
149097
 
149097
-	if opt["--verbose-level"] > 1:
149097
+	if opt["--verbose-level"] < 3:
149097
 		conn.setopt(pycurl.VERBOSE, 0)
149097
 
149097
 	conn.setopt(pycurl.CONNECTTIMEOUT,int(opt["--shell-timeout"]))
149097
@@ -129,7 +124,7 @@ def disconnect(conn):
149097
 
149097
 def send_command(conn, command, method="GET", action=None, printResult=True):
149097
 	url = conn.base_url + command
149097
-	
149097
+
149097
 	conn.setopt(pycurl.URL, url.encode("ascii"))
149097
 
149097
 	web_buffer = io.BytesIO()
149097
@@ -144,10 +139,9 @@ def send_command(conn, command, method="GET", action=None, printResult=True):
149097
 	conn.setopt(pycurl.WRITEFUNCTION, web_buffer.write)
149097
 
149097
 	try:
149097
-		time.sleep(3)
149097
 		conn.perform()
149097
 	except Exception as e:
149097
-		logging.error("ADD_DEBUG: {}".format(e))
149097
+		logging.error("send_command(): {}".format(e))
149097
 		raise(e)
149097
 
149097
 	rc = conn.getinfo(pycurl.HTTP_CODE)
149097
@@ -208,9 +202,9 @@ def define_new_opts():
149097
 	all_opt["api-type"] = {
149097
                 "getopt" : ":",
149097
                 "longopt" : "api-type",
149097
-                "help" : "--api-type=[private|public]          API-type: 'private' (default) or 'public'",
149097
+                "help" : "--api-type=[public|private]          API-type: 'public' (default) or 'private'",
149097
                 "required" : "0",
149097
-                "shortdesc" : "API-type (private|public)",
149097
+                "shortdesc" : "API-type (public|private)",
149097
                 "order" : 0
149097
         }
149097
 	all_opt["proxy"] = {
149097
@@ -238,9 +232,10 @@ def main():
149097
 	atexit.register(atexit_handler)
149097
 	define_new_opts()
149097
 
149097
-	all_opt["shell_timeout"]["default"] = "500"
149097
+	all_opt["shell_timeout"]["default"] = "15"
149097
 	all_opt["power_timeout"]["default"] = "30"
149097
 	all_opt["power_wait"]["default"] = "1"
149097
+	all_opt["stonith_status_sleep"]["default"] = "3"
149097
 	all_opt["api-type"]["default"] = "private"
149097
 	all_opt["proxy"]["default"] = ""
149097
 
149097
@@ -257,7 +252,7 @@ def main():
149097
 	## Fence operations
149097
 	####
149097
 	run_delay(options)
149097
-	
149097
+
149097
 	auth_conn = auth_connect(options)
149097
 	token = get_token(auth_conn, options)
149097
 	disconnect(auth_conn)
149097
diff --git a/tests/data/metadata/fence_ibm_powervs.xml b/tests/data/metadata/fence_ibm_powervs.xml
149097
index fe86331bd..81cea4379 100644
149097
--- a/tests/data/metadata/fence_ibm_powervs.xml
149097
+++ b/tests/data/metadata/fence_ibm_powervs.xml
149097
@@ -3,6 +3,16 @@
149097
 <longdesc>fence_ibm_powervs is an I/O Fencing agent which can be used with IBM PowerVS to fence virtual machines.</longdesc>
149097
 <vendor-url>https://www.ibm.com</vendor-url>
149097
 <parameters>
149097
+	<parameter name="api-type" unique="0" required="0" deprecated="1">
149097
+		<getopt mixed="--api-type=[public|private]" />
149097
+		<content type="string" default="private"  />
149097
+		<shortdesc lang="en">API-type (public|private)</shortdesc>
149097
+	</parameter>
149097
+	<parameter name="api_type" unique="0" required="0" obsoletes="api-type">
149097
+		<getopt mixed="--api-type=[public|private]" />
149097
+		<content type="string" default="private"  />
149097
+		<shortdesc lang="en">API-type (public|private)</shortdesc>
149097
+	</parameter>
149097
 	<parameter name="crn" unique="0" required="1">
149097
 		<getopt mixed="--crn=[crn]" />
149097
 		<content type="string"  />
149097
@@ -13,6 +23,11 @@
149097
 		<content type="string"  />
149097
 		<shortdesc lang="en">PowerVS Instance</shortdesc>
149097
 	</parameter>
149097
+	<parameter name="proxy" unique="0" required="0">
149097
+		<getopt mixed="--proxy=[http://<URL>:<PORT>]" />
149097
+		<content type="string" default=""  />
149097
+		<shortdesc lang="en">Network proxy</shortdesc>
149097
+	</parameter>
149097
 	<parameter name="region" unique="0" required="1">
149097
 		<getopt mixed="--region=[region]" />
149097
 		<content type="string"  />
149097
@@ -21,7 +36,7 @@
149097
 	<parameter name="token" unique="0" required="1">
149097
 		<getopt mixed="--token=[token]" />
149097
 		<content type="string"  />
149097
-		<shortdesc lang="en">Bearer Token</shortdesc>
149097
+		<shortdesc lang="en">API Token</shortdesc>
149097
 	</parameter>
149097
 	<parameter name="action" unique="0" required="1">
149097
 		<getopt mixed="-o, --action=[action]" />
149097
@@ -110,7 +125,7 @@
149097
 	</parameter>
149097
 	<parameter name="stonith_status_sleep" unique="0" required="0">
149097
 		<getopt mixed="--stonith-status-sleep=[seconds]" />
149097
-		<content type="second" default="1"  />
149097
+		<content type="second" default="3"  />
149097
 		<shortdesc lang="en">Sleep X seconds between status calls during a STONITH action</shortdesc>
149097
 	</parameter>
149097
 	<parameter name="retry_on" unique="0" required="0">