Skip to content

Commit

Permalink
Keep the existing interface in microk8s.status (#1042)
Browse files Browse the repository at this point in the history
With this PR we maintain the old CLI interface and out put for microk8s.status and microk8s.status --yaml.

We introduce a --format argument with which you can select a human readable pretty output format (that includes the addon descriptions) or select the new yaml format with yaml

Fixes: #1041 and eclipse-che/che#16408
  • Loading branch information
ktsakalozos authored Mar 20, 2020
1 parent 2c97438 commit 00222cf
Showing 1 changed file with 50 additions and 10 deletions.
60 changes: 50 additions & 10 deletions scripts/wrappers/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,30 @@ def is_enabled(addon, item):
return False


def print_console(isReady, enabled_addons, disabled_addons):
def print_short(isReady, enabled_addons, disabled_addons):
if isReady:
print("microk8s is running")
else:
print("microk8s is not running. Use microk8s.inspect for a deeper inspection.")

if isReady:
print("addons:")
if enabled_addons and len(enabled_addons) > 0:
for enabled in enabled_addons:
print("{}: enabled".format(enabled["name"]))
if disabled_addons and len(disabled_addons) > 0:
for disabled in disabled_addons:
print("{}: disabled".format(disabled["name"]))


def print_pretty(isReady, enabled_addons, disabled_addons):
console_formatter = "{:>1} {:<20} # {}"
if isReady:
print("microk8s is running")
else:
print("microk8s is not running. Use microk8s.inspect for a deeper inspection.")

if isReady:
if isReady:
print("addons:")
if enabled_addons and len(enabled_addons) > 0:
print('{:>2}'.format("enabled:"))
Expand All @@ -36,6 +52,23 @@ def print_console(isReady, enabled_addons, disabled_addons):
print(console_formatter.format("", disabled["name"], disabled["description"]))


def print_short_yaml(isReady, enabled_addons, disabled_addons):
print("microk8s:")
print("{:>2} {} {}".format("", "running:", isReady))

if not isReady:
print("{:>2} {} {}".format("","message:","microk8s is not running. Use microk8s.inspect for a deeper inspection."))
return

if isReady:
print("addons:")
for enabled in enabled_addons:
print(" {}: enabled".format(enabled["name"]))

for disabled in disabled_addons:
print(" {}: disabled".format(disabled["name"]))


def print_yaml(isReady, enabled_addons, disabled_addons):
print("microk8s:")
print("{:>2} {} {}".format("", "running:", isReady))
Expand All @@ -44,7 +77,7 @@ def print_yaml(isReady, enabled_addons, disabled_addons):
print("{:>2} {} {}".format("","message:","microk8s is not running. Use microk8s.inspect for a deeper inspection."))
return

if isReady:
if isReady:
print("{:>2}".format("addons:"))
for enabled in enabled_addons:
print("{:>4} name: {:<1}".format("-", enabled["name"]))
Expand All @@ -66,7 +99,7 @@ def print_addon_status(enabled):
print ("disabled")


def get_status(available_addons, isReady):
def get_status(available_addons, isReady):
enabled = []
disabled = []
if isReady:
Expand All @@ -92,19 +125,21 @@ def get_status(available_addons, isReady):

# initiate the parser with a description
parser = argparse.ArgumentParser(description='Microk8s cluster status check.', prog='microk8s.status')
parser.add_argument("-o", "--output", help="print cluster and addon status, output can be in yaml or console",
default="console", choices={"console", "yaml"})
parser.add_argument("-w", "--wait-ready", help="wait until the cluster is in ready state", nargs='?', const=True,
type=bool)
parser.add_argument("--format", help="print cluster and addon status, output can be in yaml, pretty or short",
default="short", choices={"pretty", "yaml", "short"})
parser.add_argument("-w", "--wait-ready", action='store_true', help="wait until the cluster is in ready state")
parser.add_argument("-t", "--timeout",
help="specify a timeout in seconds when waiting for the cluster to be ready.", type=int,
default=0)
parser.add_argument("-a", "--addon", help="check the status of an addon.", default="all")
parser.add_argument("--yaml", action='store_true', help="DEPRECATED, use '--format yaml' instead")

# read arguments from the command line
args = parser.parse_args()

wait_ready = args.wait_ready
timeout = args.timeout
yaml_short = args.yaml

if wait_ready:
isReady = wait_for_ready(wait_ready, timeout)
Expand All @@ -121,7 +156,12 @@ def get_status(available_addons, isReady):
if args.addon != "all":
print_addon_status(enabled)
else:
if args.output == "yaml":
if args.format == "yaml":
print_yaml(isReady, enabled, disabled)
elif args.format == "pretty":
print_pretty(isReady, enabled, disabled)
else:
print_console(isReady, enabled, disabled)
if yaml_short:
print_short_yaml(isReady, enabled, disabled)
else:
print_short(isReady, enabled, disabled)

0 comments on commit 00222cf

Please sign in to comment.