Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../bin
File: package_reinstaller.py
#!/opt/cloudlinux/venv/bin/python3 -bb
[0] Fix | Delete
# -*- coding: utf-8 -*-
[1] Fix | Delete
[2] Fix | Delete
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
[3] Fix | Delete
#
[4] Fix | Delete
# Licensed under CLOUD LINUX LICENSE AGREEMENT
[5] Fix | Delete
# http://cloudlinux.com/docs/LICENSE.TXT
[6] Fix | Delete
[7] Fix | Delete
# check /var/lve/PANEL
[8] Fix | Delete
# if FILE absent - create
[9] Fix | Delete
# if FILE empty - work as if FILE absent
[10] Fix | Delete
# read panel name from FILE
[11] Fix | Delete
# if panel name changed = check panel process end and reinstall lvemanager cagefs lve-utils
[12] Fix | Delete
import argparse
[13] Fix | Delete
import logging
[14] Fix | Delete
import os
[15] Fix | Delete
import sys
[16] Fix | Delete
from logging.handlers import RotatingFileHandler
[17] Fix | Delete
from subprocess import CalledProcessError, check_output
[18] Fix | Delete
[19] Fix | Delete
from clcommon.const import Feature
[20] Fix | Delete
from clcommon.cpapi import is_panel_feature_supported
[21] Fix | Delete
from clcommon.utils import is_ubuntu, run_command
[22] Fix | Delete
from cldetectlib import getCPName
[23] Fix | Delete
[24] Fix | Delete
PANEL_FILE_DIR = '/var/lve'
[25] Fix | Delete
PANEL_FILE = os.path.join(PANEL_FILE_DIR, 'PANEL')
[26] Fix | Delete
LOG = '/var/log/package_reinstaller.log'
[27] Fix | Delete
[28] Fix | Delete
[29] Fix | Delete
def is_panel_change():
[30] Fix | Delete
"""
[31] Fix | Delete
:return: True if names are different and new panel name
[32] Fix | Delete
"""
[33] Fix | Delete
panel_name = getCPName()
[34] Fix | Delete
logging.info('Current panel is %s', panel_name)
[35] Fix | Delete
try:
[36] Fix | Delete
with open(PANEL_FILE, encoding="utf-8") as f:
[37] Fix | Delete
old_panel_name = f.read()
[38] Fix | Delete
logging.info('Old panel is %s', old_panel_name)
[39] Fix | Delete
if old_panel_name != panel_name:
[40] Fix | Delete
return True, panel_name
[41] Fix | Delete
return False, panel_name
[42] Fix | Delete
except (OSError, IOError) as e:
[43] Fix | Delete
# if the file doesn't exist - panel doesn't change
[44] Fix | Delete
logging.warning('Panel file %s not found!, error: %s', PANEL_FILE, str(e))
[45] Fix | Delete
return False, panel_name
[46] Fix | Delete
[47] Fix | Delete
[48] Fix | Delete
def write_panel_file(panel_name):
[49] Fix | Delete
"""
[50] Fix | Delete
Save the given panel name to /var/lve/PANEL.
[51] Fix | Delete
[52] Fix | Delete
:param panel_name: The panel name to save.
[53] Fix | Delete
:type panel_name: str
[54] Fix | Delete
"""
[55] Fix | Delete
try:
[56] Fix | Delete
os.mkdir(PANEL_FILE_DIR)
[57] Fix | Delete
except OSError:
[58] Fix | Delete
pass # directory already exists
[59] Fix | Delete
with open(PANEL_FILE, 'w', encoding="utf8") as f:
[60] Fix | Delete
f.write(panel_name)
[61] Fix | Delete
[62] Fix | Delete
[63] Fix | Delete
def is_panel_process_active(panel_name):
[64] Fix | Delete
"""
[65] Fix | Delete
:param panel_name:
[66] Fix | Delete
:return: True if panel is already detected but not installed yet
[67] Fix | Delete
"""
[68] Fix | Delete
process_active = False
[69] Fix | Delete
if panel_name == 'cPanel':
[70] Fix | Delete
# Expect installation in progress if installer.lock file present
[71] Fix | Delete
process_active = os.path.isfile('/root/installer.lock')
[72] Fix | Delete
elif panel_name == 'Plesk':
[73] Fix | Delete
process_active = True
[74] Fix | Delete
not_running_pattern = 'Plesk Installer is not running'
[75] Fix | Delete
plesk_bin = '/usr/sbin/plesk'
[76] Fix | Delete
# Plesk is installing if we detect it and plesk bin doesn't exist
[77] Fix | Delete
if not os.path.exists(plesk_bin):
[78] Fix | Delete
return process_active
[79] Fix | Delete
try:
[80] Fix | Delete
result = check_output(
[81] Fix | Delete
[
[82] Fix | Delete
plesk_bin,
[83] Fix | Delete
'installer',
[84] Fix | Delete
'--query-status',
[85] Fix | Delete
],
[86] Fix | Delete
text=True,
[87] Fix | Delete
)
[88] Fix | Delete
process_active = not_running_pattern not in result
[89] Fix | Delete
except (CalledProcessError, FileNotFoundError):
[90] Fix | Delete
return process_active
[91] Fix | Delete
elif panel_name == 'DirectAdmin':
[92] Fix | Delete
# Location changed in DirectAdmin 1.659
[93] Fix | Delete
# https://docs.directadmin.com/changelog/version-1.659.html#migrate-scripts-setup-txt-to-conf-setup-txt
[94] Fix | Delete
new_da_setup_txt = '/usr/local/directadmin/conf/setup.txt'
[95] Fix | Delete
old_da_setup_txt = '/usr/local/directadmin/scripts/setup.txt'
[96] Fix | Delete
process_active = not os.path.exists(old_da_setup_txt) and not os.path.exists(new_da_setup_txt)
[97] Fix | Delete
[98] Fix | Delete
return process_active
[99] Fix | Delete
[100] Fix | Delete
[101] Fix | Delete
def packages_to_reinstall():
[102] Fix | Delete
"""
[103] Fix | Delete
Gather packages to reinstall depending on the features supported by the panel.
[104] Fix | Delete
[105] Fix | Delete
:return: List of package names.
[106] Fix | Delete
:rtype: list[str]
[107] Fix | Delete
"""
[108] Fix | Delete
packages = ['lvemanager', 'lve-utils', 'alt-python27-cllib']
[109] Fix | Delete
if is_panel_feature_supported(Feature.CAGEFS):
[110] Fix | Delete
packages.append('cagefs')
[111] Fix | Delete
if is_panel_feature_supported(Feature.XRAY):
[112] Fix | Delete
packages.append("lvemanager-xray")
[113] Fix | Delete
if is_panel_feature_supported(Feature.WPOS):
[114] Fix | Delete
packages.append("cloudlinux-awp-plugin")
[115] Fix | Delete
return packages
[116] Fix | Delete
[117] Fix | Delete
[118] Fix | Delete
def package_reinstall_apt(pkgs):
[119] Fix | Delete
"""
[120] Fix | Delete
Invokes the package reinstallation command with apt.
[121] Fix | Delete
"""
[122] Fix | Delete
cmd_env = os.environ.copy()
[123] Fix | Delete
# For debconf to avoid making any interactive queries.
[124] Fix | Delete
cmd_env['DEBIAN_FRONTEND'] = 'noninteractive'
[125] Fix | Delete
cmd_env['DEBCONF_NONINTERACTIVE_SEEN'] = 'true'
[126] Fix | Delete
[127] Fix | Delete
return run_command(
[128] Fix | Delete
['/usr/bin/apt-get', 'install', '--reinstall', '--yes'] + pkgs,
[129] Fix | Delete
env_data=cmd_env,
[130] Fix | Delete
return_full_output=True,
[131] Fix | Delete
)
[132] Fix | Delete
[133] Fix | Delete
[134] Fix | Delete
def package_reinstall_yum(pkgs):
[135] Fix | Delete
"""
[136] Fix | Delete
Invokes the package reinstallation command with yum.
[137] Fix | Delete
"""
[138] Fix | Delete
return run_command(
[139] Fix | Delete
['/usr/bin/yum', 'reinstall', '-y'] + pkgs,
[140] Fix | Delete
return_full_output=True,
[141] Fix | Delete
)
[142] Fix | Delete
[143] Fix | Delete
[144] Fix | Delete
def perform_package_reinstallation():
[145] Fix | Delete
"""
[146] Fix | Delete
Perform the actuall package reinstallation, invoking the package manager.
[147] Fix | Delete
Uses apt-get on Ubuntu-based machines, and yum otherwise.
[148] Fix | Delete
[149] Fix | Delete
:return: Tuple of (return code, stdout, stderr)
[150] Fix | Delete
:rtype: tuple[int, str, str]
[151] Fix | Delete
"""
[152] Fix | Delete
to_reinstall = packages_to_reinstall()
[153] Fix | Delete
if is_ubuntu():
[154] Fix | Delete
return package_reinstall_apt(to_reinstall)
[155] Fix | Delete
return package_reinstall_yum(to_reinstall)
[156] Fix | Delete
[157] Fix | Delete
[158] Fix | Delete
def check():
[159] Fix | Delete
"""
[160] Fix | Delete
Check if control panel name equals to the cached one
[161] Fix | Delete
and reinstall packages if not so
[162] Fix | Delete
:return: None
[163] Fix | Delete
"""
[164] Fix | Delete
(panel_changed, new_panel_name) = is_panel_change()
[165] Fix | Delete
if not panel_changed:
[166] Fix | Delete
logging.warning('The panel was not changed, skipping package reinstallation')
[167] Fix | Delete
return
[168] Fix | Delete
[169] Fix | Delete
logging.info('The control panel has changed, reinstalling packages')
[170] Fix | Delete
[171] Fix | Delete
if new_panel_name in ['cPanel', 'Plesk', 'DirectAdmin']:
[172] Fix | Delete
if not is_panel_process_active(new_panel_name):
[173] Fix | Delete
retcode, std_out, std_err = perform_package_reinstallation()
[174] Fix | Delete
if retcode == 0 or (retcode == 1 and std_err.find('Nothing to do')):
[175] Fix | Delete
write_panel_file(new_panel_name)
[176] Fix | Delete
logging.info(
[177] Fix | Delete
'Packages have been reinstalled!\nSTDOUT: \n%s\nSTDERR: \n%s\n',
[178] Fix | Delete
std_out,
[179] Fix | Delete
std_err,
[180] Fix | Delete
)
[181] Fix | Delete
else:
[182] Fix | Delete
logging.warning(
[183] Fix | Delete
'The panel is currently being installed, skipping package reinstallation!'
[184] Fix | Delete
)
[185] Fix | Delete
else:
[186] Fix | Delete
write_panel_file(new_panel_name)
[187] Fix | Delete
[188] Fix | Delete
[189] Fix | Delete
def init():
[190] Fix | Delete
"""
[191] Fix | Delete
Save the current control panel name to cache if it's not saved yet.
[192] Fix | Delete
:return: None
[193] Fix | Delete
"""
[194] Fix | Delete
if not os.path.exists(PANEL_FILE):
[195] Fix | Delete
write_panel_file(getCPName())
[196] Fix | Delete
[197] Fix | Delete
[198] Fix | Delete
if __name__ == '__main__':
[199] Fix | Delete
logging.basicConfig(
[200] Fix | Delete
handlers=[RotatingFileHandler(LOG, maxBytes=1024 * 1024, backupCount=2)],
[201] Fix | Delete
level=logging.INFO,
[202] Fix | Delete
format='%(asctime)s %(levelname)s:%(name)s:%(message)s',
[203] Fix | Delete
)
[204] Fix | Delete
[205] Fix | Delete
parser = argparse.ArgumentParser()
[206] Fix | Delete
[207] Fix | Delete
subparsers = parser.add_subparsers(dest='subparser')
[208] Fix | Delete
subparsers.add_parser(
[209] Fix | Delete
'init',
[210] Fix | Delete
help="Init the panel name cache file; "
[211] Fix | Delete
f"works only once and creates the {PANEL_FILE} file",
[212] Fix | Delete
)
[213] Fix | Delete
subparsers.add_parser(
[214] Fix | Delete
'check',
[215] Fix | Delete
help="Check for control panel change; "
[216] Fix | Delete
"Reinstall packages if control panel name doesn't match the one in the cache file.",
[217] Fix | Delete
)
[218] Fix | Delete
args = parser.parse_args()
[219] Fix | Delete
[220] Fix | Delete
if args.subparser == 'init':
[221] Fix | Delete
init()
[222] Fix | Delete
elif args.subparser == 'check':
[223] Fix | Delete
check()
[224] Fix | Delete
else:
[225] Fix | Delete
parser.print_help()
[226] Fix | Delete
# print(f'Command {args.subparser} is not implemented')
[227] Fix | Delete
sys.exit(1)
[228] Fix | Delete
[229] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function