|
| 1 | +__author__ = "Vanessa Sochat" |
| 2 | +__copyright__ = "Copyright 2021, Vanessa Sochat" |
| 3 | +__license__ = "MPL 2.0" |
| 4 | + |
| 5 | + |
| 6 | +from shpc.logger import logger |
| 7 | +from .base import ContainerTechnology |
| 8 | +import shpc.main.templates |
| 9 | +import shpc.utils |
| 10 | + |
| 11 | +from datetime import datetime |
| 12 | +import json |
| 13 | +import os |
| 14 | +import sys |
| 15 | + |
| 16 | + |
| 17 | +class DockerContainer(ContainerTechnology): |
| 18 | + """ |
| 19 | + A Docker container controller. |
| 20 | + """ |
| 21 | + |
| 22 | + # The module technology adds extensions here |
| 23 | + templatefile = "docker" |
| 24 | + command = "docker" |
| 25 | + features = {} |
| 26 | + |
| 27 | + def __init__(self): |
| 28 | + if shpc.utils.which("docker")["return_code"] != 0: |
| 29 | + logger.exit("Docker is required to use the 'docker' base.") |
| 30 | + super(DockerContainer, self).__init__() |
| 31 | + |
| 32 | + def shell(self, image): |
| 33 | + """ |
| 34 | + Interactive shell into a container image. |
| 35 | + """ |
| 36 | + os.system( |
| 37 | + "docker run -it --rm --entrypoint %s %s" |
| 38 | + % (self.settings.docker_shell, image) |
| 39 | + ) |
| 40 | + |
| 41 | + def add_registry(self, uri): |
| 42 | + """ |
| 43 | + Given a "naked" name, add the registry if it's Docker Hub |
| 44 | + """ |
| 45 | + # Is this a core library container, or Docker Hub without prefix? |
| 46 | + if uri.count("/") == 0: |
| 47 | + uri = "docker.io/library/%s" % uri |
| 48 | + elif uri.count("/") == 1: |
| 49 | + uri = "docker.io/%s" % uri |
| 50 | + return uri |
| 51 | + |
| 52 | + def registry_pull(self, module_dir, container_dir, config, tag): |
| 53 | + """ |
| 54 | + Pull a container to the library. |
| 55 | + """ |
| 56 | + pull_type = "docker" if getattr(config, "docker") else "gh" |
| 57 | + if pull_type != "docker": |
| 58 | + logger.exit("%s only supports Docker (oci registry) pulls." % self.command) |
| 59 | + |
| 60 | + # Podman doesn't keep a record of digest->tag, so we use tag |
| 61 | + uri = "%s:%s" % (self.add_registry(config.docker), tag.name) |
| 62 | + return self.pull(uri) |
| 63 | + |
| 64 | + def pull(self, uri): |
| 65 | + """ |
| 66 | + Pull a unique resource identifier. |
| 67 | + """ |
| 68 | + res = shpc.utils.run_command([self.command, "pull", uri], stream=True) |
| 69 | + if res["return_code"] != 0: |
| 70 | + logger.exit("There was an issue pulling %s" % uri) |
| 71 | + return uri |
| 72 | + |
| 73 | + def inspect(self, image): |
| 74 | + """ |
| 75 | + Inspect an image |
| 76 | + """ |
| 77 | + res = shpc.utils.run_command([self.command, "inspect", image]) |
| 78 | + if res["return_code"] != 0: |
| 79 | + logger.exit("There was an issue getting the manifest for %s" % image) |
| 80 | + raw = res["message"] |
| 81 | + return json.loads(raw) |
| 82 | + |
| 83 | + def exists(self, image): |
| 84 | + """ |
| 85 | + Exists is a derivative of inspect that just determines existence. |
| 86 | + """ |
| 87 | + if not image: |
| 88 | + return False |
| 89 | + res = shpc.utils.run_command([self.command, "inspect", image]) |
| 90 | + if res["return_code"] != 0: |
| 91 | + return False |
| 92 | + return True |
| 93 | + |
| 94 | + def get(self, module_name): |
| 95 | + """ |
| 96 | + Determine if a container uri exists. |
| 97 | + """ |
| 98 | + # If no module tag provided, try to deduce from install tree |
| 99 | + module_name = self.guess_tag(module_name) |
| 100 | + |
| 101 | + uri = self.add_registry(module_name) |
| 102 | + # If there isn't a tag in the name, add it back |
| 103 | + if ":" not in uri: |
| 104 | + uri = ":".join(uri.rsplit("/", 1)) |
| 105 | + if uri and self.exists(uri): |
| 106 | + return uri |
| 107 | + |
| 108 | + def delete(self, image): |
| 109 | + """ |
| 110 | + Delete a container when a module is deleted. |
| 111 | + """ |
| 112 | + image = self.get(image) |
| 113 | + if self.exists(image): |
| 114 | + shpc.utils.run_command([self.command, "rmi", image]) |
| 115 | + |
| 116 | + def check(self, module_name, config): |
| 117 | + """ |
| 118 | + Given a module name, check if it's the latest version. |
| 119 | + """ |
| 120 | + # Case 1: a specific tag is selected |
| 121 | + image = self.get(module_name) |
| 122 | + if self.exists(image): |
| 123 | + tag = image.split(":")[-1] |
| 124 | + if tag == config.latest.name: |
| 125 | + logger.info("⭐️ tag %s is up to date. ⭐️" % config.tag.name) |
| 126 | + else: |
| 127 | + logger.exit( |
| 128 | + "👉️ tag %s can be updated to %s! 👈️" |
| 129 | + % (module_name, config.latest.name) |
| 130 | + ) |
| 131 | + |
| 132 | + def install( |
| 133 | + self, |
| 134 | + module_path, |
| 135 | + container_path, |
| 136 | + name, |
| 137 | + template, |
| 138 | + parsed_name, |
| 139 | + aliases=None, |
| 140 | + url=None, |
| 141 | + description=None, |
| 142 | + version=None, |
| 143 | + config_features=None, |
| 144 | + features=None, |
| 145 | + ): |
| 146 | + """Install a general container path to a module |
| 147 | +
|
| 148 | + The module_dir should be created by the calling function, and |
| 149 | + the container should already be added to the module directory. In |
| 150 | + the case of install this means we did a pull from a registry, |
| 151 | + and for add we moved the container there explicitly. |
| 152 | + """ |
| 153 | + # Container features are defined in container.yaml and the settings |
| 154 | + # and specific values are determined by the container technology |
| 155 | + features = self.get_features( |
| 156 | + config_features, self.settings.container_features, features |
| 157 | + ) |
| 158 | + |
| 159 | + # Ensure that the container exists |
| 160 | + # Do we want to clean up other versions here too? |
| 161 | + manifest = self.inspect(container_path) |
| 162 | + if not manifest: |
| 163 | + sys.exit("Container %s was not found. Was it pulled?" % container_path) |
| 164 | + |
| 165 | + labels = manifest[0].get("Labels", {}) |
| 166 | + |
| 167 | + # If there's a tag in the name, don't use it |
| 168 | + name = name.split(":", 1)[0] |
| 169 | + |
| 170 | + # Make sure to render all values! |
| 171 | + out = template.render( |
| 172 | + podman_module=self.settings.podman_module, |
| 173 | + bindpaths=self.settings.bindpaths, |
| 174 | + shell=self.settings.podman_shell |
| 175 | + if self.command == "podman" |
| 176 | + else self.settings.docker_shell, |
| 177 | + image=container_path, |
| 178 | + description=description, |
| 179 | + module_dir=os.path.dirname(module_path), |
| 180 | + aliases=aliases, |
| 181 | + url=url, |
| 182 | + features=features, |
| 183 | + version=version, |
| 184 | + labels=labels, |
| 185 | + prefix=self.settings.module_exc_prefix, |
| 186 | + creation_date=datetime.now(), |
| 187 | + name=name, |
| 188 | + tool=parsed_name.tool, |
| 189 | + registry=parsed_name.registry, |
| 190 | + namespace=parsed_name.namespace, |
| 191 | + envfile=self.settings.environment_file, |
| 192 | + command=self.command, |
| 193 | + tty=self.settings.enable_tty, |
| 194 | + ) |
| 195 | + shpc.utils.write_file(module_path, out) |
0 commit comments