def __init__(self, daemon): """ Set internal fields for the IPsec process. """ super().__init__() self.dry_run = daemon.dry_run self.logger = daemon.logger self.use_ipsec = daemon.use_ipsec if not self.use_ipsec: return self.ipsec_manage = daemon.ipsec_manage self.ipsec_psk = daemon.ipsec_psk self.template_dir = daemon.template_dir self.ipsec_conf = daemon.ipsec_conf self.ipsec_secrets = daemon.ipsec_secrets self.ipsec_conf_template = util.template_read(self.template_dir, "ipsec.conf") self.ipsec_secrets_template = util.template_read(self.template_dir, "ipsec.secrets") # Create a dictionary which maps the name of the IPsec # connection to a tuple containing the local and remote addresses. self.mesh_conns = dict(zip(["%s-%s" % (m[0], m[1]) for m in daemon.mesh_links], daemon.mesh_links)) self.ipsec = util.command_path("ipsec") if not self.dry_run else util.command_path("true")
def __init__(self, daemon, overlay): ''' Set internal fields for the BGP process. ''' super().__init__() self.dry_run = daemon.dry_run self.log_level = daemon.log_level self.template_dir = daemon.template_dir self.logger = overlay.logger self.name = overlay.name self.netns = overlay.netns self.description = "BGP process for overlay '%s'" % self.name self.asn = overlay.asn self.linknet_pool = overlay.linknet_pool self.mesh_tunnels = tuple(overlay.mesh_tunnels) self.interfaces = tuple(overlay.interfaces) self.bird_ctl_dir = os.path.join(overlay.root_dir, "run", "bird") self.bird_conf_dir = os.path.join(overlay.root_dir, "etc", "bird") self.bird_log_dir = os.path.join(overlay.root_dir, "var", "log", "bird") self.bird_pid_dir = os.path.join(overlay.root_dir, "run", "bird") self.bird_ctl = os.path.join(self.bird_ctl_dir, "bird.ctl") self.bird_conf = os.path.join(self.bird_conf_dir, "bird.conf") self.bird_log = os.path.join(self.bird_log_dir, "bird.log") self.bird_pid = os.path.join(self.bird_pid_dir, "bird.pid") self.bird6_ctl = os.path.join(self.bird_ctl_dir, "bird6.ctl") self.bird6_conf = os.path.join(self.bird_conf_dir, "bird6.conf") self.bird6_log = os.path.join(self.bird_log_dir, "bird6.log") self.bird6_pid = os.path.join(self.bird_pid_dir, "bird6.pid") self.bird_conf_template = util.template_read(self.template_dir, "bird.conf") self.bird = util.command_path("bird") if not self.dry_run else "/usr/sbin/bird" self.bird6 = util.command_path("bird6") if not self.dry_run else "/usr/sbin/bird6"
def Popen(self, *args, **kwargs): ''' Start a process in this network namespace using the Popen interface. ''' if not self.is_started(): raise NotYetStartedError(self) if self.dry_run: # Create a dummy NSPopen object with a # stub release() method, to be API compatible # with the real deal. class NSPopen(subprocess.Popen): def release(self): pass return NSPopen( [util.command_path("true")], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) else: return pyroute2.netns.process.proxy.NSPopen(self.name, *args, **kwargs)
def test_l3overlayd(self): ''' Do a dry run of the l3overlay daemon with overlay configurations designed to test each static interface type. ''' test_py = os.path.join(self.tmp_dir, "test.py") with open(test_py, "w") as f: f.write(''' import importlib.machinery l3overlay = importlib.machinery.SourceFileLoader("l3overlay", "%s/l3overlay/__init__.py").load_module() l3overlay.main()''' % SRC_DIR) command = [util.command_path("python3"), test_py] for key, value in self.global_conf.items(): akey = key.replace("_", "-") arg = "--%s" % akey if value is None: continue elif isinstance(value, list) or isinstance(value, tuple): for v in value: command.extend([arg, v]) elif key.startswith("no_"): if value == False or (isinstance(value, str) and value.lower() == "false"): command.append("--no-%s" % akey) else: if value == True or (isinstance(value, str) and value.lower() == "true"): command.append(arg) else: command.extend([arg, value]) with subprocess.Popen( command, stdout = subprocess.PIPE, stderr = subprocess.PIPE, ) as process: try: stdout_data, stderr_data = process.communicate(timeout=DAEMON_WAIT_TIMEOUT) raise ExecutionError( "l3overlayd terminated unexpectedly", command, process.returncode, stdout_data.decode("UTF-8"), stderr_data.decode("UTF-8"), ) except subprocess.TimeoutExpired: pass time.sleep(DAEMON_WAIT_TIMEOUT) process.terminate() try: process.communicate(timeout=DAEMON_TERMINATE_TIMEOUT) except subprocess.TimeoutExpired as e: process.kill() stdout_data, stderr_data = process.communicate() raise ExecutionError( "l3overlayd did not terminate when expected", command, process.returncode, stdout_data.decode("UTF-8"), stderr_data.decode("UTF-8"), )