diff mbox series

[v7,5/9] rpcctl: Add a command for changing xprt dstaddr

Message ID 20220127194952.63033-6-Anna.Schumaker@Netapp.com (mailing list archive)
State New, archived
Headers show
Series Add a tool for using the new sysfs files | expand

Commit Message

Anna Schumaker Jan. 27, 2022, 7:49 p.m. UTC
From: Anna Schumaker <Anna.Schumaker@Netapp.com>

Using the socket module for dns resolution

Signed-off-by: Anna Schumaker <Anna.Schumaker@Netapp.com>
---
 tools/rpcctl/rpcctl.py | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
diff mbox series

Patch

diff --git a/tools/rpcctl/rpcctl.py b/tools/rpcctl/rpcctl.py
index bdb56d1f5476..c481f96333f9 100755
--- a/tools/rpcctl/rpcctl.py
+++ b/tools/rpcctl/rpcctl.py
@@ -2,6 +2,7 @@ 
 import argparse
 import collections
 import pathlib
+import socket
 import sys
 
 with open("/proc/mounts", 'r') as f:
@@ -22,6 +23,11 @@  def read_addr_file(path):
     except:
         return "(enoent)"
 
+def write_addr_file(path, newaddr):
+    with open(path, 'w') as f:
+        f.write(newaddr)
+    return read_addr_file(path)
+
 def read_info_file(path):
     res = collections.defaultdict(int)
     try:
@@ -73,11 +79,21 @@  class Xprt:
         main = " [main]" if self.info.get("main_xprt") else ""
         return f"xprt {self.id}: {self.type}, {self.dstaddr}{main}"
 
+    def set_dstaddr(self, newaddr):
+        resolved = socket.gethostbyname(newaddr)
+        self.dstaddr = write_addr_file(self.path / "dstaddr", resolved)
+
     def add_command(subparser):
         parser = subparser.add_parser("xprt", help="Commands for individual xprts")
         parser.add_argument("--id", metavar="ID", nargs=1, type=int, help="Id of a specific xprt to show")
         parser.set_defaults(func=Xprt.list_all)
 
+        subparser = parser.add_subparsers()
+        parser = subparser.add_parser("set", help="Set an xprt property")
+        parser.add_argument("--id", metavar="ID", nargs=1, type=int, required=True, help="Id of a specific xprt to modify")
+        parser.add_argument("--dstaddr", metavar="dstaddr", nargs=1, type=str, help="New dstaddr to set")
+        parser.set_defaults(func=Xprt.set_property)
+
     def list_all(args):
         xprts = [ Xprt(f) for f in (sunrpc / "xprt-switches").glob("**/xprt-*") ]
         xprts.sort()
@@ -85,6 +101,21 @@  class Xprt:
             if args.id == None or xprt.id == args.id[0]:
                 print(xprt)
 
+    def get_by_id(id):
+        xprts = [ Xprt(f) for f in (sunrpc / "xprt-switches").glob("**/xprt-*") ]
+        for xprt in xprts:
+            if xprt.id == id:
+                return xprt
+
+    def set_property(args):
+        xprt = Xprt.get_by_id(args.id[0])
+        try:
+            if args.dstaddr != None:
+                xprt.set_dstaddr(args.dstaddr[0])
+            print(xprt)
+        except Exception as e:
+            print(e)
+
 
 class XprtSwitch:
     def __init__(self, path, sep=":"):