Esempio n. 1
0
class AzureNodeDriver(NodeDriver):
    name = "Azure Node Provider"
    type = Provider.AZURE
    website = 'http://windowsazure.com'
    sms = None

    rolesizes = None

    NODE_STATE_MAP = {
        'RoleStateUnknown': NodeState.UNKNOWN,
        'CreatingVM': NodeState.PENDING,
        'StartingVM': NodeState.PENDING,
        'CreatingRole': NodeState.PENDING,
        'StartingRole': NodeState.PENDING,
        'ReadyRole': NodeState.RUNNING,
        'BusyRole': NodeState.PENDING,
        'StoppingRole': NodeState.PENDING,
        'StoppingVM': NodeState.PENDING,
        'DeletingVM': NodeState.PENDING,
        'StoppedVM': NodeState.STOPPED,
        'RestartingRole': NodeState.REBOOTING,
        'CyclingRole': NodeState.TERMINATED,
        'FailedStartingRole': NodeState.TERMINATED,
        'FailedStartingVM': NodeState.TERMINATED,
        'UnresponsiveRole': NodeState.TERMINATED,
        'StoppedDeallocated': NodeState.TERMINATED,
    }

    def __init__(self, subscription_id=None, key_file=None, **kwargs):
        """
        subscription_id contains the Azure subscription id
        in the form of GUID key_file contains
        the Azure X509 certificate in .pem form
        """
        self.subscription_id = subscription_id
        self.key_file = key_file
        self.sms = ServiceManagementService(subscription_id, key_file)

        super(AzureNodeDriver, self).__init__(
            self.subscription_id,
            self.key_file,
            secure=True,
            **kwargs)

    def list_sizes(self):
        """
        Lists all sizes from azure

        :rtype: ``list`` of :class:`NodeSize`
        """
        if self.rolesizes is None:
            # refresh rolesizes
            data = self.sms.list_role_sizes()
            self.rolesizes = [self._to_node_size(i) for i in data]
        return self.rolesizes

    def list_images(self, location=None):
        """
        Lists all sizes from azure

        :rtype: ``list`` of :class:`NodeSize`
        """
        data = self.sms.list_os_images()
        images = [self._to_image(i) for i in data]

        if location is not None:
            images = [image for image in images
                      if location in image.extra["location"]]
        return images

    def list_locations(self):
        """
        Lists all Location from azure

        :rtype: ``list`` of :class:`NodeLocation`
        """
        data = self.sms.list_locations()
        locations = [self._to_location(i) for i in data]
        return locations

    def list_virtual_net(self):
        """
        List all VirtualNetworkSites

        :rtype: ``list`` of :class:`VirtualNetwork`
        """
        data = self.sms.list_virtual_network_sites()
        virtualnets = [self._to_virtual_network(i) for i in data]
        return virtualnets

    def create_node(self,
                    name,
                    image,
                    size,
                    storage,
                    service_name,
                    vm_user,
                    vm_password,
                    location=None,
                    affinity_group=None,
                    virtual_network=None):
        """
        Create a vm deploiement request

        :rtype:  :class:`.Node`
        :return: ``Node`` Node instance on success.
        """

        try:
            self.sms.get_hosted_service_properties(service_name)
            pass
        except WindowsAzureMissingResourceError:
            # create cloud service
            if bool(location is not None) != bool(affinity_group is not None):
                raise ValueError(
                    "For ressource creation, set location or affinity_group" +
                    " not both")
            if location is not None:
                try:
                    self.sms.create_hosted_service(
                        service_name=service_name,
                        label=service_name,
                        location=location)
                    pass
                except Exception, e:
                    raise e
            else:
                try:
                    self.sms.create_hosted_service(
                        service_name=service_name,
                        label=service_name,
                        affinity_group=affinity_group)
                except Exception, e:
                    raise e
    )

    sms = ServiceManagementService(subscription_id, pem_file)
    
    if not os.path.exists('./azure/data'):
        os.mkdir('./azure/data')

    result = sms.list_os_images()
    process_sms_list(result.images, './azure/data/azure_os_images.csv')
    print ('Azure OS images saved in azure_os_images.csv')

    result = sms.list_vm_images()
    process_sms_list(result.vm_images, './azure/data/azure_vm_images.csv')
    print ('Azure VM images saved in azure_vm_images.csv')

    result = sms.list_role_sizes()
    process_sms_list(result.role_sizes, './azure/data/azure_role_sizes.csv')
    print ('Azure Role sizes saved in azure_role_sizes.csv')

    result = sms.list_locations()
    process_sms_list(result.locations, './azure/data/azure_locations.csv')
    print ('Azure Locations saved in azure_locations.csv')

    result = sms.list_storage_accounts()
    process_sms_list(result.storage_services, './azure/data/azure_storage_accounts.csv')
    print ('Azure Storage accounts saved in azure_storage_accounts.csv')

    # Put this result always next to for loop!
    result = sms.list_hosted_services()
    process_sms_list(result.hosted_services, './azure/data/azure_hosted_services.csv')
    print ('Azure Hosted services saved in azure_hosted_services.csv')