def test_node_console(self): class Root(Node): def a(self): return 'result' # Test Console instance. p = Pty() env = Env(Root(), pty=p) self.assertIsInstance(env.console, Console) self.assertEqual(env.console.pty, p) self.assertEqual(env.console.is_interactive, p.interactive) #
def __init__(self, protocol, writeCallback=None, doneCallback=None): self.protocol = protocol self.root_node = protocol.transport.factory.root_node self.auth_backend = protocol.transport.factory.auth_backend self.extra_loggers = protocol.transport.factory.extra_loggers self.doneCallback = doneCallback self.writeCallback = writeCallback self.username = None # Create PTY self.master, self.slave = os.openpty() # File descriptors for the shell self.shell_in = self.shell_out = os.fdopen(self.master, 'r+w', 0) # File descriptors for slave pty. stdin = stdout = os.fdopen(self.slave, 'r+w', 0) # Create pty object, for passing to deployment enviroment. self.pty = Pty(stdin, stdout)
def default_from_node(cls, node): """ Create a default environment for this node to run. It will be attached to stdin/stdout and commands will be logged to stdout. The is the most obvious default to create an ``Env`` instance. :param node: :class:`~deployer.node.base.Node` instance """ from deployer.pseudo_terminal import Pty from deployer.loggers import LoggerInterface from deployer.loggers.default import DefaultLogger pty = Pty(stdin=sys.stdin, stdout=sys.stdout, interactive=False, term_var=os.environ.get('TERM', '')) logger_interface = LoggerInterface() logger_interface.attach(DefaultLogger()) return cls(node, pty=pty, logger=logger_interface, is_sandbox=False)
def test_get_size(self): # Create pty from standard stdin/stdout p = Pty() # Test get_size -> returns height,width size = p.get_size() self.assertIsInstance(size, tuple) self.assertEqual(len(size), 2) self.assertIsInstance(size[0], int) self.assertIsInstance(size[1], int) # Test get_width width = p.get_width() self.assertIsInstance(width, int) self.assertEqual(width, size[1]) # Test get_height height = p.get_height() self.assertIsInstance(height, int) self.assertEqual(height, size[0]) # Test set_term_var/get_term_var p.set_term_var('my-custom-xterm') self.assertEqual('my-custom-xterm', p.get_term_var())
def __init__(self, stdin, stdout, run_in_new_pty, interactive): Pty.__init__(self, stdin, stdout, interactive) self._run_in_new_ptys = run_in_new_pty
def start(root_node, interactive=True, cd_path=None, logfile=None, action_name=None, parameters=None, shell=StandaloneShell, extra_loggers=None, open_scp_shell=False): """ Start the deployment shell in standalone modus. (No parrallel execution, no server/client. Just one interface, and everything sequential.) """ parameters = parameters or [] # Enable logging if logfile: logging.basicConfig(filename=logfile, level=logging.DEBUG) # Make sure that stdin and stdout are unbuffered # The alternative is to start Python with the -u option sys.stdin = os.fdopen(sys.stdin.fileno(), 'r', 0) sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # Create Pty object pty = Pty(sys.stdin, sys.stdout, interactive=interactive, term_var=os.environ.get('TERM', '')) def sigwinch_handler(n, frame): pty.trigger_resize() signal.signal(signal.SIGWINCH, sigwinch_handler) # Create runtime options options = Options() # Initialize root node root_node = root_node() # Set process title setproctitle('deploy:%s run -s' % root_node.__class__.__name__) # Loggers in_shell_logger = DefaultLogger(print_group=False) logger_interface = LoggerInterface() extra_loggers = extra_loggers or [] with logger_interface.attach_in_block(in_shell_logger): with nested( *[logger_interface.attach_in_block(l) for l in extra_loggers]): # Create shell print 'Running single threaded shell...' shell = shell(root_node, pty, options, logger_interface) if cd_path is not None: shell.cd(cd_path) if action_name and open_scp_shell: raise Exception( "Don't provide 'action_name' and 'open_scp_shell' at the same time" ) if open_scp_shell: shell.open_scp_shell() elif action_name: try: return shell.run_action(action_name, *parameters) except ActionException as e: sys.exit(1) except: import traceback traceback.print_exc() sys.exit(1) else: shell.cmdloop()