Mini Shell

Direktori : /opt/imh-python/lib/python3.9/site-packages/ngxutil/
Upload File :
Current File : //opt/imh-python/lib/python3.9/site-packages/ngxutil/profile.py

# vim: set ts=4 sw=4 expandtab syntax=python:
"""

ngxutil.profile
Profile Management Functions

@author J. Hipps <jacobh@inmotionhosting.com>

"""

import os
import logging
import yaml
from yaml import CDumper, CLoader

from ngxconf.util import parse_profiles, get_profile

logger = logging.getLogger('ngxutil')


def set_domain_profile(user, domain, profile):
    """
    Set profile for a user's domain to @profile
    """
    parse_profiles()
    tprof = get_profile(profile)

    if tprof is None:
        logger.error("No profile named '%s' was found", profile)
        return False

    domfile = '%s.yml' % (domain.replace('.', '_'))
    cpath = os.path.join('/home', user, '.imh/nginx', domfile)

    # Read existing config to pull out SSL options
    try:
        with open(cpath) as f:
            econf = yaml.load(stream=f, Loader=CLoader)
            logger.debug("Parsed user config at %s", cpath)

            nconf = {
                'ssl_certificate': econf.get('ssl_certificate'),
                'ssl_certificate_key': econf.get('ssl_certificate_key'),
                'ssl_enabled': econf.get('ssl_enabled'),
                'proxy_proto': econf.get('proxy_proto'),
            }
    except Exception as e:
        logger.error("Failed to read existing config file: %s", str(e))
        nconf = {}

    # merge in SSL options
    tprof.update(nconf)

    # Write updated config
    try:
        with open(cpath, 'w') as f:
            yaml.dump(tprof, stream=f, Dumper=CDumper, default_flow_style=False)
            logger.info("Wrote updated config file for %s:%s to %s", user, domain, cpath)
    except Exception as e:
        logger.error("Failed to write config file [%s]: %s", cpath, str(e))
        return False

    return True


Zerion Mini Shell 1.0