Example #1
0
    def parse(self, value, kic_path=None):
        value = value[5:]

        if not value:
            return {}

        value = value.strip('"')
        target = os.path.join(self.kindo_tmps_path, "{}.ki".format(str(uuid.uuid4())))
        if value[:7] == "http://" or value[:8] == "https://" and value[-3:] == ".ki":
            _, image_name = os.path.split(value)
            download_with_progressbar(value, target)
        elif value[-3:] == ".ki":
            _, image_name = os.path.split(value)
            target = os.path.realpath(value)
            if not os.path.isfile(target):
                kic_folder = os.path.dirname(kic_path)
                target = os.path.join(kic_folder, value)
        elif ":" in value and "/" in value:
            name, version = value.split(":") if ":"in value else (value, "")
            author, name = name.split("/") if "/" in name else ("", name)

            isok, res = self.api.pull(self.get_kindo_setting("username") if not author else author, name, version)
            if isok:
                download_with_progressbar(value, target)
                image_name = "{0}.ki".format(res["name"].replace("/", "-").replace(":", "-"))

        if not os.path.isfile(target):
            return {}

        return {
            "action": "FROM",
            "args": {"url": target, "name": image_name},
            "files": [],
            "images": [{"url": target, "name": image_name}]
        }
Example #2
0
    def get_kic_info(self, option):
        if option[:7].lower() == "http://" or option[:8].lower() == "https://":
            urlinfo = urlparse.urlparse(option)
            filename = os.path.split(urlinfo[1])[1]
            target = os.path.join(self.kindo_tmps_path, filename)

            download_with_progressbar(option, target)
            if os.path.isfile(target):
                return target, os.path.realpath(self.configs.get("o", self.startfolder))
            return None, None

        kic_maybe_paths = [
            option,
            os.path.join(self.startfolder, option),
            os.path.join(self.kindo_kics_path, option)
        ]

        for kic_maybe_path in kic_maybe_paths:
            if os.path.isfile(kic_maybe_path):
                kic_output_dir = self.configs.get("o", os.path.dirname(kic_maybe_path))
                if os.path.isfile(kic_output_dir):
                    if kic_output_dir[-3:].lower() != ".ki":
                        return kic_maybe_path, None

                if kic_output_dir[-3:].lower() == ".ki":
                    kic_output_dir = os.path.dirname(kic_output_dir)
                return kic_maybe_path, os.path.realpath(kic_output_dir)

        return None, None
Example #3
0
    def run(self, ssh_client, command, filesdir, imagesdir, cd, envs, ki_path=None):
        target = os.path.realpath(command["args"]["to"])

        if command["args"]["from"][:7].lower() != "http://" and command["args"]["from"][:8].lower() != "https://":
            if not ssh_client.get(command["args"]["from"], target):
                raise Exception("{0} download failed".format(command["args"]["from"]))
        else:
            download_with_progressbar(command["args"]["from"], target)
            if not os.path.isfile(target):
                raise Exception("{0} download failed".format(command["args"]["from"]))
        return cd, envs
Example #4
0
    def move_images_to_build_folder(self, images, kic_build_folder):
        image_names = []
        for image in images:
            if "name" not in image or "url" not in image:
                continue

            target = os.path.join(kic_build_folder, "images", image["name"])
            download_with_progressbar(image["url"], target)

            if not os.path.isfile(target):
                raise Exception("failed to download {0}".format(image["name"]))

            image_names.append(image["name"])
        return image_names
Example #5
0
    def download_package(self, image_info):
        url = image_info["url"]
        name = image_info["name"]

        self.logger.debug("downloading %s" % name)

        kiname = name.replace("/", "-").replace(":", "-")
        kiname = kiname if name[-3:] == ".ki" else "%s.ki" % kiname
        target = os.path.join(self.kindo_images_path, kiname)

        if os.path.isfile(target):
            return target

        self.logger.debug(url)

        if not os.path.isdir(self.kindo_images_path):
            os.makedirs(self.kindo_images_path)

        download_with_progressbar(url, target)
        return target
Example #6
0
    def move_files_to_build_folder(self, files, kic_build_folder, kic_path):
        file_names = []

        for f in files:
            if "name" not in f or "url" not in f:
                continue

            target = os.path.join(kic_build_folder, "files", f["name"])
            if f["url"][:7] == "http://" or f["url"][:8] == "https://":
                if not os.path.isfile(target):
                    download_with_progressbar(f["url"], target)
            elif not os.path.isfile(f["url"]):
                f["url"] = os.path.join(os.path.dirname(kic_path), f["url"])

                if not os.path.isfile(f["url"]):
                    raise Exception("{0} not found".format(f["url"]))

            if not os.path.isfile(target):
                shutil.copy(f["url"],  target)

            file_names.append(f["name"])
        return file_names
Example #7
0
    def download_image(self, image_info):
        url = image_info["url"]
        name = image_info["name"]

        self.logger.debug("downloading %s from %s" % (name, url))

        kiname = name.replace("/", "-").replace(":", "-")
        kiname = kiname if name[-3:] == ".ki" else "%s.ki" % kiname
        target = os.path.join(self.kindo_images_path, kiname)

        if os.path.isfile(target):
            self.logger.debug("%s existed, removing" % target)
            os.remove(target)

        if not os.path.isdir(self.kindo_images_path):
            os.makedirs(self.kindo_images_path)

        try:
            download_with_progressbar(url, target)
        except:
            raise Exception("\"%s\" can't be downloaded" % url)
        return target
Example #8
0
    def download_package(self, image_info):
        url = image_info["url"]
        name = image_info["name"]

        self.logger.debug("downloading %s from %s" % (name, url))

        kiname = name.replace("/", "-").replace(":", "-")
        kiname = kiname if name[-3:] == ".ki" else "%s.ki" % kiname
        target = os.path.join(self.kindo_images_path, kiname)

        try:
            if os.path.isfile(target):
                self.logger.debug("%s existed, removing" % target)
                os.remove(target)

            if not os.path.isdir(self.kindo_images_path):
                os.makedirs(self.kindo_images_path)

            download_with_progressbar(url, target)

            return target
        except:
            self.logger.debug(traceback.format_exc())
        return ""
Example #9
0
    def run(self, ssh_client, command, filesdir, imagesdir, cd, envs, ki_path=None):
        args = []

        if isinstance(command["args"], dict):
            args = [command["args"]]
        elif isinstance(command["args"], list):
            args = command["args"]

        files_info = []
        for arg in args:
            src = arg["from"]
            if "http://" in src or "https://" in src:
                urlinfo = urlparse.urlparse(src)
                filename = os.path.split(urlinfo["path"])[1]
                file_name, file_ext = os.path.splitext(filename)

                downloads_tmp_folder = os.path.join(self.kindo_tmps_path, "downloads")
                if not os.path.isdir(downloads_tmp_folder):
                    os.makedirs(downloads_tmp_folder)

                # create unique tmpfile
                src = os.path.join(downloads_tmp_folder, "%s%s" % (get_md5(arg["from"]), file_ext))
                if not os.path.isfile(src):
                    download_with_progressbar(arg["from"], src)
            elif not os.path.isdir(src) and not os.path.isfile(src):
                src = os.path.join(self.startfolder, arg["from"])

            if (
                not os.path.exists(src) and
                "http://" not in arg["from"] and
                "https://" not in arg["from"]
            ):
                ki_folder = os.path.dirname(ki_path)
                src = os.path.join(ki_folder, arg["from"])

            if os.path.isfile(src):
                if arg["to"][-1] == "/":
                    filedir, filename = os.path.split(src)
                    files_info.append({
                        "from": src,
                        "to": "%s%s" % (arg["to"], filename)
                    })
                elif len(args) > 2:
                    filedir, filename = os.path.split(src)
                    files_info.append({
                        "from": src,
                        "to": "%s/%s" % (arg["to"], filename)
                    })
                else:
                    files_info.append({
                        "from": src,
                        "to": arg["to"]
                    })
            elif os.path.isdir(src):
                files = get_files_info(src)

                for f in files:
                    if arg["to"][-1] == "/":
                        files_info.append({
                            "from": f["url"],
                            "to": "%s%s" % (arg["to"], f["name"])
                        })
                    elif len(files) > 1:
                        files_info.append({
                            "from": f["url"],
                            "to": "%s/%s" % (arg["to"], f["name"])
                        })
                    else:
                        files_info.append({
                            "from": f["url"],
                            "to": arg["to"]
                        })

        for file_info in files_info:
            if len(ssh_client.put(file_info["from"], file_info["to"])) == 0:
                raise Exception("{0} upload failed".format(f))
        return cd, envs