Michael Pasternak has uploaded a new change for review.

Change subject: cli: update prompt status upon "Connection failure" #880559
......................................................................

cli: update prompt status upon "Connection failure" #880559

https://bugzilla.redhat.com/show_bug.cgi?id=880559

Change-Id: I18c7ec8c986315e30fd4ae0e8e7598cef960fa05
Signed-off-by: Michael Pasternak <mpast...@redhat.com>
---
M src/cli/context.py
M src/cli/messages.py
M src/ovirtcli/command/connect.py
M src/ovirtcli/context.py
M src/ovirtcli/shell/engineshell.py
5 files changed, 40 insertions(+), 28 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-cli refs/changes/38/10038/1

diff --git a/src/cli/context.py b/src/cli/context.py
index a8d4069..2d61ab2 100644
--- a/src/cli/context.py
+++ b/src/cli/context.py
@@ -45,6 +45,7 @@
     COMMAND_ERROR = 2
     INTERRUPTED = 3
     UNKNOWN_ERROR = 4
+    COMMUNICATION_ERROR = 5
 
     Parser = Parser
     Terminal = Terminal
@@ -192,7 +193,7 @@
                 self.status = self.OK
 
     def _handle_exception(self, e):
-        from ovirtsdk.infrastructure.errors import RequestError
+        from ovirtsdk.infrastructure.errors import RequestError, 
ConnectionError
         """Handle an exception. Can be overruled in a subclass."""
         if isinstance(e, KeyboardInterrupt):
             self.status = self.INTERRUPTED
@@ -207,6 +208,12 @@
             sys.stderr.write('\nerror: %s\n\n' % str(e))
             if hasattr(e, 'help'):
                 sys.stderr.write('%s\n' % e.help)
+        elif isinstance(e, ConnectionError):
+            self.status = getattr(e, 'status', self.COMMUNICATION_ERROR)
+            sys.stderr.write('\nerror: %s\n\n' % str(e)\
+                 .replace("[ERROR]::oVirt API connection failure, ", ""))
+            if hasattr(e, 'help'):
+                sys.stderr.write('%s\n' % e.help)
         else:
             self.status = self.UNKNOWN_ERROR
             if self.settings['cli:debug']:
diff --git a/src/cli/messages.py b/src/cli/messages.py
index 7ee371f..53f13e7 100644
--- a/src/cli/messages.py
+++ b/src/cli/messages.py
@@ -46,10 +46,12 @@
         INVALID_COLLECTION_BASED_OPTION_SYNTAX = 'invalid syntax at "--%s", 
see help on collection based arguments for more details.'
         INVALID_ARGUMENT_SEGMENT = '"%s" is invalid argument segment.'
         INVALID_OPTION_SEGMENT = '"%s" is invalid segment at option "--%s".'
+        MISSING_CONFIGURATION_VARIABLE = 'missing configuration variable: %s'
     class Warning():
         CANNOT_FETCH_HOST_CERT_SUBJECT = 'could not fetch host certificate 
info.'
         CANNOT_FETCH_HOST_CERT_SUBJECT_LEGACY_SDK = 'could not fetch host 
certificate info cause used backend/sdk does not support it.'
         HOST_IDENTITY_WILL_NOT_BE_VALIDATED = 'host identity will not be 
validated.'
+        ALREADY_CONNECTED = '\nalready connected\n\n'
     class Info():
         POSSIBALE_ARGUMENTS_COMBINATIONS = ',\npossible arguments combinations 
are %s.'
         POSSIBLE_VM_STATES_FOR_CONSOLE = ',\npossible vm states are %s.'
diff --git a/src/ovirtcli/command/connect.py b/src/ovirtcli/command/connect.py
index 9bc9e6f..8955a88 100644
--- a/src/ovirtcli/command/connect.py
+++ b/src/ovirtcli/command/connect.py
@@ -73,21 +73,23 @@
         insecure = settings.get('ovirt-shell:insecure')
         filter_ = settings.get('ovirt-shell:filter')
 
-        if self.context.connection is not None:
-            stdout.write('already connected\n')
+        if self.context.connection is not None and \
+                self.__test_connectivity() and \
+                self.context.status != self.context.COMMUNICATION_ERROR:
+            stdout.write(Messages.Warning.ALREADY_CONNECTED)
             return
         if len(args) == 3:
             url, username, password = args
         else:
             url = settings.get('ovirt-shell:url')
             if not url:
-                self.error('missing configuration variable: url')
+                self.error(Messages.Error.MISSING_CONFIGURATION_VARIABLE % 
'url')
             username = settings.get('ovirt-shell:username')
             if not username:
-                self.error('missing configuration variable: username')
+                self.error(Messages.Error.MISSING_CONFIGURATION_VARIABLE % 
'username')
             password = settings.get('ovirt-shell:password')
             if not password:
-                self.error('missing configuration variable: password')
+                self.error(Messages.Error.MISSING_CONFIGURATION_VARIABLE % 
'password')
 
         try:
             self.context.connection = API(url=url,
@@ -103,10 +105,9 @@
                                           debug=debug)
 
             if context.sdk_version < MIN_FORCE_CREDENTIALS_CHECK_VERSION:
-                self.testConnectivity()
+                self.__test_connectivity()
 
             self.context.url = url
-            self.context._set_prompt()
             self.context.history.enable()
             stdout.write(OvirtCliSettings.CONNECTED_TEMPLATE % \
                          self.context.settings.get('ovirt-shell:version'))
@@ -125,12 +126,12 @@
             self.__cleanContext()
             self.error(str(e))
         finally:
-            #do not log connect command details as it may be
-            #a subject for password stealing or DOS attack
+            # do not log connect command details as it may be
+            # a subject for password stealing or DOS attack
             self.__remove_history_entry()
 
-    def testConnectivity(self):
-        self.context.connection.test(throw_exception=True)
+    def __test_connectivity(self):
+        return self.context.connection.test(throw_exception=True)
 
     def __remove_history_entry(self):
         last_entry = self.context.history.get(self.context.history.length() - 
1)
diff --git a/src/ovirtcli/context.py b/src/ovirtcli/context.py
index cc56ab3..afd057c 100644
--- a/src/ovirtcli/context.py
+++ b/src/ovirtcli/context.py
@@ -125,18 +125,7 @@
                                    version.build_, version.revision)
         return '%s.%s.%s.%s' % backend_version
 
-    def _set_prompt(self):
-        """Update the prompt."""
-        if self.connection is None:
-            prompt = self.settings['ovirt-shell:ps1.disconnected']
-        else:
-            subst = self._get_prompt_variables()
-            prompt = self.settings['ovirt-shell:ps1.connected'] % subst
-
-        self.settings['ovirt-shell:prompt'] = prompt
-
     def _read_command(self):
-        self._set_prompt()
         return super(OvirtCliExecutionContext, self)._read_command()
 
     def _clean_settings(self):
diff --git a/src/ovirtcli/shell/engineshell.py 
b/src/ovirtcli/shell/engineshell.py
index 7e2584b..82a6552 100644
--- a/src/ovirtcli/shell/engineshell.py
+++ b/src/ovirtcli/shell/engineshell.py
@@ -68,14 +68,15 @@
         HistoryCmdShell.__init__(self, context, parser)
         InfoCmdShell.__init__(self, context, parser)
 
+        self.last_output = ''
+        self.__input_buffer = ''
+        self.__org_prompt = ''
+        self.__last_status = -1
+
         self._set_prompt(mode=PromptMode.Disconnected)
         cmd.Cmd.doc_header = self.context.settings.get('ovirt-shell:commands')
         cmd.Cmd.undoc_header = 
self.context.settings.get('ovirt-shell:misc_commands')
         cmd.Cmd.intro = OvirtCliSettings.INTRO
-
-        self.last_output = ''
-        self.__input_buffer = ''
-        self.__org_prompt = ''
 
         readline.set_completer_delims(' ')
         signal.signal(signal.SIGINT, self.handler)
@@ -119,7 +120,17 @@
                     self.__input_buffer = ''
                     self._set_prompt(mode=PromptMode.Original)
 
-                return cmd.Cmd.onecmd(self, s)
+                result = cmd.Cmd.onecmd(self, s)
+
+                # if communication error occurred, change prompt state
+                if self.context.status == self.context.COMMUNICATION_ERROR:
+                    self.__last_status = self.context.COMMUNICATION_ERROR
+                    self.owner._set_prompt(mode=PromptMode.Disconnected)
+                elif self.__last_status == self.context.COMMUNICATION_ERROR:
+                    self.__last_status = -1
+                    self.owner._set_prompt(mode=PromptMode.Original)
+
+                return result
 
     def _set_prompt(self, mode=PromptMode.Default):
         if mode == PromptMode.Multiline:
@@ -131,6 +142,8 @@
                 self.prompt = self.__org_prompt
                 self.__org_prompt = ''
         elif mode == PromptMode.Disconnected or mode == PromptMode.Default:
+            if not self.__org_prompt and self.prompt != "(Cmd) ":
+                self.__org_prompt = self.prompt
             self.prompt = 
self.context.settings.get('ovirt-shell:ps1.disconnected')
         elif mode == PromptMode.Connected:
             self.prompt = 
self.context.settings.get('ovirt-shell:ps2.connected')


--
To view, visit http://gerrit.ovirt.org/10038
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I18c7ec8c986315e30fd4ae0e8e7598cef960fa05
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine-cli
Gerrit-Branch: master
Gerrit-Owner: Michael Pasternak <mpast...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to