Beispiel #1
0
    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))
Beispiel #2
0
    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))
Beispiel #3
0
 def publish(self, artifact, tools):
     artifact.strings.tag = tools.expand(self.tags[0])
     if self._imagefile:
         with tools.cwd(tools.builddir()):
             if self.compression is not None:
                 artifact.collect("{_imagefile}.{compression}")
                 if self._autoload:
                     artifact.docker.load.append("{_imagefile}.{compression}")
             else:
                 artifact.collect("{_imagefile}")
                 if self._autoload:
                     artifact.docker.load.append("{_imagefile}")
     if self.extract:
         with tools.cwd(tools.builddir("rootfs")):
             artifact.collect("rootfs", symlinks=True)
         artifact.paths.rootfs = "rootfs"
     if self._autoload:
         artifact.docker.rmi.append(artifact.strings.tag.get_value())
Beispiel #4
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))
Beispiel #5
0
 def publish(self, artifact, tools):
     with tools.cwd(self._builddir):
         artifact.collect("docker/docker")
     artifact.environ.PATH.append("docker")