def create(self, vnName, ipAddress, ipv6Address=None):
        interfaceName = 'veth' + self.vmName
        vm_instance = vnc_api.VirtualMachine(name=self.vmName)
        self.vnc_client.virtual_machine_create(vm_instance)
        vm_interface = vnc_api.VirtualMachineInterface(name=interfaceName,
                                                       parent_obj=vm_instance)
        vn = OpenContrailVN(vnName).VNget()
        vm_interface.set_virtual_network(vn)
        self.vnc_client.virtual_machine_interface_create(vm_interface)
        vm_interface = self.vnc_client.virtual_machine_interface_read(
            id=vm_interface.uuid)
        ipuuid = str(uuid.uuid4())
        ip = vnc_api.InstanceIp(name=ipuuid,
                                instance_ip_address=ipAddress.split('/')[0])
        ip.set_virtual_machine_interface(vm_interface)
        ip.set_virtual_network(vn)
        self.vnc_client.instance_ip_create(ip)

        mac = vm_interface.virtual_machine_interface_mac_addresses.mac_address[
            0]
        vrouterInterface = interfaceName + 'p0'

        if ipv6Address:
            ipv6uuid = str(uuid.uuid4())
            ipv6 = vnc_api.InstanceIp(
                name=ipv6uuid, instance_ip_address=ipv6Address.split('/')[0])
            ipv6.set_instance_ip_family('v6')
            ipv6.uuid = ipv6uuid
            ipv6.set_virtual_machine_interface(vm_interface)
            ipv6.set_virtual_network(vn)
            self.vnc_client.instance_ip_create(ipv6)
            ContrailVRouterApi().add_port(
                vm_instance.uuid,
                vm_interface.uuid,
                vrouterInterface,
                mac,
                display_name=vm_instance.name,
                vm_project_id=self.tenant.uuid,
                port_type='NovaVMPort',
                ip6_address=ipv6Address.split('/')[0])
        else:
            ContrailVRouterApi().add_port(vm_instance.uuid,
                                          vm_interface.uuid,
                                          vrouterInterface,
                                          mac,
                                          display_name=vm_instance.name,
                                          vm_project_id=self.tenant.uuid,
                                          port_type='NovaVMPort')
Esempio n. 2
0
        def create(name, image, flavor, nics, *args, **kwargs):
            vm = vnc_api.VirtualMachine(name)
            FakeNovaClient.vnc_lib.virtual_machine_create(vm)
            for network in nics:
                if 'nic-id' in network:
                    vn = FakeNovaClient.vnc_lib.virtual_network_read(
                        id=network['net-id'])
                    vmi = vnc_api.VirtualMachineInterface(vn.name,
                                                          parent_obj=vm)
                    vmi.set_virtual_network(vn)
                    FakeNovaClient.vnc_lib.virtual_machine_interface_create(
                        vmi)

                    ip_address = FakeNovaClient.vnc_lib.virtual_network_ip_alloc(
                        vn, count=1)[0]
                    ip_obj = vnc_api.InstanceIp(ip_address, ip_address)
                    ip_obj.add_virtual_network(vn)
                    ip_obj.add_virtual_machine_interface(vmi)
                    FakeNovaClient.vnc_lib.instance_ip_create(ip_obj)
                elif 'port-id' in network:
                    vmi = FakeNovaClient.vnc_lib.virtual_machine_interface_read(
                        id=network['port-id'])
                    vmi.add_virtual_machine(vm)
                    FakeNovaClient.vnc_lib.virtual_machine_interface_update(
                        vmi)
            # end for network
            vm.id = vm.uuid
            vm.delete = FakeNovaClient.delete_vm.__get__(
                vm, vnc_api.VirtualMachine)
            vm.get = stub
            return vm
Esempio n. 3
0
 def createVmi(self, vnName, requestedIp):
     interfaceName = 'veth' + self.vmName
     try:
         ip = self.vnc_client.instance_ip_read(fq_name_str=interfaceName)
         if ip:
             print 'deleting ip instance'
             self.vnc_client.instance_ip_delete(id=ip.uuid)
     except:
         print 'no ip instance'
     try:
         vm_interface = self.vnc_client.virtual_machine_interface_read(
             fq_name=[self.vmName, interfaceName])
         if vm_interface:
             print 'deleting vm interface'
             self.vnc_client.virtual_machine_interface_delete(
                 id=vm_interface.uuid)
     except:
         print 'no vm interface'
     try:
         vm = self.vnc_client.virtual_machine_read(fq_name_str=self.vmName)
         if vm:
             print 'deleting vm'
             self.vnc_client.virtual_machine_delete(id=vm.uuid)
     except:
         print 'no vm'
     vm_instance = vnc_api.VirtualMachine(name=self.vmName)
     self.vnc_client.virtual_machine_create(vm_instance)
     vm_interface = vnc_api.VirtualMachineInterface(name=interfaceName,
                                                    parent_obj=vm_instance)
     vn = OpenContrailVN(vnName).VNget()
     vm_interface.set_virtual_network(vn)
     self.vnc_client.virtual_machine_interface_create(vm_interface)
     vm_interface = self.vnc_client.virtual_machine_interface_read(
         id=vm_interface.uuid)
     ip = vnc_api.InstanceIp(name=interfaceName,
                             instance_ip_address=requestedIp.split('/')[0])
     ip.set_virtual_machine_interface(vm_interface)
     ip.set_virtual_network(vn)
     self.vnc_client.instance_ip_create(ip)
     ip = self.vnc_client.instance_ip_read(id=ip.uuid)
     ipAddress = ip.get_instance_ip_address()
     print 'ipaddress: %s' % ipAddress
     subnet = vn.network_ipam_refs[0]['attr'].ipam_subnets[0]
     gw = subnet.default_gateway
     mac = vm_interface.virtual_machine_interface_mac_addresses.mac_address[
         0]
     vrouterInterface = interfaceName + 'p0'
     ContrailVRouterApi().add_port(vm_instance.uuid,
                                   vm_interface.uuid,
                                   vrouterInterface,
                                   mac,
                                   display_name=vm_instance.name,
                                   vm_project_id=self.tenant.uuid,
                                   port_type='NovaVMPort')
     return {
         'ip': ipAddress,
         'interface': interfaceName,
         'gateway': gw,
         'mac': mac
     }
Esempio n. 4
0
def createInstanceIp(ip, vmInterface, vn):
    instIpUUID = str(uuid.uuid4())
    ipInst = vnc_api.InstanceIp(name = instIpUUID, instance_ip_address = ip)
    ipInst.set_virtual_machine_interface(vmInterface)
    ipInst.set_virtual_network(vn)
    try:
        vnc_client.instance_ip_create(ipInst)
    except:
        print 'cannot create instance ip'
Esempio n. 5
0
 def add_iip(self, obj, network):
     vn_fq_name = [self.domain, self.tenant, network]
     vn_obj = self.vnc_handle.virtual_network_read(fq_name=vn_fq_name)
     id = str(uuid.uuid4())
     print id
     iip_obj = vnc_api.InstanceIp(name=id)
     iip_obj.add_virtual_network(vn_obj)
     iip_obj.set_instance_ip_family('v4')
     iip_obj.add_virtual_machine_interface(obj)
     self.vnc_handle.instance_ip_create(iip_obj)
Esempio n. 6
0
 def _allocate_iip_for_family(self, vn_obj, pt_obj, vmi_obj, iip_family):
     iip_name = self._get_iip_name(pt_obj, vmi_obj, iip_family)
     iip_obj = vnc_api.InstanceIp(name=iip_name,
                                  instance_ip_family=iip_family)
     iip_obj.add_virtual_network(vn_obj)
     try:
         self.vnc_lib().instance_ip_create(iip_obj)
     except vnc_api.RefsExistError:
         iip_obj = self.vnc_lib().instance_ip_read(fq_name=[iip_name])
     return iip_obj
def vnf_instance_ip(vnc_vmi):
    ip_name = 'vnf-instance-ip-name'
    ip_uuid = 'vnf-instance-ip-uuid'
    ip = vnc_api.InstanceIp(name=ip_uuid,
                            display_name=ip_name,
                            id_perms=vnc_api.IdPermsType(
                                creator='not-vcenter-manager', enable=True))
    ip.set_instance_ip_address('0.255.255.239')
    ip.set_uuid(ip_uuid)
    ip.set_virtual_machine_interface(vnc_vmi)
    return ip
 def create_instance_ip(self, vn_obj, vmi_obj, ip_addr=None,
                        subnet_uuid=None, ip_family='v4'):
     ip_name = str(uuid.uuid4())
     ip_obj = vnc_api.InstanceIp(name=ip_name)
     ip_obj.uuid = ip_name
     if subnet_uuid:
         ip_obj.set_subnet_uuid(subnet_uuid)
     ip_obj.set_virtual_machine_interface(vmi_obj)
     ip_obj.set_virtual_network(vn_obj)
     if hasattr(ip_obj, 'set_instance_ip_family'):
         ip_obj.set_instance_ip_family(ip_family)
     if ip_addr:
         ip_obj.set_instance_ip_address(ip_addr)
     ip_id = self._resource_create(ip_obj)
     return ip_id
def instance_ip(vnc_vmi, vnc_vn_1, vnc_vm):
    ip_name = 'ip-' + vnc_vn_1.name + '-' + vnc_vm.name
    ip_uuid = VirtualMachineInterfaceModel.create_uuid(ip_name)

    ip = vnc_api.InstanceIp(
        name=ip_uuid,
        display_name=ip_name,
        id_perms=ID_PERMS,
    )

    ip.set_instance_ip_address('10.10.10.1')
    ip.set_uuid(ip_uuid)
    ip.set_virtual_network(vnc_vn_1)
    ip.set_virtual_machine_interface(vnc_vmi)
    ip.annotations = ip.annotations or vnc_api.KeyValuePairs()
    ip.annotations.add_key_value_pair(
        vnc_api.KeyValuePair('vrouter-uuid', 'vrouter-uuid-1'))
    ip.annotations.add_key_value_pair(vnc_api.KeyValuePair('key-1', 'value-1'))
    return ip
    def create(self):
        """Create a vm and vmi, find or create a network, and attach
        the vmi to a new veth interface

        Arguments:

          vm_name   name of the vm to create
          net_name  name of the netwok to arrach to
          subnet    x.x.x.x/len - optional if network already exists
          netns     Network namespace where the veth interface is bound to.  Defaults to net_name    

        Returns:

          A dict with the following elements:
          
          port_id  uuid of port
          ip
          veth     name of veth interface
          netns    Network namespace where the veth interface is bound to
          
        """
        
        # remember what to clean up if things go wrong
        port_created = False
        veth_created = False
        netns_created = False
        ip_created = False
        vmi_created = False
        vnet_created = False
        vm_created = False

        try:
            # sanity check
            net_name = self.args.get('net_name')
            if not net_name:
                raise ValueError("Network name argument is required")

            # sanitize netns since it gets passed to the shell
            netns = self.args.get('netns')
            if not netns:
                netns = net_name
            if not re.match(r'^[-.\w]+$', netns):
                raise ValueError("netns=[%s] must be a valid namespace name"
                                 + " (a single word)" % netns)

            
            vnc_client = self.vnc_connect()

            proj_fq_name = self.args['project'].split(':')

            # find or create the VM
            vm_fq_name = proj_fq_name + [ self.args['vm_name'] ]

            # debug
            #import pdb; pdb.set_trace()

            try:
                vm = vnc_client.virtual_machine_read(fq_name = vm_fq_name)
                if vm:
                    raise ValueError(("Virtual machine named %s already exists."
                                      + "  Use --delete to delete it")
                                     % self.args['vm_name'])
            except vnc_api.NoIdError:
                # create vm if necessary
                vm = vnc_api.VirtualMachine(':'.join(vm_fq_name),
                                            fq_name=vm_fq_name)
                vnc_client.virtual_machine_create(vm)
                vm = vnc_client.virtual_machine_read(fq_name = vm_fq_name)
                vm_created = True
                
            # find or create the network
            vnet_fq_name = proj_fq_name + [ net_name ]
            vnet_created = False
            try:
                vnet = vnc_client.virtual_network_read(fq_name = vnet_fq_name)
            except vnc_api.NoIdError:
                # create the network if it doesn't exist
                vnet = vnc_api.VirtualNetwork(vnet_fq_name[-1],
                                              parent_type = 'project',
                                              fq_name = vnet_fq_name)

                # add a subnet
                ipam = vnc_client.network_ipam_read(
                    fq_name = ['default-domain',
                               'default-project',
                               'default-network-ipam'])
                (prefix, plen) = self.args['subnet'].split('/')
                subnet = vnc_api.IpamSubnetType(
                    subnet = vnc_api.SubnetType(prefix, int(plen)))
                vnet.add_network_ipam(ipam, vnc_api.VnSubnetsType([subnet]))

                vnc_client.virtual_network_create(vnet)
                vnet_created = True

            # find or create the vmi
            vmi_fq_name = vm.fq_name + ['0']
            vmi_created = False
            try:
                vmi = vnc_client.virtual_machine_interface_read(
                    fq_name = vmi_fq_name)
            except vnc_api.NoIdError:
                vmi = vnc_api.VirtualMachineInterface(
                    parent_type = 'virtual-machine',
                    fq_name = vmi_fq_name)
                vmi_created = True
            vmi.set_virtual_network(vnet)
            if vmi_created:
                vnc_client.virtual_machine_interface_create(vmi)
            else:
                vnc_client.virtual_machine_interface_update(vmi)
            # re-read the vmi to get its mac addresses
            vmi = vnc_client.virtual_machine_interface_read(
                fq_name = vmi_fq_name)
            # create an IP for the VMI if it doesn't already have one
            ips = vmi.get_instance_ip_back_refs()
            if not ips:
                ip = vnc_api.InstanceIp(vm.name + '.0')
                ip.set_virtual_machine_interface(vmi)
                ip.set_virtual_network(vnet)
                ip_created = vnc_client.instance_ip_create(ip)

            # Create the veth port.  Create a veth pair.  Put one end
            # in the VMI port and the other in a network namespace
            
            # get the ip, mac, and gateway from the vmi
            ip_uuid = vmi.get_instance_ip_back_refs()[0]['uuid']
            ip = vnc_client.instance_ip_read(id=ip_uuid).instance_ip_address
            mac = vmi.virtual_machine_interface_mac_addresses.mac_address[0]
            subnet = vnet.network_ipam_refs[0]['attr'].ipam_subnets[0]
            gw = subnet.default_gateway
            dns = gw # KLUDGE - that's the default, but some networks
                     # have other DNS configurations
            ipnetaddr = netaddr.IPNetwork("%s/%s" %
                                          (subnet.subnet.ip_prefix,
                                           subnet.subnet.ip_prefix_len))
            
            # set up the veth pair with one part for vrouter and one
            # for the netns

            # find a name that's not already used in the default or
            # netns namespaces
            link_exists = link_exists_func('', netns)
            veth_vrouter = new_interface_name(suffix=vnet.uuid, prefix="ve1",
                                              exists_func=link_exists)
            veth_host = new_interface_name(suffix=vnet.uuid, prefix="ve0",
                                           exists_func=link_exists)
            
            sudo("ip link add %s type veth peer name %s",
                 (veth_vrouter, veth_host))
            veth_created = True
            try:
                sudo("ip netns add %s", (netns,))
                netns_created = True
            except ProcessExecutionError:
                pass
            
            sudo("ip link set %s netns %s",
                 (veth_host, netns))
            sudo("ip netns exec %s ip link set dev %s address %s",
                 (netns, veth_host, mac))
            sudo("ip netns exec %s ip address add %s broadcast %s dev %s",
                 (netns,
                  ("%s/%s" % (ip, subnet.subnet.ip_prefix_len)),
                  ipnetaddr.broadcast, veth_host))
            sudo("ip netns exec %s ip link set dev %s up",
                 (netns, veth_host))
            sudo("ip netns exec %s route add default gw %s dev %s",
                 (netns, gw, veth_host))
            sudo("ip link set dev %s up", (veth_vrouter,))

            # make a namespace-specific resolv.conf
            resolv_conf = "/etc/netns/%s/resolv.conf" % netns
            resolv_conf_body = "nameserver %s\n" % dns
            sudo("mkdir -p %s", (os.path.dirname(resolv_conf),))
            sudo("tee %s", (resolv_conf,), process_input=resolv_conf_body)

            # finally, create the Contrail port
            port = instance_service.ttypes.Port(
                uuid_from_string(vmi.uuid),
                uuid_from_string(vm.uuid), 
                veth_vrouter,
                ip,
                uuid_from_string(vnet.uuid),
                mac,
                )
            rpc = vrouter_rpc()
            rpc.AddPort([port])
            port_created = True
            
            return(dict(
                port_id = uuid_array_to_str(port.port_id),
                vm_id = vm.uuid,
                net_id = vnet.uuid,
                vmi_id = vmi.uuid,
                veth = veth_host,
                netns = netns,
                ip = ip,
                mac = mac,
                gw = gw,
                dns = dns,
                netmask = str(ipnetaddr.netmask),
                broadcast = str(ipnetaddr.broadcast),
                ))

        except:
            # something went wrong, clean up
            if port_created:
                rpc.DeletePort(port.port_id)
            if veth_created:
                sudo("ip link delete %s", (veth_vrouter,),
                     check_exit_code=False)
            if netns_created:
                sudo("ip netns delete %s", (netns,), check_exit_code=False)
            if ip_created:
                vnc_client.instance_ip_delete(id=ip_created)
            if vmi_created:
                vnc_client.virtual_machine_interface_delete(id=vmi.uuid)
            if vnet_created:
                vnc_client.virtual_network_delete(id=vnet.uuid)
            if vm_created:
                vnc_client.virtual_machine_delete(id=vm.uuid)
            raise
Esempio n. 11
0
    if vmi_created:
        vnc.virtual_machine_interface_create(vm_interface)
    else:
        vnc.virtual_machine_interface_update(vm_interface)
    # re-read the vmi to get its mac addresses
    vm_interface = vnc.virtual_machine_interface_read(fq_name=vmi_fq_name)
    print "[INFO] VM_Ineterface: %s fq_name: %s" % (vmi_name,
                                                    vm_interface.fq_name)

# create an IP for the VMI if it doesn't already have one
print "=========>>> CONFIGURE VM INTERFACE IP INFO ... ..."
ips = vm_interface.get_instance_ip_back_refs()
print "[INFO] VM_Interface: %s 's ips:%s" % (vmi_name, ips)
if not ips:
    print "[WARN] ips is [], Creating ..."
    ip = vnc_api.InstanceIp(vm_instance.name + '.' + vmi_name)
    ip.set_virtual_machine_interface(vm_interface)
    ip.set_virtual_network(vnet)
    ip_created = vnc.instance_ip_create(ip)

# get the ip, mac, and gateway from the vmi
ip_uuid = vm_interface.get_instance_ip_back_refs()[0]['uuid']
ip = vnc.instance_ip_read(id=ip_uuid).instance_ip_address
mac = vm_interface.virtual_machine_interface_mac_addresses.mac_address[0]
subnet = vnet.network_ipam_refs[0]['attr'].ipam_subnets[0]
ipaddr = "%s/%s" % (ip, subnet.subnet.ip_prefix_len)
gw = subnet.default_gateway
dns = gw
print "[INFO] VM_INTERFACE IPINFO:\n [ip_uuid:%s,ip:%s,mac:%s,subnet:%s,gw:%s,dns:%s]" % (
    ip_uuid, ip, mac, ipaddr, gw, dns)
Esempio n. 12
0
    def handle_create(self):
        obj_0 = vnc_api.InstanceIp(name=self.properties[self.NAME])

        if self.properties.get(self.DISPLAY_NAME) is not None:
            obj_0.set_display_name(self.properties.get(self.DISPLAY_NAME))
        if self.properties.get(self.SERVICE_HEALTH_CHECK_IP) is not None:
            obj_0.set_service_health_check_ip(
                self.properties.get(self.SERVICE_HEALTH_CHECK_IP))
        if self.properties.get(self.SECONDARY_IP_TRACKING_IP) is not None:
            obj_1 = vnc_api.SubnetType()
            if self.properties.get(self.SECONDARY_IP_TRACKING_IP, {}).get(
                    self.SECONDARY_IP_TRACKING_IP_IP_PREFIX) is not None:
                obj_1.set_ip_prefix(
                    self.properties.get(self.SECONDARY_IP_TRACKING_IP, {}).get(
                        self.SECONDARY_IP_TRACKING_IP_IP_PREFIX))
            if self.properties.get(self.SECONDARY_IP_TRACKING_IP, {}).get(
                    self.SECONDARY_IP_TRACKING_IP_IP_PREFIX_LEN) is not None:
                obj_1.set_ip_prefix_len(
                    self.properties.get(self.SECONDARY_IP_TRACKING_IP, {}).get(
                        self.SECONDARY_IP_TRACKING_IP_IP_PREFIX_LEN))
            obj_0.set_secondary_ip_tracking_ip(obj_1)
        if self.properties.get(self.INSTANCE_IP_ADDRESS) is not None:
            obj_0.set_instance_ip_address(
                self.properties.get(self.INSTANCE_IP_ADDRESS))
        if self.properties.get(self.INSTANCE_IP_MODE) is not None:
            obj_0.set_instance_ip_mode(
                self.properties.get(self.INSTANCE_IP_MODE))
        if self.properties.get(self.SUBNET_UUID) is not None:
            obj_0.set_subnet_uuid(self.properties.get(self.SUBNET_UUID))
        if self.properties.get(self.INSTANCE_IP_FAMILY) is not None:
            obj_0.set_instance_ip_family(
                self.properties.get(self.INSTANCE_IP_FAMILY))
        if self.properties.get(self.SERVICE_INSTANCE_IP) is not None:
            obj_0.set_service_instance_ip(
                self.properties.get(self.SERVICE_INSTANCE_IP))
        if self.properties.get(self.INSTANCE_IP_SECONDARY) is not None:
            obj_0.set_instance_ip_secondary(
                self.properties.get(self.INSTANCE_IP_SECONDARY))

        # reference to physical_router_refs
        if self.properties.get(self.PHYSICAL_ROUTER_REFS):
            for index_0 in range(
                    len(self.properties.get(self.PHYSICAL_ROUTER_REFS))):
                try:
                    ref_obj = self.vnc_lib().physical_router_read(
                        id=self.properties.get(
                            self.PHYSICAL_ROUTER_REFS)[index_0])
                except vnc_api.NoIdError:
                    ref_obj = self.vnc_lib().physical_router_read(
                        fq_name_str=self.properties.get(
                            self.PHYSICAL_ROUTER_REFS)[index_0])
                obj_0.add_physical_router(ref_obj)

        # reference to virtual_machine_interface_refs
        if self.properties.get(self.VIRTUAL_MACHINE_INTERFACE_REFS):
            for index_0 in range(
                    len(
                        self.properties.get(
                            self.VIRTUAL_MACHINE_INTERFACE_REFS))):
                try:
                    ref_obj = self.vnc_lib().virtual_machine_interface_read(
                        id=self.properties.get(
                            self.VIRTUAL_MACHINE_INTERFACE_REFS)[index_0])
                except vnc_api.NoIdError:
                    ref_obj = self.vnc_lib().virtual_machine_interface_read(
                        fq_name_str=self.properties.get(
                            self.VIRTUAL_MACHINE_INTERFACE_REFS)[index_0])
                obj_0.add_virtual_machine_interface(ref_obj)

        # reference to virtual_network_refs
        if self.properties.get(self.VIRTUAL_NETWORK_REFS):
            for index_0 in range(
                    len(self.properties.get(self.VIRTUAL_NETWORK_REFS))):
                try:
                    ref_obj = self.vnc_lib().virtual_network_read(
                        id=self.properties.get(
                            self.VIRTUAL_NETWORK_REFS)[index_0])
                except vnc_api.NoIdError:
                    ref_obj = self.vnc_lib().virtual_network_read(
                        fq_name_str=self.properties.get(
                            self.VIRTUAL_NETWORK_REFS)[index_0])
                obj_0.add_virtual_network(ref_obj)

        try:
            obj_uuid = super(ContrailInstanceIp, self).resource_create(obj_0)
        except:
            raise Exception(
                _('instance-ip %s could not be updated.') % self.name)

        self.resource_id_set(obj_uuid)
 def create(self, networkId, ipAddress, ipv6Address=None):
     vn = OpenContrailVN(networkId).VNget()
     ip = vnc_api.InstanceIp(name=self.epName,
                             instance_ip_address=ipAddress.split('/')[0])
     ip.set_virtual_network(vn)
     self.vnc_client.instance_ip_create(ip)