Ejemplo n.º 1
0
    def add_public_net(radl):
        """
        Add a public net to the radl specified
        """
        now = str(int(time.time() * 100))

        public_nets = []
        for net in radl.networks:
            if net.isPublic():
                public_nets.append(net)

        if public_nets:
            public_net = None
            for net in public_nets:
                num_net = radl.systems[0].getNumNetworkWithConnection(net.id)
                if num_net is not None:
                    public_net = net
                    break

            if not public_net:
                # There are a public net but it has not been used in this
                # VM
                public_net = public_nets[0]
                num_net = radl.systems[0].getNumNetworkIfaces()
        else:
            # There no public net, create one
            public_net = network.createNetwork("public." + now, True)
            radl.networks.append(public_net)
            num_net = radl.systems[0].getNumNetworkIfaces()

        return public_net, num_net
Ejemplo n.º 2
0
    def setSSHPort(self, ssh_port):
        """
        Set the SSH port in the RADL info of this VM
        """
        if ssh_port != self.getSSHPort():
            now = str(int(time.time() * 100))

            public_net = None
            for net in self.info.networks:
                if net.isPublic():
                    public_net = net

            # If it do
            if public_net is None:
                public_net = network.createNetwork("public." + now, True)
                self.info.networks.append(public_net)

            outports_str = str(ssh_port) + "-22"
            outports = public_net.getOutPorts()
            if outports:
                for outport in outports:
                    if outport.get_local_port() != 22 and outport.get_protocol(
                    ) != "tcp":
                        if outport.get_protocol() != "tcp":
                            outports_str += (str(outport.get_remote_port()) +
                                             "-" +
                                             str(outport.get_local_port()))
                        else:
                            outports_str += (str(outport.get_remote_port()) +
                                             "/udp" + "-" +
                                             str(outport.get_local_port()) +
                                             "/udp")
            public_net.setValue('outports', outports_str)

            # get the ID
            num_net = self.getNumNetworkWithConnection(public_net.id)
            if num_net is None:
                # There are a public net but it has not been used in this VM
                num_net = self.getNumNetworkIfaces()

            self.info.systems[0].setValue(
                'net_interface.' + str(num_net) + '.connection', public_net.id)
Ejemplo n.º 3
0
    def setSSHPort(self, ssh_port):
        """
        Set the SSH port in the RADL info of this VM
        """
        if ssh_port != self.getSSHPort():
            now = str(int(time.time() * 100))

            public_net = None
            for net in self.info.networks:
                if net.isPublic():
                    public_net = net

            # If it do
            if public_net is None:
                public_net = network.createNetwork("public." + now, True)
                self.info.networks.append(public_net)

            outports_str = str(ssh_port) + "-22"
            outports = public_net.getOutPorts()
            if outports:
                for (remote_port, _, local_port, local_protocol) in outports:
                    if local_port != 22 and local_protocol != "tcp":
                        if local_protocol != "tcp":
                            outports_str += str(remote_port) + \
                                "-" + str(local_port)
                        else:
                            outports_str += str(remote_port) + \
                                "/udp" + "-" + str(local_port) + "/udp"
            public_net.setValue('outports', outports_str)

            # get the ID
            num_net = self.getNumNetworkWithConnection(public_net.id)
            if num_net is None:
                # There are a public net but it has not been used in this VM
                num_net = self.getNumNetworkIfaces()

            self.info.systems[0].setValue(
                'net_interface.' + str(num_net) + '.connection', public_net.id)
Ejemplo n.º 4
0
    def setIps(self, public_ips, private_ips):
        """
        Set the specified IPs in the VM RADL info
        """
        now = str(int(time.time() * 100))
        vm_system = self.info.systems[0]

        if public_ips and not set(public_ips).issubset(set(private_ips)):
            public_nets = []
            for net in self.info.networks:
                if net.isPublic():
                    public_nets.append(net)

            if public_nets:
                public_net = None
                for net in public_nets:
                    num_net = self.getNumNetworkWithConnection(net.id)
                    if num_net is not None:
                        public_net = net
                        break

                if not public_net:
                    # There are a public net but it has not been used in this
                    # VM
                    public_net = public_nets[0]
                    num_net = self.getNumNetworkIfaces()
            else:
                # There no public net, create one
                public_net = network.createNetwork("public." + now, True)
                self.info.networks.append(public_net)
                num_net = self.getNumNetworkIfaces()

            for public_ip in public_ips:
                if public_ip not in private_ips:
                    vm_system.setValue('net_interface.' + str(num_net) + '.ip',
                                       str(public_ip))
                    vm_system.setValue(
                        'net_interface.' + str(num_net) + '.connection',
                        public_net.id)

        if private_ips:
            private_net_map = {}

            for private_ip in private_ips:
                private_net_mask = None

                # Get the private network mask
                for mask in Config.PRIVATE_NET_MASKS:
                    if IPAddress(private_ip) in IPNetwork(mask):
                        private_net_mask = mask
                        break

                if not private_net_mask:
                    parts = private_ip.split(".")
                    private_net_mask = "%s.0.0.0/8" % parts[0]
                    VirtualMachine.logger.warn(
                        "%s is not in known private net groups. Using mask: %s"
                        % (private_ip, private_net_mask))

                # Search in previous used private ips
                private_net = None
                for net_mask, net in private_net_map.iteritems():
                    if IPAddress(private_ip) in IPNetwork(net_mask):
                        private_net = net

                # Search in the RADL nets, first in the nets this VM is
                # connected to
                if private_net is None:
                    for net in self.info.networks:
                        if (not net.isPublic()
                                and net not in private_net_map.values()
                                and self.getNumNetworkWithConnection(
                                    net.id) is not None):
                            private_net = net
                            private_net_map[private_net_mask] = net
                            break

                # Search in the rest of RADL nets
                if private_net is None:
                    for net in self.info.networks:
                        if not net.isPublic(
                        ) and net not in private_net_map.values():
                            private_net = net
                            private_net_map[private_net_mask] = net
                            break

                # if it is still None, then create a new one
                if private_net is None:
                    private_net = network.createNetwork(
                        "private." + private_net_mask.split('/')[0])
                    self.info.networks.append(private_net)
                    num_net = self.getNumNetworkIfaces()
                else:
                    # If there are are private net, get the ID
                    num_net = self.getNumNetworkWithConnection(private_net.id)
                    if num_net is None:
                        # There are a private net but it has not been used in
                        # this VM
                        num_net = self.getNumNetworkIfaces()

                vm_system.setValue('net_interface.' + str(num_net) + '.ip',
                                   str(private_ip))
                vm_system.setValue(
                    'net_interface.' + str(num_net) + '.connection',
                    private_net.id)
Ejemplo n.º 5
0
    def setIps(self, public_ips, private_ips):
        """
        Set the specified IPs in the VM RADL info
        """
        now = str(int(time.time() * 100))
        vm_system = self.info.systems[0]

        if public_ips and not set(public_ips).issubset(set(private_ips)):
            public_nets = []
            for net in self.info.networks:
                if net.isPublic():
                    public_nets.append(net)

            if public_nets:
                public_net = None
                for net in public_nets:
                    num_net = self.getNumNetworkWithConnection(net.id)
                    if num_net is not None:
                        public_net = net
                        break

                if not public_net:
                    # There are a public net but it has not been used in this
                    # VM
                    public_net = public_nets[0]
                    num_net = self.getNumNetworkIfaces()
            else:
                # There no public net, create one
                public_net = network.createNetwork("public." + now, True)
                self.info.networks.append(public_net)
                num_net = self.getNumNetworkIfaces()

            for public_ip in public_ips:
                if public_ip not in private_ips:
                    vm_system.setValue('net_interface.' +
                                       str(num_net) + '.ip', str(public_ip))
                    vm_system.setValue(
                        'net_interface.' + str(num_net) + '.connection', public_net.id)

        if private_ips:
            private_net_map = {}

            for private_ip in private_ips:
                private_net_mask = None

                # Get the private network mask
                for mask in Config.PRIVATE_NET_MASKS:
                    if IPAddress(private_ip) in IPNetwork(mask):
                        private_net_mask = mask
                        break

                if not private_net_mask:
                    parts = private_ip.split(".")
                    private_net_mask = "%s.0.0.0/8" % parts[0]
                    VirtualMachine.logger.warn("%s is not in known private net groups. Using mask: %s" % (
                        private_ip, private_net_mask))

                # Search in previous used private ips
                private_net = None
                for net_mask, net in private_net_map.iteritems():
                    if IPAddress(private_ip) in IPNetwork(net_mask):
                        private_net = net

                # Search in the RADL nets, first in the nets this VM is
                # connected to
                if private_net is None:
                    for net in self.info.networks:
                        if (not net.isPublic() and net not in private_net_map.values() and
                                self.getNumNetworkWithConnection(net.id) is not None):
                            private_net = net
                            private_net_map[private_net_mask] = net
                            break

                # Search in the rest of RADL nets
                if private_net is None:
                    for net in self.info.networks:
                        if not net.isPublic() and net not in private_net_map.values():
                            private_net = net
                            private_net_map[private_net_mask] = net
                            break

                # if it is still None, then create a new one
                if private_net is None:
                    private_net = network.createNetwork(
                        "private." + private_net_mask.split('/')[0])
                    self.info.networks.append(private_net)
                    num_net = self.getNumNetworkIfaces()
                else:
                    # If there are are private net, get the ID
                    num_net = self.getNumNetworkWithConnection(private_net.id)
                    if num_net is None:
                        # There are a private net but it has not been used in
                        # this VM
                        num_net = self.getNumNetworkIfaces()

                vm_system.setValue('net_interface.' +
                                   str(num_net) + '.ip', str(private_ip))
                vm_system.setValue(
                    'net_interface.' + str(num_net) + '.connection', private_net.id)
Ejemplo n.º 6
0
    def setIps(self,
               public_ips,
               private_ips,
               remove_old=False,
               ignore_nets=None):
        """
        Set the specified IPs in the VM RADL info
        """
        if not ignore_nets:
            ignore_nets = []
        vm_system = self.info.systems[0]

        # First remove old ip values
        # in case that some IP has been removed from the VM
        if remove_old:
            cont = 0
            while vm_system.getValue('net_interface.%d.connection' % cont):
                if vm_system.getValue('net_interface.%d.ip' % cont):
                    vm_system.delValue('net_interface.%d.ip' % cont)
                cont += 1

        if public_ips and not set(public_ips).issubset(set(private_ips)):
            public_net, num_net = self.add_public_net(self.info)

            real_public_ips = [
                public_ip for public_ip in public_ips
                if public_ip not in private_ips
            ]
            if real_public_ips:
                vm_system.setValue('net_interface.%s.connection' % num_net,
                                   public_net.id)
                if len(real_public_ips) > 1:
                    self.log_warn("Node with more that one public IP!")
                    self.log_debug(real_public_ips)
                    if len(real_public_ips) == 2:
                        ip1 = IPAddress(real_public_ips[0])
                        ip2 = IPAddress(real_public_ips[1])
                        if ip1.version != ip2.version:
                            self.log_info(
                                "It seems that there are one IPv4 and other IPv6. Get the IPv4 one."
                            )
                            if ip1.version == 4:
                                vm_system.setValue(
                                    'net_interface.%s.ip' % num_net,
                                    str(real_public_ips[0]))
                                vm_system.setValue(
                                    'net_interface.%s.ipv6' % num_net,
                                    str(real_public_ips[1]))
                            else:
                                vm_system.setValue(
                                    'net_interface.%s.ip' % num_net,
                                    str(real_public_ips[1]))
                                vm_system.setValue(
                                    'net_interface.%s.ipv6' % num_net,
                                    str(real_public_ips[0]))
                        else:
                            self.log_info(
                                "It seems that both are from the same version first one will be used"
                            )
                            vm_system.setValue('net_interface.%s.ip' % num_net,
                                               str(real_public_ips[0]))
                    else:
                        self.log_info(
                            "It seems that there are more that 2 last ones will be used"
                        )
                        for ip in real_public_ips:
                            if IPAddress(ip).version == 4:
                                vm_system.setValue(
                                    'net_interface.%s.ip' % num_net, str(ip))
                            else:
                                vm_system.setValue(
                                    'net_interface.%s.ipv6' % num_net, str(ip))
                else:
                    # The usual case
                    if IPAddress(real_public_ips[0]).version == 6:
                        self.log_warn("Node only with one IPv6!!")
                        vm_system.setValue('net_interface.%s.ipv6' % num_net,
                                           str(real_public_ips[0]))
                    else:
                        vm_system.setValue('net_interface.%s.ip' % num_net,
                                           str(real_public_ips[0]))

        if private_ips:
            private_net_map = {}

            for private_ip in private_ips:
                private_net_mask = None

                # Get the private network mask
                for mask in Config.PRIVATE_NET_MASKS:
                    if IPAddress(private_ip) in IPNetwork(mask):
                        private_net_mask = mask
                        break

                if not private_net_mask:
                    parts = private_ip.split(".")
                    private_net_mask = "%s.0.0.0/8" % parts[0]
                    self.log_warn(
                        "%s is not in known private net groups. Using mask: %s"
                        % (private_ip, private_net_mask))

                # Search in previous used private ips
                private_net = None
                for net_mask, net in private_net_map.items():
                    if IPAddress(private_ip) in IPNetwork(net_mask):
                        private_net = net

                # Search in the RADL nets, first in the nets this VM is
                # connected to and check the CIDR of the nets
                if private_net is None:
                    for net in self.info.networks:
                        if (not net.isPublic()
                                and net not in private_net_map.values()
                                and net.id not in ignore_nets
                                and self.getNumNetworkWithConnection(net.id)
                                is not None and net.getValue('cidr')
                                and IPAddress(private_ip) in IPNetwork(
                                    net.getValue('cidr'))):
                            private_net = net
                            private_net_map[net.getValue('cidr')] = net
                            break

                # Now in the RADL nets this VM is connected to
                # but without CIDR set
                if private_net is None:
                    for net in self.info.networks:
                        if (not net.isPublic()
                                and net not in private_net_map.values()
                                and net.id not in ignore_nets
                                and self.getNumNetworkWithConnection(net.id)
                                is not None and not net.getValue('cidr')):
                            private_net = net
                            private_net_map[private_net_mask] = net
                            break

                # Search in the rest of RADL nets
                if private_net is None:
                    # First check the CIDR
                    for net in self.info.networks:
                        if (not net.isPublic()
                                and net not in private_net_map.values()
                                and net.id not in ignore_nets
                                and net.getValue('cidr')
                                and IPAddress(private_ip) in IPNetwork(
                                    net.getValue('cidr'))):
                            private_net = net
                            private_net_map[private_net_mask] = net
                            break

                    # The search in the rest
                    for net in self.info.networks:
                        if (not net.isPublic()
                                and net not in private_net_map.values()
                                and net.id not in ignore_nets
                                and not net.getValue('cidr')):
                            private_net = net
                            private_net_map[private_net_mask] = net
                            break

                # if it is still None, then create a new one
                if private_net is None:
                    private_net = network.createNetwork(
                        "private." + private_net_mask.split('/')[0])
                    self.info.networks.append(private_net)
                    num_net = self.getNumNetworkIfaces()
                else:
                    # If there are are private net, get the ID
                    num_net = self.getNumNetworkWithConnection(private_net.id)
                    if num_net is None:
                        # There are a private net but it has not been used in
                        # this VM
                        num_net = self.getNumNetworkIfaces()

                if IPAddress(private_ip).version == 6:
                    vm_system.setValue('net_interface.%s.ipv6' % num_net,
                                       str(private_ip))
                else:
                    vm_system.setValue('net_interface.%s.ip' % num_net,
                                       str(private_ip))
                vm_system.setValue('net_interface.%s.connection' % num_net,
                                   private_net.id)