Exemplo n.º 1
0
    def _clean_status_container(self, status):
        targets = []
        for container in self.client.containers(all=True):
            if container["Status"].startswith(status):
                # Sanitize
                if container["Names"] is None:
                    container["Names"] = ["NO_NAME"]
                targets.append(container)

        if len(targets) == 0:
            print "No containers %s found." % (status.lower())
            return

        # Display available elements
        print "%d containers %s founds." % (len(targets), status.lower())
        ligs = [["NAME", "IMAGE", "COMMAND"]]
        ligs += [[",".join(c["Names"]).replace("/", ""), c["Image"], c["Command"]]
                 for c in targets]
        Utils.print_table(ligs)

        if Utils.ask("Remove some of them", default="N"):
            for container in targets:
                if Utils.ask(
                        "\tRemove %s" % container["Names"][0].replace("/", ""),
                        default="N"):
                    # Force is false to avoid bad moves
                    print "\t-> Removing %s..." % container["Id"][:10]
                    self.client.remove_container(container["Id"], v=False,
                                                 link=False,
                                                 force=False)
Exemplo n.º 2
0
    def run(self):
        # Format inputs
        reg_from = self.get_registryaddr("from")
        reg_to = self.get_registryaddr("to")
        imgsrc = self.args.IMGSRC
        if ":" not in imgsrc:
            imgsrc += ":latest"
        imgdst = self.args.IMGDST if (self.args.IMGDST != '-') else imgsrc
        if ":" not in imgdst:
            imgdst += ":latest"
        isrc = reg_from + imgsrc
        idst = reg_to + imgdst

        # Confirm transfer
        if not Utils.ask("Transfer %s -> %s" % (isrc, idst)):
            return

        # Search for source image avaibility
        isrc_id = None
        for img in self.client.images():
            if isrc in img["RepoTags"]:
                isrc_id = img["Id"]
                print "'%s' is locally available (%s), use it" % (isrc, img["Id"][:10])

        # Source image is not available, pull it
        if isrc_id is None:
            if not Utils.ask("'%s' is not locally available, try to pull it" % isrc):
                return
            # Try to pull Image without creds
            res = self.client.pull(isrc, insecure_registry=True)
            if "error" in res:
                print "An error as occurred (DEBUG: %s)" % res
                return
            print "'%s' successfully pulled !" % isrc

            raise NotImplementedError("Get image id")

        # Tag the element
        idst, idst_tag = ":".join(idst.split(":")[:-1]), idst.split(":")[-1]
        self.client.tag(isrc_id, idst, tag=idst_tag, force=False)

        # Push the element, insecure mode
        print "Pushing..."
        for status in self.client.push(idst, tag=idst_tag, stream=True,
                                       insecure_registry=True):
            sys.stdout.write("\r" + json.loads(status)["status"])
            sys.stdout.flush()
        print "\nTransfer complete !"
Exemplo n.º 3
0
    def clean_creds(self):
        credentials = docker.auth.load_config()
        if len(credentials.keys()) == 0:
            print "No credential founded."
            return

        print "%d identities founds: " % len(credentials.keys())
        for address, creds in credentials.items():
            print "\t- %s@%s" % (creds["username"], address)

        changed = False
        if Utils.ask("Remove some of them", default="N"):
            for address, creds in credentials.items():
                if Utils.ask(
                        "\tRemove %s@%s" % (creds["username"], address),
                        default="N"):
                    del credentials[address]
                    changed = True

        if changed:
            ActionClean.dump_config(credentials)
Exemplo n.º 4
0
    def clean_images_none(self):
        images = self.client.images()
        nones = []
        for img in images:
            if len(img["RepoTags"]) == 1 and img["RepoTags"][0] == "<none>:<none>":
                nones.append(img)

        if len(nones) == 0:
            print "No unamed images found."
            return

        print "%d images unammed ('<none>:<none>') founds." % len(nones)
        if Utils.ask("Remove them", default="N"):
            for img in nones:
                print "Removing %s..." % img["Id"][:10]
                # Force is false to avoid bad moves
                self.client.remove_image(img["Id"], force=False, noprune=False)