D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
opt
/
cloudlinux
/
venv
/
lib
/
python3.11
/
site-packages
/
clpackages
/
Filename :
cl_packages_arg_parse.py
back
Copy
# -*- coding: utf-8 -*- # Command line arguments parser for cloudlinux-packages utility # cloudlinux-packages Utility to control lvectl packages # # Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved # # Licensed under CLOUD LINUX LICENSE AGREEMENT # http://cloudlinux.com/docs/LICENSE.TXT from __future__ import absolute_import from docopt import docopt from docopt import DocoptExit from schema import Schema, And, Or, Use, SchemaError from clcommon.cpapi import is_panel_feature_supported from clcommon.features import Feature PROG_NAME = "cloudlinux-packages" SET_USAGE_NO_LVE = f""" {PROG_NAME} set [--json] (--provider <str> --package <str>) [--inodes <N,M>] {PROG_NAME} set [--json] (--for-reseller <str> --package <str>) [--inodes <N,M>] """.strip('\n ') SET_USAGE_LVE = f""" {PROG_NAME} set [--json] (--provider <str> --package <str>) [--speed <str> --pmem <str> --vmem <str> --nproc <str> --maxEntryProcs <str> --io <str> --iops <str> --inodes <N,M> --mysql-cpu <int> --mysql-io <int>] {PROG_NAME} set [--json] (--for-reseller <str> --package <str>) [--speed <str> --pmem <str> --vmem <str> --nproc <str> --maxEntryProcs <str> --io <str> --iops <str> --inodes <N,M>] """.strip('\n ') USAGE_TEMPLATE = """ Usage: {prog_name} [get] [--json] [--provider <str> | --for-reseller <str>] [--package <str>] [--limits=<keys>] [--human-readable-numbers] {set_usage} {prog_name} (-h | --help) """.strip('\n ') USAGE_LVE = USAGE_TEMPLATE.format(prog_name=PROG_NAME, set_usage=SET_USAGE_LVE) USAGE_NO_LVE = USAGE_TEMPLATE.format(prog_name=PROG_NAME, set_usage=SET_USAGE_NO_LVE) LIMITS_OPTIONS_LVE = """ --speed <str> Limit CPU usage for LVE. --pmem <str> Limit physical memory usage for applications inside LVE. --vmem <str> Limit virtual memory for applications inside LVE. --nproc <str> Limit number of processes for LVE. --maxEntryProcs <str> Limit number of entry processes for LVE. --io <str> Define io limits for LVE (KB/s). --iops <str> Limit io per second for LVE. --mysql-cpu <int> Set MySQL governor CPU limit (pct). --mysql-io <int> Set MySQL governor IO limit (read + write MB/s) """ OPTIONS_TEMPLATE = """ Options: --json Return data in JSON format. --provider <str> Show data only for specific reseller. --for-reseller <str> Show data only for specific reseller. --package <str> Show data only for specific package. Use only with --provider WARNING: package name must be unicode-escaped string --limits <keys> Available keys: speed, nproc, pmem, vmem, maxEntryProcs, io, iops, inodes --human-readable-numbers Return PMEM and VMEM limits in KBytes, MBytes or GBytes {limit_options} --inodes <N,M> Set inode limits. N - soft, M - hard. -h, --help Show this help message and exit """.strip('\n ') LIMITS_OPTIONS_LVE = OPTIONS_TEMPLATE.format(limit_options=LIMITS_OPTIONS_LVE) LIMITS_OPTIONS_NO_LVE = OPTIONS_TEMPLATE.format(limit_options="") DOCSTRING_TEMPLATE = """Utility to get/set any Cloudlinux package limits {usage} {options} """ DOCSTRIG_LVE = DOCSTRING_TEMPLATE.format(usage=USAGE_LVE, options=LIMITS_OPTIONS_LVE) DOCSTRIG_NO_LVE = DOCSTRING_TEMPLATE.format(usage=USAGE_NO_LVE, options=LIMITS_OPTIONS_NO_LVE) def _limits_keys_validate(keys): """ Validate limits keys """ avialable_keys_list = ["inodes"] if is_panel_feature_supported(Feature.LVE): avialable_keys_list += [ "speed", "nproc", "pmem", "vmem", "maxEntryProcs", "io", "iops", "mysql-cpu", "mysql-io" ] return len(set(keys.split(",")) - set(avialable_keys_list)) == 0 SCHEMA_NO_LVE = { "get": bool, "set": bool, "--json": And(bool, lambda x: x, error="use --json option, other modes currently unsupported"), "--provider": Or(None, str), "--for-reseller": Or(None, str), "--package": Or(None, str), "--limits": Or(None, _limits_keys_validate, error="Invalid keys"), "--inodes": Or(None, str), "--help": bool, "--human-readable-numbers": bool, } SCHEMA_LVE = SCHEMA_NO_LVE | { "--speed": Or(None, str), "--pmem": Or(None, str), "--vmem": Or(None, str), "--nproc": Or(None, str), "--maxEntryProcs": Or(None, str), "--io": Or(None, str), "--iops": Or(None, str), "--mysql-cpu": Or(None, And(Use(int), lambda x: x >= 0), error="--mysql-cpu must be non-negative integer value"), "--mysql-io": Or(None, And(Use(int), lambda x: x >= 0), error="--mysql-io must be non-negative integer value"), } def parse_cloudlinux_packages_opts(argv, _is_json_need=False): """ Parse arguments for cloudlinux-packages command :param argv: sys.argv :param _is_json_need: sys.argv contains --json key :return cortege: (error_flag, s_message) """ # program name docstring = DOCSTRIG_LVE if is_panel_feature_supported(Feature.LVE) else DOCSTRIG_NO_LVE try: args = docopt(docstring, argv) except DocoptExit: s_error_string = 'ERROR: Invalid parameter passed' if not _is_json_need: s_error_string += "\n\n" + docstring return False, s_error_string # get mode by default if not args["get"] and not args["set"]: args["get"] = True if args['--for-reseller']: # --for-reseller option found, convert it to --provider args['--provider'] = args['--for-reseller'] schema_dict = SCHEMA_LVE if is_panel_feature_supported(Feature.LVE) else SCHEMA_NO_LVE s = Schema(schema_dict) try: args = s.validate(args) status = True except SchemaError as e: args = str(e) status = False return status, args