Skip to content

Commit

Permalink
Refactor optparse to argparse. Also added manage-sources-list-d CLI o…
Browse files Browse the repository at this point in the history
…ption (LP:2077360) (#265)
  • Loading branch information
HJK-X authored Sep 6, 2024
1 parent 9b6719c commit a0c696e
Show file tree
Hide file tree
Showing 16 changed files with 315 additions and 129 deletions.
31 changes: 16 additions & 15 deletions landscape/client/broker/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Configuration class for the broker."""

import os

from landscape.client.deployment import Configuration
Expand All @@ -22,7 +23,7 @@ def exchange_store_path(self):
def make_parser(self):
"""Parser factory for broker-specific options.
@return: An L{OptionParser} preset for all the options
@return: An L{ArgumentParser} preset for all the options
from L{Configuration.make_parser} plus:
- C{account_name}
- C{registration_key}
Expand All @@ -35,66 +36,66 @@ def make_parser(self):
"""
parser = super().make_parser()

parser.add_option(
parser.add_argument(
"-a",
"--account-name",
metavar="NAME",
help="The account this computer belongs to.",
)
parser.add_option(
parser.add_argument(
"-p",
"--registration-key",
metavar="KEY",
help="The account-wide key used for registering clients.",
)
parser.add_option(
parser.add_argument(
"-t",
"--computer-title",
metavar="TITLE",
help="The title of this computer",
)
parser.add_option(
parser.add_argument(
"--exchange-interval",
default=15 * 60,
type="int",
type=int,
metavar="INTERVAL",
help="The number of seconds between server exchanges.",
)
parser.add_option(
parser.add_argument(
"--urgent-exchange-interval",
default=1 * 60,
type="int",
type=int,
metavar="INTERVAL",
help="The number of seconds between urgent server exchanges.",
)
parser.add_option(
parser.add_argument(
"--ping-interval",
default=30,
type="int",
type=int,
metavar="INTERVAL",
help="The number of seconds between pings.",
)
parser.add_option(
parser.add_argument(
"--http-proxy",
metavar="URL",
help="The URL of the HTTP proxy, if one is needed.",
)
parser.add_option(
parser.add_argument(
"--https-proxy",
metavar="URL",
help="The URL of the HTTPS proxy, if one is needed.",
)
parser.add_option(
parser.add_argument(
"--access-group",
default="",
help="Suggested access group for this computer.",
)
parser.add_option(
parser.add_argument(
"--tags",
help="Comma separated list of tag names to be sent "
"to the server.",
)
parser.add_option(
parser.add_argument(
"--hostagent-uid",
help="Only set this value if this computer is a WSL instance "
"managed by Landscape, in which case set it to be the uid that "
Expand Down
38 changes: 22 additions & 16 deletions landscape/client/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
This module, and specifically L{LandscapeSetupScript}, implements the support
for the C{landscape-config} script.
"""

import getpass
import io
import logging
Expand Down Expand Up @@ -190,7 +191,7 @@ def make_parser(self):
"""
parser = super().make_parser()

parser.add_option(
parser.add_argument(
"--import",
dest="import_from",
metavar="FILENAME_OR_URL",
Expand All @@ -199,68 +200,76 @@ def make_parser(self):
"passed in the command line, with precedence "
"being given to real command line options.",
)
parser.add_option(
parser.add_argument(
"--script-users",
metavar="USERS",
help="A comma-separated list of users to allow "
"scripts to run. To allow scripts to be run "
"by any user, enter: ALL",
)
parser.add_option(
parser.add_argument(
"--include-manager-plugins",
metavar="PLUGINS",
default="",
help="A comma-separated list of manager plugins "
"to enable in addition to the defaults.",
)
parser.add_option(
parser.add_argument(
"--manage-sources-list-d",
nargs="?",
default="true",
const="true",
help="Repository profiles manage the files in "
"’etc/apt/sources.list.d'. (default: true)",
)
parser.add_argument(
"-n",
"--no-start",
action="store_true",
help="Don't start the client automatically.",
)
parser.add_option(
parser.add_argument(
"--ok-no-register",
action="store_true",
help="Return exit code 0 instead of 2 if the client "
"can't be registered.",
)
parser.add_option(
parser.add_argument(
"--silent",
action="store_true",
default=False,
help="Run without manual interaction.",
)
parser.add_option(
parser.add_argument(
"--disable",
action="store_true",
default=False,
help="Stop running clients and disable start at boot.",
)
parser.add_option(
parser.add_argument(
"--init",
action="store_true",
default=False,
help="Set up the client directories structure and exit.",
)
parser.add_option(
parser.add_argument(
"--is-registered",
action="store_true",
help="Exit with code 0 (success) if client is "
"registered else returns {}. Displays "
"registration info.".format(EXIT_NOT_REGISTERED),
)
parser.add_option(
parser.add_argument(
"--skip-registration",
action="store_true",
help="Don't send a new registration request",
)
parser.add_option(
parser.add_argument(
"--force-registration",
action="store_true",
help="Force sending a new registration request",
)
parser.add_option(
parser.add_argument(
"--register-if-needed",
action="store_true",
help=(
Expand Down Expand Up @@ -831,10 +840,7 @@ def main(args, print=print):
sys.exit(1)

init_app_logging(
config.log_dir,
config.log_level,
"landscape-config",
config.quiet
config.log_dir, config.log_level, "landscape-config", config.quiet
)

if config.skip_registration and config.force_registration:
Expand Down
40 changes: 20 additions & 20 deletions landscape/client/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from datetime import datetime
from datetime import timezone
from logging import debug
from optparse import SUPPRESS_HELP
from argparse import SUPPRESS
from typing import Sequence

from twisted.logger import globalLogBeginner
Expand Down Expand Up @@ -72,7 +72,7 @@ def __init__(self):
def make_parser(self):
"""Parser factory for supported options.
@return: An OptionParser preset with options that all
@return: An ArgumentParser preset with options that all
programs commonly accept. These include
- config
- data_path
Expand All @@ -94,7 +94,7 @@ class Configuration(BaseConfiguration):
def make_parser(self):
"""Parser factory for supported options.
@return: An L{OptionParser} preset for all options
@return: An L{ArgumentParser} preset for all options
from L{BaseConfiguration.make_parser} plus:
- C{quiet} (C{False})
- C{log_dir} (C{"/var/log/landscape"})
Expand All @@ -107,57 +107,57 @@ def make_parser(self):
"""
parser = super().make_parser()
logging.add_cli_options(parser, logdir="/var/log/landscape")
parser.add_option(
parser.add_argument(
"-u",
"--url",
default=self.DEFAULT_URL,
help="The server URL to connect to.",
)
parser.add_option(
parser.add_argument(
"--ping-url",
help="The URL to perform lightweight exchange initiation with.",
default="http://landscape.canonical.com/ping",
)
parser.add_option(
parser.add_argument(
"-k",
"--ssl-public-key",
help="The public SSL key to verify the server. "
"Only used if the given URL is https.",
)
parser.add_option(
parser.add_argument(
"--ignore-sigint",
action="store_true",
default=False,
help="Ignore interrupt signals.",
)
parser.add_option(
parser.add_argument(
"--ignore-sigusr1",
action="store_true",
default=False,
help="Ignore SIGUSR1 signal to " "rotate logs.",
)
parser.add_option(
parser.add_argument(
"--package-monitor-interval",
default=30 * 60,
type="int",
type=int,
help="The interval between package monitor runs "
"(default: 1800).",
)
parser.add_option(
parser.add_argument(
"--apt-update-interval",
default=6 * 60 * 60,
type="int",
type=int,
help="The interval between apt update runs (default: 21600).",
)
parser.add_option(
parser.add_argument(
"--flush-interval",
default=5 * 60,
type="int",
type=int,
metavar="INTERVAL",
help="The number of seconds between flushes to disk "
"for persistent data.",
)
parser.add_option(
parser.add_argument(
"--stagger-launch",
metavar="STAGGER_RATIO",
dest="stagger_launch",
Expand All @@ -166,20 +166,20 @@ def make_parser(self):
help="Ratio, between 0 and 1, by which to stagger "
"various tasks of landscape.",
)
parser.add_option(
parser.add_argument(
"--snap-monitor-interval",
default=30 * 60,
type="int",
type=int,
help="The interval between snap monitor runs (default 1800).",
)

# Hidden options, used for load-testing to run in-process clones
parser.add_option("--clones", default=0, type=int, help=SUPPRESS_HELP)
parser.add_option(
parser.add_argument("--clones", default=0, type=int, help=SUPPRESS)
parser.add_argument(
"--start-clones-over",
default=25 * 60,
type=int,
help=SUPPRESS_HELP,
help=SUPPRESS,
)

return parser
Expand Down
10 changes: 5 additions & 5 deletions landscape/client/manager/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,30 @@ def make_parser(self):
"""
parser = super().make_parser()

parser.add_option(
parser.add_argument(
"--manager-plugins",
metavar="PLUGIN_LIST",
help="Comma-delimited list of manager plugins to "
"use. ALL means use all plugins.",
default="ALL",
)
parser.add_option(
parser.add_argument(
"--include-manager-plugins",
metavar="PLUGIN_LIST",
help="Comma-delimited list of manager plugins to "
"enable, in addition to the defaults.",
)
parser.add_option(
parser.add_argument(
"--script-users",
metavar="USERS",
help="Comma-delimited list of usernames that scripts"
" may be run as. Default is to allow all "
"users.",
)
parser.add_option(
parser.add_argument(
"--script-output-limit",
metavar="SCRIPT_OUTPUT_LIMIT",
type="int",
type=int,
default=512,
help="Maximum allowed output size that scripts"
" can send. "
Expand Down
2 changes: 1 addition & 1 deletion landscape/client/monitor/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def make_parser(self):
"""
parser = super().make_parser()

parser.add_option(
parser.add_argument(
"--monitor-plugins",
metavar="PLUGIN_LIST",
help="Comma-delimited list of monitor plugins to "
Expand Down
Loading

0 comments on commit a0c696e

Please sign in to comment.