Replaced RuntimeErrors with specific errors

Change-Id: Ief2e74fa07e4c5168f5cc00fd4017d80331cb180
Closes-Bug: #1199071
This commit is contained in:
Andrew Lazarev 2014-05-21 16:39:25 -07:00
parent c1d6d02ab7
commit 629932c307
22 changed files with 94 additions and 65 deletions

View File

@ -15,6 +15,7 @@
from oslo.config import cfg
from sahara import exceptions as ex
from sahara.openstack.common import log
from sahara import version
@ -94,16 +95,17 @@ def parse_configs(argv=None, conf_files=None):
CONF(ARGV, project='sahara', version=version_string,
default_config_files=conf_files)
except cfg.RequiredOptError as roe:
# TODO(slukjanov): replace RuntimeError with concrete exception
raise RuntimeError("Option '%s' is required for config group "
"'%s'" % (roe.opt_name, roe.group.name))
raise ex.ConfigurationError(
"Option '%s' is required for config group '%s'" %
(roe.opt_name, roe.group.name))
validate_configs()
def validate_network_configs():
if CONF.use_namespaces and not CONF.use_neutron:
raise RuntimeError('use_namespaces can not be set to "True" when '
'use_neutron is set to "False"')
raise ex.ConfigurationError(
'use_namespaces can not be set to "True" when use_neutron is set '
'to "False"')
def validate_configs():

View File

@ -20,7 +20,7 @@ from eventlet import greenpool
from eventlet import semaphore
from oslo.config import cfg
from sahara import exceptions
from sahara import exceptions as ex
from sahara.openstack.common import log as logging
@ -97,8 +97,7 @@ def has_ctx():
def ctx():
if not has_ctx():
# TODO(slukjanov): replace with specific error
raise RuntimeError("Context isn't available here")
raise ex.IncorrectStateError("Context isn't available here")
return getattr(_CTX_STORE, _CTX_KEY)
@ -176,7 +175,7 @@ class ThreadGroup(object):
self.cv.wait()
if self.exc:
raise exceptions.ThreadException(self.failed_thread, self.exc)
raise ex.ThreadException(self.failed_thread, self.exc)
def __enter__(self):
return self

View File

@ -32,7 +32,7 @@ class SaharaException(Exception):
class NotFoundException(SaharaException):
message = "Object not found"
message = "Object '%s' is not found"
# It could be a various property of object which was not found
value = None
@ -195,3 +195,24 @@ class HeatStackException(SaharaException):
def __init__(self, heat_stack_status):
self.code = "HEAT_STACK_EXCEPTION"
self.message = "Heat stack failed with status %s" % heat_stack_status
class ConfigurationError(SaharaException):
code = "CONFIGURATION_ERROR"
def __init__(self, message):
self.message = message
class IncorrectStateError(SaharaException):
code = "INCORRECT_STATE_ERROR"
def __init__(self, message):
self.message = message
class SystemError(SaharaException):
code = "SYSTEM_ERROR"
def __init__(self, message):
self.message = message

View File

@ -19,6 +19,7 @@ from oslo.config import cfg
import six
from stevedore import enabled
from sahara import exceptions as ex
from sahara.openstack.common import log as logging
from sahara.utils import resources
@ -96,9 +97,8 @@ class PluginManager(object):
for ext in extension_manager.extensions:
if ext.name in self.plugins:
# TODO(slukjanov): replace with specific exception
raise RuntimeError("Plugin with name '%s' already exists." %
ext.name)
raise ex.ConfigurationError(
"Plugin with name '%s' already exists." % ext.name)
ext.obj.name = ext.name
self.plugins[ext.name] = ext.obj
LOG.info("Plugin '%s' loaded (%s)"
@ -107,9 +107,9 @@ class PluginManager(object):
if len(self.plugins) < len(config_plugins):
loaded_plugins = set(six.iterkeys(self.plugins))
requested_plugins = set(config_plugins)
# TODO(slukjanov): replace with specific exception
raise RuntimeError("Plugins couldn't be loaded: %s"
% ", ".join(requested_plugins - loaded_plugins))
raise ex.ConfigurationError(
"Plugins couldn't be loaded: %s" %
", ".join(requested_plugins - loaded_plugins))
def get_plugins(self, base):
return [

View File

@ -392,14 +392,14 @@ class AmbariClient():
else:
LOG.critical('Failed to change state of Hadoop '
'components')
raise RuntimeError('Failed to change state of Hadoop '
'components')
raise ex.HadoopProvisionError(
'Failed to change state of Hadoop components')
else:
LOG.error(
'Command failed. Status: {0}, response: {1}'.
format(result.status_code, result.text))
raise RuntimeError('Hadoop/Ambari command failed.')
raise ex.HadoopProvisionError('Hadoop/Ambari command failed.')
def _get_host_list(self, servers):
host_list = [server.instance.fqdn().lower() for server in servers]

View File

@ -391,14 +391,14 @@ class AmbariClient():
else:
LOG.critical('Failed to change state of Hadoop '
'components')
raise RuntimeError('Failed to change state of Hadoop '
'components')
raise ex.HadoopProvisionError(
'Failed to change state of Hadoop components')
else:
LOG.error(
'Command failed. Status: {0}, response: {1}'.
format(result.status_code, result.text))
raise RuntimeError('Hadoop/Ambari command failed.')
raise ex.HadoopProvisionError('Hadoop/Ambari command failed.')
def _get_host_list(self, servers):
host_list = [server.instance.fqdn().lower() for server in servers]

View File

@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from sahara import exceptions as ex
from sahara.plugins import base as plugins_base
from sahara.utils import resources
@ -119,14 +120,14 @@ class ProvisioningPluginBase(plugins_base.PluginInterface):
for config_name in configs[applicable_target]:
confs = config_objs_map.get(applicable_target)
if not confs:
# TODO(slukjanov): raise specific exception
raise RuntimeError("Can't find applicable target '%s'"
% applicable_target)
raise ex.ConfigurationError(
"Can't find applicable target '%s' for '%s'"
% (applicable_target, config_name))
conf = confs.get(config_name)
if not conf:
# TODO(slukjanov): raise specific exception
raise RuntimeError("Can't find config '%s' in '%s'"
% (config_name, applicable_target))
raise ex.ConfigurationError(
"Can't find config '%s' in '%s'"
% (config_name, applicable_target))
result.append(UserInput(
conf, configs[applicable_target][config_name]))

View File

@ -17,6 +17,7 @@ from oslo.config import cfg
from sahara import conductor as c
from sahara import context
from sahara import exceptions as ex
from sahara.openstack.common import log as logging
from sahara.plugins.general import utils
from sahara.plugins import provisioning as p
@ -205,8 +206,8 @@ def get_config_value(service, name, cluster=None):
if c.applicable_target == service and c.name == name:
return c.default_value
raise RuntimeError("Unable get parameter '%s' from service %s" %
(name, service))
raise ex.ConfigurationError("Unable get parameter '%s' from service %s" %
(name, service))
def generate_cfg_from_general(cfg, configs, general_config,

View File

@ -15,6 +15,7 @@
from sahara import context
from sahara.openstack.common import log as logging
from sahara.plugins.general import exceptions as ex
from sahara.plugins.vanilla import utils as vu
from sahara.plugins.vanilla.v2_3_0 import config_helper as c_helper
from sahara.utils import files
@ -31,7 +32,8 @@ def start_instance(instance):
elif process in ['resourcemanager', 'nodemanager']:
start_yarn_process(instance, process)
else:
raise RuntimeError("Process is not supported")
raise ex.HadoopProvisionError(
"Process %s is not supported" % process)
def start_hadoop_process(instance, process):

View File

@ -19,6 +19,7 @@ import six
from sahara import conductor as c
from sahara import context
from sahara import exceptions as exc
from sahara.openstack.common import excutils
from sahara.openstack.common import log as logging
from sahara.service import engine as e
@ -275,8 +276,7 @@ class DirectEngine(e.Engine):
server = nova.get_instance_info(instance)
if server.status == 'ERROR':
# TODO(slukjanov): replace with specific error
raise RuntimeError("node %s has error status" % server.name)
raise exc.SystemError("Node %s has error status" % server.name)
return server.status == 'ACTIVE'

View File

@ -17,6 +17,7 @@ import re
from sahara import conductor as c
from sahara import context
from sahara import exceptions as ex
from sahara.openstack.common import log as logging
from sahara.utils.openstack import cinder
from sahara.utils.openstack import nova
@ -52,8 +53,8 @@ def _await_attach_volumes(instance, count_volumes):
timeout -= step
context.sleep(step)
raise RuntimeError("Error attach volume to instance %s" %
instance.instance_name)
raise ex.SystemError("Error attach volume to instance %s" %
instance.instance_name)
def _attach_volumes_to_node(node_group, instance, volume_type=None):
@ -79,7 +80,7 @@ def _create_attach_volume(ctx, instance, size, display_name=None,
while volume.status != 'available':
volume = cinder.get_volume(volume.id)
if volume.status == 'error':
raise RuntimeError("Volume %s has error status" % volume.id)
raise ex.SystemError("Volume %s has error status" % volume.id)
context.sleep(1)

View File

@ -32,12 +32,7 @@ HADOOP_SWIFT_REGION = 'fs.swift.service.sahara.region'
def retrieve_tenant():
try:
return context.current().tenant_name
except RuntimeError:
LOG.exception("Cannot retrieve tenant for swift integration.")
#todo(slukjanov?) raise special error here
raise RuntimeError("Cannot retrieve tenant for swift integration")
return context.current().tenant_name
def get_swift_configs():

View File

@ -421,7 +421,7 @@ class ITConfig:
'\nINFO: Configuration file "%s" not found *\n'
'**************************************************'
% config)
print(RuntimeError(message), file=sys.stderr)
print(message, file=sys.stderr)
else:
config = os.path.join(

View File

@ -77,7 +77,7 @@ class TestConductorClusterApi(base.SaharaWithDbTestCase):
if obj.id == id:
return obj
raise RuntimeError('No such object with id %s' % id)
return None
def test_update_by_id(self):
ctx, cluster = self._make_sample()

View File

@ -15,6 +15,7 @@
import unittest2
from sahara import exceptions as ex
from sahara.plugins import provisioning as p
@ -41,14 +42,14 @@ class ProvisioningPluginBaseTest(unittest2.TestCase):
def test__map_to_user_inputs_failure(self):
c1, c2, c3, plugin = _build_configs_and_plugin()
with self.assertRaises(RuntimeError):
with self.assertRaises(ex.ConfigurationError):
plugin._map_to_user_inputs(None, {
'at-X': {
'n-1': 'v-1',
},
})
with self.assertRaises(RuntimeError):
with self.assertRaises(ex.ConfigurationError):
plugin._map_to_user_inputs(None, {
'at-1': {
'n-X': 'v-1',

View File

@ -17,6 +17,7 @@ import mock
from sahara import conductor as cond
from sahara import context
from sahara import exceptions as e
from sahara.plugins.general import exceptions as ex
from sahara.plugins.vanilla import plugin as p
from sahara.plugins.vanilla.v1_2_1 import config_helper as c_h
@ -184,7 +185,7 @@ class VanillaPluginTest(base.SaharaWithDbTestCase):
c_h.get_config_value('HDFS', 'spam', cluster), 'eggs')
self.assertEqual(
c_h.get_config_value('HDFS', 'dfs.safemode.extension'), 30000)
self.assertRaises(RuntimeError,
self.assertRaises(e.ConfigurationError,
c_h.get_config_value,
'MapReduce', 'spam', cluster)

View File

@ -98,7 +98,7 @@ class TestAttachVolume(base.SaharaWithDbTestCase):
instance = r.InstanceResource({'instance_id': '123454321',
'instance_name': 'instt'})
self.assertIsNone(volumes._await_attach_volumes(instance, 2))
self.assertRaises(RuntimeError, volumes._await_attach_volumes,
self.assertRaises(ex.SystemError, volumes._await_attach_volumes,
instance, 3)
def test_get_unmounted_devices(self):

View File

@ -18,6 +18,7 @@ import os
import paramiko
import six
from sahara import exceptions as ex
from sahara.openstack.common import processutils
from sahara.utils import tempfiles
@ -46,13 +47,11 @@ def generate_key_pair(key_length=2048):
args.extend(['-b', key_length])
processutils.execute(*args)
if not os.path.exists(keyfile):
# TODO(slukjanov): replace with specific exception
raise RuntimeError("Private key file hasn't been created")
raise ex.SystemError("Private key file hasn't been created")
private_key = open(keyfile).read()
public_key_path = keyfile + '.pub'
if not os.path.exists(public_key_path):
# TODO(slukjanov): replace with specific exception
raise RuntimeError("Public key file hasn't been created")
raise ex.SystemError("Public key file hasn't been created")
public_key = open(public_key_path).read()
return private_key, public_key

View File

@ -16,6 +16,7 @@
import json
from oslo.config import cfg
from sahara import exceptions as ex
CONF = cfg.CONF
@ -37,12 +38,13 @@ def url_for(service_catalog, service_type, admin=False, endpoint_type=None):
try:
return _get_endpoint_url(endpoints, endpoint_type)
except Exception:
raise RuntimeError("For service %s not found "
"endpoint with type %s"
% (service_type, endpoint_type))
raise ex.SystemError(
"Endpoint with type %s is not found for service %s"
% (endpoint_type, service_type))
else:
raise Exception('Service "%s" not found' % service_type)
raise ex.SystemError('Service "%s" not found in service catalog'
% service_type)
def _get_service_from_catalog(catalog, service_type):

View File

@ -116,8 +116,8 @@ class ClusterTemplate(object):
if stack.stack_name == self.cluster.name:
return ClusterStack(self, stack)
raise RuntimeError('Failed to find just created stack %s' %
self.cluster.name)
raise ex.HeatStackException(
'Failed to find just created stack %s' % self.cluster.name)
def _serialize_resources(self):
resources = []

View File

@ -22,6 +22,7 @@ import requests
from requests import adapters
from sahara import context
from sahara import exceptions as ex
from sahara.openstack.common import log as logging
from sahara.utils.openstack import base
@ -73,8 +74,8 @@ class NeutronClientRemoteWrapper():
break
if not matching_router:
raise RuntimeError('Neutron router not found corresponding to '
'network {0}'.format(self.network))
raise ex.SystemError('Neutron router corresponding to network {0} '
'is not found'.format(self.network))
return matching_router['id']
@ -169,7 +170,7 @@ class NetcatSocket:
try:
self.process.stdin.write(content)
except IOError as e:
raise RuntimeError(' '.join(self.cmd), e.strerror)
raise ex.SystemError(e)
return len(content)
def sendall(self, content):
@ -180,13 +181,13 @@ class NetcatSocket:
return self.process.stdout
if mode.startswith('w'):
return self.process.stdin
raise RuntimeError("Unknown mode %s" % mode)
raise ex.IncorrectStateError("Unknown file mode %s" % mode)
def recv(self, size):
try:
return os.read(self.process.stdout.fileno(), size)
except IOError as e:
raise RuntimeError(' '.join(self.cmd), e.strerror)
raise ex.SystemError(e)
def _terminate(self):
self.process.terminate()

View File

@ -17,6 +17,8 @@ import contextlib
import shutil
import tempfile
from sahara import exceptions as ex
@contextlib.contextmanager
def tempdir(**kwargs):
@ -29,5 +31,6 @@ def tempdir(**kwargs):
finally:
try:
shutil.rmtree(tmpdir)
except OSError:
raise RuntimeError("error")
except OSError as e:
raise ex.SystemError("Failed to delete temp dir %s (reason: %s)" %
(tmpdir, e))