Files
CyberPanel/plogical/virtualHostUtilities.py

1081 lines
40 KiB
Python
Raw Normal View History

2018-06-30 15:29:56 +05:00
#!/usr/local/CyberCP/bin/python2
2018-06-01 02:08:21 +05:00
import os
2017-10-24 19:16:36 +05:00
import os.path
2018-06-01 02:08:21 +05:00
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 shutil
2017-12-09 22:30:10 +05:00
import argparse
import installUtilities
import sslUtilities
from os.path import join
from os import listdir, rmdir
from shutil import move
2018-05-24 23:37:10 +05:00
from multiprocessing import Process
2018-08-26 04:55:51 +05:00
from websiteFunctions.models import Websites, ChildDomains, aliasDomains
2018-06-01 02:08:21 +05:00
from loginSystem.models import Administrator
from packages.models import Package
import subprocess
import shlex
from plogical.mailUtilities import mailUtilities
import CyberCPLogFileWriter as logging
from dnsUtilities import DNS
from vhost import vhost
2018-07-13 21:45:40 +05:00
from applicationInstaller import ApplicationInstaller
2018-08-22 00:37:43 +05:00
from acl import ACLManager
2018-06-01 02:08:21 +05:00
2017-10-24 19:16:36 +05:00
2018-05-08 21:25:37 +05:00
## If you want justice, you have come to the wrong place.
2017-10-24 19:16:36 +05:00
class virtualHostUtilities:
Server_root = "/usr/local/lsws"
2017-12-09 22:30:10 +05:00
cyberPanel = "/usr/local/CyberCP"
2017-10-24 19:16:36 +05:00
@staticmethod
2018-08-22 00:37:43 +05:00
def createVirtualHost(virtualHostName, administratorEmail, phpVersion, virtualHostUser, ssl,
2018-07-30 17:33:28 +05:00
dkimCheck, openBasedir, websiteOwner, packageName, tempStatusPath = '/home/cyberpanel/fakePath'):
2018-08-22 00:37:43 +05:00
try:
2018-07-30 17:33:28 +05:00
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'Running some checks..,0')
2018-07-30 17:33:28 +05:00
2018-08-22 00:37:43 +05:00
####### Limitations check
admin = Administrator.objects.get(userName=websiteOwner)
if ACLManager.websitesLimitCheck(admin, 1) == 0:
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'You\'ve reached maximum websites limit as a reseller. [404]')
return 0, 'You\'ve reached maximum websites limit as a reseller.'
####### Limitations Check End
2017-10-24 19:16:36 +05:00
2018-06-01 02:08:21 +05:00
if Websites.objects.filter(domain=virtualHostName).count() > 0:
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'This website already exists. [404]')
return 0, "This website already exists."
2017-10-24 19:16:36 +05:00
2018-06-01 02:08:21 +05:00
if ChildDomains.objects.filter(domain=virtualHostName).count() > 0:
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'This website already exists as child domain. [404]')
return 0, "This website already exists as child domain."
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
####### Limitations Check End
2017-12-09 22:30:10 +05:00
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'Creating DNS records..,10')
2018-07-30 17:33:28 +05:00
2018-06-01 02:08:21 +05:00
##### Zone creation
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
DNS.dnsTemplate(virtualHostName, admin)
2017-12-09 22:30:10 +05:00
2018-08-22 00:37:43 +05:00
## Zone creation
2017-12-09 22:30:10 +05:00
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'Setting up directories..,25')
2018-07-30 17:33:28 +05:00
2018-06-01 02:08:21 +05:00
if vhost.checkIfVirtualHostExists(virtualHostName) == 1:
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'Virtual Host Directory already exists. [404]')
return 0, "Virtual Host Directory already exists!"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if vhost.checkIfAliasExists(virtualHostName) == 1:
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'This domain exists as Alias. [404]')
return 0, "This domain exists as Alias."
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if dkimCheck == 1:
if mailUtilities.checkIfDKIMInstalled() == 0:
raise BaseException("OpenDKIM is not installed, install OpenDKIM from DKIM Manager.")
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
retValues = mailUtilities.setupDKIM(virtualHostName)
if retValues[0] == 0:
raise BaseException(retValues[1])
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
retValues = vhost.createDirectoryForVirtualHost(virtualHostName, administratorEmail,
virtualHostUser, phpVersion, openBasedir)
if retValues[0] == 0:
raise BaseException(retValues[1])
2017-12-09 22:30:10 +05:00
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'Creating configurations..,50')
2018-07-30 17:33:28 +05:00
2018-06-01 02:08:21 +05:00
retValues = vhost.createConfigInMainVirtualHostFile(virtualHostName)
if retValues[0] == 0:
raise BaseException(retValues[1])
2017-12-09 22:30:10 +05:00
2018-07-30 17:33:28 +05:00
2018-06-01 02:08:21 +05:00
if ssl == 1:
2018-08-22 00:37:43 +05:00
sslPath = "/home/" + virtualHostName + "/public_html"
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'Setting up SSL..,70')
2018-06-01 02:08:21 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
retValues = sslUtilities.issueSSLForDomain(virtualHostName, administratorEmail, sslPath)
if retValues[0] == 0:
raise BaseException(retValues[1])
else:
installUtilities.installUtilities.reStartLiteSpeed()
2017-11-05 03:02:51 +05:00
2018-06-01 02:08:21 +05:00
if ssl == 0:
installUtilities.installUtilities.reStartLiteSpeed()
2017-12-09 22:30:10 +05:00
2018-06-05 00:53:45 +05:00
vhost.finalizeVhostCreation(virtualHostName, virtualHostUser)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## Create Configurations ends here
2017-11-05 03:02:51 +05:00
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'DKIM Setup..,90')
2018-07-30 17:33:28 +05:00
2018-06-01 02:08:21 +05:00
## DKIM Check
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if dkimCheck == 1:
DNS.createDKIMRecords(virtualHostName)
2018-05-12 02:21:42 +05:00
2018-06-01 02:08:21 +05:00
selectedPackage = Package.objects.get(packageName=packageName)
2018-05-12 02:21:42 +05:00
2018-06-01 02:08:21 +05:00
website = Websites(admin=admin, package=selectedPackage, domain=virtualHostName,
adminEmail=administratorEmail,
phpSelection=phpVersion, ssl=ssl, externalApp=virtualHostUser)
2018-05-12 02:21:42 +05:00
2018-06-01 02:08:21 +05:00
website.save()
2018-05-12 02:21:42 +05:00
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'Website successfully created. [200]')
2018-06-01 02:08:21 +05:00
return 1, 'None'
2017-12-09 22:30:10 +05:00
2018-05-12 02:21:42 +05:00
except BaseException, msg:
2018-08-26 04:55:51 +05:00
vhost.deleteVirtualHostConfigurations(virtualHostName)
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [createVirtualHost]")
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, str(msg) + " [404]")
2018-06-01 02:08:21 +05:00
return 0, str(msg)
2017-11-05 03:02:51 +05:00
2018-05-12 02:21:42 +05:00
@staticmethod
2018-06-01 02:08:21 +05:00
def issueSSL(virtualHost, path, adminEmail):
2017-11-05 03:02:51 +05:00
try:
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
retValues = sslUtilities.issueSSLForDomain(virtualHost, adminEmail, path)
2018-05-12 02:21:42 +05:00
2018-06-01 02:08:21 +05:00
if retValues[0] == 0:
print "0," + str(retValues[1])
2018-08-23 15:39:28 +05:00
return 0, str(retValues[1])
2018-05-12 02:21:42 +05:00
2018-06-01 02:08:21 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
2018-05-12 02:21:42 +05:00
2018-06-01 02:08:21 +05:00
print "1,None"
2018-06-05 00:53:45 +05:00
return 1, None
2017-12-09 22:30:10 +05:00
except BaseException, msg:
2018-08-23 15:39:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [issueSSL]")
2018-06-01 02:08:21 +05:00
print "0," + str(msg)
2018-06-05 00:53:45 +05:00
return 0, str(msg)
2017-10-24 19:16:36 +05:00
@staticmethod
2018-06-01 02:08:21 +05:00
def getAccessLogs(fileName, page):
2017-10-24 19:16:36 +05:00
try:
2018-06-01 02:08:21 +05:00
numberOfTotalLines = int(subprocess.check_output(["wc", "-l", fileName]).split(" ")[0])
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if numberOfTotalLines < 25:
data = subprocess.check_output(["cat", fileName])
else:
if page == 1:
end = numberOfTotalLines
start = end - 24
if start <= 0:
start = 1
startingAndEnding = "'" + str(start) + "," + str(end) + "p'"
command = "sed -n " + startingAndEnding + " " + fileName
proc = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
data = proc.stdout.read()
2017-10-24 19:16:36 +05:00
else:
2018-06-01 02:08:21 +05:00
end = numberOfTotalLines - ((page - 1) * 25)
start = end - 24
if start <= 0:
start = 1
startingAndEnding = "'" + str(start) + "," + str(end) + "p'"
command = "sed -n " + startingAndEnding + " " + fileName
proc = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
data = proc.stdout.read()
print data
2017-10-24 19:16:36 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(
2018-06-01 02:08:21 +05:00
str(msg) + " [getAccessLogs]")
print "1,None"
2017-10-24 19:16:36 +05:00
@staticmethod
2018-06-01 02:08:21 +05:00
def getErrorLogs(fileName, page):
2017-10-24 19:16:36 +05:00
try:
2018-06-01 02:08:21 +05:00
numberOfTotalLines = int(subprocess.check_output(["wc", "-l", fileName]).split(" ")[0])
2017-10-24 19:16:36 +05:00
2018-06-01 02:08:21 +05:00
if numberOfTotalLines < 25:
data = subprocess.check_output(["cat", fileName])
2018-05-24 23:37:10 +05:00
else:
2018-06-01 02:08:21 +05:00
if page == 1:
end = numberOfTotalLines
start = end - 24
if start <= 0:
start = 1
startingAndEnding = "'" + str(start) + "," + str(end) + "p'"
command = "sed -n " + startingAndEnding + " " + fileName
proc = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
data = proc.stdout.read()
else:
end = numberOfTotalLines - ((page - 1) * 25)
start = end - 24
if start <= 0:
start = 1
startingAndEnding = "'" + str(start) + "," + str(end) + "p'"
command = "sed -n " + startingAndEnding + " " + fileName
proc = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
data = proc.stdout.read()
print data
2017-10-24 19:16:36 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(
2018-06-01 02:08:21 +05:00
str(msg) + " [getErrorLogs]")
print "1,None"
2017-10-24 19:16:36 +05:00
@staticmethod
2018-06-01 02:08:21 +05:00
def saveVHostConfigs(fileName, tempPath):
2017-10-24 19:16:36 +05:00
try:
2018-06-01 02:08:21 +05:00
vhost = open(fileName, "w")
2017-10-24 19:16:36 +05:00
2018-06-01 02:08:21 +05:00
vhost.write(open(tempPath, "r").read())
2017-10-24 19:16:36 +05:00
2018-06-01 02:08:21 +05:00
vhost.close()
2017-10-24 19:16:36 +05:00
2018-06-01 02:08:21 +05:00
if os.path.exists(tempPath):
os.remove(tempPath)
2017-10-24 19:16:36 +05:00
2018-06-01 02:08:21 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
2017-12-22 12:36:24 +05:00
2018-06-01 02:08:21 +05:00
print "1,None"
2017-10-24 19:16:36 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(
2018-06-01 02:08:21 +05:00
str(msg) + " [saveVHostConfigs]")
print "0," + str(msg)
2018-01-02 11:49:03 +05:00
@staticmethod
2018-06-01 02:08:21 +05:00
def saveRewriteRules(virtualHost, fileName, tempPath):
2018-01-02 11:49:03 +05:00
try:
2018-06-01 02:08:21 +05:00
vhost.addRewriteRules(virtualHost, fileName)
2018-01-02 11:49:03 +05:00
2018-06-01 02:08:21 +05:00
vhostFile = open(fileName, "w")
vhostFile.write(open(tempPath, "r").read())
vhostFile.close()
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
if os.path.exists(tempPath):
os.remove(tempPath)
2018-05-08 21:25:37 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
2018-06-01 02:08:21 +05:00
print "1,None"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [saveRewriteRules]")
print "0," + str(msg)
2018-05-01 00:49:47 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def installWordPress(domainName, finalPath, virtualHostUser, dbName, dbUser, dbPassword):
try:
2018-05-14 22:26:25 +05:00
2018-06-01 02:08:21 +05:00
FNULL = open(os.devnull, 'w')
2018-05-01 00:49:47 +05:00
2018-06-01 02:08:21 +05:00
if not os.path.exists(finalPath):
os.makedirs(finalPath)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## checking for directories/files
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
dirFiles = os.listdir(finalPath)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if len(dirFiles) == 1:
if dirFiles[0] == ".well-known":
pass
else:
print "0,Target directory should be empty before installation, otherwise data loss could occur."
return
elif len(dirFiles) == 0:
pass
else:
print "0,Target directory should be empty before installation, otherwise data loss could occur."
return
2018-05-03 01:22:28 +05:00
2018-06-01 02:08:21 +05:00
## Get wordpress
2018-05-09 23:10:57 +05:00
2018-06-01 02:08:21 +05:00
if not os.path.exists("latest.tar.gz"):
command = 'wget --no-check-certificate http://wordpress.org/latest.tar.gz -O latest.tar.gz'
cmd = shlex.split(command)
res = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2018-05-03 01:22:28 +05:00
2018-06-01 02:08:21 +05:00
command = 'tar -xzvf latest.tar.gz -C ' + finalPath
2018-05-03 01:22:28 +05:00
2018-06-01 02:08:21 +05:00
cmd = shlex.split(command)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
res = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## Get plugin
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if not os.path.exists("litespeed-cache.1.1.5.1.zip"):
command = 'wget --no-check-certificate https://downloads.wordpress.org/plugin/litespeed-cache.1.1.5.1.zip'
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
cmd = shlex.split(command)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
res = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
command = 'unzip litespeed-cache.1.1.5.1.zip -d ' + finalPath
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
cmd = shlex.split(command)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
res = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
root = finalPath
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
for filename in listdir(join(root, 'wordpress')):
move(join(root, 'wordpress', filename), join(root, filename))
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
rmdir(root + "wordpress")
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
shutil.copytree(finalPath + "litespeed-cache", finalPath + "wp-content/plugins/litespeed-cache")
shutil.rmtree(finalPath + "litespeed-cache")
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## edit config file
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
wpconfigfile = finalPath + "wp-config-sample.php"
2017-11-05 03:02:51 +05:00
2018-06-01 02:08:21 +05:00
data = open(wpconfigfile, "r").readlines()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
writeDataToFile = open(wpconfigfile, "w")
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
defDBName = "define('DB_NAME', '" + dbName + "');" + "\n"
defDBUser = "define('DB_USER', '" + dbUser + "');" + "\n"
defDBPassword = "define('DB_PASSWORD', '" + dbPassword + "');" + "\n"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
for items in data:
if items.find("DB_NAME") > -1:
if items.find("database_name_here") > -1:
writeDataToFile.writelines(defDBName)
elif items.find("DB_USER") > -1:
if items.find("username_here") > -1:
writeDataToFile.writelines(defDBUser)
elif items.find("DB_PASSWORD") > -1:
writeDataToFile.writelines(defDBPassword)
else:
writeDataToFile.writelines(items)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
writeDataToFile.close()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
os.rename(wpconfigfile, finalPath + 'wp-config.php')
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
command = "chown -R " + virtualHostUser + ":" + virtualHostUser + " " + "/home/" + domainName + "/public_html/"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
cmd = shlex.split(command)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
res = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
vhost.addRewriteRules(domainName)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
print "1,None"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
# remove the downloaded files
try:
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
shutil.rmtree(finalPath)
except:
logging.CyberCPLogFileWriter.writeToFile("shutil.rmtree(finalPath)")
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
homeDir = "/home/" + domainName + "/public_html"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if not os.path.exists(homeDir):
FNULL = open(os.devnull, 'w')
os.mkdir(homeDir)
command = "chown -R " + virtualHostUser + ":" + virtualHostUser + " " + homeDir
cmd = shlex.split(command)
res = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
print "0," + str(msg)
return
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def installJoomla(domainName, finalPath, virtualHostUser, dbName, dbUser, dbPassword, username, password, prefix,
2018-07-13 21:45:40 +05:00
sitename, tempStatusPath):
2018-06-01 02:08:21 +05:00
try:
2017-12-09 22:30:10 +05:00
2018-07-13 21:45:40 +05:00
extraArgs = {}
extraArgs['domainName'] = domainName
extraArgs['finalPath'] = finalPath
extraArgs['virtualHostUser'] = virtualHostUser
extraArgs['dbName'] = dbName
extraArgs['dbUser'] = dbUser
extraArgs['dbPassword'] = dbPassword
extraArgs['username'] = username
extraArgs['password'] = password
extraArgs['prefix'] = prefix
extraArgs['sitename'] = sitename
extraArgs['tempStatusPath'] = tempStatusPath
2017-12-09 22:30:10 +05:00
2018-07-13 21:45:40 +05:00
background = ApplicationInstaller('joomla', extraArgs)
background.start()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
2018-07-13 21:45:40 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + ' [installJoomla]')
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def issueSSLForHostName(virtualHost, path):
try:
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
destPrivKey = "/usr/local/lscp/key.pem"
destCert = "/usr/local/lscp/cert.pem"
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
## removing old certs for lscpd
if os.path.exists(destPrivKey):
os.remove(destPrivKey)
if os.path.exists(destCert):
os.remove(destCert)
2018-08-22 00:37:43 +05:00
adminEmail = "email@" + virtualHost
2017-12-14 06:56:27 -05:00
2018-08-22 00:37:43 +05:00
retValues = sslUtilities.issueSSLForDomain(virtualHost, adminEmail, path)
2017-12-14 06:56:27 -05:00
2018-08-22 00:37:43 +05:00
if retValues[0] == 0:
print "0," + str(retValues[1])
return 0, retValues[1]
2017-12-14 06:56:27 -05:00
2018-08-22 00:37:43 +05:00
pathToStoreSSLFullChain = '/etc/letsencrypt/live/' + virtualHost + '/fullchain.pem'
pathToStoreSSLPrivKey = '/etc/letsencrypt/live/' + virtualHost + '/privkey.pem'
2018-02-14 10:39:05 +05:00
2018-06-01 02:08:21 +05:00
shutil.copy(pathToStoreSSLPrivKey, destPrivKey)
shutil.copy(pathToStoreSSLFullChain, destCert)
2018-02-14 10:39:05 +05:00
2018-06-01 02:08:21 +05:00
command = 'systemctl restart lscpd'
2017-12-14 06:56:27 -05:00
cmd = shlex.split(command)
2018-06-01 02:08:21 +05:00
subprocess.call(cmd)
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
print "1,None"
2018-06-05 00:53:45 +05:00
return 1,'None'
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
2018-08-23 15:39:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [issueSSLForHostName]")
2018-06-01 02:08:21 +05:00
print "0," + str(msg)
2018-06-05 00:53:45 +05:00
return 0, str(msg)
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def issueSSLForMailServer(virtualHost, path):
try:
2017-12-14 06:56:27 -05:00
2018-08-22 00:37:43 +05:00
srcFullChain = '/etc/letsencrypt/live/' + virtualHost + '/fullchain.pem'
srcPrivKey = '/etc/letsencrypt/live/' + virtualHost + '/privkey.pem'
2017-12-14 06:56:27 -05:00
2018-08-22 00:37:43 +05:00
adminEmail = "email@" + virtualHost
retValues = sslUtilities.issueSSLForDomain(virtualHost, adminEmail, path)
2017-12-14 06:56:27 -05:00
2018-08-22 00:37:43 +05:00
if retValues[0] == 0:
print "0," + str(retValues[1])
return 0, retValues[1]
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
## MailServer specific functions
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
if os.path.exists("/etc/postfix/cert.pem"):
os.remove("/etc/postfix/cert.pem")
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
if os.path.exists("/etc/postfix/key.pem"):
os.remove("/etc/postfix/key.pem")
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
if os.path.exists("/etc/pki/dovecot/private/dovecot.pem"):
os.remove("/etc/pki/dovecot/private/dovecot.pem")
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
if os.path.exists("/etc/pki/dovecot/certs/dovecot.pem"):
os.remove("/etc/pki/dovecot/certs/dovecot.pem")
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
if os.path.exists("/etc/dovecot/key.pem"):
os.remove("/etc/dovecot/key.pem")
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
if os.path.exists("/etc/dovecot/cert.pem"):
os.remove("/etc/dovecot/cert.pem")
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
## Postfix
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
shutil.copy(srcPrivKey, "/etc/postfix/key.pem")
shutil.copy(srcFullChain, "/etc/postfix/cert.pem")
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
## Dovecot
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
shutil.copy(srcPrivKey, "/etc/pki/dovecot/private/dovecot.pem")
shutil.copy(srcFullChain, "/etc/pki/dovecot/certs/dovecot.pem")
2017-12-16 11:59:47 +05:00
2018-06-01 02:08:21 +05:00
## Dovecot 2ND
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
shutil.copy(srcPrivKey, "/etc/dovecot/key.pem")
shutil.copy(srcFullChain, "/etc/dovecot/cert.pem")
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
## Update postmaster address dovecot
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
filePath = "/etc/dovecot/dovecot.conf"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
data = open(filePath, 'r').readlines()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
writeFile = open(filePath, 'w')
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
for items in data:
if items.find('postmaster_address') > -1:
writeFile.writelines(' postmaster_address = postmaster@' + virtualHost + '\n')
else:
writeFile.writelines(items)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
writeFile.close()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## Update myhostname address postfix
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
filePath = "/etc/postfix/main.cf"
2018-06-01 02:08:21 +05:00
data = open(filePath, 'r').readlines()
2018-06-01 02:08:21 +05:00
writeFile = open(filePath, 'w')
2018-06-01 02:08:21 +05:00
for items in data:
if items.find('myhostname') > -1:
writeFile.writelines('myhostname = ' + virtualHost + '\n')
else:
writeFile.writelines(items)
2018-06-01 02:08:21 +05:00
writeFile.close()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
p = Process(target=mailUtilities.restartServices, args=('restart',))
p.start()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
print "1,None"
2018-06-05 00:53:45 +05:00
return 1,'None'
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [issueSSLForHostName]")
print "0," + str(msg)
2018-06-05 00:53:45 +05:00
return 0,str(msg)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def createAlias(masterDomain, aliasDomain, ssl, sslPath, administratorEmail, owner=None):
try:
2018-04-26 17:59:47 +05:00
2018-08-23 15:39:28 +05:00
admin = Administrator.objects.get(userName=owner)
DNS.dnsTemplate(aliasDomain, admin)
2018-04-26 17:59:47 +05:00
2018-08-23 15:39:28 +05:00
if vhost.checkIfAliasExists(aliasDomain) == 1:
2018-06-01 02:08:21 +05:00
print "0, This domain already exists as vHost or Alias."
return
2018-08-23 15:39:28 +05:00
confPath = os.path.join(virtualHostUtilities.Server_root, "conf/httpd_config.conf")
data = open(confPath, 'r').readlines()
writeToFile = open(confPath, 'w')
listenerTrueCheck = 0
for items in data:
if items.find("listener") > -1 and items.find("Default") > -1:
listenerTrueCheck = 1
if items.find(masterDomain) > -1 and items.find('map') > -1 and listenerTrueCheck == 1:
data = filter(None, items.split(" "))
if data[1] == masterDomain:
writeToFile.writelines(items.rstrip('\n') + ", " + aliasDomain + "\n")
listenerTrueCheck = 0
else:
writeToFile.writelines(items)
writeToFile.close()
installUtilities.installUtilities.reStartLiteSpeed()
2018-06-01 02:08:21 +05:00
if ssl == 1:
retValues = sslUtilities.issueSSLForDomain(masterDomain, administratorEmail, sslPath, aliasDomain)
if retValues[0] == 0:
print "0," + str(retValues[1])
return
else:
vhost.createAliasSSLMap(confPath, masterDomain, aliasDomain)
2018-08-26 04:55:51 +05:00
website = Websites.objects.get(domain=masterDomain)
newAlias = aliasDomains(master=website, aliasDomain = aliasDomain)
newAlias.save()
2018-06-01 02:08:21 +05:00
print "1,None"
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [createAlias]")
print "0," + str(msg)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def issueAliasSSL(masterDomain, aliasDomain, sslPath, administratorEmail):
try:
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
confPath = os.path.join(virtualHostUtilities.Server_root, "conf/httpd_config.conf")
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
retValues = sslUtilities.issueSSLForDomain(masterDomain, administratorEmail, sslPath, aliasDomain)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
if retValues[0] == 0:
print "0," + str(retValues[1])
return
else:
vhost.createAliasSSLMap(confPath, masterDomain, aliasDomain)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
print "1,None"
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [issueAliasSSL]")
print "0," + str(msg)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def deleteAlias(masterDomain, aliasDomain):
try:
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
confPath = os.path.join(virtualHostUtilities.Server_root, "conf/httpd_config.conf")
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
data = open(confPath, 'r').readlines()
writeToFile = open(confPath, 'w')
aliases = []
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
for items in data:
if items.find(masterDomain) > -1 and items.find('map') > -1:
data = filter(None, items.split(" "))
if data[1] == masterDomain:
length = len(data)
for i in range(3, length):
currentAlias = data[i].rstrip(',').strip('\n')
if currentAlias != aliasDomain:
aliases.append(currentAlias)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
aliasString = ""
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
for alias in aliases:
aliasString = ", " + alias
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
writeToFile.writelines(
' map ' + masterDomain + " " + masterDomain + aliasString + "\n")
aliases = []
aliasString = ""
else:
writeToFile.writelines(items)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
else:
writeToFile.writelines(items)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
writeToFile.close()
installUtilities.installUtilities.reStartLiteSpeed()
2018-04-26 17:59:47 +05:00
2018-08-26 04:55:51 +05:00
delAlias = aliasDomains.objects.get(aliasDomain=aliasDomain)
delAlias.delete()
2018-06-01 02:08:21 +05:00
print "1,None"
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [deleteAlias]")
print "0," + str(msg)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def changeOpenBasedir(domainName, openBasedirValue):
try:
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
confPath = virtualHostUtilities.Server_root + "/conf/vhosts/" + domainName
completePathToConfigFile = confPath + "/vhost.conf"
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
data = open(completePathToConfigFile, 'r').readlines()
2018-07-05 15:22:48 +05:00
2018-06-01 02:08:21 +05:00
if openBasedirValue == 'Disable':
writeToFile = open(completePathToConfigFile, 'w')
for items in data:
2018-07-05 15:22:48 +05:00
if items.find('php_admin_value') > -1:
2018-06-01 02:08:21 +05:00
continue
writeToFile.writelines(items)
writeToFile.close()
2018-04-26 17:59:47 +05:00
else:
2018-06-01 02:08:21 +05:00
## Check if phpini already active
2018-04-26 17:59:47 +05:00
2018-07-05 15:22:48 +05:00
fileManagerCheck = 0
writeToFile = open(completePathToConfigFile, 'w')
2018-06-01 02:08:21 +05:00
for items in data:
2018-07-05 15:22:48 +05:00
if items.find('context /.filemanager') > -1:
writeToFile.writelines(items)
fileManagerCheck = 1
continue
2018-06-01 02:08:21 +05:00
if items.find('phpIniOverride') > -1:
2018-07-05 15:22:48 +05:00
writeToFile.writelines(items)
if fileManagerCheck == 1:
writeToFile.writelines('php_admin_value open_basedir "/tmp:/usr/local/lsws/Example/html/FileManager:$VH_ROOT"\n')
fileManagerCheck = 0
continue
2018-06-01 02:08:21 +05:00
else:
2018-07-05 15:22:48 +05:00
writeToFile.writelines('php_admin_value open_basedir "/tmp:$VH_ROOT"\n')
continue
2018-04-26 17:59:47 +05:00
2018-07-05 15:22:48 +05:00
writeToFile.writelines(items)
2018-05-08 21:25:37 +05:00
2018-07-05 15:22:48 +05:00
writeToFile.close()
2018-05-08 21:25:37 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
2018-06-01 02:08:21 +05:00
print "1,None"
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [changeOpenBasedir]")
print "0," + str(msg)
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
2018-08-23 15:39:28 +05:00
def saveSSL(virtualHost, keyPath, certPath):
2018-06-01 02:08:21 +05:00
try:
2018-05-08 21:25:37 +05:00
2018-08-22 00:37:43 +05:00
pathToStoreSSL = '/etc/letsencrypt/live/' + virtualHost
2018-10-03 18:46:44 +05:00
command = 'mkdir -p ' + pathToStoreSSL
subprocess.call(shlex.split(command))
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
pathToStoreSSLPrivKey = pathToStoreSSL + "/privkey.pem"
pathToStoreSSLFullChain = pathToStoreSSL + "/fullchain.pem"
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
privkey = open(pathToStoreSSLPrivKey, 'w')
privkey.write(open(keyPath, "r").read())
privkey.close()
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
fullchain = open(pathToStoreSSLFullChain, 'w')
fullchain.write(open(certPath, "r").read())
fullchain.close()
2018-05-08 21:25:37 +05:00
2018-08-22 00:37:43 +05:00
os.remove(keyPath)
os.remove(certPath)
2018-08-23 15:39:28 +05:00
sslUtilities.sslUtilities.installSSLForDomain(virtualHost)
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
FNULL = open(os.devnull, 'w')
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
command = "chown " + "lsadm" + ":" + "lsadm" + " " + pathToStoreSSL
cmd = shlex.split(command)
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
print "1,None"
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [saveSSL]")
print "0," + str(msg)
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
2018-08-22 00:37:43 +05:00
def createDomain(masterDomain, virtualHostName, phpVersion, path, ssl, dkimCheck, openBasedir, owner, tempStatusPath = '/home/cyberpanel/fakePath'):
2018-06-01 02:08:21 +05:00
try:
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'Running some checks..,0')
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
## Check if this domain either exists as website or child domain
2018-05-08 21:25:37 +05:00
2018-08-22 00:37:43 +05:00
admin = Administrator.objects.get(userName=owner)
DNS.dnsTemplate(virtualHostName, admin)
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
if Websites.objects.filter(domain=virtualHostName).count() > 0:
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'This Domain already exists as a website. [404]')
return 0, "This Domain already exists as a website."
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
if ChildDomains.objects.filter(domain=virtualHostName).count() > 0:
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'This domain already exists as child domain. [404]')
return 0, "This domain already exists as child domain."
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
####### Limitations check
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
master = Websites.objects.get(domain=masterDomain)
domainsInPackage = master.package.allowedDomains
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
if domainsInPackage == 0:
pass
elif domainsInPackage > master.childdomains_set.all().count():
pass
2018-05-08 21:25:37 +05:00
else:
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath,
'Exceeded maximum number of domains for this package. [404]')
return 0, "Exceeded maximum number of domains for this package"
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
####### Limitations Check End
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
if vhost.checkIfVirtualHostExists(virtualHostName) == 1:
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath,'Virtual Host Directory already exists. [404]')
return 0, "Virtual Host Directory already exists!"
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
if vhost.checkIfAliasExists(virtualHostName) == 1:
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath,'This domain exists as Alias. [404]')
return 0, "This domain exists as Alias."
2018-05-21 21:52:35 +05:00
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'DKIM Setup..,30')
2018-07-30 17:33:28 +05:00
2018-06-01 02:08:21 +05:00
if dkimCheck == 1:
if mailUtilities.checkIfDKIMInstalled() == 0:
raise BaseException("OpenDKIM is not installed, install OpenDKIM from DKIM Manager.")
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
retValues = mailUtilities.setupDKIM(virtualHostName)
if retValues[0] == 0:
raise BaseException(retValues[1])
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
FNULL = open(os.devnull, 'w')
2018-05-21 21:52:35 +05:00
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'Creating configurations..,50')
2018-07-30 17:33:28 +05:00
2018-06-01 02:08:21 +05:00
retValues = vhost.createDirectoryForDomain(masterDomain, virtualHostName, phpVersion, path,
master.adminEmail, master.externalApp, openBasedir)
if retValues[0] == 0:
raise BaseException(retValues[1])
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
retValues = vhost.createConfigInMainDomainHostFile(virtualHostName, masterDomain)
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
if retValues[0] == 0:
raise BaseException(retValues[1])
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
## Now restart litespeed after initial configurations are done
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
if ssl == 1:
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'Creating SSL..,50')
2018-06-01 02:08:21 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
retValues = sslUtilities.issueSSLForDomain(virtualHostName, master.adminEmail, path)
installUtilities.installUtilities.reStartLiteSpeed()
if retValues[0] == 0:
raise BaseException(retValues[1])
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## Final Restart
if ssl == 0:
installUtilities.installUtilities.reStartLiteSpeed()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
vhost.finalizeDomainCreation(master.externalApp, path)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## DKIM Check
2017-12-09 22:30:10 +05:00
2018-08-22 00:37:43 +05:00
if dkimCheck == 1:
DNS.createDKIMRecords(virtualHostName)
2017-12-09 22:30:10 +05:00
2018-08-22 00:37:43 +05:00
website = ChildDomains(master=master, domain=virtualHostName, path=path, phpSelection=phpVersion, ssl=ssl)
2018-06-01 02:08:21 +05:00
website.save()
2017-12-09 22:30:10 +05:00
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'Domain successfully created. [200]')
2018-06-01 02:08:21 +05:00
return 1, "None"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
numberOfWebsites = Websites.objects.count() + ChildDomains.objects.count()
2018-06-05 00:53:45 +05:00
vhost.deleteCoreConf(virtualHostName, numberOfWebsites)
2018-08-22 00:37:43 +05:00
logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, str(msg) + ". [404]")
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [createDomain]")
return 0, str(msg)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def deleteDomain(virtualHostName):
try:
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
numberOfWebsites = Websites.objects.count() + ChildDomains.objects.count()
vhost.deleteCoreConf(virtualHostName, numberOfWebsites)
delWebsite = ChildDomains.objects.get(domain=virtualHostName)
delWebsite.delete()
installUtilities.installUtilities.reStartLiteSpeed()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
print "1,None"
2018-06-05 00:53:45 +05:00
return 1,'None'
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [deleteDomain]")
print "0," + str(msg)
2018-06-05 00:53:45 +05:00
return 0,str(msg)
@staticmethod
def getDiskUsage(path, totalAllowed):
try:
totalUsageInMB = subprocess.check_output(["sudo", "du", "-hs", path, "--block-size=1M"]).split()[0]
percentage = float(100) / float(totalAllowed)
percentage = float(percentage) * float(totalUsageInMB)
data = [int(totalUsageInMB), int(percentage)]
return data
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [getDiskUsage]")
return [int(0), int(0)]
2017-12-09 22:30:10 +05:00
2018-06-25 17:53:11 +05:00
@staticmethod
def permissionControl(path):
try:
command = 'sudo chown -R cyberpanel:cyberpanel ' + path
cmd = shlex.split(command)
res = subprocess.call(cmd)
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg))
@staticmethod
def leaveControl(path):
try:
command = 'sudo chown -R root:root ' + path
cmd = shlex.split(command)
res = subprocess.call(cmd)
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg))
2017-12-09 22:30:10 +05:00
def main():
parser = argparse.ArgumentParser(description='CyberPanel Installer')
parser.add_argument('function', help='Specific a function to call!')
parser.add_argument('--virtualHostName', help='Domain name!')
parser.add_argument('--administratorEmail', help='Administration Email!')
parser.add_argument('--phpVersion', help='PHP Version')
parser.add_argument("--virtualHostUser", help="Virtual Host Directory Owner and Group!")
parser.add_argument("--numberOfSites", help="Number of sites!")
parser.add_argument("--ssl", help="Weather to activate SSL")
parser.add_argument("--sslPath", help="Path to website document root!")
2018-05-01 00:49:47 +05:00
parser.add_argument('--dkimCheck', help='To enable or disable DKIM support for domain.')
2018-05-21 21:52:35 +05:00
parser.add_argument('--openBasedir', help='To enable or disable open_basedir protection for domain.')
2018-06-01 02:08:21 +05:00
parser.add_argument('--websiteOwner', help='Website Owner Name')
parser.add_argument('--package', help='Website package')
parser.add_argument('--restore', help='Restore Check.')
2017-12-09 22:30:10 +05:00
## arguments for creation child domains
parser.add_argument('--masterDomain', help='Master Domain Needed While Creating Child Domains!')
parser.add_argument('--path', help='Path Needed for Child domains Creation!')
parser.add_argument('--restart', help='OLS Restart Frequency while child domain creation!')
## arguments for logs
parser.add_argument('--page', help='Page number to fetch logs!')
## arguments for configuration files
parser.add_argument('--tempPath', help='Temporary path where configuration data is placed!')
## save ssl arguments
parser.add_argument('--tempKeyPath', help='Temporary path to store key!')
parser.add_argument('--tempCertPath', help='Temporary path to store cert!')
parser.add_argument('--sslCheck', help='Weather SSL is already activated or not!')
## install wordpress arguments
parser.add_argument('--dbName', help='Database Name!')
parser.add_argument('--dbUser', help='Database User!')
parser.add_argument('--dbPassword', help='Database Password!')
## calculate bw arguments
parser.add_argument('--bandwidth', help='Pack Bandwidth!')
2017-12-14 06:56:27 -05:00
## extras
parser.add_argument('--username', help='Admin Username!')
parser.add_argument('--password', help='Admin Password!')
parser.add_argument('--prefix', help='Database Prefix!')
parser.add_argument('--sitename', help='Site Name!')
2017-12-09 22:30:10 +05:00
2018-05-08 21:25:37 +05:00
## Arguments for alias domain
parser.add_argument('--aliasDomain', help='Alias Domain!')
2018-05-21 21:52:35 +05:00
## Arguments for OpenBasedir
parser.add_argument('--openBasedirValue', help='open_base dir protection value!')
2018-07-13 21:45:40 +05:00
parser.add_argument('--tempStatusPath', help='Temporary Status file path.')
2018-05-08 21:25:37 +05:00
2017-12-09 22:30:10 +05:00
args = parser.parse_args()
if args.function == "createVirtualHost":
2018-05-01 00:49:47 +05:00
try:
dkimCheck = int(args.dkimCheck)
except:
dkimCheck = 0
2018-05-21 21:52:35 +05:00
try:
openBasedir = int(args.openBasedir)
except:
openBasedir = 0
2018-07-30 17:33:28 +05:00
try:
tempStatusPath = args.tempStatusPath
except:
tempStatusPath = '/home/cyberpanel/fakePath'
2018-08-22 00:37:43 +05:00
virtualHostUtilities.createVirtualHost(args.virtualHostName, args.administratorEmail, args.phpVersion, args.virtualHostUser, int(args.ssl), dkimCheck, openBasedir, args.websiteOwner, args.package, tempStatusPath)
2017-12-09 22:30:10 +05:00
elif args.function == "deleteVirtualHostConfigurations":
2018-08-26 04:55:51 +05:00
vhost.deleteVirtualHostConfigurations(args.virtualHostName)
2017-12-09 22:30:10 +05:00
elif args.function == "createDomain":
2018-05-03 01:22:28 +05:00
try:
dkimCheck = int(args.dkimCheck)
except:
dkimCheck = 0
2018-05-21 21:52:35 +05:00
try:
openBasedir = int(args.openBasedir)
except:
openBasedir = 0
2018-07-30 17:33:28 +05:00
try:
tempStatusPath = args.tempStatusPath
except:
tempStatusPath = '/home/cyberpanel/fakePath'
2018-08-22 00:37:43 +05:00
virtualHostUtilities.createDomain(args.masterDomain, args.virtualHostName, args.phpVersion, args.path, int(args.ssl), dkimCheck, openBasedir, args.websiteOwner, tempStatusPath)
2017-12-09 22:30:10 +05:00
elif args.function == "issueSSL":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.issueSSL(args.virtualHostName,args.path,args.administratorEmail)
2017-12-09 22:30:10 +05:00
elif args.function == "changePHP":
2018-06-01 02:08:21 +05:00
vhost.changePHP(args.path,args.phpVersion)
2017-12-09 22:30:10 +05:00
elif args.function == "getAccessLogs":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.getAccessLogs(args.path,int(args.page))
2017-12-09 22:30:10 +05:00
elif args.function == "getErrorLogs":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.getErrorLogs(args.path,int(args.page))
2017-12-09 22:30:10 +05:00
elif args.function == "saveVHostConfigs":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.saveVHostConfigs(args.path,args.tempPath)
2017-12-09 22:30:10 +05:00
elif args.function == "saveRewriteRules":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.saveRewriteRules(args.virtualHostName,args.path,args.tempPath)
2017-12-09 22:30:10 +05:00
elif args.function == "saveSSL":
2018-08-23 15:39:28 +05:00
virtualHostUtilities.saveSSL(args.virtualHostName,args.tempKeyPath,args.tempCertPath)
2017-12-09 22:30:10 +05:00
elif args.function == "installWordPress":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.installWordPress(args.virtualHostName,args.path,args.virtualHostUser,args.dbName,args.dbUser,args.dbPassword)
2017-12-14 06:56:27 -05:00
elif args.function == "installJoomla":
2018-07-13 21:45:40 +05:00
virtualHostUtilities.installJoomla(args.virtualHostName,args.path,args.virtualHostUser,args.dbName,args.dbUser,args.dbPassword,args.username,args.password,args.prefix,args.sitename, args.tempStatusPath)
2017-12-09 22:30:10 +05:00
elif args.function == "issueSSLForHostName":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.issueSSLForHostName(args.virtualHostName,args.path)
2018-04-26 17:59:47 +05:00
elif args.function == "issueSSLForMailServer":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.issueSSLForMailServer(args.virtualHostName,args.path)
2017-12-09 22:30:10 +05:00
elif args.function == "findDomainBW":
2018-06-01 02:08:21 +05:00
vhost.findDomainBW(args.virtualHostName, int(args.bandwidth))
2018-05-08 21:25:37 +05:00
elif args.function == 'createAlias':
2018-06-01 02:08:21 +05:00
virtualHostUtilities.createAlias(args.masterDomain,args.aliasDomain,int(args.ssl),args.sslPath, args.administratorEmail, args.websiteOwner)
2018-05-08 21:25:37 +05:00
elif args.function == 'issueAliasSSL':
2018-06-01 02:08:21 +05:00
virtualHostUtilities.issueAliasSSL(args.masterDomain, args.aliasDomain, args.sslPath, args.administratorEmail)
2018-05-08 21:25:37 +05:00
elif args.function == 'deleteAlias':
2018-06-01 02:08:21 +05:00
virtualHostUtilities.deleteAlias(args.masterDomain, args.aliasDomain)
2018-05-21 21:52:35 +05:00
elif args.function == 'changeOpenBasedir':
2018-06-01 02:08:21 +05:00
virtualHostUtilities.changeOpenBasedir(args.virtualHostName, args.openBasedirValue)
elif args.function == 'deleteDomain':
virtualHostUtilities.deleteDomain(args.virtualHostName)
2017-12-09 22:30:10 +05:00
if __name__ == "__main__":
main()