def flask():
    docker = _docker.from_env()
    docker_image_name = 'kaskara/examples:flask'
    docker_directory = os.path.join(DIR_HERE, 'examples/flask')

    # build the Docker image
    docker.images.build(path=docker_directory, tag=docker_image_name, rm=True)

    with _dockerblade.DockerDaemon() as dockerblade:
        files = {'flask/signals.py', 'flask/helpers.py'}
        yield kaskara.Project(dockerblade=dockerblade,
                              image=docker_image_name,
                              directory='/opt/flask/src',
                              files=files)
def test_persist(daemon):
    with ExitStack() as exit_stack:
        daemon = dockerblade.DockerDaemon()
        exit_stack.push(daemon)

        container = daemon.provision('alpine:3.10')
        exit_stack.callback(container.remove)

        files = container.filesystem()
        files.write('/tmp/foo', 'hello world')

        image_id = container.persist()
        try:
            assert daemon.client.images.get(image_id)
        finally:
            daemon.client.images.remove(image_id)
Exemple #3
0
    def __init__(
        self,
        *,
        workspace: t.Optional[str] = None,
        docker_url: str = _DEFAULT_URL,
    ) -> None:
        if not workspace:
            logger.debug("no workspace specified: using default workspace.")
            dir_home = os.path.expanduser("~")
            workspace = os.path.join(dir_home, ".roswire")
            logger.debug(f"default workspace: {workspace}")
            if not os.path.exists(workspace):
                logger.debug("initialising default workspace")
                os.mkdir(workspace)
        else:
            logger.debug(f"using specified workspace: {workspace}")
            if not os.path.exists(workspace):
                m = f"workspace not found: {workspace}"
                raise ROSWireException(m)

        self.__workspace = os.path.abspath(workspace)
        self._dockerblade = dockerblade.DockerDaemon(docker_url)
Exemple #4
0
 def __attrs_post_init__(self) -> None:
     self.dockerblade = dockerblade.DockerDaemon(self.docker_url)
Exemple #5
0
class Daikon(DockerTool):
    client: dockerblade.DockerDaemon = \
        attr.ib(default=dockerblade.DockerDaemon())
    IMAGE = 'specminers/daikon'
    _DOCKER_DIRECTORY = os.path.dirname(
        pkg_resources.resource_filename(__name__, 'Dockerfile'))  # noqa

    def __call__(self, *filenames: str) -> str:
        """Executes the Daikon binary.

        Raises
        ------
        ValueError
            If no filenames are provided as input.
        FileNotFoundError
            If a given input file cannot be found.
        RuntimeError
            If the image for the tool has not been installed.
        """
        if not self.is_installed():
            message = f'image for tool is not installed [{self.IMAGE}]'
            raise RuntimeError(message)

        logger.debug(f"running Daikon on files: {', '.join(filenames)}")
        if not filenames:
            raise ValueError('expected one or more filenames as input')

        # ensure that all paths are absolute
        filenames = tuple(os.path.abspath(filename) for filename in filenames)

        for filename in filenames:
            if not os.path.isfile(filename):
                raise FileNotFoundError(filename)

        ctr_dir = '/tmp/.specminers'
        host_to_ctr_fn = {
            fn: os.path.join(ctr_dir, os.path.basename(fn))
            for fn in filenames
        }
        ctr_filenames = [fn for fn in host_to_ctr_fn.values()]
        volumes = {
            fn_host: {
                'bind': fn_ctr,
                'mode': 'ro'
            }
            for fn_host, fn_ctr in host_to_ctr_fn.items()
        }

        # launch container
        with contextlib.ExitStack() as stack:
            container = self.client.provision(self.IMAGE, volumes=volumes)
            shell = container.shell('/bin/sh')
            stack.callback(container.remove)

            # generate invariants
            command = ('java daikon.Daikon '
                       '--no_show_progress --no_text_output --noversion '
                       '-o /tmp/mined.inv.tgz '
                       f"{' '.join(shlex.quote(f) for f in ctr_filenames)}")
            shell.check_call(command)

            # read invariants
            command = 'java daikon.PrintInvariants /tmp/mined.inv.tgz'
            output = shell.check_output(command)

        logger.debug(f"daikon output:\n{output}")
        return output
def daemon():
    with dockerblade.DockerDaemon() as daemon:
        yield daemon