Пример #1
0
    def test_is_public_and_is_private_subnet(self):
        public_ips = [
            '213.151.0.8',
            '86.87.86.1',
            '8.8.8.8',
            '8.8.4.4'
        ]

        private_ips = [
            '192.168.1.100',
            '10.0.0.1',
            '172.16.0.0'
        ]

        for address in public_ips:
            is_public = is_public_subnet(ip=address)
            is_private = is_private_subnet(ip=address)

            self.assertTrue(is_public)
            self.assertFalse(is_private)

        for address in private_ips:
            is_public = is_public_subnet(ip=address)
            is_private = is_private_subnet(ip=address)

            self.assertFalse(is_public)
            self.assertTrue(is_private)
Пример #2
0
    def _to_node(self, domain):
        state, max_mem, memory, vcpu_count, used_cpu_time = domain.info()
        state = self.NODE_STATE_MAP.get(state, NodeState.UNKNOWN)

        public_ips, private_ips = [], []

        ip_addresses = self._get_ip_addresses_for_domain(domain)

        for ip_address in ip_addresses:
            if is_public_subnet(ip_address):
                public_ips.append(ip_address)
            else:
                private_ips.append(ip_address)

        extra = {
            'uuid': domain.UUIDString(),
            'os_type': domain.OSType(),
            'types': self.connection.getType(),
            'used_memory': memory / 1024,
            'vcpu_count': vcpu_count,
            'used_cpu_time': used_cpu_time
        }

        node = Node(id=domain.ID(),
                    name=domain.name(),
                    state=state,
                    public_ips=public_ips,
                    private_ips=private_ips,
                    driver=self,
                    extra=extra)
        node._uuid = domain.UUIDString()  # we want to use a custom UUID
        return node
Пример #3
0
    def test_is_public_and_is_private_subnet(self):
        public_ips = ["213.151.0.8", "86.87.86.1", "8.8.8.8", "8.8.4.4"]

        private_ips = ["192.168.1.100", "10.0.0.1", "172.16.0.0"]

        for address in public_ips:
            is_public = is_public_subnet(ip=address)
            is_private = is_private_subnet(ip=address)

            self.assertTrue(is_public)
            self.assertFalse(is_private)

        for address in private_ips:
            is_public = is_public_subnet(ip=address)
            is_private = is_private_subnet(ip=address)

            self.assertFalse(is_public)
            self.assertTrue(is_private)
Пример #4
0
    def _to_node(self, vm):
        config = vm.summary.config
        datacenter = self._get_datacenter(vm)

        extra = {
            "path": config.vmPathName,
            "operating_system": config.guestFullName,
            "os_type": "windows" if "Microsoft" in config.guestFullName \
                       else "unix",
            "memory_MB": config.memorySizeMB,
            "cpus": config.numCpu,
            "overallStatus": str(vm.summary.overallStatus),
            "datacenter": datacenter,
        }

        kwargs = {
            "id":
            config.instanceUuid,
            "name":
            config.name,
            "state":
            self.NODE_STATE_MAP.get(vm.summary.runtime.powerState,
                                    NodeState.UNKNOWN),
            "public_ips": [],
            "private_ips": [],
            "driver":
            self,
            "extra":
            extra,
        }

        for nic in vm.guest.net:
            for addr in nic.ipAddress:
                try:
                    if is_public_subnet(addr):
                        kwargs['public_ips'].append(addr)
                    else:
                        kwargs['private_ips'].append(addr)
                except OSError:  # inet_aton error for ipv6 addrs
                    pass

        node = Node(**kwargs)
        node._uuid = kwargs['id']
        return node
Пример #5
0
    def _to_node(self, domain):
        state, max_mem, memory, vcpu_count, used_cpu_time = domain.info()
        state = self.NODE_STATE_MAP.get(state, NodeState.UNKNOWN)

        public_ips, private_ips = [], []

        ip_addresses = self._get_ip_addresses_for_domain(domain)

        for ip_address in ip_addresses:
            if is_public_subnet(ip_address):
                public_ips.append(ip_address)
            else:
                private_ips.append(ip_address)

        extra = {'uuid': domain.UUIDString(), 'os_type': domain.OSType(),
                 'types': self.connection.getType(),
                 'used_memory': memory / 1024, 'vcpu_count': vcpu_count,
                 'used_cpu_time': used_cpu_time}

        node = Node(id=domain.ID(), name=domain.name(), state=state,
                    public_ips=public_ips, private_ips=private_ips,
                    driver=self, extra=extra)
        node._uuid = domain.UUIDString()  # we want to use a custom UUID
        return node
    def _virtual_machine_to_node(self, virtual_machine):
        ip = virtual_machine.get('ip')
        state = virtual_machine.get('state', 'unknown')

        public_ips = None
        private_ips = None
        try:
            if is_public_subnet(ip):
                public_ips = [ip]
            else:
                private_ips = [ip]
        except:
            pass
        
        return VirtualMachine(id=virtual_machine.get('id'),
                              name=virtual_machine.get('instanceID'),
                              state=self._state_to_node_state(state),
                              public_ips=public_ips,
                              private_ips=private_ips,
                              driver=self,
                              size=virtual_machine.get('serviceOffer', {}).get('href'),
                              image=None,
                              #created_at=virtual_machine.get('created'),
                              extra=dict(virtual_machine))
Пример #7
0
def _to_node(self, api_node):
    public_networks_labels = ['public', 'internet']

    public_ips, private_ips = [], []

    for label, values in api_node['addresses'].items():
        for value in values:
            ip = value['addr']

            is_public_ip = False

            try:
                public_subnet = is_public_subnet(ip)
            except:
                # IPv6
                public_subnet = False

            # Openstack Icehouse sets 'OS-EXT-IPS:type' to 'floating' for
            # public and 'fixed' for private
            explicit_ip_type = value.get('OS-EXT-IPS:type', None)

            if public_subnet:
                # Check for public subnet
                is_public_ip = True
            elif explicit_ip_type == 'floating':
                is_public_ip = True
            elif explicit_ip_type == 'fixed':
                is_public_ip = False
            elif label in public_networks_labels:
                # Try label next
                is_public_ip = True

            if is_public_ip:
                public_ips.append(ip)
            else:
                private_ips.append(ip)

    # Sometimes 'image' attribute is not present if the node is in an error
    # state
    image = api_node.get('image', None)
    image_id = image.get('id', None) if image else None
    config_drive = api_node.get("config_drive", False)
    volumes_attached = api_node.get('os-extended-volumes:volumes_attached')

    return Node(
        id=api_node['id'],
        name=api_node['name'],
        state=self.NODE_STATE_MAP.get(api_node['status'], NodeState.UNKNOWN),
        public_ips=public_ips,
        private_ips=private_ips,
        driver=self,
        extra=dict(
            addresses=api_node['addresses'],
            hostId=api_node['hostId'],
            access_ip=api_node.get('accessIPv4'),
            access_ipv6=api_node.get('accessIPv6', None),
            # Docs says "tenantId", but actual is "tenant_id". *sigh*
            # Best handle both.
            tenantId=api_node.get('tenant_id') or api_node['tenantId'],
            userId=api_node.get('user_id', None),
            imageId=image_id,
            flavorId=api_node['flavor']['id'],
            uri=next(link['href'] for link in api_node['links']
                     if link['rel'] == 'self'),
            metadata=api_node['metadata'],
            password=api_node.get('adminPass', None),
            created=api_node['created'],
            updated=api_node['updated'],
            key_name=api_node.get('key_name', None),
            disk_config=api_node.get('OS-DCF:diskConfig', None),
            config_drive=config_drive,
            availability_zone=api_node.get('OS-EXT-AZ:availability_zone'),
            volumes_attached=volumes_attached,
            task_state=api_node.get("OS-EXT-STS:task_state", None),
            vm_state=api_node.get("OS-EXT-STS:vm_state", None),
            power_state=api_node.get("OS-EXT-STS:power_state", None),
            progress=api_node.get("progress", None),
            fault=api_node.get('fault')),
    )
def _to_node(self, api_node):
    public_networks_labels = ['public', 'internet']

    public_ips, private_ips = [], []

    for label, values in api_node['addresses'].items():
        for value in values:
            ip = value['addr']

            is_public_ip = False

            try:
                public_subnet = is_public_subnet(ip)
            except:
                # IPv6
                public_subnet = False

            # Openstack Icehouse sets 'OS-EXT-IPS:type' to 'floating' for
            # public and 'fixed' for private
            explicit_ip_type = value.get('OS-EXT-IPS:type', None)

            if public_subnet:
                # Check for public subnet
                is_public_ip = True
            elif explicit_ip_type == 'floating':
                is_public_ip = True
            elif explicit_ip_type == 'fixed':
                is_public_ip = False
            elif label in public_networks_labels:
                # Try label next
                is_public_ip = True

            if is_public_ip:
                public_ips.append(ip)
            else:
                private_ips.append(ip)

    # Sometimes 'image' attribute is not present if the node is in an error
    # state
    image = api_node.get('image', None)
    image_id = image.get('id', None) if image else None
    config_drive = api_node.get("config_drive", False)
    volumes_attached = api_node.get('os-extended-volumes:volumes_attached')

    return Node(
        id=api_node['id'],
        name=api_node['name'],
        state=self.NODE_STATE_MAP.get(api_node['status'],
                                      NodeState.UNKNOWN),
        public_ips=public_ips,
        private_ips=private_ips,
        driver=self,
        extra=dict(
            addresses=api_node['addresses'],
            hostId=api_node['hostId'],
            access_ip=api_node.get('accessIPv4'),
            access_ipv6=api_node.get('accessIPv6', None),
            # Docs says "tenantId", but actual is "tenant_id". *sigh*
            # Best handle both.
            tenantId=api_node.get('tenant_id') or api_node['tenantId'],
            userId=api_node.get('user_id', None),
            imageId=image_id,
            flavorId=api_node['flavor']['id'],
            uri=next(link['href'] for link in api_node['links'] if
                     link['rel'] == 'self'),
            metadata=api_node['metadata'],
            password=api_node.get('adminPass', None),
            created=api_node['created'],
            updated=api_node['updated'],
            key_name=api_node.get('key_name', None),
            disk_config=api_node.get('OS-DCF:diskConfig', None),
            config_drive=config_drive,
            availability_zone=api_node.get('OS-EXT-AZ:availability_zone'),
            volumes_attached=volumes_attached,
            task_state=api_node.get("OS-EXT-STS:task_state", None),
            vm_state=api_node.get("OS-EXT-STS:vm_state", None),
            power_state=api_node.get("OS-EXT-STS:power_state", None),
            progress=api_node.get("progress", None),
            fault=api_node.get('fault')
        ),
    )
Пример #9
0
def fixed_OpenStackNodeDriver_to_node(self, api_node):
    from libcloud.utils.networking import is_public_subnet
    from libcloud.compute.base import Node
    from libcloud.compute.types import NodeState

    public_networks_labels = ['public', 'internet']

    public_ips, private_ips = [], []

    for label, values in api_node['addresses'].items():
        for value in values:
            ip = value['addr']

            is_public_ip = False

            try:
                public_subnet = is_public_subnet(ip)
            except:
                # IPv6
                public_subnet = False

            # Openstack Icehouse sets 'OS-EXT-IPS:type' to 'floating' for
            # public and 'fixed' for private
            explicit_ip_type = value.get('OS-EXT-IPS:type', None)

            if explicit_ip_type == 'floating':
                is_public_ip = True
            elif explicit_ip_type == 'fixed':
                is_public_ip = False
            elif label in public_networks_labels:
                # Try label next
                is_public_ip = True
            elif public_subnet:
                # Check for public subnet
                is_public_ip = True

            if is_public_ip:
                public_ips.append(ip)
            else:
                private_ips.append(ip)

    # Sometimes 'image' attribute is not present if the node is in an error
    # state
    image = api_node.get('image', None)
    image_id = image.get('id', None) if image else None

    if api_node.get("config_drive", "false").lower() == "true":
        config_drive = True
    else:
        config_drive = False

    return Node(
        id=api_node['id'],
        name=api_node['name'],
        state=self.NODE_STATE_MAP.get(api_node['status'], NodeState.UNKNOWN),
        public_ips=public_ips,
        private_ips=private_ips,
        driver=self,
        extra=dict(
            hostId=api_node['hostId'],
            access_ip=api_node.get('accessIPv4'),
            # Docs says "tenantId", but actual is "tenant_id". *sigh*
            # Best handle both.
            tenantId=api_node.get('tenant_id') or api_node['tenantId'],
            imageId=image_id,
            flavorId=api_node['flavor']['id'],
            uri=next(link['href'] for link in api_node['links']
                     if link['rel'] == 'self'),
            metadata=api_node['metadata'],
            password=api_node.get('adminPass', None),
            created=api_node['created'],
            updated=api_node['updated'],
            key_name=api_node.get('key_name', None),
            disk_config=api_node.get('OS-DCF:diskConfig', None),
            config_drive=config_drive,
            availability_zone=api_node.get('OS-EXT-AZ:availability_zone',
                                           None),
        ),
    )
Пример #10
0
def _fixed_OpenStackNodeDriver_to_node(self, api_node):
    """
    This is a copy of
    libcloud.compute.drivers.openstack.OpenStack_1_1_NodeDriver._to_node
    from libcloud 0.16.0 to fix
    https://github.com/apache/libcloud/pull/411
    """
    from libcloud.utils.networking import is_public_subnet
    from libcloud.compute.base import Node
    from libcloud.compute.types import NodeState

    public_networks_labels = ['public', 'internet']

    public_ips, private_ips = [], []

    for label, values in api_node['addresses'].items():
        for value in values:
            ip = value['addr']

            is_public_ip = False

            try:
                public_subnet = is_public_subnet(ip)
            except:
                # IPv6
                public_subnet = False

            # Openstack Icehouse sets 'OS-EXT-IPS:type' to 'floating' for
            # public and 'fixed' for private
            explicit_ip_type = value.get('OS-EXT-IPS:type', None)

            if explicit_ip_type == 'floating':
                is_public_ip = True
            elif explicit_ip_type == 'fixed':
                is_public_ip = False
            elif label in public_networks_labels:
                # Try label next
                is_public_ip = True
            elif public_subnet:
                # Check for public subnet
                is_public_ip = True

            if is_public_ip:
                public_ips.append(ip)
            else:
                private_ips.append(ip)

    # Sometimes 'image' attribute is not present if the node is in an error
    # state
    image = api_node.get('image', None)
    image_id = image.get('id', None) if image else None

    if api_node.get("config_drive", "false").lower() == "true":
        config_drive = True
    else:
        config_drive = False

    return Node(
        id=api_node['id'],
        name=api_node['name'],
        state=self.NODE_STATE_MAP.get(api_node['status'],
                                      NodeState.UNKNOWN),
        public_ips=public_ips,
        private_ips=private_ips,
        driver=self,
        extra=dict(
            hostId=api_node['hostId'],
            access_ip=api_node.get('accessIPv4'),
            # Docs says "tenantId", but actual is "tenant_id". *sigh*
            # Best handle both.
            tenantId=api_node.get('tenant_id') or api_node['tenantId'],
            imageId=image_id,
            flavorId=api_node['flavor']['id'],
            uri=next(link['href'] for link in api_node['links'] if
                     link['rel'] == 'self'),
            metadata=api_node['metadata'],
            password=api_node.get('adminPass', None),
            created=api_node['created'],
            updated=api_node['updated'],
            key_name=api_node.get('key_name', None),
            disk_config=api_node.get('OS-DCF:diskConfig', None),
            config_drive=config_drive,
            availability_zone=api_node.get('OS-EXT-AZ:availability_zone',
                                           None),
        ),
    )
Пример #11
0
    def _to_node(self, vm):
        assert(isinstance(vm, VIVirtualMachine))

        properties = vm.get_properties()
        status = vm.get_status()

        uuid = vm.properties.config.uuid
        instance_uuid = vm.properties.config.instanceUuid

        id = uuid
        name = properties['name']
        public_ips = []
        private_ips = []

        state = self.NODE_STATE_MAP.get(status, NodeState.UNKNOWN)
        ip_address = properties.get('ip_address', None)
        net = properties.get('net', [])
        resource_pool_id = str(vm.properties.resourcePool._obj)

        try:
            operating_system = vm.properties.summary.guest.guestFullName,
        except Exception:
            operating_system = 'unknown'

        extra = {
            'uuid': uuid,
            'instance_uuid': instance_uuid,
            'path': properties['path'],
            'resource_pool_id': resource_pool_id,
            'hostname': properties.get('hostname', None),
            'guest_id': properties['guest_id'],
            'devices': properties.get('devices', {}),
            'disks': properties.get('disks', []),
            'net': net,

            'overall_status': vm.properties.overallStatus,
            'operating_system': operating_system,

            'cpus': vm.properties.config.hardware.numCPU,
            'memory_mb': vm.properties.config.hardware.memoryMB
        }

        # Add primary IP
        if ip_address:
            if is_public_subnet(ip_address):
                public_ips.append(ip_address)
            else:
                private_ips.append(ip_address)

        # Add other IP addresses
        for nic in net:
            ip_addresses = nic['ip_addresses']
            for ip_address in ip_addresses:
                try:
                    is_public = is_public_subnet(ip_address)
                except Exception:
                    # TODO: Better support for IPv6
                    is_public = False

                if is_public:
                    public_ips.append(ip_address)
                else:
                    private_ips.append(ip_address)

        # Remove duplicate IPs
        public_ips = list(set(public_ips))
        private_ips = list(set(private_ips))

        node = Node(id=id, name=name, state=state, public_ips=public_ips,
                    private_ips=private_ips, driver=self, extra=extra)
        return node
Пример #12
0
    def _to_node(self, vm):
        assert(isinstance(vm, VIVirtualMachine))

        properties = vm.get_properties()
        status = vm.get_status()

        uuid = vm.properties.config.uuid
        instance_uuid = vm.properties.config.instanceUuid

        id = uuid
        name = properties['name']
        public_ips = []
        private_ips = []

        state = self.NODE_STATE_MAP.get(status, NodeState.UNKNOWN)
        ip_address = properties.get('ip_address', None)
        net = properties.get('net', [])
        resource_pool_id = str(vm.properties.resourcePool._obj)

        try:
            operating_system = vm.properties.summary.guest.guestFullName,
        except Exception:
            operating_system = 'unknown'

        extra = {
            'uuid': uuid,
            'instance_uuid': instance_uuid,
            'path': properties['path'],
            'resource_pool_id': resource_pool_id,
            'hostname': properties.get('hostname', None),
            'guest_id': properties['guest_id'],
            'devices': properties.get('devices', {}),
            'disks': properties.get('disks', []),
            'net': net,

            'overall_status': vm.properties.overallStatus,
            'operating_system': operating_system,

            'cpus': vm.properties.config.hardware.numCPU,
            'memory_mb': vm.properties.config.hardware.memoryMB
        }

        # Add primary IP
        if ip_address:
            if is_public_subnet(ip_address):
                public_ips.append(ip_address)
            else:
                private_ips.append(ip_address)

        # Add other IP addresses
        for nic in net:
            ip_addresses = nic['ip_addresses']
            for ip_address in ip_addresses:
                try:
                    is_public = is_public_subnet(ip_address)
                except Exception:
                    # TODO: Better support for IPv6
                    is_public = False

                if is_public:
                    public_ips.append(ip_address)
                else:
                    private_ips.append(ip_address)

        # Remove duplicate IPs
        public_ips = list(set(public_ips))
        private_ips = list(set(private_ips))

        node = Node(id=id, name=name, state=state, public_ips=public_ips,
                    private_ips=private_ips, driver=self, extra=extra)
        return node