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)
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)
def get_configmap_data(args): """ Get storage info data from kadalu configmap """ cmd = utils.kubectl_cmd(args) + ["get", "configmap", "kadalu-info", "-nkadalu", "-ojson"] try: resp = utils.execute(cmd) config_data = json.loads(resp.stdout) volname = args.name data = config_data['data'] storage_name = "%s.info" % volname storage_info_data = data[storage_name] # Return data in 'dict' format return json.loads(storage_info_data) except utils.CommandError as err: utils.command_error(cmd, err.stderr) return None except KeyError: # Validate method expects None when 'storage' not found. return None
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
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)
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)
def run(args): """Shows List of Storages""" cmd = utils.kubectl_cmd(args) + [ "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)
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 = "/mnt/" + storage.storage_name + "/stat.db" query = ("select size from summary;" "select count(pvname), sum(size), min(size), " "avg(size), max(size) from pv_stats") cmd = utils.kubectl_cmd(args) + [ "exec", "-it", "-nkadalu", "kadalu-csi-provisioner-0", "-c", "kadalu-provisioner", "--", "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]) if storage.pv_count: 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
def get_kube_nodes(args): """ gets all nodes """ if args.dry_run: return [] cmd = utils.kubectl_cmd(args) + ["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) return None except FileNotFoundError: utils.kubectl_cmd_help(args.kubectl_cmd) return None