def command_freeze(session_name, socket_name, socket_path): """Snapshot a session into a config. If SESSION_NAME is provided, snapshot that session. Otherwise, use the current session.""" t = Server(socket_name=socket_name, socket_path=socket_path) try: if session_name: session = t.find_where({'session_name': session_name}) else: session = t.list_sessions()[0] if not session: raise exc.TmuxpException('Session not found.') except exc.TmuxpException as e: print(e) return sconf = freeze(session) configparser = kaptan.Kaptan() newconfig = config.inline(sconf) configparser.import_config(newconfig) config_format = click.prompt( 'Convert to', value_proc=_validate_choices(['yaml', 'json']), default='yaml' ) if config_format == 'yaml': newconfig = configparser.export( 'yaml', indent=2, default_flow_style=False, safe=True ) elif config_format == 'json': newconfig = configparser.export('json', indent=2) else: sys.exit('Unknown config format.') print(newconfig) print( '---------------------------------------------------------------' '\n' 'Freeze does it best to snapshot live tmux sessions.\n' ) if click.confirm( 'The new config *WILL* require adjusting afterwards. Save config?' ): dest = None while not dest: save_to = os.path.abspath( os.path.join( get_config_dir(), '%s.%s' % (sconf.get('session_name'), config_format), ) ) dest_prompt = click.prompt( 'Save to: %s' % save_to, value_proc=get_abs_path, default=save_to ) if os.path.exists(dest_prompt): print('%s exists. Pick a new filename.' % dest_prompt) continue dest = dest_prompt dest = os.path.abspath(os.path.relpath(os.path.expanduser(dest))) if click.confirm('Save to %s?' % dest): destdir = os.path.dirname(dest) if not os.path.isdir(destdir): os.makedirs(destdir) buf = open(dest, 'w') buf.write(newconfig) buf.close() print('Saved to %s.' % dest) else: print( 'tmuxp has examples in JSON and YAML format at ' '<http://tmuxp.readthedocs.io/en/latest/examples.html>\n' 'View tmuxp docs at <http://tmuxp.readthedocs.io/>.' ) sys.exit()
class MultiMiner(object): """ multi miner class use session name suffix as the identifier for runner plan Command List start [planname] run the corresponding plan, or the default if planname not given stop stop the miner tmux session TMUX commands you might use C-b d detach the current session C-b left go to the next pane on the left C-b right (or one of these other directions) C-b up C-b down """ def __init__(self): self.server = Server() self.miner_session = None self.miner_layout = '' # get the miner session if exists. # we don't use exact match # the session name is like 'multiminer-zen' if self.server.has_session(SESSION_NAME, exact=False): for sess in self.server.list_sessions(): if sess['session_name'].startswith(SESSION_NAME): self.miner_session = sess self.miner_layout = sess['session_name'][len(SESSION_NAME)+1:] break def _miner_exist(func): def _decorator(self, *args, **kwargs): if self.miner_session is None: print("NO MINER SESSION RUNNING") return return func(self, *args, **kwargs) return _decorator @_miner_exist def miners(self): pass @_miner_exist def stop(self): try: self.server.kill_session(SESSION_NAME) print("MINER STOPPED") except: # LibTmuxException pass def start(self, target="default"): # load config file with open("./config.yaml", 'r') as stream: try: config = yaml.load(stream) # kill all miner sessions if self.server.has_session(SESSION_NAME, exact=False): self.server.kill_session(target_session=SESSION_NAME) print("Killing the running miners ...") # run new session self.miner_session = self.server.new_session( session_name=SESSION_NAME + '-' + target, kill_session=True, ) self._build_layout(target, config) except yaml.YAMLError as e: print(e) except: pass pass def _build_layout(self, target, config): runner_config = config['runners'][target] # we use only one window, the default window default_window = self.miner_session.attached_window p = None for miner_conf in runner_config: device_config = miner_conf['devices'] if device_config is None or len(device_config) == 0: # skip if no or 0 device configed continue if p is None: p = default_window.attached_pane else: # split the current window for more panes p = default_window.split_window( target=p.id, attach=True, start_directory= None, vertical=False ) default_window.select_layout('even-horizontal') # default layout default_window.server._update_panes() wallet_config = config['wallets'][miner_conf['wallet']] miner_config = config['miners'][miner_conf['miner']] # build cmd cmd = self._build_miner_cmd(miner_conf, wallet_config, miner_config, device_config) print(cmd) p.send_keys(cmd, suppress_history=True) self.server.attach_session(SESSION_NAME) def _build_miner_cmd(self, miner_conf, wallet, miner, device): cmd = '' miner_name = miner_conf['miner'] if miner_name == 'zm': # zm --server servername.com --port 1234 --user username -- dev 0 1 2 --time --color # TODO: support temperature adaptation protocol = '' if wallet['ssl'] is True: protocol = 'ssl://' cmd = '%s --server %s%s --port %d --user %s --pass %s --dev %s --time --color' % (miner['location'], protocol, wallet['server'], wallet['port'], wallet['address'], wallet['pass'], " ".join(map(str, device))) if miner_name == 'bminer': # ./bminer -uri $SCHEME://$USERNAME@$POOL -api 127.0.0.1:1880 protocol = 'stratum://' if wallet['ssl'] is True: protocol = 'stratum+ssl://' cmd = '%s -uri %s%s:%s@%s:%s -devices %s' % (miner['location'], protocol, wallet['address'], wallet['pass'], wallet['server'], wallet['port'], ",".join(map(str, device))) if miner_name == 'ethminer': # ./ethminer -P stratum+ssl://0x2f112f0f47fda00fb52493a990d49a75faed69e3.miner1@us1.ethermine.org:5555 -U protocol = 'stratum+tcp://' if wallet['ssl'] is True: protocol = 'stratum+ssl://' cmd = '%s -P %s%s:%s@%s:%s -U --cuda-devices %s' % (miner['location'], protocol, wallet['address'], wallet['pass'], wallet['server'], wallet['port'], " ".join(map(str, device))) if miner_name == 'ccminer': protocol = 'stratum+tcp://' alg = miner_conf['wallet'].split('-')[-1] if wallet['ssl'] is True: print("ccminer not support ssl") return "exit" cmd = '%s -a %s -o %s%s:%s -u %s -p %s -d %s' % (miner['location'], alg, protocol, wallet['server'], wallet['port'], wallet['address'], wallet['pass'], ",".join(map(str, device))) return cmd
def command_shell(session_name, window_name, socket_name, socket_path, command): """Launch python shell for tmux server, session, window and pane. Priority given to loaded session/wndow/pane objects: - session_name and window_name arguments - current shell: environmental variable of TMUX_PANE (which gives us window and session) - ``server.attached_session``, ``session.attached_window``, ``window.attached_pane`` """ server = Server(socket_name=socket_name, socket_path=socket_path) try: server.sessions except LibTmuxException as e: if 'No such file or directory' in str(e): raise LibTmuxException( 'no tmux session found. Start a tmux session and try again. \n' 'Original error: ' + str(e) ) else: raise e current_pane = None if os.getenv('TMUX_PANE') is not None: try: current_pane = [ p for p in server._list_panes() if p.get('pane_id') == os.getenv('TMUX_PANE') ][0] except IndexError: pass try: if session_name: session = server.find_where({'session_name': session_name}) elif current_pane is not None: session = server.find_where({'session_id': current_pane['session_id']}) else: session = server.list_sessions()[0] if not session: raise exc.TmuxpException('Session not found: %s' % session_name) except exc.TmuxpException as e: print(e) return try: if window_name: window = session.find_where({'window_name': window_name}) if not window: raise exc.TmuxpException('Window not found: %s' % window_name) elif current_pane is not None: window = session.find_where({'window_id': current_pane['window_id']}) else: window = session.list_windows()[0] except exc.TmuxpException as e: print(e) return try: if current_pane is not None: pane = window.find_where({'pane_id': current_pane['pane_id']}) # NOQA: F841 else: pane = window.attached_pane # NOQA: F841 except exc.TmuxpException as e: print(e) return if command is not None: exec(command) else: from ._compat import breakpoint as tmuxp_breakpoint tmuxp_breakpoint()
def command_freeze(session_name, socket_name, socket_path): """Snapshot a session into a config. If SESSION_NAME is provided, snapshot that session. Otherwise, use the current session.""" t = Server(socket_name=socket_name, socket_path=socket_path) try: if session_name: session = t.find_where({'session_name': session_name}) else: session = t.list_sessions()[0] if not session: raise exc.TmuxpException('Session not found.') except exc.TmuxpException as e: print(e) return sconf = freeze(session) configparser = kaptan.Kaptan() newconfig = config.inline(sconf) configparser.import_config(newconfig) config_format = click.prompt( 'Convert to', value_proc=_validate_choices(['yaml', 'json']), default='yaml' ) if config_format == 'yaml': newconfig = configparser.export( 'yaml', indent=2, default_flow_style=False, safe=True ) elif config_format == 'json': newconfig = configparser.export('json', indent=2) else: sys.exit('Unknown config format.') print(newconfig) print( '---------------------------------------------------------------' '\n' 'Freeze does it best to snapshot live tmux sessions.\n' ) if click.confirm( 'The new config *WILL* require adjusting afterwards. Save config?' ): dest = None while not dest: save_to = os.path.abspath( os.path.join( get_config_dir(), '%s.%s' % (sconf.get('session_name'), config_format), ) ) dest_prompt = click.prompt( 'Save to: %s' % save_to, value_proc=get_abs_path, default=save_to, confirmation_prompt=True, ) if os.path.exists(dest_prompt): print('%s exists. Pick a new filename.' % dest_prompt) continue dest = dest_prompt dest = os.path.abspath(os.path.relpath(os.path.expanduser(dest))) if click.confirm('Save to %s?' % dest): destdir = os.path.dirname(dest) if not os.path.isdir(destdir): os.makedirs(destdir) buf = open(dest, 'w') buf.write(newconfig) buf.close() print('Saved to %s.' % dest) else: print( 'tmuxp has examples in JSON and YAML format at ' '<http://tmuxp.readthedocs.io/en/latest/examples.html>\n' 'View tmuxp docs at <http://tmuxp.readthedocs.io/>.' ) sys.exit()
def session_completion(ctx, params, incomplete): t = Server() choices = [session.name for session in t.list_sessions()] return sorted([str(c) for c in choices if str(c).startswith(incomplete)])