Beispiel #1
0
    def delete_image(self):
        """
        Mark given image(s) for deletion from registry
        :return: 0 if all images marked for deletion, otherwise 2 on any failure
        """
        if self.args.debug:
            util.write_out(str(self.args))

        beu = BackendUtils()
        # Ensure the input values match up first
        delete_objects = []

        # We need to decide on new returns for dbus because we now check image
        # validity prior to executing the delete.  If there is going to be a
        # failure, it will be here.
        #
        # The failure here is basically that it couldnt verify/find the image.

        for image in self.args.delete_targets:
            be, img_obj = beu.get_backend_and_image(image, str_preferred_backend=self.args.storage)
            delete_objects.append((be, img_obj))

        if self.args.remote:
            return self._delete_remote(self.args.delete_targets)

        max_img_name = max([len(x.input_name) for _, x in delete_objects]) + 2

        if not self.args.assumeyes:
            util.write_out("Do you wish to delete the following images?\n")
            two_col = "   {0:" + str(max_img_name) + "} {1}"
            util.write_out(two_col.format("IMAGE", "STORAGE"))
            for del_obj in delete_objects:
                be, img_obj = del_obj
                util.write_out(two_col.format(img_obj.input_name, be.backend))
            confirm = util.input("\nConfirm (y/N) ")
            confirm = confirm.strip().lower()
            if not confirm in ['y', 'yes']:
                util.write_err("User aborted delete operation for {}".format(self.args.delete_targets))
                sys.exit(2)

        # Perform the delete
        for del_obj in delete_objects:
            be, img_obj = del_obj
            be.delete_image(img_obj.input_name, force=self.args.force)

        # We need to return something here for dbus
        return
Beispiel #2
0
class Info(Atomic):
    def __init__(self):
        super(Info, self).__init__()
        self.beu = BackendUtils()

    def version(self):
        self._version(util.write_out)

    def _version(self, write_func):
        layer_objects = self.get_layer_objects()
        max_version_len = max([len(x.long_version) for x in layer_objects])
        max_version_len = max_version_len if max_version_len > 9 else 9
        max_img_len = len(
            max([y for x in layer_objects for y in x.repotags], key=len)) + 9
        max_img_len = max_img_len if max_img_len > 12 else 12
        col_out = "{0:" + str(max_img_len) + "} {1:" + str(
            max_version_len) + "} {2:10}"

        write_func(col_out.format("IMAGE NAME", "VERSION", "IMAGE ID"))
        for layer in layer_objects:
            for int_img_name in range(len(layer.repotags)):
                version = layer.long_version if int_img_name < 1 else ""
                iid = layer.id[:12] if int_img_name < 1 else ""
                space = "" if int_img_name < 1 else "  Tag: "
                write_func(
                    col_out.format(space + layer.repotags[int_img_name],
                                   version, iid))
                write_func("")

    def get_layer_objects(self):
        _, img_obj = self.beu.get_backend_and_image(
            self.image, str_preferred_backend=self.args.storage)
        return img_obj.layers

    def dbus_version(self):
        layer_objects = self.get_layer_objects()
        versions = []
        for layer in layer_objects:
            versions.append({
                "Image": layer.repotags,
                "Version": layer.long_version,
                "iid": layer.id
            })
        return versions

    def info_tty(self):
        if self.args.debug:
            util.write_out(str(self.args))
        util.write_out(self.info())

    def info(self):
        """
        Retrieve and print all LABEL information for a given image.
        """

        if self.args.storage == 'ostree' and self.args.force:
            # Ostree and remote combos are illegal
            raise ValueError(
                "The --remote option cannot be used with the 'ostree' storage option."
            )

        if self.args.force:
            # The user wants information on a remote image
            be = self.beu.get_backend_from_string(self.args.storage)
            img_obj = be.make_remote_image(self.image)
        else:
            # The image is local
            be, img_obj = self.beu.get_backend_and_image(
                self.image, str_preferred_backend=self.args.storage)

        with closing(StringIO()) as buf:
            try:
                info_name = img_obj.fq_name
            except RegistryInspectError:
                info_name = img_obj.input_name
            buf.write("Image Name: {}\n".format(info_name))
            buf.writelines(
                sorted([
                    "{}: {}\n".format(k, v)
                    for k, v in list(img_obj.labels.items())
                ]))
            if img_obj.template_variables_set:
                buf.write(
                    "\n\nTemplate variables with default value, but overridable with --set:\n"
                )
                buf.writelines([
                    "{}: {}\n".format(k, v) for k, v in list(
                        sorted(img_obj.template_variables_set.items()))
                ])
            if img_obj.template_variables_unset:
                buf.write(
                    "\n\nTemplate variables that has no default value, and must be set with --set:\n"
                )
                buf.writelines([
                    "{}: {}\n".format(k, v) for k, v in list(
                        sorted(img_obj.template_variables_unset.items()))
                ])
            return buf.getvalue()
Beispiel #3
0
class Info(Atomic):
    def __init__(self):
        super(Info, self).__init__()
        self.beu = BackendUtils()

    def version(self):
        self._version(util.write_out)

    def _version(self, write_func):
        layer_objects = self.get_layer_objects()
        max_version_len = max([len(x.long_version) for x in layer_objects])
        max_version_len = max_version_len if max_version_len > 9 else 9
        max_img_len = len(max([y for x in layer_objects for y in x.repotags], key=len)) + 9
        max_img_len = max_img_len if max_img_len > 12 else 12
        col_out = "{0:" + str(max_img_len) + "} {1:" + str(max_version_len) + "} {2:10}"

        write_func(col_out.format("IMAGE NAME", "VERSION", "IMAGE ID"))
        for layer in layer_objects:
            for int_img_name in range(len(layer.repotags)):
                version = layer.long_version if int_img_name < 1 else ""
                iid = layer.id[:12] if int_img_name < 1 else ""
                space = "" if int_img_name < 1 else "  Tag: "
                write_func(col_out.format(space + layer.repotags[int_img_name], version, iid))
                write_func("")

    def get_layer_objects(self):
        _, img_obj = self.beu.get_backend_and_image(self.image, str_preferred_backend=self.args.storage)
        return img_obj.layers

    def dbus_version(self):
        layer_objects = self.get_layer_objects()
        versions = []
        for layer in layer_objects:
            versions.append({"Image": layer.repotags, "Version": layer.long_version, "iid": layer.id})
        return versions

    def info_tty(self):
        if self.args.debug:
            util.write_out(str(self.args))
        util.write_out(self.info())

    def info(self):
        """
        Retrieve and print all LABEL information for a given image.
        """

        if self.args.storage == 'ostree' and self.args.force:
            # Ostree and remote combos are illegal
            raise ValueError("The --remote option cannot be used with the 'ostree' storage option.")

        if self.args.force:
            # The user wants information on a remote image
            be = self.beu.get_backend_from_string(self.args.storage)
            img_obj = be.make_remote_image(self.image)
        else:
            # The image is local
            be, img_obj = self.beu.get_backend_and_image(self.image, str_preferred_backend=self.args.storage)

        with closing(StringIO()) as buf:
            try:
                info_name = img_obj.fq_name
            except RegistryInspectError:
                info_name = img_obj.input_name
            buf.write("Image Name: {}\n".format(info_name))
            buf.writelines(sorted(["{}: {}\n".format(k, v) for k,v in list(img_obj.labels.items())]))
            if img_obj.template_variables_set:
                buf.write("\n\nTemplate variables with default value, but overridable with --set:\n")
                buf.writelines(["{}: {}\n".format(k, v) for k,v in
                                list(sorted(img_obj.template_variables_set.items()))])
            if img_obj.template_variables_unset:
                buf.write("\n\nTemplate variables that has no default value, and must be set with --set:\n")
                buf.writelines(["{}: {}\n".format(k, v) for k,v in
                                list(sorted(img_obj.template_variables_unset.items()))])
            return buf.getvalue()