From bdd421efaec88585d54d17078e07ba7a28923703 Mon Sep 17 00:00:00 2001
From: "Gao,Yan" <ygao@suse.com>
Date: Thu, 30 Apr 2020 22:25:01 +0200
Subject: [PATCH 6/9] Fix: fencer: add `port` or `plug` parameter according to
 metadata for RHCS-style fence-agents

According to:
https://github.com/ClusterLabs/fence-agents/commit/de490e059

, `port` parameter is massively deprecated in favor of `plug`:

```
<parameter name="plug" ... obsoletes="port">...
<parameter name="port" ... deprecated="1">...
```

With this commit, according to the metadata, if `port` parameter is not
supported, `plug` parameter is added if supported. By default, `port`
is added.
---
 fencing/commands.c             | 12 +++++++++++-
 include/crm/fencing/internal.h |  4 +++-
 lib/fencing/st_client.c        | 20 +++++++++++++++-----
 lib/fencing/st_client.c          |  4 ++--
 4 files changed, 31 insertions(+), 9 deletions(-)

Index: pacemaker-1.1.16+20170320.77ea74d/fencing/commands.c
===================================================================
--- pacemaker-1.1.16+20170320.77ea74d.orig/fencing/commands.c
+++ pacemaker-1.1.16+20170320.77ea74d/fencing/commands.c
@@ -319,6 +319,7 @@ stonith_device_execute(stonith_device_t
 {
     int exec_rc = 0;
     const char *action_str = NULL;
+    const char *host_arg = NULL;
     async_command_t *cmd = NULL;
     stonith_action_t *action = NULL;
     int active_cmds = 0;
@@ -401,11 +402,19 @@ stonith_device_execute(stonith_device_t
         action_str = "off";
     }
 
+    if (is_set(device->flags, st_device_supports_parameter_port)) {
+        host_arg = "port";
+
+    } else if (is_set(device->flags, st_device_supports_parameter_plug)) {
+        host_arg = "plug";
+    }
+
     action = stonith_action_create(device->agent,
                                    action_str,
                                    cmd->victim,
                                    cmd->victim_nodeid,
-                                   cmd->timeout, device->params, device->aliases);
+                                   cmd->timeout, device->params,
+                                   device->aliases, host_arg);
 
     /* for async exec, exec_rc is pid if positive and error code if negative/zero */
     exec_rc = stonith_action_execute_async(action, (void *)cmd, cmd->done_cb);
@@ -939,6 +948,7 @@ build_device_from_xml(xmlNode * msg)
 
     device->agent_metadata = get_agent_metadata(device->agent);
     read_action_metadata(device);
+    set_bit(device->flags, stonith__device_parameter_flags(device->agent_metadata));
 
     value = g_hash_table_lookup(device->params, "nodeid");
     if (!value) {
Index: pacemaker-1.1.16+20170320.77ea74d/include/crm/fencing/internal.h
===================================================================
--- pacemaker-1.1.16+20170320.77ea74d.orig/include/crm/fencing/internal.h
+++ pacemaker-1.1.16+20170320.77ea74d/include/crm/fencing/internal.h
@@ -38,7 +38,9 @@ stonith_action_t *stonith_action_create(
                                         const char *victim,
                                         uint32_t victim_nodeid,
                                         int timeout,
-                                        GHashTable * device_args, GHashTable * port_map);
+                                        GHashTable * device_args,
+                                        GHashTable * port_map,
+                                        const char * host_arg);
 
 GPid
 stonith_action_execute_async(stonith_action_t * action,
Index: pacemaker-1.1.16+20170320.77ea74d/lib/fencing/st_client.c
===================================================================
--- pacemaker-1.1.16+20170320.77ea74d.orig/lib/fencing/st_client.c
+++ pacemaker-1.1.16+20170320.77ea74d/lib/fencing/st_client.c
@@ -508,8 +508,9 @@ append_host_specific_args(const char *vi
 }
 
 static char *
-make_args(const char *agent, const char *action, const char *victim, uint32_t victim_nodeid, GHashTable * device_args,
-          GHashTable * port_map)
+make_args(const char *agent, const char *action, const char *victim,
+          uint32_t victim_nodeid, GHashTable * device_args,
+          GHashTable * port_map, const char *host_arg)
 {
     char buffer[512];
     char *arg_list = NULL;
@@ -568,7 +569,14 @@ make_args(const char *agent, const char
             const char *map = g_hash_table_lookup(device_args, STONITH_ATTR_ARGMAP);
 
             if (map == NULL) {
-                param = "port";
+                // By default, `port` is added
+                if (host_arg == NULL) {
+                    param = "port";
+
+                } else {
+                    param = host_arg;
+                }
+
                 value = g_hash_table_lookup(device_args, param);
 
             } else {
@@ -677,13 +685,15 @@ stonith_action_create(const char *agent,
                       const char *_action,
                       const char *victim,
                       uint32_t victim_nodeid,
-                      int timeout, GHashTable * device_args, GHashTable * port_map)
+                      int timeout, GHashTable * device_args,
+                      GHashTable * port_map, const char *host_arg)
 {
     stonith_action_t *action;
 
     action = calloc(1, sizeof(stonith_action_t));
     crm_debug("Initiating action %s for agent %s (target=%s)", _action, agent, victim);
-    action->args = make_args(agent, _action, victim, victim_nodeid, device_args, port_map);
+    action->args = make_args(agent, _action, victim, victim_nodeid,
+                             device_args, port_map, host_arg);
     action->agent = strdup(agent);
     action->action = strdup(_action);
     if (victim) {
@@ -1279,7 +1289,8 @@ stonith_api_device_metadata(stonith_t *
      */
 
     if (safe_str_eq(provider, "redhat")) {
-        stonith_action_t *action = stonith_action_create(agent, "metadata", NULL, 0, 5, NULL, NULL);
+        stonith_action_t *action = stonith_action_create(agent, "metadata", NULL, 0,
+                                                         5, NULL, NULL, NULL);
         int exec_rc = stonith_action_execute(action, &rc, &buffer);
         xmlNode *xml = NULL;
         xmlNode *actions = NULL;
