コード例 #1
0
 def __enter__(self):
     client = get_client()
     c = client.create_container(self.image.get_id())
     container = DockerContainer(self.image, c["Id"])
     try:
         export_docker_container_to_directory(client, container, self.mount_point)
     finally:
         container.delete(force=True)
     return super(DockerImageViaArchiveFS, self).__enter__()
コード例 #2
0
    def rm_cont(self, x: DockerContainer):

        try:
            x.delete(force=True)
        except DockerAPIError as e:
            self.LOG.error(e)
            return False
        else:
            return True
コード例 #3
0
    def clear_mule(self, mule: DockerContainer):

        ls_output = mule.execute('ls {}'.format(DockerConst.STG_MOUNT))

        if not ls_output:
            return

        for file_name in ls_output[0].strip().decode('utf-8').split('\n'):
            mule.execute('rm -rf {}/{}'.format(DockerConst.STG_MOUNT,
                                               file_name))
コード例 #4
0
    def run_via_binary(self, run_command_instance=None, *args, **kwargs):
        """
        create a container using this image and run it in background;
        this method is useful to test real user scenarios when users invoke containers using
        binary

        :param image: instance of Image
        :param run_command_instance: instance of DockerRunBuilder
        :return: instance of DockerContainer
        """
        logger.info("run container via binary in background")
        run_command_instance = run_command_instance or DockerRunBuilder()
        if not isinstance(run_command_instance, DockerRunBuilder):
            raise ConuException(
                "run_command_instance needs to be an instance of DockerRunBuilder"
            )
        run_command_instance.image_name = self.get_id()
        run_command_instance.options += ["-d"]
        popen_instance = subprocess.Popen(run_command_instance.build(),
                                          stdout=subprocess.PIPE)
        stdout = popen_instance.communicate()[0].strip().decode("utf-8")
        if popen_instance.returncode > 0:
            raise ConuException("Container exited with an error: %s" %
                                popen_instance.returncode)
        # no error, stdout is the container id
        return DockerContainer(self, stdout)
コード例 #5
0
    def watch_cont(self, container: DockerContainer):

        try:
            while container.is_running():
                time.sleep(1)
        except (KeyboardInterrupt, InterruptedError):
            self.interrupted = True
コード例 #6
0
ファイル: image.py プロジェクト: kosciCZ/conu
    def run_via_binary(self, run_command_instance=None, *args, **kwargs):
        """
        create a container using this image and run it in background;
        this method is useful to test real user scenarios when users invoke containers using
        binary

        :param run_command_instance: instance of DockerRunBuilder
        :return: instance of DockerContainer
        """
        logger.info("run container via binary in background")
        run_command_instance = run_command_instance or DockerRunBuilder()
        if not isinstance(run_command_instance, DockerRunBuilder):
            raise ConuException(
                "run_command_instance needs to be an instance of DockerRunBuilder"
            )
        run_command_instance.image_name = self.get_id()
        run_command_instance.options += ["-d"]

        def callback():
            try:
                # FIXME: catch std{out,err}, print stdout to logger.debug, stderr to logger.error
                run_cmd(run_command_instance.build())
            except subprocess.CalledProcessError as ex:
                raise ConuException("Container exited with an error: %s" %
                                    ex.returncode)

        container_id, _ = self._run_container(run_command_instance, callback)

        container_name = self.d.inspect_container(container_id)['Name'][1:]
        return DockerContainer(self, container_id, name=container_name)
コード例 #7
0
ファイル: image.py プロジェクト: mateimicu/conu
    def run_via_binary(self, run_command_instance=None, command=None, volumes=None, additional_opts=None, *args, **kwargs):
        """
        create a container using this image and run it in background;
        this method is useful to test real user scenarios when users invoke containers using
        binary

        :param run_command_instance: instance of DockerRunBuilder
        :param command: list of str, command to run in the container, examples:
            - ["ls", "/"]
            - ["bash", "-c", "ls / | grep bin"]
        :param volumes: tuple or list of tuples in the form:

            * `("/path/to/directory", )`
            * `("/host/path", "/container/path")`
            * `("/host/path", "/container/path", "mode")`
            * `(conu.Directory('/host/path'), "/container/path")` (source can be also Directory instance)

        :param additional_opts: list of str, additional options for `docker run`
        :return: instance of DockerContainer
        """

        logger.info("run container via binary in background")

        if (command is not None or additional_opts is not None) and run_command_instance is not None:
            raise ConuException("run_command_instance and command parameters cannot be passed into method at same time")

        if run_command_instance is None:
            command = command or []
            additional_opts = additional_opts or []

            if (isinstance(command, list) or isinstance(command, tuple) and
                isinstance(additional_opts, list) or isinstance(additional_opts, tuple)):
                run_command_instance = DockerRunBuilder(command=command, additional_opts=additional_opts)
            else:
                raise ConuException("command and additional_opts needs to be list of str or None")
        else:
            run_command_instance = run_command_instance or DockerRunBuilder()
            if not isinstance(run_command_instance, DockerRunBuilder):
                raise ConuException("run_command_instance needs to be an instance of DockerRunBuilder")

        run_command_instance.image_name = self.get_id()
        run_command_instance.options += ["-d"]

        if volumes:
            run_command_instance.options += self.get_volume_options(volumes=volumes)

        def callback():
            try:
                # FIXME: catch std{out,err}, print stdout to logger.debug, stderr to logger.error
                run_cmd(run_command_instance.build())
            except subprocess.CalledProcessError as ex:
                raise ConuException("Container exited with an error: %s" % ex.returncode)

        container_id, _ = self._run_container(run_command_instance, callback)

        container_name = self.d.inspect_container(container_id)['Name'][1:]
        return DockerContainer(self, container_id, name=container_name)
コード例 #8
0
    def run_via_api(self, container_params=None):
        """
        create a container using this image and run it in background via Docker-py API.
        https://docker-py.readthedocs.io/en/stable/api.html
        Note: If you are using Healthchecks, be aware that support of some options were introduced
        just with version of Docker-py API 1.29
        :param container_params: DockerContainerParameters
        :return: instance of DockerContainer
        """

        if not container_params:
            container_params = DockerContainerParameters()

        # Host-specific configuration
        host_config = self.d.create_host_config(
            auto_remove=container_params.remove,
            cap_add=container_params.cap_add,
            cap_drop=container_params.cap_drop,
            devices=container_params.devices,
            dns=container_params.dns,
            group_add=container_params.group_add,
            init=container_params.init,
            ipc_mode=container_params.ipc_mode,
            isolation=container_params.isolation,
            mem_limit=container_params.mem_limit,
            mounts=container_params.mounts,
            pids_limit=container_params.pids_limit,
            privileged=container_params.privileged,
            publish_all_ports=container_params.publish_all_ports,
            port_bindings=container_params.port_mappings,
            read_only=container_params.read_only)

        container = self.d.create_container(
            self.get_id(),
            command=container_params.command,
            detach=True,
            hostname=container_params.hostname,
            user=container_params.user,
            stdin_open=container_params.stdin_open,
            tty=container_params.tty,
            ports=container_params.exposed_ports,
            environment=container_params.env_variables,
            volumes=container_params.volumes,
            name=container_params.name,
            entrypoint=container_params.entrypoint,
            working_dir=container_params.working_dir,
            host_config=host_config,
            mac_address=container_params.mac_address,
            labels=container_params.labels,
            stop_signal=container_params.stop_signal,
            healthcheck=container_params.healthcheck,
            runtime=container_params.runtime)

        return DockerContainer(self,
                               container['Id'],
                               name=container_params.name)
コード例 #9
0
ファイル: backend.py プロジェクト: dbecvarik/conu
    def list_containers(self):
        """
        List all available docker containers.

        Container objects returned from this methods will contain a limited
        amount of metadata in property `short_metadata`. These are just a subset
        of `.get_metadata()`, but don't require an API call against dockerd.

        :return: collection of instances of :class:`conu.DockerContainer`
        """
        return [DockerContainer(None, c["Id"], short_metadata=c) for c in self.d.containers(all=True)]
コード例 #10
0
ファイル: image.py プロジェクト: kosciCZ/conu
    def run_via_binary_in_foreground(self,
                                     run_command_instance=None,
                                     popen_params=None,
                                     container_name=None):
        """
        Create a container using this image and run it in foreground;
        this method is useful to test real user scenarios when users invoke containers using
        binary and pass input into the container via STDIN. You are also responsible for:

         * redirecting STDIN when intending to use container.write_to_stdin afterwards by setting
              popen_params={"stdin": subprocess.PIPE} during run_via_binary_in_foreground

         * checking whether the container exited successfully via:
              container.popen_instance.returncode

        Please consult the documentation for subprocess python module for best practices on
        how you should work with instance of Popen

        :param run_command_instance: instance of DockerRunBuilder
        :param popen_params: dict, keyword arguments passed to Popen constructor
        :param container_name: str, pretty container identifier
        :return: instance of DockerContainer
        """
        logger.info("run container via binary in foreground")
        run_command_instance = run_command_instance or DockerRunBuilder()
        if not isinstance(run_command_instance, DockerRunBuilder):
            raise ConuException(
                "run_command_instance needs to be an instance of DockerRunBuilder"
            )
        popen_params = popen_params or {}
        run_command_instance.image_name = self.get_id()
        if container_name:
            run_command_instance.options += ["--name", container_name]

        def callback():
            return subprocess.Popen(run_command_instance.build(),
                                    **popen_params)

        container_id, popen_instance = self._run_container(
            run_command_instance, callback)

        actual_name = self.d.inspect_container(container_id)['Name'][1:]
        if container_name and container_name != actual_name:
            raise ConuException(
                "Unexpected container name value. Expected = " +
                str(container_name) + " Actual = " + str(actual_name))
        if not container_name:
            container_name = actual_name
        return DockerContainer(self,
                               container_id,
                               popen_instance=popen_instance,
                               name=container_name)
コード例 #11
0
    def run_via_binary_in_foreground(self,
                                     run_command_instance=None,
                                     popen_params=None,
                                     container_name=None):
        """
        Create a container using this image and run it in foreground;
        this method is useful to test real user scenarios when users invoke containers using
        binary and pass input into the container via STDIN. Please bear in mind that conu doesn't
        know the ID of the container when created like this, so it's highly recommended to name
        your container. You are also responsible for checking whether the container exited
        successfully via:

            container.popen_instance.returncode

        Please consult the documentation for subprocess python module for best practices on
        how you should work with instance of Popen

        :param run_command_instance: instance of DockerRunBuilder
        :param popen_params: dict, keyword arguments passed to Popen constructor
        :param container_name: str, pretty container identifier
        :return: instance of DockerContainer
        """
        logger.info("run container via binary in foreground")
        run_command_instance = run_command_instance or DockerRunBuilder()
        if not isinstance(run_command_instance, DockerRunBuilder):
            raise ConuException(
                "run_command_instance needs to be an instance of DockerRunBuilder"
            )
        popen_params = popen_params or {}
        run_command_instance.image_name = self.get_id()
        if container_name:
            run_command_instance.options += ["--name", container_name]
        logger.debug("command = %s", str(run_command_instance))
        popen_instance = subprocess.Popen(run_command_instance.build(),
                                          **popen_params)
        container_id = None
        return DockerContainer(self,
                               container_id,
                               popen_instance=popen_instance,
                               name=container_name)
コード例 #12
0
    def list_containers(self):
        """
        List all available docker containers.

        Container objects returned from this methods will contain a limited
        amount of metadata in property `short_metadata`. These are just a subset
        of `.inspect()`, but don't require an API call against dockerd.

        :return: collection of instances of :class:`conu.DockerContainer`
        """
        result = []
        for c in self.d.containers(all=True):
            name = None
            names = c.get("Names", None)
            if names:
                name = names[0]
            i = DockerImage(None, identifier=c["ImageID"])
            cont = DockerContainer(i, c["Id"], name=name)
            # TODO: docker_client.containers produces different metadata than inspect
            inspect_to_container_metadata(cont.metadata, c, i)
            result.append(cont)
        return result
コード例 #13
0
    def run_via_binary_in_foreground(
            self, run_command_instance=None, command=None, volumes=None,
            additional_opts=None, popen_params=None, container_name=None):
        """
        Create a container using this image and run it in foreground;
        this method is useful to test real user scenarios when users invoke containers using
        binary and pass input into the container via STDIN. You are also responsible for:

         * redirecting STDIN when intending to use container.write_to_stdin afterwards by setting
              popen_params={"stdin": subprocess.PIPE} during run_via_binary_in_foreground

         * checking whether the container exited successfully via:
              container.popen_instance.returncode

        Please consult the documentation for subprocess python module for best practices on
        how you should work with instance of Popen

        :param run_command_instance: instance of DockerRunBuilder
        :param command: list of str, command to run in the container, examples:
            - ["ls", "/"]
            - ["bash", "-c", "ls / | grep bin"]
        :param volumes: tuple or list of tuples in the form:

            * `("/path/to/directory", )`
            * `("/host/path", "/container/path")`
            * `("/host/path", "/container/path", "mode")`
            * `(conu.Directory('/host/path'), "/container/path")` (source can be also
                Directory instance)

        :param additional_opts: list of str, additional options for `docker run`
        :param popen_params: dict, keyword arguments passed to Popen constructor
        :param container_name: str, pretty container identifier
        :return: instance of DockerContainer
        """
        logger.info("run container via binary in foreground")

        if (command is not None or additional_opts is not None) \
                and run_command_instance is not None:
            raise ConuException(
                "run_command_instance and command parameters cannot be "
                "passed into method at same time")

        if run_command_instance is None:
            command = command or []
            additional_opts = additional_opts or []

            if (isinstance(command, list) or isinstance(command, tuple) and
                    isinstance(additional_opts, list) or isinstance(additional_opts, tuple)):
                run_command_instance = DockerRunBuilder(
                    command=command, additional_opts=additional_opts)
            else:
                raise ConuException("command and additional_opts needs to be list of str or None")
        else:
            run_command_instance = run_command_instance or DockerRunBuilder()
            if not isinstance(run_command_instance, DockerRunBuilder):
                raise ConuException("run_command_instance needs to be an "
                                    "instance of DockerRunBuilder")

        popen_params = popen_params or {}

        run_command_instance.image_name = self.get_id()
        if container_name:
            run_command_instance.options += ["--name", container_name]

        if volumes:
            run_command_instance.options += self.get_volume_options(volumes=volumes)

        def callback():
            return subprocess.Popen(run_command_instance.build(), **popen_params)

        container_id, popen_instance = self._run_container(run_command_instance, callback)

        actual_name = self.d.inspect_container(container_id)['Name'][1:]
        if container_name and container_name != actual_name:
            raise ConuException(
                "Unexpected container name value. Expected = "
                + str(container_name) + " Actual = " + str(actual_name))
        if not container_name:
            container_name = actual_name
        return DockerContainer(
            self, container_id, popen_instance=popen_instance, name=container_name)
コード例 #14
0
    def _exec_in_cont(self, cont: DockerContainer, cmd: str):

        cont.execute(cmd.split(' '), blocking=True)
コード例 #15
0
    def copy_to(self, src: str, dest: str, cont: DockerContainer):

        cont.copy_to(src=src, dest=dest)
コード例 #16
0
ファイル: test_filesystem.py プロジェクト: ficap/conu
 def teardown_class(cls):
     for c in cls.containers_to_remove:
         try:
             DockerContainer(cls.image, c).delete(force=True, volumes=True)
         except NotFound:  # FIXME: implementation leak, we should own exc for this
             pass