def execute( script, name=None, verbose=True, shell=True, pty=True, combine_stderr=True, dir=None ): """Run arbitrary scripts on a remote host.""" script = dedent(script).strip() if verbose: prefix = "[%s]" % env.host_string if env.colors: prefix = env.color_settings['host_prefix'](prefix) print("%s run: %s" % (prefix, name or script)) name = name or DEFAULT_SCRIPT_NAME with hide('running', 'stdout', 'stderr'): run('cat > ' + name + ' << FABEND\n' + script + '\nFABEND\n', dir=dir) run('chmod +x ' + name, dir=dir) try: if verbose > 1: with show('stdout', 'stderr'): out = run('./' + name, shell, pty, combine_stderr, dir) else: out = run('./' + name, shell, pty, combine_stderr, dir) finally: run('rm ' + name, dir=dir) return out
def _multirun( self, command, settings_list, shell, pty, combine_stderr, dir, format, warn_only, condensed, quiet_exit, laggards_timeout, wait_for ): callable_command = hasattr(command, '__call__') done = 0 idx = 0 ctx = self.ctx processes = {} total = len(settings_list) pool_size = env.multirun_pool_size socket_path = '/tmp/fab.%s' % uuid4() server = socket(AF_UNIX, SOCK_STREAM) server.bind(socket_path) server.listen(pool_size) for client_id in range(min(pool_size, total)): from_parent, to_child = pipe() pid = fork() if pid: processes[client_id] = [from_parent, to_child, pid, idx] idx += 1 write(to_child, pack('H', idx)) else: atfork() def die(*args): if quiet_exit: output.status = False sys.exit() signal(SIGALRM, die) if condensed: sys.__ori_stdout__ = sys.stdout sys.__ori_stderr__ = sys.stderr sys.stdout = sys.stderr = DEVNULL while 1: alarm(env.multirun_child_timeout) data = read(from_parent, INDEX_HEADER_SIZE) alarm(0) idx = unpack('H', data)[0] - 1 if idx == -1: die() try: if callable_command: with settings( ctx=ctx, warn_only=warn_only, **settings_list[idx] ): try: response = command() except Exception, error: handle_failure(command, warn_only) response = error else: with settings( ctx=ctx, warn_only=warn_only, **settings_list[idx] ): response = run( command, shell, pty, combine_stderr, dir, format ) except BaseException, error: response = error client = socket(AF_UNIX, SOCK_STREAM) client.connect(socket_path) client.send(dumps((client_id, idx, response))) client.close()