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

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