Files
CyberPanel/plogical/upgrade.py

588 lines
19 KiB
Python
Raw Normal View History

2017-10-24 19:16:36 +05:00
import os
2018-07-19 22:38:37 +05:00
import os.path
import sys
import django
sys.path.append('/usr/local/CyberCP')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
django.setup()
2017-10-24 19:16:36 +05:00
import shlex
import subprocess
2018-04-23 19:23:03 +05:00
import shutil
2017-10-24 19:16:36 +05:00
import requests
2018-04-23 19:23:03 +05:00
import json
2018-05-14 22:26:25 +05:00
import time
2018-07-19 22:38:37 +05:00
from baseTemplate.models import version
2017-10-24 19:16:36 +05:00
class Upgrade:
logPath = "/usr/local/lscp/logs/upgradeLog"
2018-07-19 22:38:37 +05:00
@staticmethod
def stdOut(message):
print("\n\n")
print ("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "] #########################################################################\n")
print("[" + time.strftime("%I-%M-%S-%a-%b-%Y") + "] " + message + "\n")
print ("[" + time.strftime(
"%I-%M-%S-%a-%b-%Y") + "] #########################################################################\n")
2017-10-24 19:16:36 +05:00
@staticmethod
2018-04-23 19:23:03 +05:00
def downloadLink():
2018-07-19 22:38:37 +05:00
try:
url = "https://cyberpanel.net/version.txt"
r = requests.get(url, verify=True)
data = json.loads(r.text)
version_number = str(data['version'])
2018-08-19 00:28:31 +05:00
version_build = str(0)
2018-07-19 22:38:37 +05:00
return (version_number + "." + version_build + ".tar.gz")
except BaseException, msg:
Upgrade.stdOut(str(msg) + ' [downloadLink]')
os._exit(0)
@staticmethod
def setupVirtualEnv():
try:
##
count = 0
while (1):
command = "yum install -y libattr-devel xz-devel gpgme-devel mariadb-devel curl-devel"
res = subprocess.call(shlex.split(command))
if res == 1:
count = count + 1
Upgrade.stdOut(
"Trying to install project dependant modules, trying again, try number: " + str(count))
if count == 3:
Upgrade.stdOut("Failed to install project dependant modules! [setupVirtualEnv]")
os._exit(0)
else:
Upgrade.stdOut("Project dependant modules installed successfully!!")
break
##
count = 0
while (1):
command = "pip install virtualenv"
res = subprocess.call(shlex.split(command))
if res == 1:
count = count + 1
Upgrade.stdOut(
"Trying to install virtualenv, trying again, try number: " + str(count))
if count == 3:
Upgrade.stdOut("Failed install virtualenv! [setupVirtualEnv]")
os._exit(0)
else:
Upgrade.stdOut("virtualenv installed successfully!")
break
####
count = 0
while (1):
command = "virtualenv --system-site-packages /usr/local/CyberCP"
res = subprocess.call(shlex.split(command))
if res == 1:
count = count + 1
Upgrade.stdOut(
"Trying to setup virtualenv, trying again, try number: " + str(count))
if count == 3:
Upgrade.stdOut("Failed to setup virtualenv! [setupVirtualEnv]")
os._exit(0)
else:
Upgrade.stdOut("virtualenv setuped successfully!")
break
##
env_path = '/usr/local/CyberCP'
subprocess.call(['virtualenv', env_path])
activate_this = os.path.join(env_path, 'bin', 'activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
##
count = 0
while (1):
command = "pip install --ignore-installed -r /usr/local/CyberCP/requirments.txt"
res = subprocess.call(shlex.split(command))
if res == 1:
count = count + 1
Upgrade.stdOut(
"Trying to install project dependant modules, trying again, try number: " + str(count))
if count == 3:
Upgrade.InstallLog.writeToFile(
"Failed to install project dependant modules! [setupVirtualEnv]")
break
else:
Upgrade.stdOut("Project dependant modules installed successfully!!")
break
command = "systemctl stop gunicorn.socket"
res = subprocess.call(shlex.split(command))
command = "virtualenv --system-site-packages /usr/local/CyberCP"
res = subprocess.call(shlex.split(command))
except OSError, msg:
Upgrade.stdOut(str(msg) + " [setupVirtualEnv]")
os._exit(0)
@staticmethod
def upgradeOpenLiteSpeed():
try:
##
count = 0
while (1):
command = "yum upgrade -y openlitespeed"
res = subprocess.call(shlex.split(command))
if res == 1:
count = count + 1
Upgrade.stdOut(
"Trying to upgrade OpenLiteSpeed, trying again, try number: " + str(count))
if count == 3:
Upgrade.stdOut("Failed to upgrade OpenLiteSpeed! [upgradeOpenLiteSpeed]")
else:
Upgrade.stdOut("OpenLiteSpeed successfully upgraded!")
break
##
except OSError, msg:
Upgrade.stdOut(str(msg) + " [upgradeOpenLiteSpeed]")
os._exit(0)
@staticmethod
def updateGunicornConf():
try:
path = '/etc/systemd/system/gunicorn.service'
cont = """[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
PIDFile=/run/gunicorn/pid
User=cyberpanel
Group=cyberpanel
RuntimeDirectory=gunicorn
WorkingDirectory=/usr/local/CyberCP
ExecStart=/usr/local/CyberCP/bin/gunicorn --pid /run/gunicorn/gucpid \
--bind 127.0.0.1:5003 CyberCP.wsgi
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target"""
writeToFile = open(path, 'w')
writeToFile.write(cont)
writeToFile.close()
command = 'systemctl daemon-reload'
subprocess.call(shlex.split(command))
command = 'systemctl restart gunicorn.socket'
subprocess.call(shlex.split(command))
except BaseException, msg:
Upgrade.stdOut(str(msg) + " [updateGunicornConf]")
os._exit(0)
@staticmethod
def fileManager():
## Copy File manager files
count = 1
while (1):
command = "rm -rf /usr/local/lsws/Example/html/FileManager"
res = subprocess.call(shlex.split(command))
if res == 1:
count = count + 1
Upgrade.stdOut(
"Trying to remove old File manager files, try number: " + str(count))
if count == 3:
Upgrade.stdOut("Failed to remove old File manager files! [upgrade]")
os._exit(0)
else:
Upgrade.stdOut("Old File Manager files successfully removed!")
break
count = 1
while (1):
command = "mv /usr/local/CyberCP/install/FileManager /usr/local/lsws/Example/html"
res = subprocess.call(shlex.split(command))
if res == 1:
count = count + 1
Upgrade.stdOut(
"Trying to upgrade File manager, try number: " + str(count))
if count == 3:
Upgrade.stdOut("Failed to upgrade File manager! [upgrade]")
os._exit(0)
else:
Upgrade.stdOut("File manager successfully upgraded!")
break
command = "chmod -R 777 /usr/local/lsws/Example/html/FileManager"
subprocess.call(shlex.split(command))
2018-07-23 02:09:33 +05:00
@staticmethod
def setupCLI():
try:
count = 0
while (1):
command = "ln -s /usr/local/CyberCP/cli/cyberPanel.py /usr/bin/cyberpanel"
res = subprocess.call(shlex.split(command))
if res == 1:
count = count + 1
Upgrade.stdOut(
"Trying to setup CLI, trying again, try number: " + str(count))
if count == 3:
Upgrade.stdOut(
"Failed to setup CLI! [setupCLI]")
2018-07-26 04:11:10 +05:00
break
2018-07-23 02:09:33 +05:00
else:
Upgrade.stdOut("CLI setup successfull!")
break
command = "chmod +x /usr/local/CyberCP/cli/cyberPanel.py"
res = subprocess.call(shlex.split(command))
except OSError, msg:
Upgrade.stdOut(str(msg) + " [setupCLI]")
return 0
2018-07-19 22:38:37 +05:00
@staticmethod
def staticContent():
count = 1
while (1):
command = "rm -rf /usr/local/lscp/cyberpanel/static"
res = subprocess.call(shlex.split(command))
if res == 1:
count = count + 1
Upgrade.stdOut(
"Trying to remove old static content, try number: " + str(count))
if count == 3:
Upgrade.stdOut("Failed to remove old static content! [upgrade]")
os._exit(0)
else:
Upgrade.stdOut("Static content successfully removed!")
break
count = 1
while (1):
command = "mv /usr/local/CyberCP/static /usr/local/lscp/cyberpanel"
res = subprocess.call(shlex.split(command))
if res == 1:
count = count + 1
Upgrade.stdOut(
"Trying to update static content, try number: " + str(count))
if count == 3:
Upgrade.stdOut("Failed to update static content! [upgrade]")
os._exit(0)
else:
Upgrade.stdOut("Static content in place!")
break
@staticmethod
def upgradeVersion():
vers = version.objects.get(pk=1)
getVersion = requests.get('https://cyberpanel.net/version.txt')
latest = getVersion.json()
vers.currentVersion = latest['version']
vers.build = latest['build']
vers.save()
2017-10-24 19:16:36 +05:00
2018-08-19 00:28:31 +05:00
@staticmethod
def applyLoginSystemMigrations():
try:
cwd = os.getcwd()
os.chdir('/usr/local/CyberCP')
try:
##
command = "python manage.py makemigrations loginSystem"
res = subprocess.call(shlex.split(command))
command = "python manage.py migrate loginSystem"
res = subprocess.call(shlex.split(command))
except:
pass
from loginSystem.models import Administrator, ACL
2017-10-24 19:16:36 +05:00
2018-08-19 00:28:31 +05:00
adminACL = ACL(name='admin', adminStatus=1)
adminACL.save()
## Reseller ACL
resellerACL = ACL(name='reseller',
createNewUser=1,
deleteUser=1,
createWebsite=1,
resellerCenter=1,
modifyWebsite=1,
suspendWebsite=1,
deleteWebsite=1,
createPackage=1,
deletePackage=1,
modifyPackage=1,
createNameServer=1,
restoreBackup=1,
)
resellerACL.save()
## User ACL
userACL = ACL(name='user')
userACL.save()
allUsers = Administrator.objects.all()
for items in allUsers:
if items.userName == 'admin':
items.acl = adminACL
else:
items.acl = userACL
os.chdir(cwd)
except OSError, msg:
Upgrade.stdOut(str(msg) + " [applyLoginSystemMigrations]")
os._exit(0)
2017-10-24 19:16:36 +05:00
2018-04-23 19:23:03 +05:00
@staticmethod
def upgrade():
2017-10-24 19:16:36 +05:00
2018-04-23 19:23:03 +05:00
os.chdir("/usr/local")
2017-10-24 19:16:36 +05:00
2018-07-26 04:11:10 +05:00
2018-07-19 22:38:37 +05:00
## Current Version
Version = version.objects.get(pk=1)
2018-08-19 00:28:31 +05:00
if Version.currentVersion == '1.7' and Version.build == 0:
Upgrade.stdOut('You can not upgrade to v1.7.1 via automatic upgrade.')
os._exit(0)
2018-07-26 04:11:10 +05:00
2018-07-19 22:38:37 +05:00
##
2018-04-23 19:23:03 +05:00
versionNumbring = Upgrade.downloadLink()
2017-10-24 19:16:36 +05:00
2018-08-19 00:28:31 +05:00
2018-07-26 04:11:10 +05:00
if os.path.exists('/usr/local/CyberPanel.' + versionNumbring):
os.remove('/usr/local/CyberPanel.' + versionNumbring)
2018-07-19 22:38:37 +05:00
if float(Version.currentVersion) < 1.6:
Upgrade.stdOut('Upgrades works for version 1.6 onwards.')
os._exit(0)
## RC Check
rcCheck = 1
if os.path.exists('/usr/local/CyberCP/postfixSenderPolicy'):
rcCheck = 0
2018-04-23 19:23:03 +05:00
## Download latest version.
2017-10-24 19:16:36 +05:00
2018-07-19 22:38:37 +05:00
count = 0
while (1):
command = "wget https://cyberpanel.net/CyberPanel." + versionNumbring
res = subprocess.call(shlex.split(command))
if res == 1:
count = count + 1
Upgrade.stdOut(
"Downloading latest version, trying again, try number: " + str(count))
if count == 3:
Upgrade.stdOut("Failed to download latest version! [upgrade]")
os._exit(0)
else:
Upgrade.stdOut("Latest version successfully downloaded!")
break
2017-10-24 19:16:36 +05:00
2018-04-23 19:23:03 +05:00
## Backup settings file.
2017-10-24 19:16:36 +05:00
2018-07-19 22:38:37 +05:00
Upgrade.stdOut("Backing up settings file.")
2018-04-23 19:23:03 +05:00
shutil.copy("/usr/local/CyberCP/CyberCP/settings.py","/usr/local/settings.py")
2017-10-24 19:16:36 +05:00
2018-07-19 22:38:37 +05:00
Upgrade.stdOut("Settings file backed up.")
2018-04-23 19:23:03 +05:00
## Extract Latest files
2017-10-24 19:16:36 +05:00
2018-07-19 22:38:37 +05:00
count = 1
while (1):
command = "tar zxf CyberPanel." + versionNumbring
res = subprocess.call(shlex.split(command))
if res == 1:
count = count + 1
Upgrade.stdOut(
"Trying to extract new version, trying again, try number: " + str(count))
if count == 3:
Upgrade.stdOut("Failed to extract new version! [upgrade]")
os._exit(0)
else:
Upgrade.stdOut("New version successfully extracted!")
break
2017-10-24 19:16:36 +05:00
2018-04-23 19:23:03 +05:00
## Copy settings file
2017-10-24 19:16:36 +05:00
2018-07-19 22:38:37 +05:00
Upgrade.stdOut('Restoring settings file!')
2017-10-24 19:16:36 +05:00
2018-07-19 22:38:37 +05:00
data = open("/usr/local/settings.py", 'r').readlines()
writeToFile = open("/usr/local/CyberCP/CyberCP/settings.py", 'w')
2017-10-24 19:16:36 +05:00
2018-07-19 22:38:37 +05:00
for items in data:
if items.find("'filemanager',") > -1:
writeToFile.writelines(items)
if Version.currentVersion == '1.6':
writeToFile.writelines(" 'emailPremium'\n")
else:
writeToFile.writelines(items)
2017-10-24 19:16:36 +05:00
2018-07-19 22:38:37 +05:00
writeToFile.close()
2017-10-24 19:16:36 +05:00
2018-07-19 22:38:37 +05:00
Upgrade.stdOut('Settings file restored!')
2017-10-24 19:16:36 +05:00
2018-07-19 22:38:37 +05:00
## Move static files
Upgrade.staticContent()
## Upgrade File Manager
Upgrade.fileManager()
2017-10-24 19:16:36 +05:00
2018-04-23 19:23:03 +05:00
## Install TLDExtract
2017-10-24 19:16:36 +05:00
2018-07-19 22:38:37 +05:00
count = 1
while (1):
command = "pip install tldextract"
res = subprocess.call(shlex.split(command))
if res == 1:
count = count + 1
Upgrade.stdOut(
"Trying to install tldextract, trying again, try number: " + str(count))
if count == 3:
Upgrade.stdOut(
"Failed to install tldextract! [upgrade]")
os._exit(0)
else:
Upgrade.stdOut("tldextract successfully installed! [pip]")
break
2017-10-24 19:16:36 +05:00
2018-05-06 14:18:41 +05:00
## Install dnspython
#command = "pip install dnspython"
#subprocess.call(shlex.split(command))
2017-10-24 19:16:36 +05:00
## MailServer Model Changes
2018-07-19 22:38:37 +05:00
if Version.currentVersion == '1.6' and rcCheck :
os.chdir('/usr/local/CyberCP')
count = 1
while (1):
command = "echo 'ALTER TABLE e_forwardings DROP PRIMARY KEY;ALTER TABLE e_forwardings ADD id INT AUTO_INCREMENT PRIMARY KEY;' | python manage.py dbshell"
res = subprocess.check_output(command, shell=True)
if res == 1:
count = count + 1
Upgrade.stdOut(
"Trying to patch database for email forwarding, trying again, try number: " + str(count))
if count == 3:
Upgrade.stdOut(
"Failed to patch database for email forwarding! [upgrade]")
os._exit(0)
else:
Upgrade.stdOut("Database successfully patched for email forwarding!")
break
count = 1
while (1):
command = "python manage.py makemigrations emailPremium"
res = subprocess.call(shlex.split(command))
if res == 1:
count = count + 1
Upgrade.stdOut(
"Trying to setup migration file for email limits, trying again, try number: " + str(count))
if count == 3:
Upgrade.stdOut(
"Failed to setup migration file for email limits! [upgrade]")
os._exit(0)
else:
Upgrade.stdOut("Migrations file for email limits successfully prepared!")
break
count = 1
while (1):
command = "python manage.py migrate emailPremium"
res = subprocess.call(shlex.split(command))
if res == 1:
count = count + 1
Upgrade.stdOut(
"Trying to execute migration file for email limits, trying again, try number: " + str(count))
if count == 3:
Upgrade.stdOut(
"Failed to execute migration file for email limits! [upgrade]")
os._exit(0)
else:
Upgrade.stdOut("Migrations file for email limits successfully executed!")
break
Upgrade.stdOut('Setting up virtual enviroment for CyberPanel.')
Upgrade.setupVirtualEnv()
Upgrade.stdOut('Virtual enviroment for CyberPanel successfully installed.')
if Version.currentVersion == '1.6':
Upgrade.updateGunicornConf()
command = 'systemctl restart gunicorn.socket'
subprocess.call(shlex.split(command))
2017-10-24 19:16:36 +05:00
2018-08-19 00:28:31 +05:00
#if Version.currentVersion == '1.7' and Version.build == 0:
# Upgrade.applyLoginSystemMigrations()
2018-07-19 22:38:37 +05:00
## Upgrade OpenLiteSpeed
2017-10-24 19:16:36 +05:00
2018-07-19 22:38:37 +05:00
Upgrade.upgradeOpenLiteSpeed()
2018-07-23 02:09:33 +05:00
Upgrade.setupCLI()
2018-05-14 22:26:25 +05:00
time.sleep(3)
2018-04-23 19:23:03 +05:00
## Upgrade version
2017-10-24 19:16:36 +05:00
2018-07-19 22:38:37 +05:00
Upgrade.upgradeVersion()
2017-10-24 19:16:36 +05:00
2018-07-19 22:38:37 +05:00
Upgrade.stdOut("Upgrade Completed.")
2017-10-24 19:16:36 +05:00
2018-04-23 19:23:03 +05:00
Upgrade.upgrade()