summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Grover <agrover@redhat.com>2014-03-03 12:29:21 -0800
committerAndy Grover <agrover@redhat.com>2014-03-03 12:29:21 -0800
commitd549395b1984a7b1018061b6312440db678a2741 (patch)
treecdb907a32c3f0732326ae1e16f50274015545c1a
parentc44d362136b79b008742f5210a740450103876af (diff)
downloadtargetcli-d549395b1984a7b1018061b6312440db678a2741.tar.gz
Change targetcli for configshell exception change
If run_cmdline raises an exception, return 1 as exit code and print exception text to stderr. Keep running shell.run_interactive until shell._exit is set, logging error messages. Overriding execute_command in UINode no longer needed, remove. Signed-off-by: Andy Grover <agrover@redhat.com>
-rwxr-xr-xscripts/targetcli17
-rw-r--r--targetcli/ui_backstore.py14
-rw-r--r--targetcli/ui_node.py15
-rw-r--r--targetcli/ui_root.py12
-rw-r--r--targetcli/ui_target.py19
5 files changed, 33 insertions, 44 deletions
diff --git a/scripts/targetcli b/scripts/targetcli
index a334d11..4f802e9 100755
--- a/scripts/targetcli
+++ b/scripts/targetcli
@@ -18,10 +18,12 @@ License for the specific language governing permissions and limitations
under the License.
'''
+from __future__ import print_function
+
from os import getuid
from targetcli import UIRoot
from rtslib import RTSLibError
-from configshell import ConfigShell
+from configshell import ConfigShell, ExecutionError
import sys
from targetcli import __version__ as targetcli_version
@@ -69,7 +71,11 @@ def main():
sys.exit(-1)
if len(sys.argv) > 1:
- shell.run_cmdline(" ".join(sys.argv[1:]))
+ try:
+ shell.run_cmdline(" ".join(sys.argv[1:]))
+ except Exception as e:
+ print(str(e), file=sys.stderr)
+ sys.exit(1)
sys.exit(0)
shell.con.display("targetcli shell version %s\n"
@@ -79,7 +85,12 @@ def main():
if not is_root:
shell.con.display("You are not root, disabling privileged commands.\n")
- shell.run_interactive()
+ while not shell._exit:
+ try:
+ shell.run_interactive()
+ except (RTSLibError, ExecutionError) as msg:
+ shell.log.error(str(msg))
+
if shell.prefs['auto_save_on_exit'] and is_root:
shell.log.info("Global pref auto_save_on_exit=true")
root_node.ui_command_saveconfig()
diff --git a/targetcli/ui_backstore.py b/targetcli/ui_backstore.py
index 467ab0e..b67a3bb 100644
--- a/targetcli/ui_backstore.py
+++ b/targetcli/ui_backstore.py
@@ -145,12 +145,12 @@ class UIBackstore(UINode):
try:
child = self.get_child(name)
except ValueError:
- self.shell.log.error("No storage object named %s." % name)
- else:
- child.rtsnode.delete()
- self.remove_child(child)
- self.shell.log.info("Deleted storage object %s." % name)
- self.parent.parent.refresh()
+ raise ExecutionError("No storage object named %s." % name)
+
+ child.rtsnode.delete()
+ self.remove_child(child)
+ self.shell.log.info("Deleted storage object %s." % name)
+ self.parent.parent.refresh()
def ui_complete_delete(self, parameters, text, current_param):
'''
@@ -181,7 +181,7 @@ class UIBackstore(UINode):
try:
storageobject.set_attribute("emulate_model_alias", 1)
except RTSLibError:
- self.shell.log.error("'export_backstore_name_as_model' is set but"
+ raise ExecutionError("'export_backstore_name_as_model' is set but"
" emulate_model_alias\n not supported by kernel.")
diff --git a/targetcli/ui_node.py b/targetcli/ui_node.py
index a91e0c4..cfa7b11 100644
--- a/targetcli/ui_node.py
+++ b/targetcli/ui_node.py
@@ -77,21 +77,6 @@ class UINode(ConfigNode):
for child in self.children:
child.refresh()
- def execute_command(self, command, pparams=[], kparams={}):
- '''
- We overload this one in order to handle our own exceptions cleanly,
- and not just configshell's ExecutionError.
- '''
- try:
- result = ConfigNode.execute_command(self, command,
- pparams, kparams)
- except RTSLibError as msg:
- self.shell.log.error(str(msg))
- else:
- self.shell.log.debug("Command %s succeeded." % command)
- return result
-
-
def ui_command_refresh(self):
'''
Refreshes and updates the objects tree from the current path.
diff --git a/targetcli/ui_root.py b/targetcli/ui_root.py
index e59d86d..ac60133 100644
--- a/targetcli/ui_root.py
+++ b/targetcli/ui_root.py
@@ -101,15 +101,13 @@ class UIRoot(UINode):
errors = self.rtsroot.restore_from_file(savefile, clear_existing)
+ self.refresh()
+
if errors:
- self.shell.log.error("Configuration restored, %d recoverable errors:" % \
- len(errors))
- for error in errors:
- self.shell.log.error(error)
- else:
- self.shell.log.info("Configuration restored from %s" % savefile)
+ raise ExecutionError("Configuration restored, %d recoverable errors:\n%s" % \
+ (len(errors), "\n".join(errors)))
- self.refresh()
+ self.shell.log.info("Configuration restored from %s" % savefile)
def ui_command_clearconfig(self, confirm=None):
'''
diff --git a/targetcli/ui_target.py b/targetcli/ui_target.py
index d66f10c..2ab299d 100644
--- a/targetcli/ui_target.py
+++ b/targetcli/ui_target.py
@@ -456,8 +456,8 @@ class UITPG(UIRTSLibNode):
try:
self.rtsnode.enable = True
self.shell.log.info("The TPGT has been enabled.")
- except:
- self.shell.log.error("The TPGT could not be enabled.")
+ except RTSLibError:
+ raise ExecutionError("The TPGT could not be enabled.")
def ui_command_disable(self):
'''
@@ -626,13 +626,11 @@ class UINodeACLs(UINode):
# Since all WWNs have a '.' in them, let's avoid confusion.
if '.' in new_tag:
- self.shell.log.error("'.' not permitted in tag names.")
- return
+ raise ExecutionError("'.' not permitted in tag names.")
src = list(self.find_tagged(wwn_or_tag))
if not src:
- self.shell.log.error("wwn_or_tag %s not found." % wwn_or_tag)
- return
+ raise ExecutionError("wwn_or_tag %s not found." % wwn_or_tag)
old_tag_members = list(self.find_tagged(new_tag))
@@ -809,8 +807,7 @@ class UINodeACL(UIRTSLibNode):
try:
mapped_lun = int(mapped_lun)
except ValueError:
- self.shell.log.error("mapped_lun must be an integer")
- return
+ raise ExecutionError("mapped_lun must be an integer")
try:
if tpg_lun_or_backstore.startswith("lun"):
@@ -820,8 +817,7 @@ class UINodeACL(UIRTSLibNode):
try:
so = self.get_node(tpg_lun_or_backstore).rtsnode
except ValueError:
- self.shell.log.error("LUN or storage object not found")
- return
+ raise ExecutionError("LUN or storage object not found")
ui_tpg = self.parent.parent
@@ -1027,8 +1023,7 @@ class UILUNs(UINode):
try:
so = self.get_node(storage_object).rtsnode
except ValueError:
- self.shell.log.error("Invalid storage object %s." % storage_object)
- return
+ raise ExecutionError("Invalid storage object %s." % storage_object)
if so in (l.storage_object for l in self.parent.rtsnode.luns):
raise ExecutionError("lun for storage object %s already exists" \