def attach_to_container(self, container_pid, is_route=False, gateway=None): self._is_attach_to_container = True self._attach_to_container_pid = container_pid try: ip('link', 'set', 'netns', self._attach_to_container_pid, self._veth_name) nsenter('-t', self._attach_to_container_pid, '-n', 'ip', 'link', 'set', self._veth_name, 'up') if '0.0.0.0' not in self._ip_netmask: nsenter('-t', self._attach_to_container_pid, '-n', 'ip', 'addr', 'add', self._ip_netmask, 'dev', self._veth_name) except Exception as e: raise VEthAttachException("attach macvlan eth error:\n{}".format(e.message)) if is_route: if not gateway: raise ValueError("Please set the gateway for %s" % self._veth_name) else: nsenter('-t', self._attach_to_container_pid, '-n', 'ip', 'route', 'del', 'default') nsenter('-t', self._attach_to_container_pid, '-n', 'ip', 'route', 'add', 'default', 'via', gateway, 'dev', self._veth_name) #arping my gateway, cause the gateway to flush the ARP cache for my IP address try: nsenter('-t', self._attach_to_container_pid, '-n', 'ping', '-c', '1', gateway) except Exception as e: logger.warning(e.message) return self
def delete(self): try: if self._is_attach_to_container: nsenter('-t', self._attach_to_container_pid, '-n', 'ip', 'link', 'del', self._veth_name) else: ip('link', 'del', self._veth_name) except Exception as e: raise VEthDeleteException("delete macvlan eth error:\n{}".format(e.message))
def delete(self): try: if self._is_attach_to_container: nsenter('-t', self._attach_to_container_pid, '-n', 'ip', 'link', 'del', self._veth_name) else: ip('link', 'del', self._veth_name) except Exception as e: raise VEthDeleteException("delete macvlan eth error:\n{}".format( e.message))
def down(self): try: if self._is_attach_to_container: nsenter('-t', self._attach_to_container_pid, '-n', 'ip', 'link', self._veth_name, 'down') else: ip('link', 'set', self._veth_name, 'down') except Exception as e: raise VEthDownException("down macvlan eth error:\n{}".format(e.message)) return self
def down(self): try: if self._is_attach_to_container: nsenter('-t', self._attach_to_container_pid, '-n', 'ip', 'link', self._veth_name, 'down') else: ip('link', 'set', self._veth_name, 'down') except Exception as e: raise VEthDownException("down macvlan eth error:\n{}".format( e.message)) return self
def attach_to_container(self, container_pid, is_route=False, gateway=None): self._is_attach_to_container = True self._attach_to_container_pid = container_pid try: ip('link', 'set', 'netns', self._attach_to_container_pid, self._veth_name) nsenter('-t', self._attach_to_container_pid, '-n', 'ip', 'link', 'set', self._veth_name, 'up') if '0.0.0.0' not in self._ip_netmask: nsenter('-t', self._attach_to_container_pid, '-n', 'ip', 'addr', 'add', self._ip_netmask, 'dev', self._veth_name) except Exception as e: raise VEthAttachException("attach macvlan eth error:\n{}".format( e.message)) if is_route: if not gateway: raise ValueError("Please set the gateway for %s" % self._veth_name) else: nsenter('-t', self._attach_to_container_pid, '-n', 'ip', 'route', 'del', 'default') nsenter('-t', self._attach_to_container_pid, '-n', 'ip', 'route', 'add', 'default', 'via', gateway, 'dev', self._veth_name) #arping my gateway, cause the gateway to flush the ARP cache for my IP address try: nsenter('-t', self._attach_to_container_pid, '-n', 'ping', '-c', '1', gateway) except Exception as e: logger.warning(e.message) return self