def acquire(self, artifact, deps, tools): raise_task_error_if(not self._user(tools), self, "Username has not been configured") raise_task_error_if(not self._password(tools), self, "Password has not been configured") with tools.cwd(tools.builddir()): tools.write_file("docker-credential", self._password(tools)) tools.run("cat docker-credential | docker login -u {user} --password-stdin {server}", user=self._user(tools))
def acquire(self, artifact, deps, tools): try: image = deps[self.image] image = str(image.strings.tag) except Exception: image = tools.expand(self.image) self._info(f"Creating container from image '{image}'") self.container = tools.run( "docker run -i -d {_user} {_environment} {_volumes} {image} {_arguments}", image=image, output_on_error=True) self._info("Created container '{container}'") info = tools.run("docker inspect {container}", output_on_error=True) artifact.container = self.container artifact.info = json.loads(info)[0]
def acquire(self, artifact, deps, tools, owner): self.joltcachedir = config.get_cachedir() try: image = deps[self.image] image = str(image.strings.tag) except Exception: image = tools.expand(self.image) self._info(f"Creating container from image '{image}'") self.container = tools.run( "docker run -i -d {_cap_adds} {_cap_drops} {_entrypoint} {_labels} {_ports} {_privileged} {_user} {_environment} {_volumes} {image} {_arguments}", image=image, output_on_error=True) self._info("Created container '{container}'") info = tools.run("docker inspect {container}", output_on_error=True) artifact.container = self.container artifact.info = json.loads(info)[0]
def run(self, deps, tools): context = tools.expand_relpath(self.context, self.joltdir) dockerfile = tools.expand_path(self.dockerfile) self._imagefile = tools.expand( self.imagefile) if self.imagefile else None self._autoload = self._imagefile and self.autoload self.tags = [self.tools.expand(tag) for tag in self.tags] pull = " --pull" if self.pull else "" squash = " --squash" if self.squash else "" # If dockerfile is not relative to joltdir, look for it in context if not path.exists(dockerfile): with tools.cwd(context): dockerfile = tools.expand_path(self.dockerfile) if not path.exists(dockerfile): with tools.cwd(tools.builddir()): tools.write_file("Dockerfile", self.dockerfile) dockerfile = tools.expand_path("Dockerfile") self.info("Building image from {} in {}", tools.expand_relpath(dockerfile), tools.expand_relpath(context)) with tools.cwd(context): tools.run( "docker build {_platform} . -f {} {_buildargs} {_labels} {_tags} {pull}{squash}", utils.quote(dockerfile), pull=pull, squash=squash) try: if self.push: self.info("Pushing image") for tag in self.tags: tools.run("docker push {}", tag) if self._imagefile or self.extract: self.info("Saving image to file") with tools.cwd(tools.builddir()): tools.run("docker image save {} -o {}", self.tags[0], self._imagefile or "image.tar") if self.extract: with tools.cwd(tools.builddir()): tools.extract(self._imagefile or "image.tar", "layers/") manifest = json.loads( tools.read_file("layers/manifest.json")) for image in manifest: for layer in image.get("Layers", []): self.info("Extracting layer {}", fs.path.dirname(layer)) self._extract_layer(tools, fs.path.join("layers", layer), "rootfs/") elif self._imagefile: with tools.cwd(tools.builddir()): if self.compression is not None: tools.compress("{_imagefile}", "{_imagefile}.{compression}") finally: if self.cleanup: self.info("Removing image from Docker daemon") for tag in self.tags: utils.call_and_catch(tools.run("docker image rm {}", tag))
def release(self, artifact, deps, tools, owner): tools.run("docker logout {server}")
def release(self, artifact, deps, tools, owner): self._info("Stopping container '{container}'") tools.run("docker stop {container}", output_on_error=True) self._info("Deleting container '{container}'") tools.run("docker rm {container}", output_on_error=True)
def run(self, deps, tools): buildargs = " ".join(["--build-arg " + tools.expand(arg) for arg in self.buildargs]) context = tools.expand_relpath(self.context, self.joltdir) dockerfile = tools.expand_path(self.dockerfile) self._imagefile = tools.expand(self.imagefile) if self.imagefile else None self._autoload = self._imagefile and self.autoload pull = " --pull" if self.pull else "" tags = [tools.expand(tag) for tag in self.tags] # If dockerfile is not relative to joltdir, look for it in context if not path.exists(dockerfile): with tools.cwd(context): dockerfile = tools.expand_path(self.dockerfile) if not path.exists(dockerfile): with tools.cwd(tools.builddir()): tools.write_file("Dockerfile", self.dockerfile) dockerfile = tools.expand_path("Dockerfile") self.info("Building image from {} in {}", tools.expand_relpath(dockerfile), tools.expand_relpath(context)) with tools.cwd(context): tools.run("docker build . -f {} -t {} {}{}", dockerfile, tags[0], buildargs, pull) for tag in tags[1:]: tools.run("docker tag {} {}", tags[0], tag) try: if self.push: self.info("Pushing image") for tag in tags: tools.run("docker push {}", tag) if self.extract: self.info("Extracting image") tools.run("docker create --name {canonical_name}.{identity} {}", tags[0]) try: with tools.cwd(tools.builddir("rootfs")): tools.run("docker export {canonical_name}.{identity} -o rootfs.tar") tools.extract("rootfs.tar", "rootfs/") finally: tools.run("docker rm {canonical_name}.{identity}") if self._imagefile: self.info("Saving image to file") with tools.cwd(tools.builddir()): tools.run("docker image save {} -o {_imagefile}", tags[0]) if self.compression is not None: tools.compress("{_imagefile}", "{_imagefile}.{compression}") finally: if self.cleanup: self.info("Removing image from Docker daemon") for tag in tags: utils.call_and_catch(tools.run("docker image rm {}", tag))