Blame SOURCES/0007-daemonized-mode-add-interactive-shell-support.patch

a135b2
From 15abdd481c5babd4128f3f0a9f0ac2f983bf1c89 Mon Sep 17 00:00:00 2001
a135b2
From: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
a135b2
Date: Fri, 3 Apr 2020 13:01:06 +0530
a135b2
Subject: [PATCH 1/3] daemonized-mode: add interactive shell support
a135b2
a135b2
set interactive mode as default to make it appear similar to cli approach,
a135b2
a135b2
$ targetcli
a135b2
targetcli shell version 2.1.51
a135b2
Entering targetcli interactive mode for daemonized approach.
a135b2
Type 'exit' to quit.
a135b2
a135b2
/> pwd
a135b2
/
a135b2
/> cd /iscsi
a135b2
/> pwd
a135b2
/iscsi
a135b2
/> exit
a135b2
a135b2
Here we introduce a new global option daemon_use_batch_mode for switching between
a135b2
batch and interactive modes.
a135b2
a135b2
$ targetcli set global daemon_use_batch_mode=true
a135b2
Parameter daemon_use_batch_mode is now 'true'.
a135b2
a135b2
$ targetcli
a135b2
targetcli shell version 2.1.51
a135b2
Entering targetcli batch mode for daemonized approach.
a135b2
Enter multiple commands separated by newline and type 'exit' to run them
a135b2
all in one go.
a135b2
a135b2
/> pwd
a135b2
/> cd /iscsi
a135b2
/> pwd
a135b2
/
a135b2
/iscsi
a135b2
a135b2
Fixes: #160
a135b2
Signed-off-by: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
a135b2
---
a135b2
 scripts/targetcli    | 57 ++++++++++++++++++++++++++++----------------
a135b2
 targetcli/ui_node.py |  3 +++
a135b2
 targetclid.8         | 31 ++++++++++++++++++++++--
a135b2
 3 files changed, 69 insertions(+), 22 deletions(-)
a135b2
a135b2
diff --git a/scripts/targetcli b/scripts/targetcli
a135b2
index f11ece4..3496126 100755
a135b2
--- a/scripts/targetcli
a135b2
+++ b/scripts/targetcli
a135b2
@@ -64,6 +64,7 @@ class TargetCLI(ConfigShell):
a135b2
                      'max_backup_files': '10',
a135b2
                      'auto_add_default_portal': True,
a135b2
                      'auto_use_daemon': False,
a135b2
+                     'daemon_use_batch_mode': False,
a135b2
                     }
a135b2
 
a135b2
 def usage():
a135b2
@@ -160,9 +161,8 @@ def call_daemon(shell, req):
a135b2
 
a135b2
     sock.send(b'-END@OF@DATA-')
a135b2
     sock.close()
a135b2
-    sys.exit(0)
a135b2
 
a135b2
-def get_arguments(shell):
a135b2
+def switch_to_daemon(shell, interactive):
a135b2
     readline.set_completer(completer)
a135b2
     readline.set_completer_delims('')
a135b2
 
a135b2
@@ -173,27 +173,40 @@ def get_arguments(shell):
a135b2
 
a135b2
     if len(sys.argv) > 1:
a135b2
         command = " ".join(sys.argv[1:])
a135b2
+        call_daemon(shell, command.encode())
a135b2
+        sys.exit(0)
a135b2
+
a135b2
+    if interactive:
a135b2
+        shell.con.display("targetcli shell version %s\n"
a135b2
+                          "Entering targetcli interactive mode for daemonized approach.\n"
a135b2
+                          "Type 'exit' to quit.\n"
a135b2
+                          % targetcli_version)
a135b2
     else:
a135b2
-        inputs = []
a135b2
         shell.con.display("targetcli shell version %s\n"
a135b2
-                        "Entering targetcli batch mode for daemonized approach.\n"
a135b2
-                        "Enter multiple commands separated by newline and "
a135b2
-                        "type 'exit' to run them all in one go.\n"
a135b2
-                        % targetcli_version)
a135b2
-        while True:
a135b2
-            shell.con.raw_write("/> ")
a135b2
-            command = six.moves.input()
a135b2
-            if command.lower() == "exit":
a135b2
-                break
a135b2
+                          "Entering targetcli batch mode for daemonized approach.\n"
a135b2
+                          "Enter multiple commands separated by newline and "
a135b2
+                          "type 'exit' to run them all in one go.\n"
a135b2
+                          % targetcli_version)
a135b2
+
a135b2
+    inputs = []
a135b2
+    real_exit=False
a135b2
+    while True:
a135b2
+        shell.con.raw_write("/> ")
a135b2
+        command = six.moves.input()
a135b2
+        if command.lower() == "exit":
a135b2
+            real_exit=True
a135b2
+        if not interactive:
a135b2
             inputs.append(command)
a135b2
-        command = '%'.join(inputs)  # delimit multiple commands with '%'
a135b2
-
a135b2
-    if not command:
a135b2
-        sys.exit(1)
a135b2
-
a135b2
-    usage_version(command);
a135b2
+            if real_exit:
a135b2
+                command = '%'.join(inputs)  # delimit multiple commands with '%'
a135b2
+                call_daemon(shell, command.encode())
a135b2
+                break
a135b2
+        else:
a135b2
+            if real_exit:
a135b2
+                break
a135b2
+            call_daemon(shell, command.encode())
a135b2
 
a135b2
-    return command
a135b2
+    sys.exit(0)
a135b2
 
a135b2
 def main():
a135b2
     '''
a135b2
@@ -225,8 +238,12 @@ def main():
a135b2
         if sys.argv[1] in ("disable-daemon", "--disable-daemon"):
a135b2
             disable_daemon=True
a135b2
 
a135b2
+    interactive_mode = True
a135b2
+    if shell.prefs['daemon_use_batch_mode']:
a135b2
+        interactive_mode = False
a135b2
+
a135b2
     if use_daemon and not disable_daemon:
a135b2
-        call_daemon(shell, get_arguments(shell).encode())
a135b2
+        switch_to_daemon(shell, interactive_mode)
a135b2
         # does not return
a135b2
 
a135b2
     try:
a135b2
diff --git a/targetcli/ui_node.py b/targetcli/ui_node.py
a135b2
index 58a70c6..0485950 100644
a135b2
--- a/targetcli/ui_node.py
a135b2
+++ b/targetcli/ui_node.py
a135b2
@@ -52,6 +52,9 @@ class UINode(ConfigNode):
a135b2
         self.define_config_group_param(
a135b2
             'global', 'auto_use_daemon', 'bool',
a135b2
             'If true, commands will be sent to targetclid.')
a135b2
+        self.define_config_group_param(
a135b2
+            'global', 'daemon_use_batch_mode', 'bool',
a135b2
+            'If true, use batch mode for daemonized approach.')
a135b2
 
a135b2
     def assert_root(self):
a135b2
         '''
a135b2
diff --git a/targetclid.8 b/targetclid.8
a135b2
index a783091..5760168 100644
a135b2
--- a/targetclid.8
a135b2
+++ b/targetclid.8
a135b2
@@ -30,11 +30,38 @@ $ targetcli set global auto_use_daemon=true
a135b2
 .br
a135b2
 $ targetcli ls
a135b2
 .TP
a135b2
-You can use batch mode for sending multiple commands in one go,
a135b2
+You can use interactive mode,
a135b2
 .br
a135b2
 $ targetcli <hit-enter>
a135b2
 .br
a135b2
-targetcli shell version 2.1.50
a135b2
+targetcli shell version 2.1.51
a135b2
+.br
a135b2
+Entering targetcli interactive mode for daemonized approach.
a135b2
+.br
a135b2
+Type 'exit' to quit.
a135b2
+.br
a135b2
+/> pwd
a135b2
+.br
a135b2
+/
a135b2
+.br
a135b2
+/> cd /iscsi
a135b2
+.br
a135b2
+/> pwd
a135b2
+.br
a135b2
+/iscsi
a135b2
+.br
a135b2
+/> exit
a135b2
+.br
a135b2
+.TP
a135b2
+You can also use batch mode for sending multiple commands in one go,
a135b2
+.br
a135b2
+$ targetcli set global daemon_use_batch_mode=true
a135b2
+.br
a135b2
+Parameter daemon_use_batch_mode is now 'true'.
a135b2
+.br
a135b2
+$ targetcli <hit-enter>
a135b2
+.br
a135b2
+targetcli shell version 2.1.51
a135b2
 .br
a135b2
 Entering targetcli batch mode for daemonized approach.
a135b2
 .br
a135b2
-- 
a135b2
2.21.0
a135b2