Mini Shell

Direktori : /usr/share/web-monitoring-tool/
Upload File :
Current File : //usr/share/web-monitoring-tool/daemon_control.py

#!/opt/cloudlinux/venv/bin/python3 -bb
# coding=utf-8
#
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2020 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENCE.TXT
#

# pylint: disable=no-absolute-import

import getopt
import os
import platform
import shutil
import subprocess
import sys

from sentry import init_wmt_sentry_client
from wmt.common.utils import setup_logger, enable_wmt_daemon, disable_wmt_daemon
from wmt.common.const import (
    WMT_SCANNER_SERVICE_SOLO,
    WMT_SCANNER_SERVICE_SOLO_FILE,
    SERVICE_BIN,
    CHKCONFIG_BIN,
    SYSTEMCTL_BIN
)

logger = setup_logger('daemon_control')

DAEMON_NAME: str = 'cl_wmt_scanner'
_WMT_SERVICE_DIR: str = '/usr/share/web-monitoring-tool/service'
# CL6 service files
_CL6_SERVICE_FILE_PATH: str = '/etc/rc.d/init.d'
_CL6_SERVICE_WMT_FILE: str = 'cl_wmt_scanner'
# CL7/8 service files
_CL7_8_SYSTEMD_UNIT_FILE_PATH: str = '/etc/systemd/system'
_CL7_8_SYSTEMD_WMT_UNIT_FILE: str = 'cl_wmt_scanner.service'


def usage():
    print('')
    print('Use following syntax to manage LVE Manager install utility:')
    print(sys.argv[0] + " [OPTIONS]")
    print('Options:')
    print(" -i | --install     : install web monitoring tool service")
    print(" -d | --delete      : delete  web monitoring tool service")

def cleanup_solo_daemon():
    deprecated_solo_service = os.path.join(_CL7_8_SYSTEMD_UNIT_FILE_PATH,
                                                          WMT_SCANNER_SERVICE_SOLO_FILE)
    if not os.path.exists(deprecated_solo_service):
        return False
    logger.info('Cleaning up deprecated CloudLinux Solo service: %s', deprecated_solo_service)
    disable_wmt_daemon(WMT_SCANNER_SERVICE_SOLO, False)
    stop_remove_wmt_daemon(WMT_SCANNER_SERVICE_SOLO_FILE, False)
    return True

def install_wmt_daemon(service_name, is_cl6):
    """
    Install CL wmt daemon
    :return: None
    """
    if is_cl6:
        # CL6
        # /usr/share/cloudlinux/cl_plus/service/cl_wmt_scanner -> /etc/rc.d/init.d/cl_wmt_scanner
        src = os.path.join(_WMT_SERVICE_DIR, service_name)
        dest = os.path.join(_CL6_SERVICE_FILE_PATH, service_name)
        shutil.copy(src, dest)
        os.chmod(dest, 0o755)
    else:
        # to clean up scanner_solo.service
        is_cleaned_up = cleanup_solo_daemon()
        # CL7, CL8
        # /usr/share/cloudlinux/cl_plus/service/cl_wmt_scanner.service -> /etc/systemd/system/cl_wmt_scanner.service
        src = os.path.join(_WMT_SERVICE_DIR, service_name)
        dest = os.path.join(_CL7_8_SYSTEMD_UNIT_FILE_PATH, service_name)
        shutil.copy(src, dest)
        os.chmod(dest, 0o644)
        subprocess.run('{} daemon-reload'.format(SYSTEMCTL_BIN), shell=True, executable='/bin/bash')

        # so updated (not new) Solo machines have wmt_scanner started
        if is_cleaned_up:
            logger.info('Activating %s for updated CloudLinux Solo server', service_name)
            enable_wmt_daemon(service_name, is_cl6)

def stop_remove_wmt_daemon(service_name, is_cl6):
    """
    Remove WMT daemon from system
    :return:
    """
    if is_cl6:
        # CL6
        try:
            # Remove /etc/rc.d/init.d/cl_plus_sender
            src = os.path.join(_CL6_SERVICE_FILE_PATH, service_name)
            os.remove(src)
        except (OSError, IOError):
            pass
    else:
        # CL7, CL8
        try:
            # Remove /etc/systemd/system/cl_plus_sender.service
            src = os.path.join(_CL7_8_SYSTEMD_UNIT_FILE_PATH, service_name)
            os.remove(src)
        except (OSError, IOError):
            pass
        subprocess.run('{} daemon-reload >/dev/null 2>&1'.format(SYSTEMCTL_BIN), shell=True, executable='/bin/bash')


if __name__ == "__main__":
    init_wmt_sentry_client()
    _is_cl6 = 'el6' in platform.release()
    if not _is_cl6:
        wmt_service_name = _CL7_8_SYSTEMD_WMT_UNIT_FILE
        wmt_daemon_name = DAEMON_NAME
    else:
        wmt_service_name = _CL6_SERVICE_WMT_FILE
        wmt_daemon_name = DAEMON_NAME
    logger.info(f'Scanner service "{wmt_service_name}"')
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hid", ["help", "install", "delete"])
    except getopt.GetoptError as err:
        # print help information and exit:
        print(str(err))  # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    try:
        for o, a in opts:
            if o in ("-h", "--help"):
                usage()
                sys.exit()
            elif o in ("-i", "--install"):
                install_wmt_daemon(wmt_service_name, _is_cl6)
            elif o in ("-d", "--delete"):
                disable_wmt_daemon(wmt_daemon_name, _is_cl6)
                stop_remove_wmt_daemon(wmt_service_name, _is_cl6)
            else:
                usage()
                sys.exit(2)
    except Exception as e:
        logger.exception(e)

Zerion Mini Shell 1.0