Beispiel #1
0
    def test_instance_cmds(self, docker_container):
        image = docker_container[1]
        myinstance = Client.instance(image)
        assert myinstance.get_uri().startswith("instance://")

        print("...Case 2: List instances")
        instances = Client.instances()
        assert len(instances) == 1
        instances = Client.instances(return_json=True)
        assert len(instances) == 1
        assert isinstance(instances[0], dict)

        print("...Case 3: Commands to instances")
        result = Client.execute(myinstance, ["echo", "hello"])
        assert result == "hello\n"

        print("...Case 4: Return value from instance")
        result = Client.execute(myinstance, "ls /", return_result=True)
        print(result)
        assert "tmp\nusr\nvar" in result["message"]
        assert result["return_code"] == 0

        print("...Case 5: Stop instances")
        myinstance.stop()
        instances = Client.instances()
        assert instances == []
        myinstance1 = Client.instance(image)
        myinstance2 = Client.instance(image)
        assert myinstance1 is not None
        assert myinstance2 is not None
        instances = Client.instances()
        assert len(instances) == 2
        Client.instance_stopall()
        instances = Client.instances()
        assert instances == []
Beispiel #2
0
    def test_instance_cmds(self, docker_container):
        image = docker_container[1]
        myinstance = Client.instance(image)
        assert myinstance.get_uri().startswith('instance://')

        print("...Case 2: List instances")
        instances = Client.instances()
        assert len(instances) == 1
        instances = Client.instances(return_json=True)
        assert len(instances) == 1
        assert isinstance(instances[0], dict)

        print("...Case 3: Commands to instances")
        result = Client.execute(myinstance, ['echo', 'hello'])
        assert result == 'hello\n'

        print('...Case 4: Return value from instance')
        result = Client.execute(myinstance, 'ls /', return_result=True)
        print(result)
        assert 'tmp\nusr\nvar' in result['message']
        assert result['return_code'] == 0

        print("...Case 5: Stop instances")
        myinstance.stop()
        instances = Client.instances()
        assert instances == []
        myinstance1 = Client.instance(image)
        myinstance2 = Client.instance(image)
        assert myinstance1 is not None
        assert myinstance2 is not None
        instances = Client.instances()
        assert len(instances) == 2
        Client.instance_stopall()
        instances = Client.instances()
        assert instances == []
Beispiel #3
0
def test_instance_class():
    instance = Client.instance("docker://ubuntu", start=False)
    assert instance.get_uri() == "instance://" + instance.name
    assert instance.name != ""

    name = "coolName"
    instance = Client.instance("docker://busybox:1.30.1",
                               start=False,
                               name=name)
    assert instance.get_uri() == "instance://" + instance.name
    assert instance.name == name
Beispiel #4
0
def test_instance_class():
    instance = Client.instance('docker://ubuntu', start=False)
    assert instance.get_uri() == 'instance://' + instance.name
    assert instance.name != ''

    name = 'coolName'
    instance = Client.instance('docker://busybox:1.30.1',
                               start=False,
                               name=name)
    assert instance.get_uri() == 'instance://' + instance.name
    assert instance.name == name
Beispiel #5
0
def get_instance(instanceDict):
    try:
        t_ins = Client.instance(image=instanceDict["image"],
                                name=instanceDict["ins_name"],
                                options=instanceDict["options"])
        return t_ins
    except Exception as e:
        logging.error(e)
        return None
Beispiel #6
0
    def __init__(self, mode, container_image, volumes, extra_kwargs):
        assert mode in ('docker', 'singularity')
        self.mode = mode

        if mode == 'docker':
            import docker
            client = docker.from_env()
            if extra_kwargs.get('requires_gpu', False):
                extra_kwargs.pop('requires_gpu')
                extra_kwargs["device_requests"] = [
                    docker.types.DeviceRequest(count=-1,
                                               capabilities=[['gpu']])
                ]

            # check if the image is already present locally
            repo_tags = []
            for image in client.images.list():
                repo_tags.extend(image.attrs['RepoTags'])

            if container_image not in repo_tags:
                client.images.pull(container_image)

            self.docker_container = client.containers.create(container_image,
                                                             tty=True,
                                                             volumes=volumes,
                                                             **extra_kwargs)

        elif mode == 'singularity':
            from spython.main import Client
            # load local image file if it exists, otherwise search dockerhub
            if Path(container_image).exists():
                self.singularity_image = container_image
            else:
                self.singularity_image = Client.pull(
                    f'docker://{container_image}')

            if not Path(self.singularity_image).exists():
                raise FileNotFoundError(
                    f'Unable to locate container image {container_image}')

            # bin options
            singularity_bind = ','.join([
                f'{volume_src}:{volume["bind"]}'
                for volume_src, volume in volumes.items()
            ])
            options = ['--bind', singularity_bind]

            # gpu options
            if extra_kwargs.get('requires_gpu', False):
                # only nvidia at the moment
                options += ['--nv']

            self.client_instance = Client.instance(self.singularity_image,
                                                   start=False,
                                                   options=options)
def test_AFNI_libraries():
    SINGULARITY_IMAGE_PATH = '/home/circleci/project/C-PAC-CI.simg'
    if not os.path.exists(SINGULARITY_IMAGE_PATH):
        try:
            SINGULARITY_IMAGE_PATH = [
                d for d in os.listdir(str(Path(__file__).parent.parent.parent))
                if (d.endswith('.simg') or d.endswith('.sif'))
            ][0]
        except:
            raise Exception("Singularity image not in expected location.")
    if os.path.exists(SINGULARITY_IMAGE_PATH):
        afni_libraries = Client.execute(
            Client.instance(SINGULARITY_IMAGE_PATH),
            ['./dev/circleci_data/AFNI_libraries.sh'])
        assert "not found" not in afni_libraries, '\n'.join([
            line.strip() for line in afni_libraries.split('\n')
            if "not found" in line
        ])
Beispiel #8
0
 def _try_to_stream(self, args, stream_command='run'):
     self._bindings_as_option()
     try:
         if stream_command == 'run':
             for line in Client.run(Client.instance(self.image),
                                    args=args,
                                    options=self.options,
                                    stream=True,
                                    return_result=True):
                 yield line
         elif stream_command == 'execute':
             for line in Client.execute(self.image,
                                        command=args['command'].split(' '),
                                        options=self.options,
                                        stream=True,
                                        quiet=False):
                 yield line
     except CalledProcessError:  # pragma: no cover
         return