def _getServer(self,name,firstWindow=""): try: s=tmuxp.Server() s.list_sessions() except Exception as e: if firstWindow=="": j.tools.cuisine.local.tmux.createSession(name,["ignore"]) else: j.tools.cuisine.local.tmux.createSession(name,[firstWindow]) s=tmuxp.Server() s.list_sessions() return s
def stop_sim(sim): logging.main_logger.debug("[run_sim] 'stop_sim' called") # check if simulator is running if not sim.is_alive(): logging.main_logger.warning("[run_sim] Sim %s is not running", sim.name) return False # tmux sever tmux_server = tmuxp.Server() # check if there is already a session if not tmux_server.has_session(sim.name): logging.main_logger.warning("[run_sim] No session found for %s", sim.name) return False else: session = tmux_server.findWhere({"session_name": sim.name}) window = session.findWhere({'window_name': 'OpenSimulator'}) if not window: logging.main_logger.warning("[run_sim] No window found for %s", sim.name) return False session.select_window('OpenSimulator') pane = window.select_pane(0) pane.enter() pane.send_keys("quit", True) logging.main_logger.debug("[run_sim] OpenSimulator quitted from %s", sim.name) return True
def send_command(sim, command): logging.main_logger.debug("[send_command] 'send_command' called") # check if simulator is running if not sim.is_alive(): logging.main_logger.warning("[send_command] sim is not alive") return False # tmux sever tmux_server = tmuxp.Server() # check if there is already a session if not tmux_server.has_session(sim.name): logging.main_logger.warning("[send_command] No session found for %s", sim.name) return False else: session = tmux_server.findWhere({"session_name": sim.name}) window = session.findWhere({'window_name': 'OpenSimulator'}) if not window: logging.main_logger.warning( "[send_command] No window found for %s", sim.name) return False session.select_window('OpenSimulator') pane = window.select_pane(0) pane.enter() pane.send_keys(' '.join(command), True) logging.main_logger.debug("[send_command] Command sent to %s", sim.name) return True
def tmux_parallel(cmds, session_name): server = tmuxp.Server() session = server.new_session(session_name) for cmd in cmds: pane = session.new_window().attached_pane() for ks in cmd: pane.send_keys(ks, enter=True)
def tmux_server(self): """ tmux server """ if not self._tmux_server: self._tmux_server = tmuxp.Server() return self._tmux_server
def debug_simulation(): import os import tmuxp #import usi.shell server = tmuxp.Server() session = server.sessions[0] window = session.new_window(window_name="GDB Simulation") #usi.shell.stop() window.pane[0].send_keys("gdb program %s" % os.getpid())
def __init__(self): self.server = tmuxp.Server() sessions = self.server.attached_sessions() assert len(sessions) == 1 self.session = tmuxp.Session(server=self.server, session_id=sessions[0]['session_id']) self.window = self.session.attached_window() self.window.tmux('set-window-option', 'main-pane-width', '50') self.window.tmux('set-window-option', 'remain-on-exit', 'on') self.window.tmux('set-window-option', 'set-remain-on-exit', 'on')
def kill_sim(sim): logging.main_logger.debug("[run_sim] 'kill_sim' called") # tmux sever tmux_server = tmuxp.Server() # check if there is already a session if tmux_server.has_session(sim.name): tmux_server.kill_session(target_session=sim.name) logging.main_logger.debug("[run_sim] OpenSimulator killed from %s", sim.name) return True
def __enter__(self): self.tmux_config = self.tempfile(self.tmux_config_contents()) self.bash_config = self.tempfile(self.bash_config_contents()) self.server = tmuxp.Server() if self.use_existing_session: try: session_dict = self.server.attached_sessions( ) # until tmuxp bug is fixed if session_dict is None: raise tmuxp.exc.TmuxpException self.session = tmuxp.Session(self.server, **session_dict[0]) except tmuxp.exc.TmuxpException: self.session = self.server.new_session( session_name='rlundotesting') else: self.session = self.server.new_session( session_name='rlundotesting', kill_session=True) self.window = self.session.new_window(attach=False) try: output = self.window.panes[0].cmd( 'respawn-pane', '-k', 'bash --rcfile %s --noprofile' % (self.bash_config.name, )) if output.stderr: raise ValueError( repr(output.stderr) + " " + repr(self.window.panes)) (pane, ) = self.window.panes output = pane.cmd( 'respawn-pane', '-k', 'bash --rcfile %s --noprofile' % (self.bash_config.name, )) if output.stderr: raise ValueError( repr(output.stderr) + " " + repr(self.window.panes)) pane.cmd( 'split-window', '-h', 'bash --rcfile %s --noprofile' % (self.bash_config.name, )) pane.cmd( 'split-window', '-v', 'bash --rcfile %s --noprofile' % (self.bash_config.name, )) if self.width is not None: pane.set_width(self.width) wait_for_width(pane, self.width) if self.height is not None: pane.set_height(self.height) wait_for_height(pane, self.height) wait_for_prompt(pane) return pane except: self.window.kill_window() raise
def start_sim(sim): logging.main_logger.debug("[run_sim] 'start_sim' called") # check if simulator is running if sim.is_alive(): logging.main_logger.debug("[run_sim] Sim is running") return False is_new = False # tmux sever tmux_server = tmuxp.Server() # check if there is already a session if not tmux_server.has_session(sim.name): logging.main_logger.warning("[run_sim] No session found for %s", sim.name) # create a tmux session if not exist session = tmux_server.new_session(session_name=sim.name) window = session.new_window(window_name='OpenSimulator') logging.main_logger.debug( "[run_sim] Session and window created for %s", sim.name) is_new = True else: session = tmux_server.findWhere({"session_name": sim.name}) window = session.findWhere({'window_name': 'OpenSimulator'}) if not window: logging.main_logger.warning("[run_sim] No window found for %s", sim.name) window = session.new_window(window_name='OpenSimulator', attach=False) logging.main_logger.debug("[run_sim] Window created for %s", sim.name) is_new = True session.select_window('OpenSimulator') pane = window.select_pane(0) pane.send_keys('cd ' + sim.path + '/bin', enter=False) pane.enter() if is_new: pane.send_keys("tmux pipe-pane -o -t " + sim.name + ":OpenSimulator 'cat >> " + sim.path + "/log/tmux.log'", enter=False) pane.enter() pane.send_keys('mono --server OpenSim.exe', enter=False) pane.enter() logging.main_logger.debug("[run_sim] OpenSimulator runned from %s", sim.name) return True
def tmux(self): """ Creates an new tmux session with an window for each node """ server = tmuxp.Server() session = server.new_session(session_name=self.name) cmd = ("sshpass -p {1} ssh -o UserKnownHostsFile=/dev/null " "-o StrictHostKeyChecking=no -o LogLevel=quiet -l root {0}") for node in self.nodes: name = node.name[len(self.name) + 1:] window = session.new_window(window_name=name) pane = window.panes[0] pane.send_keys(cmd.format(node.ipaddress, node.password))
def stop_project_all_instances(project_name): try: project_state = get_current_project_state(project_name) for pid in project_state["pid"]: if psutil.pid_exists(pid): print "Killing " + str(pid) + "..." kill_process_with_children(psutil.Process(pid)) # kill the tmux server if it's running tmux_server = tmuxp.Server(socket_path=get_project_meta_path(project_name) + "/.tmux_socket") if tmux_server.has_session(project_name): tmux_server.kill_session(project_name) project_state["pid"] = [] store_current_project_state(project_name, project_state) except Exception as e: print e pass
def tmux_init(devices, session_name): #start server server = tmuxp.Server() #find the session created by the init script session = server.findWhere({"session_name": session_name}) #rename the first window session.list_windows()[0].rename_window('main') #session.list_windows()[0].split_window(attach=False) #create a window containing the main log winLog = session.new_window(attach=False, window_name='mainLog') winLog.split_window(attach=False) for id in devices: win = session.new_window(attach=False, window_name=str(id)) win.split_window(attach=False) for p in win.list_panes(): print p return session
def get_tmux_session(): return tmuxp.Server().attached_sessions()[0]
def start_project_instance(project_name, start_as_master=False, use_tmux=False): if not os.path.exists(get_project_path(project_name) + "/run.sh"): raise Exception("Missing run.sh!") try: if os.path.exists(get_project_meta_path(project_name) + "/.last_pid"): os.unlink(get_project_meta_path(project_name) + "/.last_pid") os.chmod(get_project_path(project_name) + "/run.sh", 0744) if use_tmux: start_cmd = "exec ./run.sh" window_name = "slave" if start_as_master: start_cmd += " -master" window_name = "master" tmux_server = tmuxp.Server(socket_path=get_project_meta_path(project_name) + "/.tmux_socket") if tmux_server.has_session(project_name): tmux_session = tmux_server.findWhere({"session_name": project_name}) tmux_window = tmux_session.new_window(attach=True) tmux_pane = tmux_window.attached_pane() else: tmux_session = tmux_server.new_session(session_name=project_name) tmux_window = tmux_session.attached_window() tmux_pane = tmux_window.attached_pane() tmux_window.rename_window(window_name) tmux_pane.send_keys("cd " + get_project_path(project_name)) tmux_pane.send_keys("echo $$ > ./.meta/.last_pid") tmux_pane.send_keys(start_cmd) while not os.path.exists(get_project_meta_path(project_name) + "/.last_pid"): pass with open(get_project_meta_path(project_name) + "/.last_pid", "r") as f: last_pid = int(f.read().strip()) tmux_window.rename_window(window_name + " (" + str(last_pid) + ")") handle = psutil.Process(last_pid) else: start_cmd = ["exec ./run.sh"] if start_as_master: start_cmd.append("-master") handle = psutil.Popen(start_cmd, shell=True, cwd=get_project_path(project_name), stdout=subprocess.PIPE) # stderr=subprocess.PIPE # Add the new pid to the state file so we can kill it in the future state = {"pid": []} try: state = get_current_project_state(project_name) except: pass state["pid"].append(handle.pid) store_current_project_state(project_name, state) except Exception as e: print e return False return handle
for p in get_current_projects(): if not is_project_running(p): update_project(p) else: print "Project " + p + " is running, update skipped." elif is_current_project(sys.argv[2]): if not is_project_running(sys.argv[2]): update_project(sys.argv[2]) else: print "Project " + sys.argv[2] + " is running, update skipped." else: print "Unknown project " + sys.argv[2] + "!" elif len(sys.argv) >= 2 and sys.argv[1] == "sessions": if len(sys.argv) == 2 or (len(sys.argv) == 3 and sys.argv[2] == "all"): for p in get_current_projects(): tmux_server = tmuxp.Server(socket_path=get_project_meta_path(p) + "/.tmux_socket") if is_project_running(p) and tmux_server.has_session(p): print p + " instances:" tmux_session = tmux_server.findWhere({"session_name": p}) for w in tmux_session.list_windows(): print "\t" + w.get("window_name") elif is_current_project(sys.argv[2]): tmux_server = tmuxp.Server(socket_path=get_project_meta_path(sys.argv[2]) + "/.tmux_socket") if is_project_running(sys.argv[2]) and tmux_server.has_session(sys.argv[2]): print sys.argv[2] + " instances:" tmux_session = tmux_server.findWhere({"session_name": sys.argv[2]})