Mini Shell

Direktori : /proc/self/root/opt/cloudlinux/venv/lib64/python3.11/site-packages/
Upload File :
Current File : //proc/self/root/opt/cloudlinux/venv/lib64/python3.11/site-packages/remove_ubc.py

#!/opt/cloudlinux/venv/bin/python3 -bb
# -*- coding: utf-8 -*-

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

import os
import sys
import xml.dom.minidom as xml

CoreVersion = 6
core = os.uname()
if core[2].find('el5.lve0.8') != -1:
    CoreVersion = 5

# defined ve.cfg variables
ve_cfg = ''
ve_lveconfig = ''
ve_ubc = ''
ve_default = ''
ve_lve = ''
ve_defaults = ''
ve_package = ''
ubc = 'false'

VE_CFG_PATH = '/etc/container/ve.cfg'
VE_CFG_PATH_TMP = VE_CFG_PATH + '.tmp'


# Save XML config ve.cfg
def save_xml(xml_config):
    xml_config = xml_config.toprettyxml(encoding='utf-8', indent='', newl='')
    xml_config = xml_config.replace("\n", '').replace("\t", '')
    new_xml = xml.parseString(xml_config)
    buf = new_xml.toprettyxml(encoding='utf-8')
    if not buf:  # paranoia mode)
        raise RuntimeError(f"Rejected attempt to write empty {VE_CFG_PATH}")
    with open(VE_CFG_PATH_TMP, 'wb') as f:
        f.write(buf)
        f.flush()
        os.fsync(f.fileno())
    os.rename(VE_CFG_PATH_TMP, VE_CFG_PATH)


def get_XML_cfg():
    global ve_cfg
    global ve_lveconfig
    global ve_ubc
    global ve_default
    global ve_lve
    global ve_defaults
    global ve_package

    try:
        ve_cfg = xml.parse(VE_CFG_PATH)
    except Exception:
        print('error: bad ve.cfg file')
        sys.exit(1)

    try:
        ve_lveconfig = ve_cfg.getElementsByTagName("lveconfig")[0]
    except Exception:
        print('error: bad ve.cfg file')
        sys.exit(1)

    global ubc
    try:
        ve_ubc = ve_lveconfig.getElementsByTagName("system")[0].getElementsByTagName("ubc")[0].getAttribute("enabled")
        ubc = ve_ubc
    except Exception:
        ubc = 'true'

    try:
        ve_default = ve_lveconfig.getElementsByTagName("defaults")[0]
    except Exception:
        print('warning: default section error in ve.cfg')

    try:
        ve_lve = ve_lveconfig.getElementsByTagName("lve")
    except Exception:
        pass

    try:
        ve_package = ve_lveconfig.getElementsByTagName("package")
    except Exception:
        pass

    # Get defaults values from ve.cfg
    ve_defaults = {}

    try:
        ve_defaults['mem'] = int(ve_default.getElementsByTagName('mem')[0].getAttribute('limit'))
    except Exception:
        ve_defaults['mem'] = 262144
        mem = ve_cfg.createElement('mem')
        mem.setAttribute('limit', str(262144))
        try:
            ve_default.appendChild(mem)
        except Exception:
            pass

    if CoreVersion != 5:
        try:
            ve_defaults['io'] = int(ve_default.getElementsByTagName('io')[0].getAttribute('limit'))
            if ve_defaults['io'] == 25:
                ve_default.getElementsByTagName('io')[0].setAttribute('limit', str(1024))
        except Exception:
            ve_defaults['io'] = 1024
            io = ve_cfg.createElement('io')
            io.setAttribute('limit', str(1024))
            try:
                ve_default.appendChild(io)
            except Exception:
                pass

    if os.path.exists('/sys/module/lve/parameters/lve_ubc'):
        try:
            with open('/sys/module/lve/parameters/lve_ubc', "w", encoding="utf-8") as f:
                f.write('Y')
        except Exception:
            pass


def main():
    get_XML_cfg()
    for el in ve_lve:
        if ubc == 'false':
            try:
                el.getElementsByTagName('mem')[0].setAttribute('limit', str(0))
            except Exception:
                mem = ve_cfg.createElement('mem')
                mem.setAttribute('limit', str(0))
                el.appendChild(mem)

        else:
            try:
                mem = el.getElementsByTagName('mem')[0].getAttribute('limit')
            except Exception:
                mem = ve_cfg.createElement('mem')
                mem.setAttribute('limit', str(ve_defaults['mem']))
                el.appendChild(mem)
            else:
                pass

        if CoreVersion != 5:
            if ve_defaults['io'] == 25:
                try:
                    if (
                        int(el.getElementsByTagName('io')[0].getAttribute('limit')) <= 100
                        and int(el.getElementsByTagName('io')[0].getAttribute('limit')) != 0
                    ):
                        el.getElementsByTagName('io')[0].setAttribute('limit', str(1024))
                except Exception:
                    pass

    for el in ve_package:
        if ubc == 'false':
            try:
                el.getElementsByTagName('mem')[0].setAttribute('limit', str(0))
            except Exception:
                mem = ve_cfg.createElement('mem')
                mem.setAttribute('limit', str(0))
                el.appendChild(mem)
        else:
            try:
                mem = el.getElementsByTagName('mem')[0].getAttribute('limit')
            except Exception:
                mem = ve_cfg.createElement('mem')
                mem.setAttribute('limit', str(ve_defaults['mem']))
                el.appendChild(mem)

        if CoreVersion != 5:
            if ve_defaults['io'] == 25:
                try:
                    if (
                        int(el.getElementsByTagName('io')[0].getAttribute('limit')) <= 100
                        and int(el.getElementsByTagName('io')[0].getAttribute('limit')) != 0
                    ):
                        el.getElementsByTagName('io')[0].setAttribute('limit', str(1024))
                except Exception:
                    pass

    try:
        ve_ubc = ve_lveconfig.getElementsByTagName("system")
        for el in ve_ubc:
            el.parentNode.removeChild(el)
    except Exception:
        pass

    save_xml(ve_cfg)
    sys.exit(0)


if __name__ == "__main__":
    main()

Zerion Mini Shell 1.0