Mini Shell

Direktori : /proc/self/root/proc/thread-self/root/opt/tier1adv/bin/
Upload File :
Current File : //proc/self/root/proc/thread-self/root/opt/tier1adv/bin/create_db

#!/opt/imh-python/bin/python3
"""CLI tool to run as a cPanel user which creates a MySQL database"""
from argparse import ArgumentParser
import sys
import string
import random
from cpapis import uapi, CpAPIExecFail, CpAPIErrorMsg


def create_database(suffix: str):
    prefix = api('Mysql::get_restrictions')['data']['prefix']
    name = f"{prefix}{suffix}"
    chars = string.ascii_letters + string.digits + '_'
    passwd = ''.join([random.choice(chars) for _ in range(15)])
    api('Mysql::create_database', name=name)
    print("Created database:", name)
    api('Mysql::create_user', name=name, password=passwd)
    print("Created database user:", name)
    print(f"Password: '{passwd}'")
    api(
        'Mysql::set_privileges_on_database',
        user=name,
        database=name,
        privileges='ALL',
    )
    print("Assigned privileges.")


def api(mod: str, **kwargs):
    try:
        return uapi(mod, args=kwargs, check=True)['result']
    except CpAPIExecFail as exc:
        sys.exit(exc.stderr)


def main():
    try:
        parser = ArgumentParser(description=__doc__)
        parser.add_argument(
            'suffix', help='end of database name: USER_{suffix}'
        )
        suffix = parser.parse_args().suffix
        create_database(suffix)
    except KeyboardInterrupt:
        sys.exit(1)
    except CpAPIErrorMsg as exc:
        sys.exit(exc)


if __name__ == '__main__':
    main()

Zerion Mini Shell 1.0