2021-05-19 18:07:42 +02:00
|
|
|
""" Scripts helpers """
|
|
|
|
|
|
|
|
import argparse
|
2021-05-26 14:44:15 +02:00
|
|
|
import getpass
|
2021-05-19 18:07:42 +02:00
|
|
|
import logging
|
2023-01-16 12:56:12 +01:00
|
|
|
import os.path
|
2021-05-26 14:44:15 +02:00
|
|
|
import socket
|
|
|
|
import sys
|
2021-05-19 18:07:42 +02:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2021-05-19 19:19:57 +02:00
|
|
|
|
2021-05-19 18:07:42 +02:00
|
|
|
def init_logging(options, name, report=None):
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Initialize logging from calling script options"""
|
|
|
|
logformat = f"%(asctime)s - {name} - %(levelname)s - %(message)s"
|
2021-05-19 18:07:42 +02:00
|
|
|
if options.debug:
|
|
|
|
loglevel = logging.DEBUG
|
|
|
|
elif options.verbose:
|
|
|
|
loglevel = logging.INFO
|
|
|
|
else:
|
|
|
|
loglevel = logging.WARNING
|
|
|
|
|
|
|
|
handlers = []
|
|
|
|
if options.logfile:
|
|
|
|
handlers.append(logging.FileHandler(options.logfile))
|
|
|
|
if not options.logfile or options.console:
|
|
|
|
handlers.append(logging.StreamHandler())
|
|
|
|
if report:
|
|
|
|
handlers.append(report.get_handler())
|
|
|
|
logging.basicConfig(level=loglevel, format=logformat, handlers=handlers)
|
|
|
|
|
|
|
|
|
2021-05-26 14:44:15 +02:00
|
|
|
def get_default_opt_value(config, default_config, key):
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Retreive default option value from config or default config dictionaries"""
|
2021-05-26 14:44:15 +02:00
|
|
|
if config and key in config:
|
|
|
|
return config[key]
|
|
|
|
return default_config.get(key)
|
|
|
|
|
|
|
|
|
|
|
|
def get_opts_parser(desc=None, just_try=False, just_one=False, progress=False, config=None):
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Retrieve options parser"""
|
2023-07-10 11:55:09 +02:00
|
|
|
default_config = {"logfile": None}
|
2021-05-26 14:44:15 +02:00
|
|
|
|
2021-05-26 11:56:02 +02:00
|
|
|
parser = argparse.ArgumentParser(description=desc)
|
2021-05-19 18:07:42 +02:00
|
|
|
|
|
|
|
parser.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"-v", "--verbose", action="store_true", dest="verbose", help="Enable verbose mode"
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"-d", "--debug", action="store_true", dest="debug", help="Enable debug mode"
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"-l",
|
|
|
|
"--log-file",
|
2021-05-19 18:07:42 +02:00
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="logfile",
|
2023-01-16 12:56:12 +01:00
|
|
|
help=f'Log file path (default: {get_default_opt_value(config, default_config, "logfile")})',
|
|
|
|
default=get_default_opt_value(config, default_config, "logfile"),
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"-C",
|
|
|
|
"--console",
|
2021-05-19 18:07:42 +02:00
|
|
|
action="store_true",
|
|
|
|
dest="console",
|
2023-01-16 12:56:12 +01:00
|
|
|
help="Always log on console (even if log file is configured)",
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if just_try:
|
|
|
|
parser.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"-j", "--just-try", action="store_true", dest="just_try", help="Enable just-try mode"
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
2021-05-26 11:56:02 +02:00
|
|
|
if just_one:
|
|
|
|
parser.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"-J", "--just-one", action="store_true", dest="just_one", help="Enable just-one mode"
|
2021-05-26 11:56:02 +02:00
|
|
|
)
|
|
|
|
|
2021-05-19 18:07:42 +02:00
|
|
|
if progress:
|
|
|
|
parser.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"-p", "--progress", action="store_true", dest="progress", help="Enable progress bar"
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
return parser
|
|
|
|
|
|
|
|
|
2023-03-13 18:58:20 +01:00
|
|
|
def add_email_opts(parser, config=None, **defaults):
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Add email options"""
|
|
|
|
email_opts = parser.add_argument_group("Email options")
|
2021-05-19 18:07:42 +02:00
|
|
|
|
2023-07-10 11:55:09 +02:00
|
|
|
default_config = {
|
|
|
|
"smtp_host": "127.0.0.1",
|
|
|
|
"smtp_port": 25,
|
|
|
|
"smtp_ssl": False,
|
|
|
|
"smtp_tls": False,
|
|
|
|
"smtp_user": None,
|
|
|
|
"smtp_password": None,
|
|
|
|
"smtp_debug": False,
|
|
|
|
"email_encoding": sys.getdefaultencoding(),
|
|
|
|
"sender_name": getpass.getuser(),
|
|
|
|
"sender_email": f"{getpass.getuser()}@{socket.gethostname()}",
|
|
|
|
"catch_all": False,
|
|
|
|
"templates_path": None,
|
|
|
|
}
|
2023-03-13 18:58:20 +01:00
|
|
|
default_config.update(defaults)
|
2021-05-26 14:44:15 +02:00
|
|
|
|
2021-05-19 18:07:42 +02:00
|
|
|
email_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"--smtp-host",
|
2021-05-19 18:07:42 +02:00
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="email_smtp_host",
|
2023-01-16 12:56:12 +01:00
|
|
|
help=f'SMTP host (default: {get_default_opt_value(config, default_config, "smtp_host")})',
|
|
|
|
default=get_default_opt_value(config, default_config, "smtp_host"),
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"--smtp-port",
|
2021-05-19 18:07:42 +02:00
|
|
|
action="store",
|
|
|
|
type=int,
|
|
|
|
dest="email_smtp_port",
|
2023-01-06 22:13:28 +01:00
|
|
|
help=f'SMTP port (default: {get_default_opt_value(config, default_config, "smtp_port")})',
|
2023-01-16 12:56:12 +01:00
|
|
|
default=get_default_opt_value(config, default_config, "smtp_port"),
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"--smtp-ssl",
|
2021-05-19 18:07:42 +02:00
|
|
|
action="store_true",
|
|
|
|
dest="email_smtp_ssl",
|
2023-01-06 22:13:28 +01:00
|
|
|
help=f'Use SSL (default: {get_default_opt_value(config, default_config, "smtp_ssl")})',
|
2023-01-16 12:56:12 +01:00
|
|
|
default=get_default_opt_value(config, default_config, "smtp_ssl"),
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"--smtp-tls",
|
2021-05-19 18:07:42 +02:00
|
|
|
action="store_true",
|
|
|
|
dest="email_smtp_tls",
|
2023-01-06 22:13:28 +01:00
|
|
|
help=f'Use TLS (default: {get_default_opt_value(config, default_config, "smtp_tls")})',
|
2023-01-16 12:56:12 +01:00
|
|
|
default=get_default_opt_value(config, default_config, "smtp_tls"),
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"--smtp-user",
|
2021-05-19 18:07:42 +02:00
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="email_smtp_user",
|
2023-01-16 12:56:12 +01:00
|
|
|
help=(
|
|
|
|
f'SMTP username (default: {get_default_opt_value(config, default_config, "smtp_user")})'
|
|
|
|
),
|
|
|
|
default=get_default_opt_value(config, default_config, "smtp_user"),
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"--smtp-password",
|
2021-05-19 18:07:42 +02:00
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="email_smtp_password",
|
2023-01-16 12:56:12 +01:00
|
|
|
help=(
|
|
|
|
"SMTP password (default:"
|
|
|
|
f' {get_default_opt_value(config, default_config, "smtp_password")})'
|
|
|
|
),
|
|
|
|
default=get_default_opt_value(config, default_config, "smtp_password"),
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"--smtp-debug",
|
2021-05-19 18:07:42 +02:00
|
|
|
action="store_true",
|
|
|
|
dest="email_smtp_debug",
|
2023-01-16 12:56:12 +01:00
|
|
|
help=(
|
|
|
|
"Debug SMTP connection (default:"
|
|
|
|
f' {get_default_opt_value(config, default_config, "smtp_debug")})'
|
|
|
|
),
|
|
|
|
default=get_default_opt_value(config, default_config, "smtp_debug"),
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"--email-encoding",
|
2021-05-19 18:07:42 +02:00
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="email_encoding",
|
2023-01-16 12:56:12 +01:00
|
|
|
help=(
|
|
|
|
"SMTP encoding (default:"
|
|
|
|
f' {get_default_opt_value(config, default_config, "email_encoding")})'
|
|
|
|
),
|
|
|
|
default=get_default_opt_value(config, default_config, "email_encoding"),
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"--sender-name",
|
2021-05-19 18:07:42 +02:00
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="email_sender_name",
|
2023-01-16 12:56:12 +01:00
|
|
|
help=(
|
|
|
|
f'Sender name (default: {get_default_opt_value(config, default_config, "sender_name")})'
|
|
|
|
),
|
|
|
|
default=get_default_opt_value(config, default_config, "sender_name"),
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"--sender-email",
|
2021-05-19 18:07:42 +02:00
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="email_sender_email",
|
2023-01-16 12:56:12 +01:00
|
|
|
help=(
|
|
|
|
"Sender email (default:"
|
|
|
|
f' {get_default_opt_value(config, default_config, "sender_email")})'
|
|
|
|
),
|
|
|
|
default=get_default_opt_value(config, default_config, "sender_email"),
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"--catch-all",
|
2021-05-19 18:07:42 +02:00
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="email_catch_all",
|
2023-01-06 22:13:28 +01:00
|
|
|
help=(
|
2023-01-16 12:56:12 +01:00
|
|
|
"Catch all sent email: specify catch recipient email address "
|
|
|
|
f'(default: {get_default_opt_value(config, default_config, "catch_all")})'
|
|
|
|
),
|
|
|
|
default=get_default_opt_value(config, default_config, "catch_all"),
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
2023-03-13 18:58:20 +01:00
|
|
|
email_opts.add_argument(
|
|
|
|
"--templates-path",
|
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="email_templates_path",
|
|
|
|
help=(
|
|
|
|
"Load templates from specify directory "
|
|
|
|
f'(default: {get_default_opt_value(config, default_config, "templates_path")})'
|
|
|
|
),
|
|
|
|
default=get_default_opt_value(config, default_config, "templates_path"),
|
|
|
|
)
|
|
|
|
|
2021-05-19 18:07:42 +02:00
|
|
|
|
|
|
|
def init_email_client(options, **kwargs):
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Initialize email client from calling script options"""
|
2021-06-23 12:59:48 +02:00
|
|
|
from mylib.email import EmailClient # pylint: disable=import-outside-toplevel
|
2023-01-16 12:56:12 +01:00
|
|
|
|
|
|
|
log.info("Initialize Email client")
|
2023-06-19 17:07:59 +02:00
|
|
|
return EmailClient(options=options, initialize=True, **kwargs)
|
2022-06-28 11:05:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
def add_sftp_opts(parser):
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Add SFTP options to argpase.ArgumentParser"""
|
2022-06-28 11:05:43 +02:00
|
|
|
sftp_opts = parser.add_argument_group("SFTP options")
|
|
|
|
|
|
|
|
sftp_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"-H",
|
|
|
|
"--sftp-host",
|
2022-06-28 11:05:43 +02:00
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="sftp_host",
|
|
|
|
help="SFTP Host (default: localhost)",
|
2023-01-16 12:56:12 +01:00
|
|
|
default="localhost",
|
2022-06-28 11:05:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
sftp_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"--sftp-port",
|
2022-06-28 11:05:43 +02:00
|
|
|
action="store",
|
|
|
|
type=int,
|
|
|
|
dest="sftp_port",
|
|
|
|
help="SFTP Port (default: 22)",
|
2023-01-16 12:56:12 +01:00
|
|
|
default=22,
|
2022-06-28 11:05:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
sftp_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"-u", "--sftp-user", action="store", type=str, dest="sftp_user", help="SFTP User"
|
2022-06-28 11:05:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
sftp_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"-P",
|
|
|
|
"--sftp-password",
|
2022-06-28 11:05:43 +02:00
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="sftp_password",
|
2023-01-16 12:56:12 +01:00
|
|
|
help="SFTP Password",
|
2022-06-28 11:05:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
sftp_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"--sftp-known-hosts",
|
2022-06-28 11:05:43 +02:00
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="sftp_known_hosts",
|
|
|
|
help="SFTP known_hosts file path (default: ~/.ssh/known_hosts)",
|
2023-01-16 12:56:12 +01:00
|
|
|
default=os.path.expanduser("~/.ssh/known_hosts"),
|
2022-06-28 11:05:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
sftp_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"--sftp-auto-add-unknown-host-key",
|
2022-06-28 11:05:43 +02:00
|
|
|
action="store_true",
|
|
|
|
dest="sftp_auto_add_unknown_host_key",
|
2023-01-16 12:56:12 +01:00
|
|
|
help="Auto-add unknown SSH host key",
|
2022-06-28 11:05:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
return sftp_opts
|