File: //usr/share/l.v.e-manager/utils/downgrades/clear-resource-usage.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
# This script is used in cpanel-lvemanager.spec in case when lvemanager version < 5.3.7
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import os
import subprocess
import cldetectlib as detect
import shutil
SOURCE_PATH = "/usr/share/l.v.e-manager/"
LVEMANAGER_VER = "/usr/share/l.v.e-manager/version"
ACTUAL_CPANEL_VERSION = '5.3.8'
ACTUAL_DA_VERSION = '5.1.0'
ROOT_DA_DIR = "/usr/local/directadmin/plugins/"
def main():
"""
This function will determine panel name
and execute function according to the panel
:return:
"""
detect.getCP()
if detect.CP_NAME == "cPanel" and is_version_older(ACTUAL_CPANEL_VERSION):
clear_from_cpanel()
elif detect.CP_NAME == "DA" and is_version_older(ACTUAL_DA_VERSION):
clear_from_da()
def current_version():
"""
Return current lvemanager version
:return:
"""
try:
with open(LVEMANAGER_VER) as f:
version = f.read()
return version.split('-')[0]
except Exception as e:
print(e)
def is_version_older(ver):
"""
Compares if the version if older then current version
:param ver:
:return:
"""
try:
current_ver_tuple = tuple([int(i) for i in current_version().split(".")])
support_ver_tuple = tuple([int(i) for i in ver.split(".")])
return current_ver_tuple < support_ver_tuple
except Exception as e:
print(e)
def exec_command(command, env=None):
"""
This function will run passed command
in command line and returns result
:param command:
:param env:
:return:
"""
result = []
try:
p = subprocess.Popen(command, shell=True, executable='/bin/bash', stdout=subprocess.PIPE, env=env)
while 1:
output = p.stdout.readline()
if not output:
break
if output.strip() != "":
result.append(output.strip())
except Exception as e:
print ("Call process error: " + str(e))
return result
def clear_from_da():
"""
Clears the new resource usage plugin
for the DirectAdmin
:return:
"""
print('Starting clear-resource-usage.py script...')
if os.path.exists(ROOT_DA_DIR + "resource_usage"):
shutil.rmtree(ROOT_DA_DIR + "resource_usage")
def clear_from_cpanel():
"""
Clears the new resource usage plugin
for the CPanel
:return:
"""
print('Starting clear-resource-usage.py script...')
if (os.path.exists(SOURCE_PATH + "cpanel/resource_usage/plugin.tar.bz2") and
os.path.exists("/usr/local/cpanel/scripts/uninstall_plugin")):
exec_command("/usr/local/cpanel/scripts/uninstall_plugin " + SOURCE_PATH +
"cpanel/resource_usage/plugin.tar.bz2 --theme paper_lantern")
if os.path.exists("/usr/local/cpanel/base/frontend/paper_lantern/dynamicui/dynamicui_lveresusage.conf"):
exec_command(
"rm /usr/local/cpanel/base/frontend/paper_lantern/dynamicui/dynamicui_lveresusage.conf")
if __name__ == '__main__':
main()