|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import tempfile |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import docker |
| 7 | +from docker.types import Mount |
| 8 | +from docker.models.images import Image |
| 9 | + |
| 10 | +from cadetrdm.docker import ContainerAdapter |
| 11 | + |
| 12 | + |
| 13 | +class DockerAdapter(ContainerAdapter): |
| 14 | + |
| 15 | + def __init__(self): |
| 16 | + self.client = docker.from_env() |
| 17 | + self.image = None |
| 18 | + |
| 19 | + def run_case(self, case: "Case", command: str = None): |
| 20 | + |
| 21 | + if case.environment is not None: |
| 22 | + self._update_Dockerfile_with_env_reqs(case) |
| 23 | + |
| 24 | + if self.image is None: |
| 25 | + image = self._build_image(case) |
| 26 | + else: |
| 27 | + image = self.image |
| 28 | + |
| 29 | + container_tmp_filename = "/tmp/options.json" |
| 30 | + options_tmp_filename = self._dump_options(case) |
| 31 | + |
| 32 | + full_command = self._prepare_command( |
| 33 | + case=case, |
| 34 | + command=command, |
| 35 | + container_tmp_filename=container_tmp_filename |
| 36 | + ) |
| 37 | + |
| 38 | + full_log = self._run_command( |
| 39 | + container_tmp_filename=container_tmp_filename, |
| 40 | + full_command=full_command, |
| 41 | + image=image, |
| 42 | + options_tmp_filename=options_tmp_filename |
| 43 | + ) |
| 44 | + |
| 45 | + return full_log |
| 46 | + |
| 47 | + def _run_command(self, container_tmp_filename, full_command, image, options_tmp_filename): |
| 48 | + |
| 49 | + ssh_location = Path.home() / ".ssh" |
| 50 | + if not ssh_location.exists(): |
| 51 | + raise FileNotFoundError("No ssh folder found. Please report this on GitHub/CADET/CADET-RDM") |
| 52 | + |
| 53 | + container = self.client.containers.run( |
| 54 | + image=image, |
| 55 | + command=full_command, |
| 56 | + volumes={ |
| 57 | + f"{Path.home()}/.ssh": {'bind': "/root/.ssh_host_os", 'mode': "ro"}, |
| 58 | + options_tmp_filename.absolute().as_posix(): {'bind': container_tmp_filename, 'mode': 'ro'} |
| 59 | + }, |
| 60 | + detach=True, |
| 61 | + remove=True |
| 62 | + ) |
| 63 | + |
| 64 | + full_log = [] |
| 65 | + # Step 2: Attach to the container's logsu |
| 66 | + for log in container.logs(stream=True): |
| 67 | + full_log.append(log.decode("utf-8")) |
| 68 | + print(log.decode("utf-8"), end="") |
| 69 | + # Wait for the container to finish execution |
| 70 | + container.wait() |
| 71 | + print("Done.") |
| 72 | + |
| 73 | + return full_log |
| 74 | + |
| 75 | + def _prepare_command(self, case, command, container_tmp_filename): |
| 76 | + # ensure ssh in the container knows where to look for known_hosts and that .ssh/config is read-only |
| 77 | + command_ssh = 'cp -r /root/.ssh_host_os /root/.ssh && chmod 600 /root/.ssh/*' |
| 78 | + |
| 79 | + # copy over git config |
| 80 | + git_config_list = subprocess.check_output("git config --list --show-origin --global").decode().split("\n") |
| 81 | + git_config = { |
| 82 | + "user.name": None, |
| 83 | + "user.email": None, |
| 84 | + } |
| 85 | + for line in git_config_list: |
| 86 | + for key in git_config.keys(): |
| 87 | + if key in line: |
| 88 | + value = line.split("=")[-1] |
| 89 | + # print(value) |
| 90 | + git_config[key] = value |
| 91 | + |
| 92 | + git_commands = [f'git config --global {key} "{value}"' for key, value in git_config.items()] |
| 93 | + |
| 94 | + # pull the study from the URL into a "study" folder |
| 95 | + command_pull = f"rdm clone {case.study.url} study" |
| 96 | + # cd into the "study" folder |
| 97 | + command_cd = "cd study" |
| 98 | + # run main.py with the options, assuming main.py lies within a sub-folder with the same name as the study.name |
| 99 | + if command is None: |
| 100 | + command_python = f"python {case.study.name}/main.py {container_tmp_filename}" |
| 101 | + else: |
| 102 | + command_python = command |
| 103 | + |
| 104 | + commands = git_commands + [command_ssh, command_pull, command_cd, command_python] |
| 105 | + full_command = 'bash -c "' + ' && '.join(commands) + '"' |
| 106 | + return full_command |
| 107 | + |
| 108 | + def _dump_options(self, case): |
| 109 | + tmp_filename = Path("tmp/" + next(tempfile._get_candidate_names()) + ".json") |
| 110 | + case.options.dump_json_file(tmp_filename) |
| 111 | + return tmp_filename |
| 112 | + |
| 113 | + def _build_image(self, case) -> Image: |
| 114 | + cwd = os.getcwd() |
| 115 | + with open(case.study.path / "Dockerfile", "rb") as dockerfile: |
| 116 | + os.chdir(case.study.path.as_posix()) |
| 117 | + |
| 118 | + image, logs = self.client.images.build( |
| 119 | + path=case.study.path.as_posix(), |
| 120 | + # fileobj=dockerfile, # A file object to use as the Dockerfile. |
| 121 | + tag=case.study.name + ":" + case.name[:10], # A tag to add to the final image |
| 122 | + quiet=False, # Whether to return the status |
| 123 | + pull=True, # Downloads any updates to the FROM image in Dockerfiles |
| 124 | + |
| 125 | + ) |
| 126 | + if case.options.debug: |
| 127 | + for log in logs: |
| 128 | + print(log) |
| 129 | + os.chdir(cwd) |
| 130 | + return image |
| 131 | + |
| 132 | + def pull_image(self, repository, tag=None, all_tags=False, **kwargs): |
| 133 | + self.image = self.client.images.pull( |
| 134 | + repository=repository, |
| 135 | + tag=tag, |
| 136 | + all_tags=all_tags, |
| 137 | + **kwargs |
| 138 | + ) |
| 139 | + |
| 140 | + def _push_image(self, repository, tag=None, **kwargs): |
| 141 | + self.client.images.push( |
| 142 | + repository=repository, |
| 143 | + tag=tag, |
| 144 | + **kwargs |
| 145 | + ) |
| 146 | + |
| 147 | + def _tag_image(self, image: Image, repository, tag=None, **kwargs) -> Image: |
| 148 | + """ |
| 149 | + Tag this image into a repository. Similar to the ``docker tag`` |
| 150 | + command. |
| 151 | +
|
| 152 | + Args: |
| 153 | + repository (str): The repository to set for the tag |
| 154 | + tag (str): The tag name |
| 155 | + force (bool): Force |
| 156 | +
|
| 157 | + Raises: |
| 158 | + :py:class:`docker.errors.APIError` |
| 159 | + If the server returns an error. |
| 160 | +
|
| 161 | + Returns: |
| 162 | + (bool): ``True`` if successful |
| 163 | + """ |
| 164 | + image.tag(repository=repository, tag=tag, **kwargs) |
| 165 | + return image |
| 166 | + |
| 167 | + def build_and_push_image(self, case, repository, tag=None, **kwargs): |
| 168 | + image = self._build_image(case) |
| 169 | + image = self._tag_image(image, repository, tag, **kwargs) |
| 170 | + self._push_image(repository, tag, **kwargs) |
| 171 | + |
| 172 | + def _update_Dockerfile_with_env_reqs(self, case): |
| 173 | + case.study._reset_hard_to_head(force_entry=True) |
| 174 | + |
| 175 | + dockerfile = Path(case.study.path) / "Dockerfile" |
| 176 | + conda, pip = case.environment.prepare_install_instructions() |
| 177 | + # We need to switch to root to update conda packages and to the $CONDA_USER to update pip packages |
| 178 | + install_command = "\n" |
| 179 | + if len(conda) > 0: |
| 180 | + install_command += f"RUN {conda}\n" |
| 181 | + if len(pip) > 0: |
| 182 | + install_command += f"RUN {pip}\n" |
| 183 | + install_command += f"RUN pip install --force-reinstall --no-deps {pip.split('pip install')[-1]}\n" |
| 184 | + |
| 185 | + with open(dockerfile, "a") as handle: |
| 186 | + handle.write(install_command) |
| 187 | + |
| 188 | + def __del__(self): |
| 189 | + self.client.close() |
0 commit comments