def destroy(self): netns_name = validate_uuid(self.args.vm_id) nic_left = {} if uuid.UUID(self.args.vmi_left_id).int: nic_left = {'uuid': validate_uuid(self.args.vmi_left_id)} nic_right = {} if uuid.UUID(self.args.vmi_right_id).int: nic_right = {'uuid': validate_uuid(self.args.vmi_right_id)} netns_mgr = NetnsManager(netns_name, nic_left, nic_right, root_helper=self.args.root_helper, cfg_file=self.args.cfg_file, gw_ip=None, pool_id=self.args.pool_id, loadbalancer_id=self.args.loadbalancer_id) netns_mgr.unplug_namespace_interface() if self.args.service_type == self.SOURCE_NAT: netns_mgr.destroy() elif self.args.service_type == self.LOAD_BALANCER: netns_mgr.release_lbaas('destroy') netns_mgr.destroy() else: msg = ('The %s service type is not supported' % self.args.service_type) raise NotImplementedError(msg)
def _create_nic_def(vmi_id, vmi_mac=None, vmi_ip=None): nic = {} if uuid.UUID(vmi_id).int: nic['uuid'] = validate_uuid(vmi_id) if vmi_mac: nic['mac'] = netaddr.EUI(vmi_mac, dialect=netaddr.mac_unix) else: nic['mac'] = None if vmi_ip: nic['ip'] = netaddr.IPNetwork(vmi_ip) else: nic['ip'] = None return nic
def _create_nic_def(vmi_id, vmi_mac=None, vmi_ip=None): nic = {} if uuid.UUID(vmi_id).int: nic['uuid'] = validate_uuid(vmi_id) if vmi_mac: nic['mac'] = netaddr.EUI(vmi_mac, dialect=netaddr.mac_unix) else: nic['mac'] = None if vmi_ip: nic['ip'] = netaddr.IPNetwork(vmi_ip) else: nic['ip'] = None return nic
def destroy(self): """Destroys a SNAT VM""" vm_id = validate_uuid(self.args.vm_id) nic_left = None if uuid.UUID(self.args.vmi_left_id): nic_left = VMPort(uuid=self.args.vmi_left_id, prefix=LEFT_DEV_PREFIX) nic_right = None if uuid.UUID(self.args.vmi_right_id): nic_right = VMPort(uuid=self.args.vmi_right_id, prefix=RIGHT_DEV_PREFIX) snat_vm = SNATVirtualMachine(vm_id, nic_left, nic_right, vhd_path=self.args.vhd_path) try: snat_vm.unregister() finally: snat_vm.cleanup()
def __init__(self, uuid, prefix, mac=None, ip_cidr=None, gw_cidr=None): self.uuid = validate_uuid(uuid) if mac: self.mac = netaddr.EUI(mac, dialect=netaddr.mac_eui48) if ip_cidr: self.ip = netaddr.IPNetwork(ip_cidr) if gw_cidr: self.gw_cidr = gw_cidr if prefix == LEFT_DEV_PREFIX: dev_suffix = "1" # as seen from inside the SNAT VM win_prefix = "int" # as seen from the host elif prefix == RIGHT_DEV_PREFIX: dev_suffix = "2" win_prefix = "gw" else: raise RuntimeError("prefix must be {} or {}" .format(LEFT_DEV_PREFIX, RIGHT_DEV_PREFIX)) self.name = self.DEV_PREFIX + dev_suffix self.win_name = "{}-{}".format(win_prefix, self.uuid)[:self.NAME_LEN]
def __init__(self, uuid, prefix, mac=None, ip_cidr=None, gw_cidr=None): self.uuid = validate_uuid(uuid) if mac: self.mac = netaddr.EUI(mac, dialect=netaddr.mac_eui48) if ip_cidr: self.ip = netaddr.IPNetwork(ip_cidr) if gw_cidr: self.gw_cidr = gw_cidr if prefix == LEFT_DEV_PREFIX: dev_suffix = "1" # as seen from inside the SNAT VM win_prefix = "int" # as seen from the host elif prefix == RIGHT_DEV_PREFIX: dev_suffix = "2" win_prefix = "gw" else: raise RuntimeError("prefix must be {} or {}".format( LEFT_DEV_PREFIX, RIGHT_DEV_PREFIX)) self.name = self.DEV_PREFIX + dev_suffix self.win_name = "{}-{}".format(win_prefix, self.uuid)[:self.NAME_LEN]
def create(self): """Creates and configures a SNAT VM""" vm_id = validate_uuid(self.args.vm_id) nic_left = None if uuid.UUID(self.args.vmi_left_id): nic_left = VMPort(uuid=self.args.vmi_left_id, prefix=LEFT_DEV_PREFIX, mac=self.args.vmi_left_mac, ip_cidr=self.args.vmi_left_ip, gw_cidr=self.args.left_gw_cidr) nic_right = None if uuid.UUID(self.args.vmi_right_id): nic_right = VMPort(uuid=self.args.vmi_right_id, prefix=RIGHT_DEV_PREFIX, mac=self.args.vmi_right_mac, ip_cidr=self.args.vmi_right_ip, gw_cidr=self.args.right_gw_cidr) # TODO: Remove `forwarding_mac` when Agent is functional snat_vm = SNATVirtualMachine(vm_id, nic_left, nic_right, forwarding_mac=self.args.forwarding_mac, vm_location=self.args.vm_location, vhd_path=self.args.vhd_path) snat_vm.create(self.args.mgmt_vswitch_name) try: snat_vm.attach_vrouter(self.args.vrouter_vswitch_name) snat_vm.spawn() ipam = MgmtIPAM(self.args.mgmt_vswitch_name) mgmt_ip = ipam.generate() snat_vm.inject_ip(mgmt_ip, self.args.mgmt_vswitch_name) snat_vm.set_snat(mgmt_ip) snat_vm.register() except: snat_vm.cleanup() raise
def create(self): """Creates and configures a SNAT VM""" vm_id = validate_uuid(self.args.vm_id) nic_left = None if uuid.UUID(self.args.vmi_left_id): nic_left = VMPort(uuid=self.args.vmi_left_id, prefix=LEFT_DEV_PREFIX, mac=self.args.vmi_left_mac, ip_cidr=self.args.vmi_left_ip, gw_cidr=self.args.left_gw_cidr) nic_right = None if uuid.UUID(self.args.vmi_right_id): nic_right = VMPort(uuid=self.args.vmi_right_id, prefix=RIGHT_DEV_PREFIX, mac=self.args.vmi_right_mac, ip_cidr=self.args.vmi_right_ip, gw_cidr=self.args.right_gw_cidr) # TODO: Remove `forwarding_mac` when Agent is functional snat_vm = SNATVirtualMachine(vm_id, nic_left, nic_right, forwarding_mac=self.args.forwarding_mac, vm_location=self.args.vm_location, vhd_path=self.args.vhd_path) snat_vm.create(self.args.mgmt_vswitch_name) try: snat_vm.attach_vrouter(self.args.vrouter_vswitch_name) snat_vm.spawn() ipam = MgmtIPAM(self.args.mgmt_vswitch_name) mgmt_ip = ipam.generate() snat_vm.inject_ip(mgmt_ip, self.args.mgmt_vswitch_name) snat_vm.set_snat(mgmt_ip) snat_vm.register() except: snat_vm.cleanup() raise
def destroy(self): """Destroys a SNAT VM""" vm_id = validate_uuid(self.args.vm_id) nic_left = None if uuid.UUID(self.args.vmi_left_id): nic_left = VMPort(uuid=self.args.vmi_left_id, prefix=LEFT_DEV_PREFIX) nic_right = None if uuid.UUID(self.args.vmi_right_id): nic_right = VMPort(uuid=self.args.vmi_right_id, prefix=RIGHT_DEV_PREFIX) snat_vm = SNATVirtualMachine(vm_id, nic_left, nic_right, vhd_path=self.args.vhd_path) try: snat_vm.unregister() finally: snat_vm.cleanup()
def create(self): netns_name = validate_uuid(self.args.vm_id) nic_left = {} if uuid.UUID(self.args.vmi_left_id).int: nic_left['uuid'] = validate_uuid(self.args.vmi_left_id) if self.args.vmi_left_mac: nic_left['mac'] = netaddr.EUI(self.args.vmi_left_mac, dialect=netaddr.mac_unix) else: nic_left['mac'] = None if self.args.vmi_left_ip: nic_left['ip'] = netaddr.IPNetwork(self.args.vmi_left_ip) else: nic_left['ip'] = None nic_right = {} if uuid.UUID(self.args.vmi_right_id).int: nic_right['uuid'] = validate_uuid(self.args.vmi_right_id) if self.args.vmi_right_mac: nic_right['mac'] = netaddr.EUI(self.args.vmi_right_mac, dialect=netaddr.mac_unix) else: nic_right['mac'] = None if self.args.vmi_right_ip: nic_right['ip'] = netaddr.IPNetwork(self.args.vmi_right_ip) else: nic_right['ip'] = None netns_mgr = NetnsManager(netns_name, nic_left, nic_right, root_helper=self.args.root_helper, cfg_file=self.args.cfg_file, update=self.args.update, gw_ip=self.args.gw_ip, pool_id=self.args.pool_id, loadbalancer_id=self.args.loadbalancer_id) if (self.args.update is False): if netns_mgr.is_netns_already_exists(): # If the netns already exists, destroy it to be sure to set it # with new parameters like another external network if self.args.service_type == self.LOAD_BALANCER: netns_mgr.release_lbaas('create') netns_mgr.unplug_namespace_interface() netns_mgr.destroy() netns_mgr.create() if self.args.service_type == self.SOURCE_NAT: netns_mgr.set_snat() elif self.args.service_type == self.LOAD_BALANCER: if (netns_mgr.set_lbaas() == False): netns_mgr.destroy() msg = 'Falied to Launch LOADBALANCER' raise Exception(msg) else: msg = ('The %s service type is not supported' % self.args.service_type) raise NotImplementedError(msg) netns_mgr.plug_namespace_interface()
def destroy(self): vm_name = validate_uuid(self.args.vm_id) self._stop(vm_name, self.args.vmi_left_id, self.args.vmi_right_id, self.args.vmi_management_id) self._client.remove_container(vm_name)
def create(self): vm_name = validate_uuid(self.args.vm_id) image_name = self.args.image if self.args.instance_data: instance_data = json.loads(self.args.instance_data) else: instance_data = {} try: self._client.inspect_image(image_name) except APIError as e: if e.response.status_code == 404: self._client.pull(image_name) self._client.inspect_image(image_name) else: raise if self.args.command is not None: command = self.args.command elif "command" in instance_data: command = instance_data["command"] else: # use container default command = None try: container = self._client.inspect_container(vm_name) docker_id = container["Id"] except APIError as e: if e.response.status_code == 404: result = self._client.create_container( image=image_name, name=vm_name, command=command, detach=True, stdin_open=True, tty=True) # keep the container running docker_id = result["Id"] else: logging.error('Error when inspecting docker ' 'container: %s, status code %d, error: %s', vm_name, e.response.status_code, str(e)) raise self._stop(vm_name, self.args.vmi_left_id, self.args.vmi_right_id, self.args.vmi_management_id) if self.args.vmi_left_id is not None: nic_left = self._create_nic_def(self.args.vmi_left_id, self.args.vmi_left_mac, self.args.vmi_left_ip) else: nic_left = None if self.args.vmi_right_id is not None: nic_right = self._create_nic_def(self.args.vmi_right_id, self.args.vmi_right_mac, self.args.vmi_right_ip) else: nic_right = None if self.args.vmi_management_id is not None: nic_management = self._create_nic_def(self.args.vmi_management_id, self.args.vmi_management_mac, self.args.vmi_management_ip) nic_management['name'] = ("mng-" + nic_management['uuid'] )[:NetnsManager.DEV_NAME_LEN] nic_management = [nic_management] else: nic_management = [] self._client.start(docker_id, network_mode='none') docker_pid = self._client.inspect_container(docker_id)['State']['Pid'] netns_mgr = NetnsManager(vm_name, nic_left, nic_right, other_nics=nic_management, namespace_name=str(docker_pid)) if not os.path.exists("/var/run/netns/"): os.makedirs("/var/run/netns/") if netns_mgr.is_netns_already_exists(): # If the netns already exists, destroy it to be sure to set it # with new parameters like another external network netns_mgr.unplug_namespace_interface() netns_mgr.destroy() # connect docker network stack as new netns on which we will work os.symlink("/proc/%s/ns/net" % docker_pid, "/var/run/netns/%s" % docker_pid) netns_mgr.create() netns_mgr.plug_namespace_interface()
def destroy(self): vm_name = validate_uuid(self.args.vm_id) self._stop(vm_name, self.args.vmi_left_id, self.args.vmi_right_id, self.args.vmi_management_id) self._client.remove_container(vm_name)
def create(self): vm_name = validate_uuid(self.args.vm_id) image_name = self.args.image if self.args.instance_data: instance_data = json.loads(self.args.instance_data) else: instance_data = {} try: self._client.inspect_image(image_name) except APIError as e: if e.response.status_code == 404: self._client.pull(image_name) self._client.inspect_image(image_name) else: raise if self.args.command is not None: command = self.args.command elif "command" in instance_data: command = instance_data["command"] else: # use container default command = None try: container = self._client.inspect_container(vm_name) docker_id = container["Id"] except APIError as e: if e.response.status_code == 404: result = self._client.create_container( image=image_name, name=vm_name, command=command, detach=True, stdin_open=True, tty=True) # keep the container running docker_id = result["Id"] else: logging.error( 'Error when inspecting docker ' 'container: %s, status code %d, error: %s', vm_name, e.response.status_code, str(e)) raise self._stop(vm_name, self.args.vmi_left_id, self.args.vmi_right_id, self.args.vmi_management_id) if self.args.vmi_left_id is not None: nic_left = self._create_nic_def(self.args.vmi_left_id, self.args.vmi_left_mac, self.args.vmi_left_ip) else: nic_left = None if self.args.vmi_right_id is not None: nic_right = self._create_nic_def(self.args.vmi_right_id, self.args.vmi_right_mac, self.args.vmi_right_ip) else: nic_right = None if self.args.vmi_management_id is not None: nic_management = self._create_nic_def(self.args.vmi_management_id, self.args.vmi_management_mac, self.args.vmi_management_ip) nic_management['name'] = ( "mng-" + nic_management['uuid'])[:NetnsManager.DEV_NAME_LEN] nic_management = [nic_management] else: nic_management = [] self._client.start(docker_id, network_mode='none') docker_pid = self._client.inspect_container(docker_id)['State']['Pid'] netns_mgr = NetnsManager(vm_name, nic_left, nic_right, other_nics=nic_management, namespace_name=str(docker_pid)) if not os.path.exists("/var/run/netns/"): os.makedirs("/var/run/netns/") if netns_mgr.is_netns_already_exists(): # If the netns already exists, destroy it to be sure to set it # with new parameters like another external network netns_mgr.unplug_namespace_interface() netns_mgr.destroy() # connect docker network stack as new netns on which we will work os.symlink("/proc/%s/ns/net" % docker_pid, "/var/run/netns/%s" % docker_pid) netns_mgr.create() netns_mgr.plug_namespace_interface()