clusterman/clusterman/__main__.py
2023-10-27 12:37:05 +02:00

78 lines
2.6 KiB
Python

import argparse,sys
from clusterman.config import *
from clusterman.commands import node, plugins, frontend
def main():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="target", help='Target')
##### Node commands #####
target_node = subparsers.add_parser("node")
node_subparsers=target_node.add_subparsers(dest="command", help='Command')
# Scan
node_cmd_scan=node_subparsers.add_parser("scan")
node_cmd_scan.add_argument("-t", "--timeout" ,help="Timeout", type=float)
# Check
node_cmd_scan=node_subparsers.add_parser("check")
node_cmd_scan.add_argument("-t", "--timeout" ,help="Timeout", type=float)
# List
node_cmd_list=node_subparsers.add_parser("list")
node_cmd_list.add_argument("-g", "--group" ,help="Group to list")
# Exec
node_cmd_list=node_subparsers.add_parser("exec")
node_cmd_list.add_argument("cmd",help="Command to run",nargs=argparse.REMAINDER)
##### Frontend commands #####
target_frontend = subparsers.add_parser("frontend")
frontend_subparsers=target_frontend.add_subparsers(dest="command", help='Command')
# Info
node_cmd_scan=frontend_subparsers.add_parser("info")
##### Plugins commands #####
target_plugins = subparsers.add_parser("plugins")
target_plugins.add_argument("name", help="Plugin's name")
target_plugins.add_argument("parameters", help="Plugin's parameters",nargs=argparse.REMAINDER)
# Check if command specified:
if len(sys.argv)==1:
parser.print_help(sys.stderr)
sys.exit(1)
# Parse arguments:
args = parser.parse_args()
# Run the proper handler
if args.target == "node":
if args.command == "scan":
if args.timeout:
node.scan(node_cmd_scan.timeout)
else:
node.scan(CONF["timeout"])
elif args.command == "check":
if args.timeout:
node.check(node_cmd_scan.timeout)
else:
node.check(CONF["timeout"])
elif args.command == "list":
if args.group:
node.ls(args.group)
else:
node.ls()
elif args.command == "exec":
node.exec(args.cmd)
else:
target_node.print_help(sys.stderr)
sys.exit(1)
elif args.target == "frontend":
if args.command == "info":
frontend.info()
else:
target_frontend.print_help(sys.stderr)
sys.exit(1)
elif args.target == "plugins":
plugins.execute(args.name,args.parameters)