Просмотр исходного кода

Merge branch 'master' of github.com:gvlproject/cloudbridge

Enis Afgan 10 лет назад
Родитель
Сommit
5228a95f69

+ 18 - 1
cloudbridge/cloud/base.py

@@ -4,6 +4,7 @@ Implementation of common methods across cloud providers.
 
 import itertools
 import logging
+import os
 import time
 
 import six
@@ -13,6 +14,7 @@ from cloudbridge.cloud.interfaces.resources \
     import InvalidConfigurationException
 from cloudbridge.cloud.interfaces.resources import Bucket
 from cloudbridge.cloud.interfaces.resources import BucketObject
+from cloudbridge.cloud.interfaces.resources import Configuration
 from cloudbridge.cloud.interfaces.resources import Instance
 from cloudbridge.cloud.interfaces.resources import InstanceState
 from cloudbridge.cloud.interfaces.resources import InstanceType
@@ -52,7 +54,7 @@ log = logging.getLogger(__name__)
 DEFAULT_RESULT_LIMIT = 50
 
 
-class BaseConfiguration(dict):
+class BaseConfiguration(Configuration):
 
     def __init__(self, user_config):
         self.update(user_config)
@@ -68,6 +70,21 @@ class BaseConfiguration(dict):
         """
         return self.get('result_limit', DEFAULT_RESULT_LIMIT)
 
+    @property
+    def debug_mode(self):
+        """
+        A flag indicating whether CloudBridge is in debug mode. Setting
+        this to True will cause the underlying provider's debug
+        output to be turned on.
+
+        The flag can be toggled by sending in the cb_debug value via
+        the config dictionary, or setting the CB_DEBUG environment variable.
+
+        :rtype: ``bool``
+        :return: Whether debug mode is on.
+        """
+        return self.get('cb_debug', os.environ.get('CB_DEBUG', False))
+
 
 class BaseCloudProvider(CloudProvider):
 

+ 16 - 2
cloudbridge/cloud/interfaces/resources.py

@@ -68,6 +68,20 @@ class Configuration(dict):
         """
         pass
 
+    @abstractproperty
+    def debug_mode(self):
+        """
+        A flag indicating whether CloudBridge is in debug mode. Setting
+        this to True will cause the underlying provider's debug
+        output to be turned on.
+
+        The flag can be toggled by sending in the cb_debug value via
+        the config dictionary, or setting the CB_DEBUG environment variable.
+
+        :rtype: ``bool``
+        :return: Whether debug mode is on.
+        """
+
 
 class ObjectLifeCycleMixin(object):
 
@@ -539,7 +553,7 @@ class LaunchConfig(object):
         lc.add_network_interface(...)
 
         inst = provider.compute.instances.create(name, image, instance_type,
-                                               launch_configuration=lc)
+                                               launch_config=lc)
     """
 
     @abstractmethod
@@ -604,7 +618,7 @@ class LaunchConfig(object):
             lc.add_volume_device(source=snap)
 
             # 3. Create+attach a volume based on an image and set it as root
-            img = provider.images.get('<my_image_id>')
+            img = provider.compute.images.get('<my_image_id>')
             lc.add_volume_device(source=img, size=100, is_root=True)
 
         :type  source: ``Volume``, ``Snapshot``, ``Image`` or None.

+ 2 - 1
cloudbridge/cloud/providers/aws/impl.py

@@ -90,7 +90,8 @@ class AWSCloudProvider(BaseCloudProvider):
             region=r,
             port=self.ec2_port,
             path=self.ec2_conn_path,
-            validate_certs=False)
+            validate_certs=False,
+            debug=2 if self.config.debug_mode else 0)
         return ec2_conn
 
     def _connect_s3(self):

+ 6 - 1
cloudbridge/cloud/providers/openstack/impl.py

@@ -12,6 +12,7 @@ from keystoneclient import session
 from keystoneclient.auth.identity import Password
 from neutronclient.v2_0 import client as neutron_client
 from novaclient import client as nova_client
+from novaclient import shell as nova_shell
 from swiftclient import client as swift_client
 
 from cloudbridge.cloud.base import BaseCloudProvider
@@ -116,10 +117,14 @@ class OpenStackCloudProvider(BaseCloudProvider):
             'nova_service_name',
             os.environ.get('NOVA_SERVICE_NAME', None))
 
+        if self.config.debug_mode:
+            nova_shell.OpenStackComputeShell().setup_debugging(True)
+
         return nova_client.Client(
             api_version, username=self.username, api_key=self.password,
             project_id=self.tenant_name, auth_url=self.auth_url,
-            region_name=self.region_name, service_name=service_name)
+            region_name=self.region_name, service_name=service_name,
+            http_log_debug=True if self.config.debug_mode else False)
 
     def _connect_keystone(self):
         """

+ 11 - 5
cloudbridge/cloud/providers/openstack/services.py

@@ -526,7 +526,7 @@ class OpenStackInstanceService(BaseInstanceService):
 
         os_instance = self.provider.nova.servers.create(
             name,
-            image_id,
+            None if self._has_root_device(launch_config) else image_id,
             instance_size,
             min_count=1,
             max_count=1,
@@ -546,16 +546,14 @@ class OpenStackInstanceService(BaseInstanceService):
         """
         bdm = []
         for device in launch_config.block_devices:
-            bdm_dict = {}
-
-            # Let openstack auto assign device name
-            bdm_dict['device_name'] = None
+            bdm_dict = dict()
 
             if device.is_volume:
                 bdm_dict['destination_type'] = 'volume'
 
                 if device.is_root:
                     bdm_dict['device_name'] = '/dev/sda'
+                    bdm_dict['boot_index'] = 0
 
                 if isinstance(device.source, Snapshot):
                     bdm_dict['source_type'] = 'snapshot'
@@ -582,6 +580,14 @@ class OpenStackInstanceService(BaseInstanceService):
             bdm.append(bdm_dict)
         return bdm
 
+    def _has_root_device(self, launch_config):
+        if not launch_config:
+            return False
+        for device in launch_config.block_devices:
+            if device.is_root:
+                return True
+        return False
+
     def _format_nics(self, launch_config):
         """
         Format network IDs for the API call.

+ 1 - 1
test/test_compute_service.py

@@ -231,7 +231,7 @@ class CloudComputeServiceTestCase(ProviderTestBase):
         lc = self.provider.compute.instances.create_launch_config()
 
         # Add a new blank volume
-        lc.add_volume_device(size=1, delete_on_terminate=True)
+#         lc.add_volume_device(size=1, delete_on_terminate=True)
 
         # Attach an existing volume
 #                 lc.add_volume_device(size=1, source=test_vol,