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
def handle_create(self): obj_0 = vnc_api.VirtualMachine(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)) # reference to service_instance_refs if self.properties.get(self.SERVICE_INSTANCE_REFS): for index_0 in range( len(self.properties.get(self.SERVICE_INSTANCE_REFS))): try: ref_obj = self.vnc_lib().service_instance_read( id=self.properties.get( self.SERVICE_INSTANCE_REFS)[index_0]) except vnc_api.NoIdError: ref_obj = self.vnc_lib().service_instance_read( fq_name_str=self.properties.get( self.SERVICE_INSTANCE_REFS)[index_0]) obj_0.add_service_instance(ref_obj) try: obj_uuid = super(ContrailVirtualMachine, self).resource_create(obj_0) except: raise Exception( _('virtual-machine %s could not be updated.') % self.name) self.resource_id_set(obj_uuid)
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 }
def join(self, networkId, sandboxKey): ipInstance = self.vnc_client.instance_ip_read(fq_name=[self.epName]) vn = OpenContrailVN(networkId).VNget() hostname = socket.gethostname() interfaceName = 'veth' + self.epName + 'p0' try: vmInstance = self.vnc_client.virtual_machine_read( fq_name=[sandboxKey]) except: vmInstance = vnc_api.VirtualMachine(name=sandboxKey) self.vnc_client.virtual_machine_create(vmInstance) vmInstance = self.vnc_client.virtual_machine_read(fq_name=[sandboxKey]) vmInterface = vnc_api.VirtualMachineInterface(name=interfaceName, parent_obj=self.tenant) vmInterface.set_virtual_machine(vmInstance) vmInterface.set_virtual_network(vn) self.vnc_client.virtual_machine_interface_create(vmInterface) vmInterface = self.vnc_client.virtual_machine_interface_read( id=vmInterface.uuid) ipInstance.set_virtual_machine_interface(vmInterface) self.vnc_client.instance_ip_update(ipInstance) mac = vmInterface.virtual_machine_interface_mac_addresses.mac_address[ 0] interfaceName + 'p0' instIpAddress = ipInstance.get_instance_ip_address() return { 'mac': mac, 'vmInstanceUuid': vmInstance.uuid, 'vmInterfaceUuid': vmInterface.uuid, 'vrouterInterface': interfaceName, 'vmInstanceName': vmInstance.name, 'vmProjectId': self.tenant.uuid, 'ipAddress': instIpAddress, 'vnId': vn.uuid }
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')
def create(self, name): fq_name = [self.domain, self.tenant, name] try: vm_obj = self.vnc_handle.virtual_machine_read(fq_name=fq_name) print "Virtual Machine exists" return vm_obj.uuid except vnc_exc.NoIdError: vm_obj = vnc_api.VirtualMachine(name=name) vm_id = self.vnc_handle.virtual_machine_create(vm_obj) if vm_id: print "Successfully creted the virtual-machine object [{}]".format( vm_id) return vm_id else: print "Error, failed to create the virtual machine" raise
def ensure_vm_instance(self, instance_id): instance_name = instance_id instance_obj = vnc_api.VirtualMachine(instance_name) try: try: uuid.UUID(instance_id) instance_obj.uuid = instance_id except ValueError: # if instance_id is not a valid uuid, let # virtual_machine_create generate uuid for the vm pass self._resource_create(instance_obj) except vnc_exc.RefsExistError: instance_obj = self._resource_get(id=instance_obj.uuid) return instance_obj
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
def vnc_vm_2(): vm = vnc_api.VirtualMachine('vnc-vm-uuid-2') vm.set_uuid('vnc-vm-uuid-2') return vm
#project name proj_fq_name = project.split(':') # get vnc client vnc = vnc_api.VncApi(api_server_host=api_server, api_server_port=api_port) #find or create vm instance print "=========>>> CREATE VM INSTANCE ... ..." vm_fq_name = [vm_name] try: vm_instance = vnc.virtual_machine_read(fq_name=vm_fq_name) print "[SUCCESS] VM: %s Has Exsited!" % vm_name except vnc_api.NoIdError: print "[WARN] VM: %s Does't Exsit! Creating ... " % vm_name vm_instance = vnc_api.VirtualMachine(vm_name, fq_name=vm_fq_name) vnc.virtual_machine_create(vm_instance) finally: vm_instance = vnc.virtual_machine_read(fq_name=vm_fq_name) print "[INFO] VM: %s fq_name: %s" % (vm_name, vm_instance.fq_name) # find or create the network print "=========>>> CREATE NETWORK ... ..." #vnet_fq_name = [u'default-domain', u'vCenter', u'vn3']) vnet_fq_name = proj_fq_name + [net_name] try: vnet = vnc.virtual_network_read(fq_name=vnet_fq_name) print "[SUCCESS] VNET: %s Has Exsited!" % net_name except vnc_api.NoIdError: print "[WARN] VNET: %s Doesn't Exist! Creating ..." % net_name vnet = vnc_api.irtualNetwork(net_name,