ok

Mini Shell

Direktori : /proc/self/root/opt/cloudlinux/venv/lib/python3.11/site-packages/
Upload File :
Current File : //proc/self/root/opt/cloudlinux/venv/lib/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

#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENCE.TXT
#

from __future__ import print_function
from __future__ import absolute_import
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):
    XML = XML.toprettyxml(encoding='utf-8', indent='', newl='')
    XML = XML.replace("\n", '').replace("\t", '')
    new_xml = xml.parseString(XML)
    buf = new_xml.toprettyxml(encoding='utf-8')
    if not buf:     # paranoia mode)
        raise Exception("Rejected attempt to write empty %s" % 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:
        print('error: bad ve.cfg file')
        sys.exit(1)

    try:
        ve_lveconfig = ve_cfg.getElementsByTagName("lveconfig")[0]
    except:
        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:
        ubc = 'true'

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

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

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

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

    try:
        ve_defaults['mem'] = int(ve_default.getElementsByTagName('mem')[0].getAttribute('limit'))
    except:
        ve_defaults['mem'] = 262144
        mem = ve_cfg.createElement('mem')
        mem.setAttribute('limit',str(262144))
        try:
            ve_default.appendChild(mem)
        except:
            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:
            ve_defaults['io'] = 1024
            io = ve_cfg.createElement('io')
            io.setAttribute('limit',str(1024))
            try:
                ve_default.appendChild(io)
            except:
                pass

    if os.path.exists('/sys/module/lve/parameters/lve_ubc'):
        try:
            f = open('/sys/module/lve/parameters/lve_ubc', "w")
            f.write('Y')
            f.close()
        except:
            pass


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

        else:
            try:
                mem = el.getElementsByTagName('mem')[0].getAttribute('limit')
            except:
                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:
                    pass

    for el in ve_package:
        if (ubc == 'false'):
            try:
                el.getElementsByTagName('mem')[0].setAttribute('limit',str(0))
            except:
                mem = ve_cfg.createElement('mem')
                mem.setAttribute('limit',str(0))
                el.appendChild(mem)
        else:
            try:
                mem = el.getElementsByTagName('mem')[0].getAttribute('limit')
            except:
                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:
                    pass


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

    save_xml(ve_cfg)
    sys.exit(0)

if __name__ == "__main__":
    main()

Zerion Mini Shell 1.0