def do_create_port(self, account, port): """Create a port between a network and a virtual machine Arguments: account - a cloud account port - a description of port to create Raises: Raises an RWErrorNotFound exception if either the network or the VM associated with the port cannot be found. Returns: the ID of the newly created port. """ if port.network_id not in self.cal_to_lxc["network"]: msg = 'Unable to find the specified network ({})' raise exceptions.RWErrorNotFound(msg.format(port.network_id)) if port.vm_id not in self.cal_to_lxc["vm"]: msg = "Unable to find the specified VM ({})" raise exceptions.RWErrorNotFound(msg.format(port.vm_id)) if port.has_field("ip_address"): raise exceptions.RWErrorFailure( "IP address of the port must not be specific") network = self.cal.get_network(port.network_id) ip_pool = self.cal.get_network_ip_pool(port.network_id) port.ip_address = ip_pool.allocate_ip() net_config = lxc.NetworkConfig( type='veth', link=network.network_name[:15], name="veth" + str(uuid.uuid4())[:10], ipv4=port.ip_address, ) lxc_network_id = self.cal_to_lxc["network"][port.network_id] lxc_vm_id = self.cal_to_lxc["vm"][port.vm_id] cal_port_id = self.cal.add_port(port.network_id, port.vm_id, port) lxc_port_id = self.lxc.add_port(lxc_network_id, lxc_vm_id, net_config) self.cal_to_lxc["port"][cal_port_id] = lxc_port_id port.port_id = cal_port_id return port.port_id
def do_delete_port(self, account, port_id): """Delete the specified port Arguments: account - a cloud account port_id - the ID of the port to delete Raises: A RWErrorNotFound exception is raised if the specified port cannot be found. """ if port_id not in self.cal_to_lxc["port"]: msg = "Unable to find the specified port ({})" raise exceptions.RWErrorNotFound(msg.format(port_id)) lxc_port_id = self.cal_to_lxc["port"][port_id] # Release the port's ip address back into the network pool port = self.cal.get_port(port_id) ip_pool = self.cal.get_network_ip_pool(port.network_id) ip_pool.deallocate_ip(port.ip_address) self.cal.remove_port(port_id) self.lxc.remove_port(lxc_port_id) del self.cal_to_lxc["port"][port_id]
def do_delete_network(self, account, network_id): """ Arguments: account - a cloud account network_id - the UUID of the network to delete Raises: An RWErrorNotFound is raised if the specified network cannot be found. """ if network_id not in self.cal_to_lxc["network"]: msg = "Unable to find the specified network ({})" raise exceptions.RWErrorNotFound(msg.format(network_id)) # Get the associated bridge ID bridge_id = self.cal_to_lxc["network"][network_id] # Delete the network network = self.cal.get_network(network_id) net.delete(network.network_name) # Remove the network records self.lxc.remove_bridge(bridge_id) self.cal.remove_network(network_id) del self.cal_to_lxc["network"][network_id]
def do_start_vm(self, account, vm_id): """Starts the specified VM Arguments: vm_id - the id of the vm to start Raises: An RWErrorNotFound is raised if the specified vm id is not known to this driver. """ if vm_id not in self.cal_to_lxc["vm"]: msg = "Unable to find the specified VM ({})" raise exceptions.RWErrorNotFound(msg.format(vm_id)) container_id = self.cal_to_lxc["vm"][vm_id] snapshot = self.lxc.get_container(container_id) port_ids = self.lxc.get_container_ports(container_id) config = lxc.ContainerConfig(snapshot.name) for port_id in port_ids: port = self.lxc.get_port(port_id) config.add_network_config(port) vm = self.cal.get_vm(vm_id) # Set the management IP on the vm if not yet set if not vm.has_field("management_ip"): mgmt_ip = self.lxc.acquire_mgmt_ip() vm.management_ip = mgmt_ip # Add the management interface config.add_network_config( lxc.NetworkConfig( type="veth", link=self.lxc.mgmt_network.network_name, name="eth0", ipv4=vm.management_ip, ipv4_gateway='auto', )) # Add rift root as a mount point config.add_mount_point_config( lxc.MountConfig( local=os.environ["RIFT_ROOT"], remote=os.environ["RIFT_ROOT"][1:], read_only=False, )) userdata = None if vm.cloud_init.has_field("userdata"): userdata = vm.cloud_init.userdata snapshot.configure(config, userdata=userdata) snapshot.start()
def add_port(self, bridge_id, container_id, port): if bridge_id not in self._bridges: msg = "Unable to find bridge {}" raise exceptions.RWErrorNotFound(msg.format(bridge_id)) if container_id not in self._containers: msg = "Unable to find container {}" raise exceptions.RWErrorNotFound(msg.format(container_id)) port_id = str(uuid.uuid4()) self._ports[port_id] = port self._container_to_ports[container_id].append(port_id) self._bridge_to_ports[bridge_id].append(port_id) self._port_to_container[port_id] = container_id self._port_to_bridge[port_id] = bridge_id return port_id
def add_port(self, network_id, vm_id, port): if network_id not in self._networks: msg = "Unable to find network {}" raise exceptions.RWErrorNotFound(msg.format(network_id)) if vm_id not in self._vms: msg = "Unable to find vm {}" raise exceptions.RWErrorNotFound(msg.format(vm_id)) port_id = str(uuid.uuid4()) self._ports[port_id] = port self._vm_to_ports[vm_id].append(port_id) self._network_to_ports[network_id].append(port_id) self._port_to_vm[port_id] = vm_id self._port_to_network[port_id] = network_id return port_id
def add_vm(self, image_id, vm): if image_id not in self._images: msg = "Unable to find image {}" raise exceptions.RWErrorNotFound(msg.format(image_id)) vm_id = str(next(self._vm_id_gen)) self._vms[vm_id] = vm self._vm_to_image[vm_id] = image_id self._image_to_vms[image_id].append(vm_id) return vm_id
def remove_port(self, port_id): if port_id not in self._ports: msg = "Unable to find port {}" raise exceptions.RWErrorNotFound(msg.format(port_id)) bridge_id = self._port_to_bridge[port_id] container_id = self._port_to_container[port_id] self._container_to_ports[container_id].remove(port_id) self._bridge_to_ports[bridge_id].remove(port_id) del self._ports[port_id] del self._port_to_bridge[port_id] del self._port_to_container[port_id]
def remove_port(self, port_id): if port_id not in self._ports: msg = "Unable to find port {}" raise exceptions.RWErrorNotFound(msg.format(port_id)) network_id = self._port_to_network[port_id] vm_id = self._port_to_vm[port_id] self._vm_to_ports[vm_id].remove(port_id) self._network_to_ports[network_id].remove(port_id) del self._ports[port_id] del self._port_to_vm[port_id] del self._port_to_network[port_id]
def do_get_vm(self, account, vm_id): """Returns the specified VM Arguments: vm_id - the id of the vm to return Raises: An RWErrorNotFound is raised if the specified vm id is not known to this driver. Returns: a VMInfoItem object """ if vm_id not in self.cal_to_lxc["vm"]: msg = "Unable to find the specified VM ({})" raise exceptions.RWErrorNotFound(msg.format(vm_id)) return self.cal.get_vm(vm_id)
def do_stop_vm(self, account, vm_id): """Stops the specified VM Arguments: vm_id - the id of the vm to stop Raises: An RWErrorNotFound is raised if the specified vm id is not known to this driver. """ if vm_id not in self.cal_to_lxc["vm"]: msg = "Unable to find the specified VM ({})" raise exceptions.RWErrorNotFound(msg.format(vm_id)) # Stop the container container_id = self.cal_to_lxc["vm"][vm_id] snapshot = self.lxc.get_container(container_id) snapshot.stop()
def do_get_port(self, account, port_id): """Return the specified port Arguments: account - a cloud account port_id - the ID of the port to return Raises: A RWErrorNotFound exception is raised if the specified port cannot be found. Returns: The specified port. """ if port_id not in self.cal_to_lxc["port"]: msg = "Unable to find the specified port ({})" raise exceptions.RWErrorNotFound(msg.format(port_id)) return self.cal.get_port(port_id)
def get_image_vms(self, image_id): if image_id not in self._images: msg = "Unable to find image {}" raise exceptions.RWErrorNotFound(msg.format(image_id)) return self._image_to_vms[image_id]