def kill(self): ''' Kill the process the controller is running in ''' if self.state != ControllerState.ALIVE: self.log.warn("Killing controller %s when it is not alive!" % self.label) return msg.event("Killing controller %s" % self.cid) kill_procs([self.process]) if self.config.kill_cmd != "": self.log.info("Killing controller %s: %s" % (self.label, " ".join(self.config.expanded_kill_cmd))) popen_filtered("[%s]" % self.label, self.config.expanded_kill_cmd, self.config.cwd) self._unregister_proc(self.process) self.process = None self.state = ControllerState.DEAD
def start(self, multiplex_sockets=False): """ Start a new controller process based on the config's start_cmd attribute. Registers the Popen member variable for deletion upon a SIG* received in the simulator process """ self.log.info(self.welcome_msg) if self.state != ControllerState.DEAD: self.log.warn("Starting controller %s when it is not dead!" % self.label) return if self.config.start_cmd == "": raise RuntimeError( "No command found to start controller %s!" % self.label) self.log.info( "Launching controller %s: %s" % ( self.label, " ".join(self.config.expanded_start_cmd))) # These configurations are specific to controllers launched in namespaces # Probably it should be factored somewhere else launch_in_network_namespace = getattr(self.config, 'launch_in_network_namespace', None) if launch_in_network_namespace: unclaimed_address = IPAddressSpace.find_unclaimed_address( ip_prefix=self.config.address) (self.process, self.guest_eth_addr, self.host_device) = \ launch_namespace(" ".join(self.config.expanded_start_cmd), self.config.address, self.cid, host_ip_addr_str=unclaimed_address) else: self.process = popen_filtered("[%s]" % self.label, self.config.expanded_start_cmd, self.config.cwd) self._register_proc(self.process) self._check_snapshot_connect() self.state = ControllerState.ALIVE
def start(self): '''Start a new controller process based on the config's cmdline attribute. Registers the Popen member variable for deletion upon a SIG* received in the simulator process.''' msg.event("Starting controller %s" % (str(self.cid))) env = None if self.config.sync: # if a sync connection has been configured in the controller conf # launch the controller with environment variable 'sts_sync' set # to the appropriate listening port. This is quite a hack. env = os.environ.copy() port_match = re.search(r':(\d+)$', self.config.sync) if port_match is None: raise ValueError("sync: cannot find port in %s" % self.config.sync) port = port_match.group(1) env['sts_sync'] = "ptcp:0.0.0.0:%d" % (int(port),) if self.config.name == "pox": src_dir = os.path.join(os.path.dirname(__file__), "..") pox_ext_dir = os.path.join(self.config.cwd, "ext") if os.path.exists(pox_ext_dir): for f in ("sts/util/io_master.py", "sts/syncproto/base.py", "sts/syncproto/pox_syncer.py", "sts/__init__.py", "sts/util/socket_mux/__init__.py", "sts/util/socket_mux/pox_monkeypatcher.py", "sts/util/socket_mux/base.py", "sts/util/socket_mux/server_socket_multiplexer.py"): src_path = os.path.join(src_dir, f) if not os.path.exists(src_path): raise ValueError("Integrity violation: sts sync source path %s (abs: %s) does not exist" % (src_path, os.path.abspath(src_path))) dst_path = os.path.join(pox_ext_dir, f) dst_dir = os.path.dirname(dst_path) init_py = os.path.join(dst_dir, "__init__.py") if not os.path.exists(dst_dir): os.makedirs(dst_dir) if not os.path.exists(init_py): open(init_py, "a").close() if os.path.islink(dst_path): # remove symlink and recreate os.remove(dst_path) if not os.path.exists(dst_path): rel_link = os.path.abspath(src_path) self.log.debug("creating symlink %s -> %s", rel_link, dst_path) os.symlink(rel_link, dst_path) else: self.log.warn("Could not find pox ext dir in %s. Cannot check/link in sync module" % pox_ext_dir) self.log.info("Launching controller %s: %s" % (self.label, " ".join(self.config.expanded_cmdline))) self.process = popen_filtered("[%s]"%self.label, self.config.expanded_cmdline, self.config.cwd, env=env) self._register_proc(self.process) if self.config.sync: self.sync_connection = self.sync_connection_manager.connect(self, self.config.sync) self.alive = True
def start(self, multiplex_sockets=False): """ Start a new controller process based on the config's start_cmd attribute. Registers the Popen member variable for deletion upon a SIG* received in the simulator process """ self.log.info(self.welcome_msg) if self.state != ControllerState.DEAD: self.log.warn("Starting controller %s when it is not dead!" % self.label) return if self.config.start_cmd == "": raise RuntimeError("No command found to start controller %s!" % self.label) self.log.info("Launching controller %s: %s" % (self.label, " ".join(self.config.expanded_start_cmd))) # These configurations are specific to controllers launched in namespaces # Probably it should be factored somewhere else launch_in_network_namespace = getattr(self.config, 'launch_in_network_namespace', None) if launch_in_network_namespace: unclaimed_address = IPAddressSpace.find_unclaimed_address( ip_prefix=self.config.address) (self.process, self.guest_eth_addr, self.host_device) = \ launch_namespace(" ".join(self.config.expanded_start_cmd), self.config.address, self.cid, host_ip_addr_str=unclaimed_address) else: self.process = popen_filtered("[%s]" % self.label, self.config.expanded_start_cmd, self.config.cwd) self._register_proc(self.process) self._check_snapshot_connect() self.state = ControllerState.ALIVE
def execute_local_command(self, cmd): process = popen_filtered("[%s]" % self.label, cmd, self.config.cwd, shell=True) output = "" while True: output += process.stdout.read(100) # arbitrary if output == '' and process.poll is not None: break return output
def kill(self): """ Kill the process the controller is running in """ if self.state != ControllerState.ALIVE: self.log.warn("Killing controller %s when it is not alive!" % self.label) return msg.event("Killing controller %s (pid %d)" % (self.cid, self.pid)) kill_procs([self.process]) if self.config.kill_cmd not in ["", None]: self.log.info( "Killing controller %s: %s" % (self.label, " ".join(self.config.expanded_kill_cmd))) popen_filtered("[%s]" % self.label, self.config.expanded_kill_cmd, self.config.cwd) self._unregister_proc(self.process) self.process = None self.state = ControllerState.DEAD
def restart(self): if self.state != ControllerState.DEAD: self.log.warn("Restarting controller %s when controller is not dead!" % self.label) return if self.config.restart_cmd == "": raise RuntimeError("No command found to restart controller %s!" % self.label) self.log.info("Relaunching controller %s: %s" % (self.label, " ".join(self.config.expanded_restart_cmd))) p = popen_filtered("[%s]" % self.label, ' ',join(self.config.expanded_restart_cmd), self.config.cwd, shell=True) p.wait() self.state = ControllerState.STARTING
def kill(self): if self.state != ControllerState.ALIVE: self.log.warn("Killing controller %s when controller is not alive!" % self.label) return if self.config.kill_cmd == "": raise RuntimeError("No command found to kill controller %s!" % self.label) self.log.info("Killing controller %s: %s" % (self.label, " ".join(self.config.expanded_kill_cmd))) p = popen_filtered("[%s]" % self.label, ' '.join(self.config.expanded_kill_cmd), self.config.cwd, shell=True) p.wait() self.state = ControllerState.DEAD
def execute_command(self, cmd): """ Execute command locally and return the stdout results """ process = popen_filtered("[%s]" % self.label, cmd, self.cwd, shell=True, redirect_output=self.redirect_output) output = "" while True: recv = process.stdout.read(100) # arbitrary output += recv if recv == '' and process.poll() is not None: break if self.redirect_output: return '' return output
def start(self): ''' Start a new controller process based on the config's start_cmd attribute. Registers the Popen member variable for deletion upon a SIG* received in the simulator process ''' if self.state != ControllerState.DEAD: self.log.warn("Starting controller %s when it is not dead!" % self.label) return if self.config.start_cmd == "": raise RuntimeError("No command found to start controller %s!" % self.label) self.log.info("Launching controller %s: %s" % (self.label, " ".join(self.config.expanded_start_cmd))) if self.config.launch_in_network_namespace: (self.process, self.guest_eth_addr, self.host_device) = \ launch_namespace(" ".join(self.config.expanded_start_cmd), self.config.address, self.cid, host_ip_addr_str=IPAddressSpace.find_unclaimed_address(ip_prefix=self.config.address)) else: self.process = popen_filtered("[%s]" % self.label, self.config.expanded_start_cmd, self.config.cwd) self._register_proc(self.process) self.state = ControllerState.ALIVE
def start(self, multiplex_sockets=False): """ Start a new POX controller process based on the config's start_cmd attribute. Registers the Popen member variable for deletion upon a SIG* received in the simulator process """ self.log.info(self.welcome_msg) if self.state != ControllerState.DEAD: self.log.warn( "Starting controller %s when controller is not dead!" % self.label) return msg.event("Starting POX controller %s" % (str(self.cid))) env = None if self.config.sync: # If a sync connection has been configured in the controller conf # launch the controller with environment variable 'sts_sync' set # to the appropriate listening port. This is quite a hack. env = os.environ.copy() port_match = re.search(r':(\d+)$', self.config.sync) if port_match is None: raise ValueError("sync: cannot find port in %s" % self.config.sync) port = port_match.group(1) env['sts_sync'] = "ptcp:0.0.0.0:%d" % (int(port),) if self.config.sync or multiplex_sockets: src_dir = os.path.join(os.path.dirname(__file__), "../../") pox_ext_dir = os.path.join(self.config.cwd, "ext") if os.path.exists(pox_ext_dir): for f in ("sts/util/io_master.py", "sts/syncproto/base.py", "sts/syncproto/pox_syncer.py", "sts/__init__.py", "sts/util/socket_mux/__init__.py", "sts/util/socket_mux/pox_monkeypatcher.py", "sts/util/socket_mux/base.py", "sts/util/socket_mux/server_socket_multiplexer.py"): src_path = os.path.join(src_dir, f) if not os.path.exists(src_path): raise ValueError( "Integrity violation: sts sync source path %s (abs: %s) " "does not exist" % (src_path, os.path.abspath(src_path))) dst_path = os.path.join(pox_ext_dir, f) dst_dir = os.path.dirname(dst_path) init_py = os.path.join(dst_dir, "__init__.py") if not os.path.exists(dst_dir): os.makedirs(dst_dir) if not os.path.exists(init_py): open(init_py, "a").close() if os.path.islink(dst_path): # Remove symlink and recreate os.remove(dst_path) if not os.path.exists(dst_path): rel_link = os.path.abspath(src_path) self.log.debug("Creating symlink %s -> %s", rel_link, dst_path) os.symlink(rel_link, dst_path) else: self.log.warn("Could not find pox ext dir in %s. " + "Cannot check/link in sync module" % pox_ext_dir) if self.config.start_cmd in ["", None]: raise RuntimeError( "No command found to start controller %s!" % self.label) start_cmd = getattr(self.config, "expanded_start_cmd", self.config.start_cmd) self.log.info( "Launching controller %s: %s" % (self.label, " ".join(start_cmd))) launch_in_network_namespace = getattr(self.config, "launch_in_network_namespace", False) if launch_in_network_namespace: (self.process, self.guest_eth_addr, self.host_device) = \ launch_namespace( " ".join(start_cmd), self.config.address, self.cid, host_ip_addr_str=IPAddressSpace.find_unclaimed_address( ip_prefix=self.config.address), cwd=self.config.cwd, env=env) else: self.process = popen_filtered("[%s]" % self.label, start_cmd, self.config.cwd, env) self._register_proc(self.process) if self.config.sync: self.sync_connection = self.sync_connection_manager.connect( self, self.config.sync) self._check_snapshot_connect() self.state = ControllerState.ALIVE
def start(self, multiplex_sockets=False): """ Start a new POX controller process based on the config's start_cmd attribute. Registers the Popen member variable for deletion upon a SIG* received in the simulator process """ self.log.info(self.welcome_msg) if self.state != ControllerState.DEAD: self.log.warn( "Starting controller %s when controller is not dead!" % self.label) return msg.event("Starting POX controller %s" % (str(self.cid))) env = None if self.config.sync: # If a sync connection has been configured in the controller conf # launch the controller with environment variable 'sts_sync' set # to the appropriate listening port. This is quite a hack. env = os.environ.copy() port_match = re.search(r':(\d+)$', self.config.sync) if port_match is None: raise ValueError("sync: cannot find port in %s" % self.config.sync) port = port_match.group(1) env['sts_sync'] = "ptcp:0.0.0.0:%d" % (int(port), ) if self.config.sync or multiplex_sockets: src_dir = os.path.join(os.path.dirname(__file__), "../../") pox_ext_dir = os.path.join(self.config.cwd, "ext") if os.path.exists(pox_ext_dir): for f in ("sts/util/io_master.py", "sts/syncproto/base.py", "sts/syncproto/pox_syncer.py", "sts/__init__.py", "sts/util/socket_mux/__init__.py", "sts/util/socket_mux/pox_monkeypatcher.py", "sts/util/socket_mux/base.py", "sts/util/socket_mux/server_socket_multiplexer.py"): src_path = os.path.join(src_dir, f) if not os.path.exists(src_path): raise ValueError( "Integrity violation: sts sync source path %s (abs: %s) " "does not exist" % (src_path, os.path.abspath(src_path))) dst_path = os.path.join(pox_ext_dir, f) dst_dir = os.path.dirname(dst_path) init_py = os.path.join(dst_dir, "__init__.py") if not os.path.exists(dst_dir): os.makedirs(dst_dir) if not os.path.exists(init_py): open(init_py, "a").close() if os.path.islink(dst_path): # Remove symlink and recreate os.remove(dst_path) if not os.path.exists(dst_path): rel_link = os.path.abspath(src_path) self.log.debug("Creating symlink %s -> %s", rel_link, dst_path) os.symlink(rel_link, dst_path) else: self.log.warn("Could not find pox ext dir in %s. " + "Cannot check/link in sync module" % pox_ext_dir) if self.config.start_cmd in ["", None]: raise RuntimeError("No command found to start controller %s!" % self.label) start_cmd = getattr(self.config, "expanded_start_cmd", self.config.start_cmd) self.log.info("Launching controller %s: %s" % (self.label, " ".join(start_cmd))) launch_in_network_namespace = getattr(self.config, "launch_in_network_namespace", False) if launch_in_network_namespace: (self.process, self.guest_eth_addr, self.host_device) = \ launch_namespace( " ".join(start_cmd), self.config.address, self.cid, host_ip_addr_str=IPAddressSpace.find_unclaimed_address( ip_prefix=self.config.address), cwd=self.config.cwd, env=env) else: self.process = popen_filtered("[%s]" % self.label, start_cmd, self.config.cwd, env) self._register_proc(self.process) if self.config.sync: self.sync_connection = self.sync_connection_manager.connect( self, self.config.sync) self._check_snapshot_connect() self.state = ControllerState.ALIVE