Browse Source

A preliminary implementation to support launching instances into private cloud networks (eg VPC)

Enis Afgan 10 năm trước cách đây
mục cha
commit
61c5333ffb

+ 4 - 0
cloudbridge/cloud/base.py

@@ -184,6 +184,7 @@ class BaseLaunchConfig(LaunchConfig):
     def __init__(self, provider):
         self.provider = provider
         self.block_devices = []
+        self.net_ids = []
 
     class BlockDeviceMapping(object):
         """
@@ -245,6 +246,9 @@ class BaseLaunchConfig(LaunchConfig):
             is_volume=True, source=source, is_root=is_root, size=size,
             delete_on_terminate=delete_on_terminate)
 
+    def add_net_id(self, net_id):
+        self.net_ids.append(net_id)
+
 
 class BaseMachineImage(BaseObjectLifeCycleMixin, MachineImage):
 

+ 14 - 0
cloudbridge/cloud/interfaces/resources.py

@@ -390,6 +390,20 @@ class LaunchConfig(object):
         """
         pass
 
+    @abstractmethod
+    def add_net_id(self, net_id):
+        """
+        Add a private netword ID to the launch configuration.
+
+        :type net_id: str
+        :param net_id: Network ID to launch an instance into. This is a
+                       preliminary implementation (pending full private cloud
+                       support within cloudbridge) so native netowrk IDs need
+                       to be supplied. For OpenStack, this is the Neutron
+                       network ID. For AWS, this is a subnet ID.
+        """
+        pass
+
 
 class MachineImage(ObjectLifeCycleMixin):
 

+ 7 - 2
cloudbridge/cloud/providers/aws/services.py

@@ -439,14 +439,15 @@ class AWSInstanceService(BaseInstanceService):
             security_groups_list = None
         if launch_config:
             bdm = self._process_block_device_mappings(launch_config, zone_name)
+            net_id = self._get_net_id(launch_config)
         else:
-            bdm = None
+            bdm = net_id = None
 
         reservation = self.provider.ec2_conn.run_instances(
             image_id=image_id, instance_type=instance_size,
             min_count=1, max_count=1, placement=zone_name,
             key_name=keypair_name, security_groups=security_groups_list,
-            user_data=user_data, block_device_map=bdm)
+            user_data=user_data, block_device_map=bdm, subnet_id=net_id)
         if reservation:
             instance = AWSInstance(self.provider, reservation.instances[0])
             instance.name = name
@@ -499,6 +500,10 @@ class AWSInstanceService(BaseInstanceService):
 
         return bdm
 
+    def _get_net_id(self, launch_config):
+        return launch_config.net_ids[0] if len(launch_config.net_ids) > 0 \
+            else None
+
     def create_launch_config(self):
         return BaseLaunchConfig(self.provider)
 

+ 13 - 2
cloudbridge/cloud/providers/openstack/services.py

@@ -477,8 +477,9 @@ class OpenStackInstanceService(BaseInstanceService):
             security_groups_list = None
         if launch_config:
             bdm = self._to_block_device_mapping(launch_config)
+            nics = self._format_nics(launch_config)
         else:
-            bdm = None
+            bdm = nics = None
 
         os_instance = self._provider.nova.servers.create(
             name,
@@ -490,7 +491,8 @@ class OpenStackInstanceService(BaseInstanceService):
             key_name=keypair_name,
             security_groups=security_groups_list,
             userdata=user_data,
-            block_device_mapping_v2=bdm)
+            block_device_mapping_v2=bdm,
+            nics=nics)
         return OpenStackInstance(self._provider, os_instance)
 
     def _to_block_device_mapping(self, launch_config):
@@ -537,6 +539,15 @@ class OpenStackInstanceService(BaseInstanceService):
             bdm.append(bdm_dict)
         return bdm
 
+    def _format_nics(self, launch_config):
+        """
+        Format network IDs for the API call.
+        """
+        nics = []
+        for net_id in launch_config.net_ids:
+            nics.append({'net-id': net_id})
+        return nics
+
     def create_launch_config(self):
         return BaseLaunchConfig(self.provider)