示例#1
0
def run(w, app, service_name, command, out=None, environment=[]):
    project = compose.cli.command.get_project(w + '/services/' + app['name'])

    service = project.get_service(service_name)

    container = service.create_container(quiet=True,
                                         one_off=True,
                                         **{
                                             'command': command,
                                             'tty': False,
                                             'stdin_open': False,
                                             'detach': False,
                                             'environment': environment
                                         })

    operation = RunOperation(
        project.client,
        container.id,
        interactive=False,
        logs=False,
        stdout=out,
    )
    pty = PseudoTerminal(project.client, operation)
    sockets = pty.sockets()
    service.start_container(container)
    pty.start(sockets)
    exit_code = container.wait()

    project.client.remove_container(container.id, force=True, v=True)

    return exit_code
示例#2
0
def run_one_off_container(container_options, project, service, options):
    if not options['--no-deps']:
        deps = service.get_dependency_names()
        if deps:
            project.up(
                service_names=deps,
                start_deps=True,
                strategy=ConvergenceStrategy.never,
                rescale=False
            )

    project.initialize()

    container = service.create_container(
        quiet=True,
        one_off=True,
        **container_options)

    if options['-d']:
        service.start_container(container)
        print(container.name)
        return

    def remove_container(force=False):
        if options['--rm']:
            project.client.remove_container(container.id, force=True, v=True)

    signals.set_signal_handler_to_shutdown()
    try:
        try:
            if IS_WINDOWS_PLATFORM:
                service.connect_container_to_networks(container)
                exit_code = call_docker(["start", "--attach", "--interactive", container.id])
            else:
                operation = RunOperation(
                    project.client,
                    container.id,
                    interactive=not options['-T'],
                    logs=False,
                )
                pty = PseudoTerminal(project.client, operation)
                sockets = pty.sockets()
                service.start_container(container)
                pty.start(sockets)
                exit_code = container.wait()
        except signals.ShutdownException:
            project.client.stop(container.id)
            exit_code = 1
    except signals.ShutdownException:
        project.client.kill(container.id)
        remove_container(force=True)
        sys.exit(2)

    remove_container()
    sys.exit(exit_code)
示例#3
0
def start(client, container, interactive=True, stdout=None, stderr=None, stdin=None, logs=None):
    """
    Present the PTY of the container inside the current process.

    This is just a wrapper for PseudoTerminal(client, container).start()
    """

    operation = RunOperation(client, container, interactive=interactive, stdout=stdout,
                             stderr=stderr, stdin=stdin, logs=logs)

    PseudoTerminal(client, operation).start()