Пример #1
0
def run(args):
    """ perform install subcommand """
    operator_file = args.local_yaml
    if not operator_file:
        file_url = "https://raw.githubusercontent.com/kadalu/kadalu/master/manifests"
        version = ""
        insttype = ""

        if args.version and args.version != "latest":
            version = "-%s" % args.version

            if args.type and args.type == "openshift":
                insttype = "-openshift"

        operator_file = "%s/kadalu-operator%s%s.yaml" % (file_url, insttype,
                                                         version)

    try:
        cmd = [args.kubectl_cmd, "apply", "-f", operator_file]
        print("Executing '%s'" % " ".join(cmd))
        if args.dry_run:
            return

        resp = utils.execute(cmd)
        print("Kadalu operator create request sent successfully")
        print(resp.stdout)
        print()
    except utils.CommandError as err:
        utils.command_error(cmd, err.stderr)
    except FileNotFoundError:
        utils.kubectl_cmd_help(args.kubectl_cmd)
Пример #2
0
def run(args):
    """ perform log subcommand """

    try:

        pods, container = [args.podname], "--all-containers=true"

        if args.container:
            container = '-c' + args.container

        if not args.podname:
            cmd = utils.kubectl_cmd(args) + [
                "get", "pods", "-nkadalu", "-oname"
            ]
            resp = utils.execute(cmd)
            # Remove empty lines(pod-names) from command response
            pods = resp.stdout.split()

        for pod in pods:
            log_cmd = utils.kubectl_cmd(args) + [
                "logs", "-nkadalu", pod, container
            ]
            log_resp = utils.execute(log_cmd)
            print("----- (Kadalu Namespace) %s -----" % pod)
            print(log_resp.stdout)
            print()

    except utils.CommandError as err:
        utils.command_error(cmd, err.stderr)
    except FileNotFoundError:
        utils.kubectl_cmd_help(args.kubectl_cmd)
Пример #3
0
def request_pv_delete(args):
    """ Send PVC delete request to CSI"""

    cmd = utils.kubectl_cmd(args) + [
        "exec", "-it", "kadalu-csi-provisioner-0", "-c", "kadalu-provisioner",
        "-nkadalu", "--", "bash", "-c",
        "cd /kadalu; python3 remove_archived_pv.py %s" % (args.name)
    ]

    if args.pvc:
        cmd[-1] = cmd[-1] + " --pvc=%s" % (args.pvc)

    try:
        resp = utils.execute(cmd)
        print("Sent request for deletion of archived PVCs")
        return resp

    except utils.CommandError as err:
        print("Failed to request deletion of archived pvc of the "
              "storage \"%s\"" % args.name,
              file=sys.stderr)
        print(err, file=sys.stderr)
        print()
        return None
    except FileNotFoundError:
        utils.kubectl_cmd_help(args.kubectl_cmd)
        return None
Пример #4
0
def run(args):
    """ perform install subcommand """

    # Check if kadalu operator is present already.
    try:
        cmd = utils.kubectl_cmd(args) + [
            "get", "deployments", "-nkadalu", "-ojson"
        ]
        resp = utils.execute(cmd)
        data = json.loads(resp.stdout)
        items = data["items"]

        if items:
            operator = items[0]["metadata"].get("name")
            namespace = items[0]["metadata"].get("namespace")

            if operator is not None and namespace is not None:
                print("Kadalu operator already installed")
                return

    except utils.CommandError as err:
        utils.command_error(cmd, err.stderr)
    except FileNotFoundError:
        utils.kubectl_cmd_help(args.kubectl_cmd)

    operator_file = args.local_yaml
    if not operator_file:
        file_url = ""
        insttype = ""

        if args.version and args.version == "devel":
            file_url = "https://raw.githubusercontent.com/kadalu/kadalu/devel/manifests"
        elif args.version:
            file_url = "https://github.com/kadalu/kadalu/releases/download/%s" % args.version

        if args.type and args.type != "kubernetes":
            insttype = "-%s" % args.type

        operator_file = "%s/kadalu-operator%s.yaml" % (file_url, insttype)

    try:
        cmd = utils.kubectl_cmd(args) + ["apply", "-f", operator_file]
        print("Executing '%s'" % " ".join(cmd))
        if args.dry_run:
            return

        resp = utils.execute(cmd)
        print("Kadalu operator create request sent successfully")
        print(resp.stdout)
        print()
    except utils.CommandError as err:
        utils.command_error(cmd, err.stderr)
    except FileNotFoundError:
        utils.kubectl_cmd_help(args.kubectl_cmd)
Пример #5
0
def run(args):
    """ perform log subcommand """

    try:
        cmd = utils.kubectl_cmd(args) + [
            "exec", "-nkadalu", "kadalu-csi-provisioner-0", "-c",
            "kadalu-provisioner", "--", "/kadalu/heal-info.sh"
        ]
        resp = utils.execute(cmd)
        print(resp.stdout)
        print()

    except utils.CommandError as err:
        utils.command_error(cmd, err.stderr)
    except FileNotFoundError:
        utils.kubectl_cmd_help(args.kubectl_cmd)
Пример #6
0
def run(args):
    """ Adds the subcommand arguments back to main CLI tool """

    yaml_content = Template(YAML_TEMPLATE).substitute(name=args.name)

    print("Storage Yaml file for your reference:\n")
    print(yaml_content)

    if args.dry_run:
        return

    if not args.script_mode:
        answer = ""
        valid_answers = ["yes", "no", "n", "y"]

        while answer not in valid_answers:
            answer = input("Is this correct?(Yes/No): ")
            answer = answer.strip().lower()

        if answer in ["n", "no"]:
            return

    config, tempfile_path = tempfile.mkstemp(prefix="kadalu")
    try:
        with os.fdopen(config, 'w') as tmp:
            tmp.write(yaml_content)

        cmd = utils.kubectl_cmd(args) + ["delete", "-f", tempfile_path]
        resp = utils.execute(cmd)
        print("Storage delete request sent successfully.\n")
        print(resp.stdout)
        print()

    except utils.CommandError as err:
        os.remove(tempfile_path)
        utils.command_error(cmd, err.stderr)

    except FileNotFoundError:
        os.remove(tempfile_path)
        utils.kubectl_cmd_help(args.kubectl_cmd)

    finally:
        if os.path.exists(tempfile_path):
            os.remove(tempfile_path)
Пример #7
0
def get_kube_nodes(args):
    """ gets all nodes  """
    if args.dry_run:
        return []

    cmd = [args.kubectl_cmd, "get", "nodes", "-ojson"]
    try:
        resp = utils.execute(cmd)
        data = json.loads(resp.stdout)
        nodes = []
        for nodedata in data["items"]:
            nodes.append(nodedata["metadata"]["name"])

        print("The following nodes are available:\n  %s" % ", ".join(nodes))
        print()
        return nodes
    except utils.CommandError as err:
        utils.command_error(cmd, err.stderr)
    except FileNotFoundError:
        utils.kubectl_cmd_help(args.kubectl_cmd)
Пример #8
0
def run(args):
    """Shows List of Storages"""
    cmd = [args.kubectl_cmd, "get", "configmap",
           "kadalu-info", "-nkadalu", "-ojson"]

    try:
        resp = utils.execute(cmd)
        storages = list_storages(resp.stdout, args)
        if args.status:
            if not fetch_status(storages, args):
                sys.exit(1)

        if args.detail:
            detailed_output(storages, args)
        else:
            summary_output(storages, args)
    except utils.CommandError as err:
        utils.command_error(cmd, err.stderr)
    except FileNotFoundError:
        utils.kubectl_cmd_help(args.kubectl_cmd)
Пример #9
0
def fetch_status(storages, args):
    """Updates the Status details to input Object"""
    for storage in storages:
        if args.name is not None and args.name != storage.storage_name:
            continue

        dbpath = "/bricks/" + storage.storage_name + "/data/brick/stat.db"
        query = ("select size from summary;"
                 "select count(pvname), sum(size), min(size), "
                 "avg(size), max(size) from pv_stats")

        cmd = [
            args.kubectl_cmd, "exec", "-it",
            storage.storage_units[0].podname,
            "-c", "glusterfsd", "-nkadalu", "sqlite3",
            dbpath,
            query
        ]

        try:
            resp = utils.execute(cmd)
            parts = resp.stdout.strip().split("\n")
            storage.total_size_bytes = float(parts[0].strip())
            # num_pvs|used_size|min_pv_size|avg_pv_size|max_pv_size
            pv_stats_parts = parts[1].strip().split("|")
            storage.pv_count = int(pv_stats_parts[0])
            storage.used_size_bytes = float(pv_stats_parts[1])
            storage.min_pv_size = float(pv_stats_parts[2])
            storage.avg_pv_size = float(pv_stats_parts[3])
            storage.max_pv_size = float(pv_stats_parts[4])
        except utils.CommandError as err:
            print("Failed to get size details of the "
                  "storage \"%s\"" % storage.storage_name,
                  file=sys.stderr)
            print(err, file=sys.stderr)
            print()
        except FileNotFoundError:
            utils.kubectl_cmd_help(args.kubectl_cmd)

    return True