示例#1
0
文件: core.py 项目: tav/bolt-python
 def reboot(self, wait):
     ctx, r = self.ctx, reboot
     settings_list = self._settings
     responses = ResponseList.new(settings_list); out = responses.append
     for kwargs in settings_list:
         with settings(ctx=ctx, **kwargs):
             out(r(wait))
     return responses
示例#2
0
文件: core.py 项目: tav/bolt-python
 def local(self, command, capture=True, dir=None, format=True):
     ctx, l = self.ctx, local
     settings_list = self._settings
     responses = ResponseList.new(settings_list); out = responses.append
     for kwargs in settings_list:
         with settings(ctx=ctx, **kwargs):
             out(l(command, capture, dir, format))
     return responses
示例#3
0
文件: core.py 项目: tav/bolt-python
 def sudo(
     self, command, shell=True, pty=True, combine_stderr=True, user=None,
     dir=None, format=True
     ):
     ctx, s = self.ctx, sudo
     settings_list = self._settings
     responses = ResponseList.new(settings_list); out = responses.append
     for kwargs in settings_list:
         with settings(ctx=ctx, **kwargs):
             out(s(command, shell, pty, combine_stderr, user, dir, format))
     return responses
示例#4
0
文件: core.py 项目: tav/bolt-python
 def run(
     self, command, shell=True, pty=True, combine_stderr=True, dir=None,
     format=True, warn_only=WarnOnly
     ):
     ctx = self.ctx
     settings_list = self._settings
     responses = ResponseList.new(settings_list); out = responses.append
     if isinstance(command, basestring):
         r = run
         for kwargs in settings_list:
             with settings(ctx=ctx, warn_only=warn_only, **kwargs):
                 out(r(command, shell, pty, combine_stderr, dir, format))
     else:
         for kwargs in settings_list:
             with settings(ctx=ctx, warn_only=warn_only, **kwargs):
                 try:
                     out(command())
                 except Exception, error:
                     out(error)
                     handle_failure(command, warn_only)
示例#5
0
文件: core.py 项目: tav/bolt-python
 def execute(
     self, script, name=None, verbose=True, shell=True, pty=True,
     combine_stderr=True, dir=None
     ):
     ctx, e = self.ctx, execute
     settings_list = self._settings
     responses = ResponseList.new(settings_list); out = responses.append
     for kwargs in settings_list:
         with settings(ctx=ctx, **kwargs):
             out(e(script, name, verbose, shell, pty, combine_stderr, dir))
     return responses
示例#6
0
文件: core.py 项目: tav/bolt-python
    def execute_task(self, name, args, kwargs, ctx):
        """Execute the given task."""

        task = self.tasks[name]
        env.command = name
        if output.running:
            msg = "running task: %s" % name
            prefix = '[system] '
            if env.colors:
                prefix = env.color_settings['prefix'](prefix)
            print(prefix + msg)

        if not ctx:
            ctx = getattr(task, '__ctx__', None)

        if ctx:
            with settings(ctx=ctx):
                task(*args, **kwargs)
            return

        task(*args, **kwargs)
示例#7
0
文件: core.py 项目: tav/bolt-python
        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()
示例#8
0
文件: core.py 项目: tav/bolt-python
 def shell(
     self, builtins=SHELL_BUILTINS, shell=True, pty=True,
     combine_stderr=True, dir=None, format=True, warn_only=True
     ):
     ctx = self.ctx
     settings_list = self._settings
     if not settings_list:
         return
     global SHELL_HISTORY_FILE
     if (not SHELL_HISTORY_FILE) and readline:
         SHELL_HISTORY_FILE = expanduser(env.shell_history_file)
         try:
             readline.read_history_file(SHELL_HISTORY_FILE)
         except IOError:
             pass
         atexit.register(readline.write_history_file, SHELL_HISTORY_FILE)
     fastprint("shell mode\n\n", 'system')
     spec = ShellSpec(
         shell=shell, pty=pty, combine_stderr=combine_stderr, dir=dir,
         format=format
         )
     r = run
     count = 0
     prefix = '>> '
     if env.colors:
         prefix = env.color_settings['prefix'](prefix)
     try:
         while 1:
             try:
                 command = raw_input(prefix).strip()
             except EOFError:
                 raise KeyboardInterrupt
             if not command:
                 continue
             builtin_cmd = 0
             if command.startswith('.'):
                 if (len(command) > 1) and command[1].isalpha():
                     builtin_cmd = 1
             if builtin_cmd:
                 command = command.split(' ', 1)
                 if len(command) == 1:
                     command = command[0]
                     arg = ''
                 else:
                     command, arg = command
                 command = command[1:].strip()
                 if not command:
                     continue
                 command = command.replace('_', '-')
                 if command not in builtins:
                     warn("Couldn't find builtin command %r" % command)
                     continue
                 command = builtins[command]
                 if hasattr(command, '__single__'):
                     with settings(ctx=ctx, warn_only=warn_only):
                         try:
                             command(spec, arg)
                         except Exception:
                             handle_failure(command, warn_only)
                     continue
             for kwargs in settings_list:
                 with settings(ctx=ctx, warn_only=warn_only, **kwargs):
                     try:
                         if builtin_cmd:
                             try:
                                 command(spec, arg)
                             except Exception:
                                 handle_failure(command, warn_only)
                         else:
                             r(command, spec.shell, spec.pty,
                               spec.combine_stderr, spec.dir, spec.format)
                     except KeyboardInterrupt:
                         print
                         count += 1
                         if count > 2:
                             raise KeyboardInterrupt
                 count = 0
     except KeyboardInterrupt:
         print
         print
         fastprint("shell mode terminated\n", 'system')