Example #1
0
def test_split_command() -> None:
    cmd = Docker.split_command(None)
    assert isinstance(cmd, list)
    assert len(cmd) == 0

    cmd = Docker.split_command("")
    assert isinstance(cmd, list)
    assert len(cmd) == 0

    cmd = Docker.split_command("a")
    assert isinstance(cmd, list)
    assert len(cmd) == 1
    assert cmd[0] == "a"

    cmd = Docker.split_command("a b")
    assert isinstance(cmd, list)
    assert len(cmd) == 2
    assert cmd[0] == "a"
    assert cmd[1] == "b"

    cmd = Docker.split_command("a b c")
    assert isinstance(cmd, list)
    assert len(cmd) == 3
    assert cmd[0] == "a"
    assert cmd[1] == "b"
    assert cmd[2] == "c"

    cmd = Docker.split_command("a 'b c'")
    assert isinstance(cmd, list)
    assert len(cmd) == 2
    assert cmd[0] == "a"
    assert cmd[1] == "b c"
Example #2
0
    def create_volatile_container(
        self,
        service: str,
        command: Optional[str] = None,
        publish: Optional[List[Union[PortMapping, PortRangeMapping]]] = None,
        # used by interfaces
        detach: bool = False,
        user: Optional[str] = None,
    ) -> bool:

        compose_engine_forced = False
        if Configuration.swarm_mode:
            # import here to prevent circular imports
            from controller.app import Application

            if not Configuration.FORCE_COMPOSE_ENGINE:
                compose_engine_forced = True
                Configuration.FORCE_COMPOSE_ENGINE = True
                # init is needed to reload the configuration to force compose engine
                Application.get_controller().controller_init()

        tty = sys.stdout.isatty()

        try:
            output = self.docker.compose.run(
                service=service,
                name=service,
                command=Docker.split_command(command),
                user=user,
                detach=detach,
                tty=tty and not detach,
                stream=not tty and not detach,
                dependencies=False,
                remove=True,
                service_ports=False,
                publish=publish or [],
                use_aliases=True,
            )

            if not detach:
                for out_line in output:  # type: ignore
                    # 'stdout' or 'stderr'
                    # Both out and err are collapsed in stdout
                    # Maybe in the future would be useful to keep them separated?
                    # stdstream = out_line[0]

                    line = out_line[1]

                    if isinstance(line, bytes):
                        line = line.decode("UTF-8")

                    print(line.strip())

            if compose_engine_forced:
                Configuration.FORCE_COMPOSE_ENGINE = False
                # init is needed to reload the configuration to undo compose engine
                Application.get_controller().controller_init()

            return True
        except DockerException as e:
            log.critical(e)
            return False