Mini Shell
Direktori : /opt/sharedrads/ |
|
Current File : //opt/sharedrads/send_customer_str |
#!/opt/imh-python/bin/python3
"""Sends a resource review email to a customer"""
import argparse
from subprocess import run, PIPE, CalledProcessError
from pathlib import Path
import sys
from pp_api import PowerPanel
import yaml
import rads
def read_urls() -> dict[str, str]:
path = Path('/opt/sharedrads/etc/kb_urls.yaml')
if not path.is_file():
path = Path('/opt/dedrads/etc/kb_urls.yaml')
assert path.is_file()
with open(path, encoding='ascii') as kbs:
kb_urls = yaml.load(kbs, yaml.SafeLoader)
key = 'hub' if rads.IMH_CLASS == 'hub' else 'imh'
return kb_urls[key]
IMH_RADS_INFO = 1022
IMH_RADS_WARN = 1023
WHH_RADS_INFO = 1024
WHH_RADS_WARN = 1025
KB_URLS = read_urls()
CHECK_TODAY_CP = '/opt/sharedrads/python/send_customer_str/check_today_cp'
CHECK_DCPUMON = '/opt/sharedrads/python/send_customer_str/check_dcpumon'
CHECK_SOFTWARE = '/opt/sharedrads/check_software'
NLP_CMD = '/opt/sharedrads/python/send_customer_str/nlp.py'
def parse_args() -> tuple[str, bool, bool, bool]:
parser = argparse.ArgumentParser(description=__doc__)
# fmt: off
parser.add_argument(
'-u', '--user', dest='user', required=True, help='cPanel username'
)
body = parser.add_mutually_exclusive_group(required=True)
dest = parser.add_mutually_exclusive_group()
dest.add_argument(
'--test', '--noop', dest='test', action='store_true',
help="Don't actually email. Print to stdout"
)
body.add_argument(
'-n', '--no-warning', action='store_const', const=False, dest='warning',
help='Does not send warning part of STR',
)
body.add_argument(
'-w', '--warning', action='store_const', const=True, dest='warning',
help='Send warning part of STR',
)
parser.add_argument(
'-p', '--preview', action='store_true',
help='Preview message and approve it before sending',
)
# fmt: on
args = parser.parse_args()
if not rads.is_cpuser(args.user):
sys.exit(f'{args.user} is not a valid user')
return args.user, args.warning, args.test, args.preview
def get_stdout(cmd: list[str], **kwargs) -> str:
try:
return run(
cmd,
encoding='utf-8',
stdout=PIPE,
check=True,
errors='ignore',
**kwargs,
).stdout
except (OSError, CalledProcessError) as exc:
print(exc, file=sys.stderr)
return ''
def get_data(user: str) -> str:
dcpumon = get_stdout([CHECK_DCPUMON, user])
today_cp = get_stdout([CHECK_TODAY_CP, '-u', user])
software = get_stdout([CHECK_SOFTWARE, '--str', user])
nlp = get_stdout([NLP_CMD, user])
data = [dcpumon, today_cp, software, nlp]
return '<br />\n'.join(filter(None, data))
def main():
user, warning, test, preview = parse_args()
amp = PowerPanel()
html = get_data(user)
if rads.IMH_CLASS == 'hub':
template = WHH_RADS_WARN if warning else WHH_RADS_INFO
else:
template = IMH_RADS_WARN if warning else IMH_RADS_INFO
plain = get_stdout(['lynx', '-stdin', '-dump', '-nolog'], input=html)
if test or preview:
print(plain or html)
if test:
sys.exit(0)
if preview:
if not rads.prompt_y_n('Proceed to send AMP email?'):
sys.exit('Email canceled.')
if rads.IMH_CLASS == 'reseller' and rads.is_child(user):
user = rads.get_owner(user)
ret = amp.call(
"notification.send", cpanelUser=user, template=template, variable_1=html
)
if ret.status != 0:
sys.exit(f"Failed to send email to {user}: {ret.message}")
print('Email sent.')
if __name__ == "__main__":
main()
Zerion Mini Shell 1.0