????

Your IP : 18.216.34.208


Current Path : /usr/bin/
Upload File :
Current File : //usr/bin/cl-syncpkgs

#!/opt/cloudlinux/venv/bin/python3 -bb
# -*- coding: utf-8 -*-
"""
This script used for apply cpanel package changes to lve config
"""
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import os
import sys
import time
import traceback

from clcommon.cpapi import is_panel_feature_supported, Feature
from clcommon.utils import is_root_or_exit

import cldetectlib as detect
import lvectllib

LIBDIR = "/usr/share/l.v.e-manager/cpanel/lib"
SCRIPT_NAME = __file__.split("/")[-1]

sys.path.append(LIBDIR)
from packages import save_package, LVE_CONFIG, PKG_DIR, QUOTA_CONFIG, MYSQLGOV_PACKAGES_CONFIG
from clcommon.lib.cledition import lve_supported_or_exit
import logging
import logging.handlers


def setup_system_log():
    """
    Sets syslog logger and returns its handler
    """
    logger = logging.getLogger(SCRIPT_NAME)
    logger.setLevel(logging.INFO)
    syslogger = logging.handlers.SysLogHandler(address='/dev/log')
    formatter = logging.Formatter(fmt='%(levelname)s %(message)s')
    syslogger.setFormatter(formatter)
    logger.addHandler(syslogger)
    return logger


def _get_file_mtime(filename: str) -> float:
    """
    Get file mtime
    @param filename:
    @return: mtime
    """
    if os.path.exists(filename):
        file_mtime = os.path.getmtime(filename)
    else:
        file_mtime = float("inf")
    return file_mtime


def main():
    is_root_or_exit()
    if not detect.is_cpanel():
        sys.exit(0)
    logger = setup_system_log()
    try:
        if is_panel_feature_supported(Feature.LVE):
            lvectllib.init()
            lvectllib.get_XML_cfg()

        current_time = time.time()
        lve_mtime, quotas_mtime, governor_mtime = current_time, current_time, current_time

        if is_panel_feature_supported(Feature.LVE):
            lve_mtime = os.path.getmtime(LVE_CONFIG)

        quotas_mtime = _get_file_mtime(QUOTA_CONFIG)

        if is_panel_feature_supported(Feature.GOVERNOR):
            governor_mtime = _get_file_mtime(MYSQLGOV_PACKAGES_CONFIG)
    except Exception as e:
        logger.error(e)
        sys.exit(1)

    minute_before = current_time - 60.0 * 5
    exit_status = 0
    for pkg_name in os.listdir(PKG_DIR):
        file_path = os.path.join(PKG_DIR, pkg_name)
        if not os.path.isfile(file_path):
            continue
        pkg_mtime = os.path.getmtime(file_path)
        if any(pkg_mtime > ts for ts in (minute_before, lve_mtime, quotas_mtime, governor_mtime)):
            try:
                save_package(pkg_name)
            except Exception:
                exit_status = 1
                msg = traceback.format_exc()
                logger.error(f'Error while processing package data file {file_path}:\n{msg}\n')
                continue
    sys.exit(exit_status)


if "__main__" == __name__:
    main()