[node-patches] Change in ovirt-node[master]: console: Add module for console access

fabiand at fedoraproject.org fabiand at fedoraproject.org
Thu Jan 3 16:39:15 UTC 2013


Fabian Deutsch has uploaded a new change for review.

Change subject: console: Add module for console access
......................................................................

console: Add module for console access

Initially this module contains a class to display the progress of a
transaction on a plain console (so without a TUI), this is needed e.g.
for KDump configuration as this requires user input for ssh-copy-id.

Change-Id: Ia63946ffc00a91941be7dc2d508f76e88d36b8d1
Signed-off-by: Fabian Deutsch <fabiand at fedoraproject.org>
---
M scripts/tui/src/ovirt/node/app.py
A scripts/tui/src/ovirt/node/utils/console.py
2 files changed, 98 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/34/10634/1

diff --git a/scripts/tui/src/ovirt/node/app.py b/scripts/tui/src/ovirt/node/app.py
index 5e2db1c..f12284b 100644
--- a/scripts/tui/src/ovirt/node/app.py
+++ b/scripts/tui/src/ovirt/node/app.py
@@ -34,33 +34,34 @@
     'disable_existing_loggers': True,
     'formatters': {
         'verbose': {
-            'format': '%(levelname)s %(asctime)s %(name)s %(process)d %(message)s'
+            'format': '%(levelname)s %(asctime)s %(name)s ' +
+                      '%(process)d %(message)s'
         },
         'simple': {
             'format': '%(asctime)s %(levelname)10s %(message)s'
         },
     },
     'handlers': {
-        'file':{
-            'level':'INFO',
-            'class':'logging.FileHandler',
+        'file': {
+            'level': 'INFO',
+            'class': 'logging.FileHandler',
             'formatter': 'simple',
-            'filename':'/tmp/ovirt.log',
+            'filename': '/tmp/ovirt.log',
             'mode': 'w'
         },
-        'debug':{
-            'level':'DEBUG',
-            'class':'logging.FileHandler',
+        'debug': {
+            'level': 'DEBUG',
+            'class': 'logging.FileHandler',
             'formatter': 'verbose',
-            'filename':'/tmp/ovirt.debug.log',
+            'filename': '/tmp/ovirt.debug.log',
             'mode': 'w'
         },
     },
     'loggers': {
         'ovirt.node': {
-            'handlers':['debug'],
+            'handlers': ['debug'],
             'propagate': True,
-            'level':'DEBUG',
+            'level': 'DEBUG',
         },
         'ovirt.node': {
             'handlers': ['file'],
@@ -167,4 +168,4 @@
 
     def quit(self):
         self.logger.info("Quitting")
-        self.ui.quit()
\ No newline at end of file
+        self.ui.quit()
diff --git a/scripts/tui/src/ovirt/node/utils/console.py b/scripts/tui/src/ovirt/node/utils/console.py
new file mode 100644
index 0000000..029ad96
--- /dev/null
+++ b/scripts/tui/src/ovirt/node/utils/console.py
@@ -0,0 +1,85 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+#
+# console.py - Copyright (C) 2013 Red Hat, Inc.
+# Written by Fabian Deutsch <fabiand at redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.  A copy of the GNU General Public License is
+# also available at http://www.gnu.org/copyleft/gpl.html.
+from ovirt.node import base
+import traceback
+import sys
+
+
+def writeln(txts):
+    """Write something to stdout
+    A wrapper if we want to do this differently in future
+    """
+    if type(txts) is not list:
+        txts = [txts]
+
+    sys.stdout.write("\n".join(txts))
+    sys.stdout.write("\n")
+
+
+def wait_for_keypress():
+    sys.stdin.read(1)
+
+
+class TransactionProgress(base.Base):
+    """Display the progress of a transaction on a console
+    """
+    def __init__(self, transaction, plugin, initial_text=""):
+        self.transaction = transaction
+        self.plugin = plugin
+        self.texts = [initial_text, ""]
+        super(TransactionProgress, self).__init__()
+
+    def add_update(self, txt):
+        self.texts.append(txt)
+        self.logger.debug(txt)
+        self._print_func(txt)
+
+    def _print_func(self, txt):
+        writeln(txt)
+
+    def run(self):
+        if self.transaction:
+            self.logger.debug("Initiating transaction")
+            self.__run_transaction()
+        else:
+            self.add_update("There were no changes, nothing to do.")
+
+    def __print_title(self):
+        writeln([self.transaction.title,
+                 "-" * len(self.transaction.title)])
+
+    def __run_transaction(self):
+        try:
+            self.__print_title()
+            self.logger.debug("Preparing transaction for console %s" %
+                              self.transaction)
+            self.transaction.prepare()  # Just to display something in dry mode
+            for idx, e in enumerate(self.transaction):
+                txt = "(%s/%s) %s" % (idx + 1, len(self.transaction), e.title)
+                self.add_update(txt)
+                self.plugin.dry_or(lambda: e.commit())
+            self.add_update("\nAll changes were applied successfully.")
+        except Exception as e:
+            self.add_update("\nAn error occurred while applying the changes:")
+            self.add_update("%s" % e.message)
+            self.logger.warning("'%s' on transaction '%s': %s - %s" %
+                                (type(e), self.transaction, e, e.message))
+            self.logger.debug(str(traceback.format_exc()))


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia63946ffc00a91941be7dc2d508f76e88d36b8d1
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: master
Gerrit-Owner: Fabian Deutsch <fabiand at fedoraproject.org>



More information about the node-patches mailing list