Browse Source

Changed the instance credentials to key pair

vikramdoda 9 years ago
parent
commit
8a4798c73f

+ 1 - 1
cloudbridge/cloud/interfaces/resources.py

@@ -569,7 +569,7 @@ class Instance(ObjectLifeCycleMixin, CloudResource):
         pass
 
     @abstractmethod
-    def create_image(self, name):
+    def create_image(self, name, private_key_path=None):
         """
         Create a new image based on this instance.
 

+ 14 - 7
cloudbridge/cloud/providers/azure/resources.py

@@ -1107,8 +1107,6 @@ class AzureSubnet(BaseSubnet):
 
 
 class AzureInstance(BaseInstance):
-    # ref:
-    # http://docs.azure.amazon.com/AzureEC2/latest/UserGuide/ec2-instance-lifecycle.html
     INSTANCE_STATE_MAP = {
         'InProgress': InstanceState.PENDING,
         'Creating': InstanceState.PENDING,
@@ -1267,7 +1265,7 @@ class AzureInstance(BaseInstance):
     @property
     def image_id(self):
         """
-        Get the image ID for this insance.
+        Get the image ID for this instance.
         """
         return self._vm.storage_profile.image_reference.id
 
@@ -1283,9 +1281,6 @@ class AzureInstance(BaseInstance):
         """
         Get the security groups associated with this instance.
         """
-        # boto instance.groups field returns a ``Group`` object so need to
-        # convert that into a ``SecurityGroup`` object before creating a
-        # cloudbridge SecurityGroup object
         return [self._provider.security.security_groups.get(group_id)
                 for group_id in self._security_group_ids]
 
@@ -1347,7 +1342,7 @@ class AzureInstance(BaseInstance):
     def add_floating_ip(self, ip_address):
         try:
             ip_addresses = [ip for ip in self._provider.
-                            azure_client.list_public_ips()
+                            azure_client.list_floating_ips()
                             if ip.ip_address and ip.ip_address == ip_address]
             if len(ip_addresses) > 0:
                 """
@@ -1420,6 +1415,18 @@ class AzureInstance(BaseInstance):
             create_nic(nic_name, nic)
 
     def remove_security_group(self, sg):
+
+        '''
+            :param sg:
+            :return: None
+
+            This method removes the security group from VM.
+            In Azure, security group added to Network interface.
+            Azure supports to add only one security group to
+            network interface, we are removing the provided security group
+            if it associated with NIC else ignoring.
+        '''
+
         nic_id = \
             self._vm.network_profile.network_interfaces[0].id
         nic_params = azure_helpers. \

+ 73 - 68
cloudbridge/cloud/providers/azure/services.py

@@ -356,21 +356,21 @@ class AzureInstanceService(BaseInstanceService):
     def create(self, name, image, instance_type, subnet, zone=None,
                key_pair=None, security_groups=None, user_data=None,
                launch_config=None, **kwargs):
-        if isinstance(image, MachineImage):
-            image_id = image.id
-        else:
-            image_id = image
-            image = self.provider.compute.images.get(image_id)
+        image_id = image.id if isinstance(image, MachineImage) else image
+
         if key_pair:
             if isinstance(key_pair, KeyPair):
                 key_pair_name = key_pair.name
             else:
                 key_pair_name = key_pair
+                # retrieving key pair as we need to pass the public key
                 key_pair = self.provider.security.\
                     key_pairs.get(key_pair_name)
         # else:
         #     raise Exception("Keypair required")
 
+        key = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDI8EAhG3jKdF/raX3J3UBt9/zAXVn0aaiHd5JcgJNjwR5t6ggonvHlsELjnmT43URzu1GKsDMk6BB8jq8bWBHpuXJ+0203JHqbfmLoScB4JzWgb3dEwFahdTNVI44I31/DgpQ8KN/jA/i6XvGybf/uvuknjMEDwsv0MUiX1tDj4Hpnm2pznjdB3K4CtizUQKz3RHZvb3096vf5Rix/s4a6AVAtH0kWbKCGIbra5ahKew0wn/XV3aJGygW9EFP7DWdSfDe2gsATdNyyZFvWYO7uJU0J4ZBzLP1lrZNuTQzJoYGaPT5iV/PRY9cvikWDH+t7HQ59+duvMl7wmAdl/Xw7'  # noqa
+
         instance_size = instance_type.id if \
             isinstance(instance_type, InstanceType) else instance_type
         subnet = (self.provider.network.subnets.get(subnet)
@@ -416,8 +416,18 @@ class AzureInstanceService(BaseInstanceService):
             'location': zone_name or self._provider.region_name,
             'os_profile': {
                 'admin_username': self.provider.default_user_name,
-                'admin_password': 'cbazureuser@123',
-                'computer_name': name
+                'computer_name': name,
+                'linux_configuration': {
+                             "disable_password_authentication": True,
+                             "ssh": {
+                                 "public_keys": [{
+                                      "path":
+                                      "/home/{}/.ssh/authorized_keys".format(
+                                          self.provider.default_user_name),
+                                      "key_data": key
+                                     }]
+                                   }
+                           }
             },
             'hardware_profile': {
                 'vm_size': instance_size
@@ -443,20 +453,6 @@ class AzureInstanceService(BaseInstanceService):
         if key_pair:
             params['tags'].update(Key_Pair=key_pair_name)
 
-        if image.os_type == 'Linux' and key_pair:
-            params['os_profile']['linux_configuration'] = \
-                {
-                 "disable_password_authentication": True,
-                 "ssh": {
-                     "public_keys": [{
-                          "path":
-                          "/home/{}/.ssh/authorized_keys".format(
-                              self.provider.default_user_name),
-                          "key_data": key_pair.key
-                         }]
-                       }
-               }
-
         instance_name = "{0}-{1}".format(name, uuid.uuid4().hex[:6])
 
         self.provider.azure_client.create_vm(instance_name, params)
@@ -499,7 +495,7 @@ class AzureInstanceService(BaseInstanceService):
                                        vm_name, zone=None):
         """
         Processes block device mapping information
-        and returns a Boto BlockDeviceMapping object. If new volumes
+        and returns a Data disk dictionary list. If new volumes
         are requested (source is None and destination is VOLUME), they will be
         created and the relevant volume ids included in the mapping.
         """
@@ -520,41 +516,42 @@ class AzureInstanceService(BaseInstanceService):
             self.provider.azure_client.\
                 update_disk_tags(volume.resource_name, volume.tags)
 
-        # assign ephemeral devices from 0 onwards
-        # ephemeral_counter = 0
-
         for device in launch_config.block_devices:
-            if device.is_volume and not device.is_root:
-                if isinstance(device.source, Snapshot):
-                    snapshot_vol = device.source.create_volume()
-                    attach_volume(snapshot_vol, device.delete_on_terminate)
-                elif isinstance(device.source, Volume):
-                    attach_volume(device.source, device.delete_on_terminate)
-                elif isinstance(device.source, MachineImage):
-                    # Not supported
-                    pass
-                else:
-                    # source is None, but destination is volume, therefore
-                    # create a blank volume. If the Zone is None, this
-                    # could fail since the volume and instance may be created
-                    # in two different zones.
-                    if not zone:
-                        raise InvalidConfigurationException(
-                            "A zone must be specified when launching with a"
-                            " new blank volume block device mapping.")
-                    vol_name = "{0}_disk".format(vm_name, uuid.uuid4().hex[:6])
-                    new_vol = self.provider.block_store.volumes.create(
-                        vol_name,
-                        device.size,
-                        zone)
-                    attach_volume(new_vol, device.delete_on_terminate)
-                # bd_type.delete_on_terminate = device.delete_on_terminate
-                # if device.size:
-                #     bd_type.size = device.size
-                volumes_count += 1
+            if device.is_volume:
+                if not device.is_root:
+                    # In azure, os disk automatically created,
+                    # we are ignoring the root disk, if specified
+                    if isinstance(device.source, Snapshot):
+                        snapshot_vol = device.source.create_volume()
+                        attach_volume(snapshot_vol,
+                                      device.delete_on_terminate)
+                    elif isinstance(device.source, Volume):
+                        attach_volume(device.source,
+                                      device.delete_on_terminate)
+                    elif isinstance(device.source, MachineImage):
+                        # Not supported
+                        pass
+                    else:
+                        # source is None, but destination is volume, therefore
+                        # create a blank volume. If the Zone is None, this
+                        # could fail since the volume and instance may
+                        # be created in two different zones.
+                        if not zone:
+                            raise InvalidConfigurationException(
+                                "A zone must be specified when "
+                                "launching with a"
+                                " new blank volume block device mapping.")
+                        vol_name = \
+                            "{0}_disk".format(vm_name, uuid.uuid4().hex[:6])
+                        new_vol = self.provider.block_store.volumes.create(
+                            vol_name,
+                            device.size,
+                            zone)
+                        attach_volume(new_vol, device.delete_on_terminate)
+                    volumes_count += 1
 
             else:  # device is ephemeral
-                # bd_type.ephemeral_name = 'ephemeral%s' % ephemeral_counter
+                # in azure we cannot add the ephemeral disks explicitly
                 pass
 
         return disks
@@ -681,6 +678,8 @@ class AzureNetworkService(BaseNetworkService):
                                      limit=limit, marker=marker)
 
     def create(self, name=None):
+        # Azure requires CIDR block to be specified when creating a network
+        # so set a default one and use the largest allowed netmask.
         network_name = AzureNetwork.CB_DEFAULT_NETWORK_NAME
         if name:
             network_name = "{0}-{1}".format(name, uuid.uuid4().hex[:6])
@@ -704,7 +703,7 @@ class AzureNetworkService(BaseNetworkService):
             'public_ip', uuid.uuid4().hex[:6])
         public_ip_parameters = {
             'location': self.provider.azure_client.region_name,
-            'public_ip_allocation_method': 'Dynamic'
+            'public_ip_allocation_method': 'Static'
         }
 
         floating_ip = self.provider.azure_client.\
@@ -841,14 +840,8 @@ class AzureSubnetService(BaseSubnetService):
         default_cdir = '10.0.1.0/24'
         network = None
         subnet = None
-        try:
-            network = self.provider.azure_client\
-                .get_network(AzureNetwork.CB_DEFAULT_NETWORK_NAME)
-        except CloudError:
-            pass
 
-        if not network:
-            self.provider.network.create()
+        # No provider-default Subnet exists, look for a library-default one
         try:
             subnet = self.provider.azure_client.get_subnet(
                 AzureNetwork.CB_DEFAULT_NETWORK_NAME,
@@ -857,12 +850,24 @@ class AzureSubnetService(BaseSubnetService):
         except CloudError:
             pass
 
-        if not subnet:
-            subnet = self.provider.azure_client.create_subnet(
-                AzureNetwork.CB_DEFAULT_NETWORK_NAME,
-                AzureSubnet.CB_DEFAULT_SUBNET_NAME,
-                {'address_prefix': default_cdir}
-            )
+        if subnet:
+            return AzureSubnet(self.provider, subnet)
+
+        # No provider-default Subnet exists, try to create it (net + subnets)
+        try:
+            network = self.provider.azure_client\
+                .get_network(AzureNetwork.CB_DEFAULT_NETWORK_NAME)
+        except CloudError:
+            pass
+
+        if not network:
+            self.provider.network.create()
+
+        subnet = self.provider.azure_client.create_subnet(
+            AzureNetwork.CB_DEFAULT_NETWORK_NAME,
+            AzureSubnet.CB_DEFAULT_SUBNET_NAME,
+            {'address_prefix': default_cdir}
+        )
 
         return AzureSubnet(self.provider, subnet)