ok

Mini Shell

Direktori : /opt/cloudlinux/venv/lib/python3.11/site-packages/xray/console_utils/
Upload File :
Current File : //opt/cloudlinux/venv/lib/python3.11/site-packages/xray/console_utils/cmdline_parser.py

# -*- coding: utf-8 -*-

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2021 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

"""
This module contains an argparse command line parser
for cloudlinux-xray-manager
"""
import argparse
import logging

from xray import gettext as _
from xray.internal.utils import read_sys_id
from xray.internal.constants import advice_action_sources


def get_system_id():
    try:
        return read_sys_id()
    except Exception as e:
        logging.error('Unable to get system_id, error: %s', str(e))
        return ''


def cmd_parser_manager() -> 'argparse.ArgumentParser':
    """
    Command line parser for X Ray Manager using built-in argparse module
    :return: parser
    """
    parser = argparse.ArgumentParser(prog='cloudlinux-xray-manager',
                                     description='Utility to manage X-Ray Client')
    subparsers = parser.add_subparsers(title='Commands', dest='command',
                                       required=True)

    start_parser = subparsers.add_parser('start',
                                         help=_('Start monitoring of URL by IP for COUNT of minutes or requests'),
                                         argument_default=argparse.SUPPRESS)
    start_parser.add_argument('--system_id', metavar='SYS_ID', required=True,
                              help=_('unique ID of CL+ system'))
    start_parser.add_argument('--url', metavar='URL', required=True,
                              help=_('monitoring URL'))
    start_parser.add_argument('--client_ip', metavar='IP', default='*',
                              help=_('client IP to monitor requests from'))
    counters = start_parser.add_mutually_exclusive_group()
    counters.add_argument('--time', metavar='COUNT', type=int,
                          help=_('number of minutes to monitor'))
    counters.add_argument('--request_qty', metavar='COUNT', type=int,
                          help=_('number of requests to monitor'))

    stop_parser = subparsers.add_parser('stop',
                                        help=_('Stop monitoring of given TASK'))
    stop_parser.add_argument('--system_id', metavar='SYS_ID', required=True,
                             help=_('unique ID of CL+ system'))
    stop_parser.add_argument('--tracing_task_id', metavar='TASK',
                             required=True, help=_('unique ID of tracing task'))

    continue_parser = subparsers.add_parser('continue',
                                            help=_('Continue monitoring of given TASK'))
    continue_parser.add_argument('--system_id', metavar='SYS_ID',
                                 required=True,
                                 help=_('unique ID of CL+ system'))
    continue_parser.add_argument('--tracing_task_id', metavar='TASK',
                                 required=True,
                                 help=_('unique ID of tracing task'))

    complete_parser = subparsers.add_parser('complete',
                                            help=_('Complete given TASK'))
    complete_parser.add_argument('--system_id', metavar='SYS_ID',
                                 required=True,
                                 help=_('unique ID of CL+ system'))
    complete_parser.add_argument('--tracing_task_id', metavar='TASK',
                                 required=True,
                                 help=_('unique ID of tracing task'))

    delete_parser = subparsers.add_parser('delete',
                                          help=_('Delete given TASK'))
    delete_parser.add_argument('--system_id', metavar='SYS_ID',
                               required=True,
                               help=_('unique ID of CL+ system'))
    delete_parser.add_argument('--tracing_task_id', metavar='TASK',
                               required=True,
                               help=_('unique ID of tracing task'))

    enable_parser = subparsers.add_parser('enable-continuous',
                                          help=_('Schedule a URL for continuous monitoring'))
    enable_parser.add_argument('--system_id', metavar='SYS_ID',
                               required=True,
                               help=_('unique ID of CL+ system'))
    enable_parser.add_argument('--url', metavar='URL', required=True,
                               help=_('monitoring URL'))
    enable_parser.add_argument('--email', metavar='EMAIL', required=True,
                               help=_('e-mail address for reporting'))

    disable_parser = subparsers.add_parser('disable-continuous',
                                           help=_('Schedule to stop continuous monitoring for URL'))
    disable_parser.add_argument('--system_id', metavar='SYS_ID',
                                required=True,
                                help=_('unique ID of CL+ system'))
    disable_parser.add_argument('--url', metavar='URL', required=True,
                                help=_('monitoring URL'))

    con_start_parser = subparsers.add_parser('start-continuous',
                                             help=_('Include URL into continuous monitoring'))
    con_start_parser.add_argument('--system_id', metavar='SYS_ID',
                                  required=True,
                                  help=_('unique ID of CL+ system'))
    con_start_parser.add_argument('--url', metavar='URL', required=True,
                                  help=_('monitoring URL'))

    con_stop_parser = subparsers.add_parser('stop-continuous',
                                            help=_('Exclude URL from continuous monitoring'))
    con_stop_parser.add_argument('--system_id', metavar='SYS_ID',
                                 required=True,
                                 help=_('unique ID of CL+ system'))
    con_stop_parser.add_argument('--url', metavar='URL', required=True,
                                 help=_('monitoring URL'))

    list_parser = subparsers.add_parser('continuous-tracing-list',
                                        help=_('Get list of scheduled continuous monitoring tasks'))
    list_parser.add_argument('--system_id', metavar='SYS_ID',
                             required=True,
                             help=_('unique ID of CL+ system'))

    task_list_parser = subparsers.add_parser('tasks-list',
                                             help=_('Get list of tasks'))
    task_list_parser.add_argument('--system_id', metavar='SYS_ID',
                                  required=True,
                                  help=_('unique ID of CL+ system'))

    req_parser = subparsers.add_parser('requests-list',
                                       help=_('Get list of requests for given task'))
    req_parser.add_argument('--system_id', metavar='SYS_ID',
                            required=True,
                            help=_('unique ID of CL+ system'))
    req_parser.add_argument('--tracing_task_id', metavar='TASK',
                            required=True,
                            help=_('unique ID of tracing task'))

    req_data_parser = subparsers.add_parser('request-data',
                                            help=_('Get statistics for given request'))
    req_data_parser.add_argument('--system_id', metavar='SYS_ID',
                                 required=True,
                                 help=_('unique ID of CL+ system'))
    req_data_parser.add_argument('--tracing_task_id', metavar='TASK',
                                 required=True,
                                 help=_('unique ID of tracing task'))
    req_data_parser.add_argument('--request_id', metavar='REQUEST',
                                 required=True, type=int,
                                 help=_('unique ID of request'))

    enable_user_agent_parser = subparsers.add_parser('enable-user-agent',
                                                     help=_('Enable X-Ray User Agent'))
    enable_user_agent_parser.add_argument('--system_id', metavar='SYS_ID',
                                          default=get_system_id(),
                                          help=_('unique ID of CL+ system'))

    disable_user_agent_parser = subparsers.add_parser('disable-user-agent',
                                                      help=_('Disable X-Ray User Agent'))
    disable_user_agent_parser.add_argument('--system_id', metavar='SYS_ID',
                                           default=get_system_id(),
                                           help=_('unique ID of CL+ system'))

    status_user_agent_parser = subparsers.add_parser('user-agent-status',
                                                     help=_('Get status of X-Ray User Agent'))
    status_user_agent_parser.add_argument('--system_id', metavar='SYS_ID',
                                          required=True,
                                          help=_('unique ID of CL+ system'))

    autocomplete_parser = subparsers.add_parser('autocomplete-tasks',
                                                help=_('Completes all tasks run more than expected time'))
    autocomplete_parser.add_argument('--system_id', metavar='SYS_ID', default=get_system_id(),
                                     help=_('unique ID of CL+ system'))

    advanced_metrics_parser = subparsers.add_parser('advanced-metrics',
                                                   help=_('Advanced metrics tool'))
    advanced_metrics_parser.add_argument('--system_id', metavar='SYS_ID', default=get_system_id(),
                                        help=_('unique ID of CL+ system'))
    advanced_metrics_parser_group = advanced_metrics_parser.add_mutually_exclusive_group()
    advanced_metrics_parser_group.add_argument('--enable', action='store_true', required=False,
                                              help=_('enable advanced metrics'))
    advanced_metrics_parser_group.add_argument('--disable', action='store_true', required=False,
                                              help=_('disable advanced metrics'))
    advanced_metrics_parser_group.add_argument('--status', action='store_true', required=False,
                                              help=_('get status (none|enabled|disabled)'))

    enable_serverwide_mode_parser = subparsers.add_parser('enable-serverwide-mode')
    enable_serverwide_mode_parser.add_argument('--system_id', metavar='SYS_ID', default=get_system_id(),
                  help=_('unique ID of CL+ system'))
    disable_serverwide_mode_parser = subparsers.add_parser('disable-serverwide-mode')
    disable_serverwide_mode_parser.add_argument('--system_id', metavar='SYS_ID', default=get_system_id(),
                  help=_('unique ID of CL+ system'))

    return parser


def cmd_parser_user_manager() -> 'argparse.ArgumentParser':
    """
    Command line parser for X Ray Manager for users using built-in argparse module
    :return: parser
    """
    parser = argparse.ArgumentParser(prog='cloudlinux-xray-user-manager.py',
                                     description='Utility to manage X-Ray Client for end-users')
    subparsers = parser.add_subparsers(title='Commands', dest='command',
                                       required=True)

    start_parser = subparsers.add_parser('start',
                                         help=_('Start monitoring of URL by IP for COUNT of minutes or requests'),
                                         argument_default=argparse.SUPPRESS)
    start_parser.add_argument('--url', metavar='URL', required=True,
                              help=_('monitoring URL'))
    start_parser.add_argument('--client_ip', metavar='IP', default='*',
                              help=_('client IP to monitor requests from'))
    counters = start_parser.add_mutually_exclusive_group()
    counters.add_argument('--time', metavar='COUNT', type=int,
                          help=_('number of minutes to monitor'))
    counters.add_argument('--request_qty', metavar='COUNT', type=int,
                          help=_('number of requests to monitor'))

    stop_parser = subparsers.add_parser('stop',
                                        help=_('Stop monitoring of given TASK'))
    stop_parser.add_argument('--tracing_task_id', metavar='TASK',
                             required=True, help=_('unique ID of tracing task'))

    continue_parser = subparsers.add_parser('continue',
                                            help=_('Continue monitoring of given TASK'))
    continue_parser.add_argument('--tracing_task_id', metavar='TASK',
                                 required=True,
                                 help=_('unique ID of tracing task'))

    complete_parser = subparsers.add_parser('complete',
                                            help=_('Complete given TASK'))
    complete_parser.add_argument('--tracing_task_id', metavar='TASK',
                                 required=True,
                                 help=_('unique ID of tracing task'))

    delete_parser = subparsers.add_parser('delete',
                                          help=_('Delete given TASK'))
    delete_parser.add_argument('--tracing_task_id', metavar='TASK',
                               required=True,
                               help=_('unique ID of tracing task'))

    subparsers.add_parser('tasks-list',
                          help=_('Get list of tasks'))

    req_parser = subparsers.add_parser('requests-list',
                                       help=_('Get list of requests for given task'))
    req_parser.add_argument('--tracing_task_id', metavar='TASK',
                            required=True,
                            help=_('unique ID of tracing task'))

    req_data_parser = subparsers.add_parser('request-data',
                                            help=_('Get statistics for given request'))
    req_data_parser.add_argument('--tracing_task_id', metavar='TASK',
                                 required=True,
                                 help=_('unique ID of tracing task'))
    req_data_parser.add_argument('--request_id', metavar='REQUEST',
                                 required=True, type=int,
                                 help=_('unique ID of request'))

    subparsers.add_parser('user-agent-status',
                          help=_('Get status of X-Ray User Agent'))

    return parser


def cmd_parser_adviser() -> 'argparse.ArgumentParser':
    """
    Command line parser for X Ray Smart Advice utility
    using built-in argparse module
    :return: parser
    """
    parser = argparse.ArgumentParser(prog='cl-smart-advice',
                                     description='Utility for X-Ray Smart Advice microservice interaction')
    parser.add_argument('--api-version', help=_('Specify version of CLI, if not passed - latest will be used by default'),
                        required=False, default=None)
    subparsers = parser.add_subparsers(title='Commands', dest='command',
                                       required=True)

    list_parser = subparsers.add_parser('list',
                                        help=_('Get list of advice'))
    list_parser.add_argument('--extends', default=False, action='store_true',
                             help=_('Advice will be with extended info'))

    detail_parser = subparsers.add_parser('details',
                                          help=_('Get details for a particular advice'))
    detail_parser.add_argument('--advice_id', required=True,
                               help=_('an ID of advice to get details for'))

    apply_parser = subparsers.add_parser('apply',
                                         help=_('Apply a particular advice'))
    apply_parser.add_argument('--advice_id', required=True,
                              help=_('an ID of advice to apply'))
    apply_parser.add_argument('--ignore-errors', action="store_true",
                              default=False, help=_('apply advice anyway'))
    apply_parser.add_argument('--async', action="store_true", dest='async_mode',
                              default=False, help=_('run apply in a background process'))
    apply_parser.add_argument('--source', default='ACCELERATE_WP', choices=advice_action_sources,
                              help=_('Advice apply source'))
    apply_parser.add_argument('--accept_license_terms', default=False, action='store_true',
                              help=argparse.SUPPRESS)
    apply_parser.add_argument('--analytics_data', type=str, help=_('User analytics data'))

    rollback_parser = subparsers.add_parser('rollback', help=_('Rollback a particular advice'))
    rollback_parser.add_argument('--advice_id', required=True, help=_('an ID of advice to rollback'))
    rollback_parser.add_argument('--async', action="store_true", dest='async_mode',
                                 default=False, help=_('run rollback in a background process'))
    rollback_parser.add_argument('--source', default='ACCELERATE_WP', choices=advice_action_sources,
                                 help=_('Advice rollback source'))
    rollback_parser.add_argument('--reason', type=str, help=_('Reason for advice rollback'))
    rollback_parser.add_argument('--analytics_data', type=str, help=_('User analytics data'))

    subparsers.add_parser('counters',
                          help=_('Get advice counters total/applied'))

    status_parser = subparsers.add_parser('status',
                                          help=_('Get current status and progress for a particular advice'))
    status_parser.add_argument('--advice_id', required=True,
                               help=_('an ID of advice to get status for'))

    sites_parser = subparsers.add_parser('sites-status', help=_('Get websites statuses'))
    sites_parser.add_argument('--username', help=_('an username to get statuses info'))

    get_options_parser = subparsers.add_parser('get-options', help=_('Get options for user'))
    get_options_parser.add_argument('--username', help=_('Username to get options'))

    get_limits_parser = subparsers.add_parser('get-limits', help=_('Get limits for user'))
    get_limits_parser.add_argument('--username', help=_('Username to get limits'))

    get_usage_parser = subparsers.add_parser('get-usage', help=_('Get usage for user'))
    get_usage_parser.add_argument('--username', help=_('Username to get usage'))

    advice_plugin_install = subparsers.add_parser(
        'wordpress-plugin-install',
        help=_('Installs/uninstalls cl-smart-advice plugin for websites according to current advice list'))

    advice_plugin_uninstall = subparsers.add_parser(
        'wordpress-plugin-uninstall',
        help=_('Uninstalls cl-smart-advice plugin for websites'))

    subparsers.add_parser('update-advices-metadata', help=_('Sends actual metadata to microservice'))

    get_pullzone = subparsers.add_parser('awp-cdn-get-pullzone', help=_('Get pullzone for account`s website'))
    get_pullzone.add_argument('--username', help=_('Name of target user'))
    get_pullzone.add_argument('--account_id', help=_('Unique account identifier saved in user`s home directory'))
    get_pullzone.add_argument('--domain', help=_('Account`s domain'), required=True)
    get_pullzone.add_argument('--website', help=_('Website'), required=True)

    remove_pullzone = subparsers.add_parser('awp-cdn-remove-pullzone', help=_('Remove pullzone for account`s website'))
    remove_pullzone.add_argument('--username', help=_('Name of target user'))
    remove_pullzone.add_argument('--account_id', help=_('Unique account identifier saved in user`s home directory'))
    remove_pullzone.add_argument('--domain', help=_('Account`s domain'), required=True)
    remove_pullzone.add_argument('--website', help=_('Website'), required=True)

    purge_cdn_cache = subparsers.add_parser('awp-cdn-purge', help=_('Purge cache for account`s website'))
    purge_cdn_cache.add_argument('--username', help=_('Name of target user'))
    purge_cdn_cache.add_argument('--account_id', help=_('Unique account identifier saved in user`s home directory'))
    purge_cdn_cache.add_argument('--domain', help=_('Account`s domain'), required=True)
    purge_cdn_cache.add_argument('--website', help=_('Website'), required=True)

    purge_cdn_cache = subparsers.add_parser('awp-sync', help=_('[for internal usage] Sync account information on microservice'))
    purge_cdn_cache.add_argument('--username', help=_('Name of target user'))
    purge_cdn_cache.add_argument('--account_id',
                                 help=_('Unique account identifier saved in user`s home directory. '
                                        'Multiple could be passed split by ","'))

    get_cdn_usage = subparsers.add_parser('get-cdn-usage', help=_('Get cdn usage for account'))
    get_cdn_usage.add_argument('--account_id', help=_('Unique account identifier saved in user`s home directory'))
    get_cdn_usage.add_argument('--username', help=_('Name of target user'))

    subscription_parser = subparsers.add_parser('subscription',
                                                help=_('Subscription configuration'))
    subscription_parser.add_argument('--advice_id', required=True,
                                     help=_('an ID of advice to manage subscription for'))
    subscription_parser.add_argument('--listen', required=True, action='store_true')

    agreement_parser = subparsers.add_parser('agreement',
                                             help=_('License agreement commands'))
    agreement_parser.add_argument('--text', required=True,
                                  help=_('name of the feature to get license text for'))

    report_analytics = subparsers.add_parser('report-analytics',
                                             help=_('Sends analytics to Smart Advice'))
    report_analytics.add_argument('--feature',
                                  help=_('Feature to be tracked by analytics'),
                                  required=True)
    report_analytics.add_argument('--event',
                                  help=_('Event to be tracked by analytics'),
                                  required=True)
    report_analytics.add_argument('--source',
                                  help=_('Where event was triggered'),
                                  required=True)
    report_analytics.add_argument('--username',
                                  help=_('Users affected by event, pass multiple via "," '),
                                  required=True)
    report_analytics.add_argument('--advice_id',
                                     help=_('ID of advice to manage events')),
    report_analytics.add_argument('--user_hash',
                                     help=_('Hash of user for event'))
    report_analytics.add_argument('--journey_id',
                                     help=_('ID event journey'))
    report_analytics.add_argument('--variant_id',
                                     help=_('ID event variant_id'))

    wp_plugin_data = subparsers.add_parser('wp-plugin-data',
                                           help=_('Get latest WordPress plugin version data'))
    wp_plugin_data.add_argument('--plugin_name', required=True, help=_('Name of the plugin'))

    wp_get_plugin = subparsers.add_parser('wp-plugin-copy',
                                           help=_('Copy the latest WordPress plugin archive to the site if it\'s not already installed'))
    wp_get_plugin.add_argument('--plugin_name', required=True, help=_('Name of the plugin'))
    wp_get_plugin.add_argument('--plugin_version', required=True, help=_('Current plugin version'))
    wp_get_plugin.add_argument('--tmp_dir', required=True, help=_('Temp directory path'))

    return parser


def parse_cmd_arguments(
        parser: 'argparse.ArgumentParser') -> 'argparse.Namespace':
    """
    Parse arguments with given parser
    :param parser: an instance of ArgumentParser
    :return: arguments Namespace
    """
    try:
        args = parser.parse_args()
        return args
    except SystemExit as e:
        logging.error('Invalid utility invocation', extra={'err': str(e)})
        raise

Zerion Mini Shell 1.0