From a92465922fb74ca2c9b392e1c1b7ed5b5e306a76 Mon Sep 17 00:00:00 2001 From: andy Date: Thu, 19 Aug 2010 12:28:45 +0200 Subject: Data abstraction for compute service --- nova/utils.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index e826f9b71..9e12a5301 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -142,3 +142,36 @@ def isotime(at=None): def parse_isotime(timestr): return datetime.datetime.strptime(timestr, TIME_FORMAT) + + + +class LazyPluggable(object): + """A pluggable backend loaded lazily based on some value.""" + + def __init__(self, pivot, **backends): + self.__backends = backends + self.__pivot = pivot + self.__backend = None + + def __get_backend(self): + if not self.__backend: + backend_name = self.__pivot.value + if backend_name not in self.__backends: + raise exception.Error('Invalid backend: %s' % backend_name) + + backend = self.__backends[backend_name] + if type(backend) == type(tuple()): + name = backend[0] + fromlist = backend[1] + else: + name = backend + fromlist = backend + + self.__backend = __import__(name, None, None, fromlist) + logging.error('backend %s', self.__backend) + return self.__backend + + def __getattr__(self, key): + backend = self.__get_backend() + return getattr(backend, key) + -- cgit From 6ae66c595d4f85802045734ed1b230a292f9c953 Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Fri, 20 Aug 2010 13:26:24 +0100 Subject: Better error message on subprocess spawn fail, and it's a ProcessExecutionException irrespective of how the process is run. --- nova/utils.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index dc3c626ec..b8abb5388 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -36,6 +36,16 @@ from nova import flags FLAGS = flags.FLAGS TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" +class ProcessExecutionError(IOError): + def __init__( self, stdout=None, stderr=None, exit_code=None, cmd=None, + description=None): + if description is None: + description = "Unexpected error while running command." + if exit_code is None: + exit_code = '-' + message = "%s\nCommand: %s\nExit code: %s\nStdout: %r\nStderr: %r" % ( + description, cmd, exit_code, stdout, stderr) + IOError.__init__(self, message) def import_class(import_str): """Returns a class from a string including module and class""" @@ -73,8 +83,11 @@ def execute(cmd, process_input=None, addl_env=None, check_exit_code=True): if obj.returncode: logging.debug("Result was %s" % (obj.returncode)) if check_exit_code and obj.returncode <> 0: - raise Exception( "Unexpected exit code: %s. result=%s" - % (obj.returncode, result)) + (stdout, stderr) = result + raise ProcessExecutionError(exit_code=obj.returncode, + stdout=stdout, + stderr=stderr, + cmd=cmd) return result -- cgit From 8d0f96432b7b07fa608cae433459645880f4a44c Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 27 Aug 2010 23:10:57 -0700 Subject: split volume into service/manager/driver --- nova/utils.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index c4a8f17e9..392fa8c46 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -46,6 +46,13 @@ def import_class(import_str): except (ImportError, ValueError, AttributeError): raise exception.NotFound('Class %s cannot be found' % class_str) +def import_object(import_str): + """Returns an object including a module or module and class""" + cls = import_class(import_str) + try: + return cls() + except TypeError: + return cls def fetchfile(url, target): logging.debug("Fetching %s" % url) @@ -73,7 +80,7 @@ def execute(cmd, process_input=None, addl_env=None, check_exit_code=True): if obj.returncode: logging.debug("Result was %s" % (obj.returncode)) if check_exit_code and obj.returncode <> 0: - raise Exception( "Unexpected exit code: %s. result=%s" + raise Exception( "Unexpected exit code: %s. result=%s" % (obj.returncode, result)) return result @@ -105,7 +112,7 @@ def runthis(prompt, cmd, check_exit_code = True): exit_code = subprocess.call(cmd.split(" ")) logging.debug(prompt % (exit_code)) if check_exit_code and exit_code <> 0: - raise Exception( "Unexpected exit code: %s from cmd: %s" + raise Exception( "Unexpected exit code: %s from cmd: %s" % (exit_code, cmd)) @@ -150,21 +157,21 @@ def parse_isotime(timestr): return datetime.datetime.strptime(timestr, TIME_FORMAT) - + class LazyPluggable(object): """A pluggable backend loaded lazily based on some value.""" - + def __init__(self, pivot, **backends): self.__backends = backends self.__pivot = pivot self.__backend = None - + def __get_backend(self): if not self.__backend: backend_name = self.__pivot.value if backend_name not in self.__backends: raise exception.Error('Invalid backend: %s' % backend_name) - + backend = self.__backends[backend_name] if type(backend) == type(tuple()): name = backend[0] @@ -172,11 +179,11 @@ class LazyPluggable(object): else: name = backend fromlist = backend - + self.__backend = __import__(name, None, None, fromlist) logging.error('backend %s', self.__backend) return self.__backend - + def __getattr__(self, key): backend = self.__get_backend() return getattr(backend, key) -- cgit From 9c98cfb47175ca9ace5c0bd731085896303e3e7b Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Mon, 30 Aug 2010 00:55:19 -0700 Subject: instance runs --- nova/utils.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index 392fa8c46..12896c488 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -48,11 +48,13 @@ def import_class(import_str): def import_object(import_str): """Returns an object including a module or module and class""" - cls = import_class(import_str) try: + __import__(import_str) + return sys.modules[import_str] + except ImportError: + cls = import_class(import_str) + print cls return cls() - except TypeError: - return cls def fetchfile(url, target): logging.debug("Fetching %s" % url) -- cgit From 40899259205561b43791f1540ec3f9100a4869d1 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Mon, 30 Aug 2010 09:03:43 -0700 Subject: ip addresses work now --- nova/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index 12896c488..705df718e 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -53,7 +53,6 @@ def import_object(import_str): return sys.modules[import_str] except ImportError: cls = import_class(import_str) - print cls return cls() def fetchfile(url, target): @@ -136,6 +135,7 @@ def last_octet(address): def get_my_ip(): ''' returns the actual ip of the local machine. ''' + return '127.0.0.1' if getattr(FLAGS, 'fake_tests', None): return '127.0.0.1' try: -- cgit From 6c50b37c0b60219837f940d044542f4032a4436b Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Mon, 30 Aug 2010 15:15:00 -0700 Subject: undo change to get_my_ip --- nova/utils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index 705df718e..907c174cd 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -133,9 +133,7 @@ def last_octet(address): def get_my_ip(): - ''' returns the actual ip of the local machine. - ''' - return '127.0.0.1' + """Returns the actual ip of the local machine.""" if getattr(FLAGS, 'fake_tests', None): return '127.0.0.1' try: -- cgit From 9db707dda70bbb11d944ab357841c9bdd5ef5b07 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Tue, 7 Sep 2010 05:26:08 -0700 Subject: Lots of fixes to make the nova commands work properly and make datamodel work with mysql properly --- nova/utils.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index 536d722bb..3e4a3d94f 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -38,6 +38,16 @@ from nova import flags FLAGS = flags.FLAGS TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" +class ProcessExecutionError(IOError): + def __init__( self, stdout=None, stderr=None, exit_code=None, cmd=None, + description=None): + if description is None: + description = "Unexpected error while running command." + if exit_code is None: + exit_code = '-' + message = "%s\nCommand: %s\nExit code: %s\nStdout: %r\nStderr: %r" % ( + description, cmd, exit_code, stdout, stderr) + IOError.__init__(self, message) def import_class(import_str): """Returns a class from a string including module and class""" @@ -69,6 +79,7 @@ def fetchfile(url, target): execute("curl --fail %s -o %s" % (url, target)) def execute(cmd, process_input=None, addl_env=None, check_exit_code=True): + logging.debug("Running cmd: %s", cmd) env = os.environ.copy() if addl_env: env.update(addl_env) @@ -83,8 +94,11 @@ def execute(cmd, process_input=None, addl_env=None, check_exit_code=True): if obj.returncode: logging.debug("Result was %s" % (obj.returncode)) if check_exit_code and obj.returncode <> 0: - raise Exception( "Unexpected exit code: %s. result=%s" - % (obj.returncode, result)) + (stdout, stderr) = result + raise ProcessExecutionError(exit_code=obj.returncode, + stdout=stdout, + stderr=stderr, + cmd=cmd) return result -- cgit From fc5e1c6f0bee14fdb85ad138324062ceaa598eee Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Tue, 7 Sep 2010 21:53:40 -0700 Subject: a few formatting fixes and moved exception --- nova/utils.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index b8abb5388..d302412ad 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -31,21 +31,12 @@ import sys from nova import exception from nova import flags +from nova.exception import ProcessExecutionError FLAGS = flags.FLAGS TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" -class ProcessExecutionError(IOError): - def __init__( self, stdout=None, stderr=None, exit_code=None, cmd=None, - description=None): - if description is None: - description = "Unexpected error while running command." - if exit_code is None: - exit_code = '-' - message = "%s\nCommand: %s\nExit code: %s\nStdout: %r\nStderr: %r" % ( - description, cmd, exit_code, stdout, stderr) - IOError.__init__(self, message) def import_class(import_str): """Returns a class from a string including module and class""" @@ -118,8 +109,10 @@ def runthis(prompt, cmd, check_exit_code = True): exit_code = subprocess.call(cmd.split(" ")) logging.debug(prompt % (exit_code)) if check_exit_code and exit_code <> 0: - raise Exception( "Unexpected exit code: %s from cmd: %s" - % (exit_code, cmd)) + raise ProcessExecutionError(exit_code=exit_code, + stdout=None, + stderr=None, + cmd=cmd) def generate_uid(topic, size=8): -- cgit From 37a8ca37db8a51455faf9b4a3bead95c453e8183 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 03:20:56 -0700 Subject: logging for backend is now info instead of error --- nova/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index 3e4a3d94f..011a5cb09 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -196,7 +196,7 @@ class LazyPluggable(object): fromlist = backend self.__backend = __import__(name, None, None, fromlist) - logging.error('backend %s', self.__backend) + logging.info('backend %s', self.__backend) return self.__backend def __getattr__(self, key): -- cgit From 6cbf8b736cc2c9929c2ad69ddc8e8b4fc2d0f4ae Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Sun, 12 Sep 2010 23:09:15 -0700 Subject: removed second copy of ProcessExecutionError --- nova/utils.py | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index 8939043e6..d18dd9843 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -39,17 +39,6 @@ from nova.exception import ProcessExecutionError FLAGS = flags.FLAGS TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" -class ProcessExecutionError(IOError): - def __init__( self, stdout=None, stderr=None, exit_code=None, cmd=None, - description=None): - if description is None: - description = "Unexpected error while running command." - if exit_code is None: - exit_code = '-' - message = "%s\nCommand: %s\nExit code: %s\nStdout: %r\nStderr: %r" % ( - description, cmd, exit_code, stdout, stderr) - IOError.__init__(self, message) - def import_class(import_str): """Returns a class from a string including module and class""" mod_str, _sep, class_str = import_str.rpartition('.') -- cgit