Files
CyberPanel/plogical/DockerSites.py

1324 lines
50 KiB
Python
Raw Normal View History

2023-12-11 15:28:55 +05:00
#!/usr/local/CyberCP/bin/python
2023-12-20 16:01:01 +05:00
import json
2023-12-18 12:46:32 +05:00
import os
2023-12-11 15:28:55 +05:00
import sys
2023-12-20 16:01:01 +05:00
import time
from random import randint
2023-12-11 15:28:55 +05:00
sys.path.append('/usr/local/CyberCP')
try:
import django
except:
pass
2023-12-21 12:56:49 +05:00
try:
from plogical import randomPassword
from plogical.acl import ACLManager
from dockerManager.dockerInstall import DockerInstall
2023-12-21 12:56:49 +05:00
except:
pass
2023-12-10 10:55:39 +05:00
from plogical.processUtilities import ProcessUtilities
from plogical.CyberCPLogFileWriter import CyberCPLogFileWriter as logging
2023-12-18 12:46:32 +05:00
import argparse
2023-12-20 16:01:01 +05:00
import threading as multi
2023-12-10 10:55:39 +05:00
2023-12-21 12:56:49 +05:00
class Docker_Sites(multi.Thread):
2023-12-20 16:01:01 +05:00
Wordpress = 1
Joomla = 2
2023-12-10 10:55:39 +05:00
2023-12-20 16:01:01 +05:00
def __init__(self, function_run, data):
multi.Thread.__init__(self)
self.function_run = function_run
2023-12-10 10:55:39 +05:00
self.data = data
2023-12-21 12:56:49 +05:00
try:
self.JobID = self.data['JobID'] ##JOBID will be file path where status is being written
except:
pass
2024-01-05 12:42:03 +05:00
try:
### set docker name for listing/deleting etc
if ProcessUtilities.decideDistro() == ProcessUtilities.centos or ProcessUtilities.decideDistro() == ProcessUtilities.cent8:
self.DockerAppName = f"{self.data['name'].replace(' ', '')}-{self.data['name'].replace(' ', '-')}"
else:
self.DockerAppName = f"{self.data['name'].replace(' ', '')}_{self.data['name'].replace(' ', '-')}"
except:
pass
2023-12-10 10:55:39 +05:00
command = 'cat /etc/csf/csf.conf'
result = ProcessUtilities.outputExecutioner(command)
if result.find('SECTION:Initial Settings') > -1:
from plogical.csf import CSF
from plogical.virtualHostUtilities import virtualHostUtilities
currentSettings = CSF.fetchCSFSettings()
tcpIN = currentSettings['tcpIN']
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(f'TCPIN docker: {tcpIN}')
if tcpIN.find('8088') == -1:
ports = f'{tcpIN},8088'
portsPath = '/home/cyberpanel/' + str(randint(1000, 9999))
if os.path.exists(portsPath):
os.remove(portsPath)
writeToFile = open(portsPath, 'w')
writeToFile.write(ports)
writeToFile.close()
execPath = "sudo /usr/local/CyberCP/bin/python " + virtualHostUtilities.cyberPanel + "/plogical/csf.py"
execPath = execPath + f" modifyPorts --protocol TCP_IN --ports " + portsPath
ProcessUtilities.executioner(execPath)
tcpOUT = currentSettings['tcpOUT']
if tcpOUT.find('8088') == -1:
ports = f'{tcpOUT},8088'
portsPath = '/home/cyberpanel/' + str(randint(1000, 9999))
if os.path.exists(portsPath):
os.remove(portsPath)
writeToFile = open(portsPath, 'w')
writeToFile.write(ports)
writeToFile.close()
execPath = "sudo /usr/local/CyberCP/bin/python " + virtualHostUtilities.cyberPanel + "/plogical/csf.py"
execPath = execPath + f" modifyPorts --protocol TCP_OUT --ports " + portsPath
ProcessUtilities.executioner(execPath)
2023-12-20 16:01:01 +05:00
def run(self):
try:
if self.function_run == 'DeployWPContainer':
self.DeployWPContainer()
2023-12-21 12:56:49 +05:00
elif self.function_run == 'SubmitDockersiteCreation':
2023-12-20 16:01:01 +05:00
self.SubmitDockersiteCreation()
2023-12-27 17:41:04 +05:00
elif self.function_run == 'DeployN8NContainer':
self.DeployN8NContainer()
2023-12-20 16:01:01 +05:00
except BaseException as msg:
logging.writeToFile(str(msg) + ' [Docker_Sites.run]')
2023-12-10 10:55:39 +05:00
def InstallDocker(self):
2024-01-04 18:03:54 +05:00
if ProcessUtilities.decideDistro() == ProcessUtilities.centos or ProcessUtilities.decideDistro() == ProcessUtilities.cent8:
2024-01-05 09:46:59 +05:00
command = 'dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo'
ReturnCode = ProcessUtilities.executioner(command)
if ReturnCode:
pass
else:
return 0, ReturnCode
command = 'dnf install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y'
ReturnCode = ProcessUtilities.executioner(command)
2024-01-05 09:58:11 +05:00
if ReturnCode:
pass
else:
return 0, ReturnCode
2024-01-05 10:13:13 +05:00
command = 'systemctl enable docker'
2024-01-05 09:58:11 +05:00
ReturnCode = ProcessUtilities.executioner(command)
if ReturnCode:
pass
else:
return 0, ReturnCode
2024-01-05 10:13:13 +05:00
command = 'systemctl start docker'
ReturnCode = ProcessUtilities.executioner(command)
if ReturnCode:
pass
else:
return 0, ReturnCode
2024-01-05 11:11:59 +05:00
command = 'curl -L "https://github.com/docker/compose/releases/download/v2.23.2/docker-compose-linux-x86_64" -o /usr/bin/docker-compose'
2024-01-05 10:42:00 +05:00
ReturnCode = ProcessUtilities.executioner(command, 'root', True)
2024-01-05 10:13:13 +05:00
if ReturnCode:
pass
else:
return 0, ReturnCode
2024-01-05 11:11:59 +05:00
command = 'chmod +x /usr/bin/docker-compose'
2024-01-05 10:42:00 +05:00
ReturnCode = ProcessUtilities.executioner(command, 'root', True)
2024-01-05 09:58:11 +05:00
2024-01-05 09:46:59 +05:00
if ReturnCode:
return 1, None
else:
return 0, ReturnCode
2024-01-04 18:03:54 +05:00
else:
command = 'apt install docker-compose -y'
2024-01-05 09:46:59 +05:00
ReturnCode = ProcessUtilities.executioner(command)
2023-12-10 10:55:39 +05:00
2024-01-05 09:46:59 +05:00
if ReturnCode:
return 1, None
else:
return 0, ReturnCode
2023-12-10 10:55:39 +05:00
2023-12-18 12:46:32 +05:00
@staticmethod
def SetupProxy(port):
import xml.etree.ElementTree as ET
2023-12-18 12:46:32 +05:00
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
ConfPath = '/usr/local/lsws/conf/httpd_config.conf'
data = open(ConfPath, 'r').read()
2023-12-18 12:46:32 +05:00
StringCheck = f"127.0.0.1:{port}"
if data.find(StringCheck) == -1:
ProxyContent = f"""
extprocessor docker{port} {{
type proxy
address 127.0.0.1:{port}
maxConns 100
pcKeepAliveTimeout 60
initTimeout 60
retryTimeout 0
respBuffer 0
}}
"""
WriteToFile = open(ConfPath, 'a')
WriteToFile.write(ProxyContent)
WriteToFile.close()
else:
ConfPath = '/usr/local/lsws/conf/httpd_config.xml'
data = open(ConfPath, 'r').read()
# Parse the XML
root = ET.fromstring(data)
# Find the <extProcessorList> node
ext_processor_list = root.find('extProcessorList')
# Create the new <extProcessor> node
new_ext_processor = ET.Element('extProcessor')
# Add child elements to the new <extProcessor>
ET.SubElement(new_ext_processor, 'type').text = 'proxy'
ET.SubElement(new_ext_processor, 'name').text = f'docker{port}'
ET.SubElement(new_ext_processor, 'address').text = f'127.0.0.1:{port}'
ET.SubElement(new_ext_processor, 'maxConns').text = '35'
ET.SubElement(new_ext_processor, 'pcKeepAliveTimeout').text = '60'
ET.SubElement(new_ext_processor, 'initTimeout').text = '60'
ET.SubElement(new_ext_processor, 'retryTimeout').text = '60'
ET.SubElement(new_ext_processor, 'respBuffer').text = '0'
# Append the new <extProcessor> to the <extProcessorList>
ext_processor_list.append(new_ext_processor)
# Write the updated XML content to a new file or print it out
tree = ET.ElementTree(root)
tree.write(ConfPath, encoding='UTF-8', xml_declaration=True)
# Optionally, print the updated XML
ET.dump(root)
2023-12-18 12:46:32 +05:00
@staticmethod
def SetupHTAccess(port, htaccess):
### Update htaccess
StringCheck = f'docker{port}'
try:
Content = open(htaccess, 'r').read()
except:
Content = ''
print(f'value of content {Content}')
if Content.find(StringCheck) == -1:
HTAccessContent = f'''
RewriteEngine On
REWRITERULE ^(.*)$ HTTP://docker{port}/$1 [P]
'''
WriteToFile = open(htaccess, 'a')
WriteToFile.write(HTAccessContent)
WriteToFile.close()
2023-12-10 10:55:39 +05:00
# Takes
# ComposePath, MySQLPath, MySQLRootPass, MySQLDBName, MySQLDBNUser, MySQLPassword, CPUsMySQL, MemoryMySQL,
# port, SitePath, CPUsSite, MemorySite, ComposePath, SiteName
2023-12-18 12:46:32 +05:00
# finalURL, blogTitle, adminUser, adminPassword, adminEmail, htaccessPath, externalApp
2023-12-10 10:55:39 +05:00
def DeployWPContainer(self):
2023-12-18 12:46:32 +05:00
2023-12-10 10:55:39 +05:00
try:
logging.statusWriter(self.JobID, 'Checking if Docker is installed..,0')
command = 'docker --help'
2023-12-11 15:28:55 +05:00
result = ProcessUtilities.outputExecutioner(command)
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(f'return code of docker install {result}')
2023-12-11 15:28:55 +05:00
if result.find("not found") > -1:
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(f'About to run docker install function...')
execPath = "/usr/local/CyberCP/bin/python /usr/local/CyberCP/dockerManager/dockerInstall.py"
ProcessUtilities.executioner(execPath)
2023-12-10 10:55:39 +05:00
logging.statusWriter(self.JobID, 'Docker is ready to use..,10')
2023-12-21 12:56:49 +05:00
self.data['ServiceName'] = self.data["SiteName"].replace(' ', '-')
2023-12-18 12:46:32 +05:00
WPSite = f'''
version: '3.8'
2023-12-10 10:55:39 +05:00
services:
2023-12-21 12:56:49 +05:00
'{self.data['ServiceName']}':
2023-12-18 12:46:32 +05:00
user: root
image: cyberpanel/openlitespeed:latest
ports:
- "{self.data['port']}:8088"
# - "443:443"
2023-12-10 10:55:39 +05:00
environment:
2023-12-18 12:46:32 +05:00
DB_NAME: "{self.data['MySQLDBName']}"
DB_USER: "{self.data['MySQLDBNUser']}"
DB_PASSWORD: "{self.data['MySQLPassword']}"
WP_ADMIN_EMAIL: "{self.data['adminEmail']}"
WP_ADMIN_USER: "{self.data['adminUser']}"
WP_ADMIN_PASSWORD: "{self.data['adminPassword']}"
WP_URL: {self.data['finalURL']}
2023-12-21 12:56:49 +05:00
DB_Host: '{self.data['ServiceName']}-db:3306'
2023-12-18 12:46:32 +05:00
SITE_NAME: '{self.data['SiteName']}'
volumes:
# - "/home/docker/{self.data['finalURL']}:/usr/local/lsws/Example/html"
- "/home/docker/{self.data['finalURL']}/data:/usr/local/lsws/Example/html"
depends_on:
2023-12-21 12:56:49 +05:00
- '{self.data['ServiceName']}-db'
2023-12-10 10:55:39 +05:00
deploy:
resources:
limits:
2023-12-18 12:46:32 +05:00
cpus: '{self.data['CPUsSite']}' # Use 50% of one CPU core
memory: {self.data['MemorySite']}M # Limit memory to 512 megabytes
2023-12-21 12:56:49 +05:00
'{self.data['ServiceName']}-db':
2023-12-18 12:46:32 +05:00
image: mariadb
2023-12-10 10:55:39 +05:00
restart: always
environment:
2023-12-18 12:46:32 +05:00
# ALLOW_EMPTY_PASSWORD=no
MYSQL_DATABASE: '{self.data['MySQLDBName']}'
MYSQL_USER: '{self.data['MySQLDBNUser']}'
MYSQL_PASSWORD: '{self.data['MySQLPassword']}'
MYSQL_ROOT_PASSWORD: '{self.data['MySQLPassword']}'
2023-12-10 10:55:39 +05:00
volumes:
2023-12-18 12:46:32 +05:00
- "/home/docker/{self.data['finalURL']}/db:/var/lib/mysql"
2023-12-10 10:55:39 +05:00
deploy:
resources:
limits:
2023-12-18 12:46:32 +05:00
cpus: '{self.data['CPUsMySQL']}' # Use 50% of one CPU core
memory: {self.data['MemoryMySQL']}M # Limit memory to 512 megabytes
'''
2023-12-10 10:55:39 +05:00
### WriteConfig to compose-file
command = f"mkdir -p /home/docker/{self.data['finalURL']}"
2023-12-22 12:43:30 +05:00
result, message = ProcessUtilities.outputExecutioner(command, None, None, None, 1)
2023-12-22 12:37:10 +05:00
if result == 0:
logging.statusWriter(self.JobID, f'Error {str(message)} . [404]')
return 0
TempCompose = f'/home/cyberpanel/{self.data["finalURL"]}-docker-compose.yml'
2023-12-21 12:56:49 +05:00
WriteToFile = open(TempCompose, 'w')
2023-12-10 10:55:39 +05:00
WriteToFile.write(WPSite)
WriteToFile.close()
command = f"mv {TempCompose} {self.data['ComposePath']}"
2023-12-22 12:43:30 +05:00
result, message = ProcessUtilities.outputExecutioner(command, None, None, None, 1)
2023-12-22 12:37:10 +05:00
if result == 0:
logging.statusWriter(self.JobID, f'Error {str(message)} . [404]')
return 0
command = f"chmod 600 {self.data['ComposePath']} && chown root:root {self.data['ComposePath']}"
ProcessUtilities.executioner(command, 'root', True)
2023-12-10 10:55:39 +05:00
####
if ProcessUtilities.decideDistro() == ProcessUtilities.cent8 or ProcessUtilities.decideDistro() == ProcessUtilities.centos:
dockerCommand = 'docker compose'
else:
dockerCommand = 'docker-compose'
command = f"{dockerCommand} -f {self.data['ComposePath']} -p '{self.data['SiteName']}' up -d"
2023-12-22 12:43:30 +05:00
result, message = ProcessUtilities.outputExecutioner(command, None, None, None, 1)
2023-12-18 12:46:32 +05:00
2024-01-05 10:13:13 +05:00
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(message)
2023-12-22 12:37:10 +05:00
if result == 0:
logging.statusWriter(self.JobID, f'Error {str(message)} . [404]')
return 0
2023-12-10 10:55:39 +05:00
2023-12-22 15:07:38 +05:00
logging.statusWriter(self.JobID, 'Bringing containers online..,50')
2023-12-22 15:10:04 +05:00
time.sleep(25)
2023-12-22 15:07:38 +05:00
### checking if everything ran properly
passdata = {}
passdata["JobID"] = None
passdata['name'] = self.data['ServiceName']
da = Docker_Sites(None, passdata)
retdata, containers = da.ListContainers()
containers = json.loads(containers)
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(str(containers))
### it means less then two containers which means something went wrong
if len(containers) < 2:
logging.writeToFile(f'Unkonwn error, containers not running. [DeployWPContainer]')
logging.statusWriter(self.JobID, f'Unkonwn error, containers not running. [DeployWPContainer]')
return 0
2023-12-18 12:46:32 +05:00
### Set up Proxy
2023-12-10 10:55:39 +05:00
2023-12-18 12:46:32 +05:00
execPath = "/usr/local/CyberCP/bin/python /usr/local/CyberCP/plogical/DockerSites.py"
execPath = execPath + f" SetupProxy --port {self.data['port']}"
ProcessUtilities.executioner(execPath)
2023-12-10 10:55:39 +05:00
2023-12-18 12:46:32 +05:00
### Set up ht access
2023-12-10 10:55:39 +05:00
2023-12-18 12:46:32 +05:00
execPath = "/usr/local/CyberCP/bin/python /usr/local/CyberCP/plogical/DockerSites.py"
execPath = execPath + f" SetupHTAccess --port {self.data['port']} --htaccess {self.data['htaccessPath']}"
ProcessUtilities.executioner(execPath, self.data['externalApp'])
2023-12-10 10:55:39 +05:00
2023-12-18 12:46:32 +05:00
if ProcessUtilities.decideDistro() == ProcessUtilities.centos or ProcessUtilities.decideDistro() == ProcessUtilities.cent8:
group = 'nobody'
else:
group = 'nogroup'
2023-12-10 10:55:39 +05:00
2023-12-18 12:46:32 +05:00
command = f"chown -R nobody:{group} /home/docker/{self.data['finalURL']}/data"
ProcessUtilities.executioner(command)
2023-12-10 10:55:39 +05:00
2023-12-22 07:15:19 +05:00
### just restart ls for htaccess
from plogical.installUtilities import installUtilities
2023-12-22 12:43:30 +05:00
installUtilities.reStartLiteSpeedSocket()
2023-12-22 07:15:19 +05:00
2023-12-18 14:17:12 +05:00
logging.statusWriter(self.JobID, 'Completed. [200]')
2023-12-18 12:46:32 +05:00
# command = f"docker-compose -f {self.data['ComposePath']} ps -q wordpress"
# stdout = ProcessUtilities.outputExecutioner(command)
#
# self.ContainerID = stdout.rstrip('\n')
# command = f'docker-compose -f {self.data["ComposePath"]} exec {self.ContainerID} curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar'
# result = ProcessUtilities.outputExecutioner(command)
#
# if os.path.exists(ProcessUtilities.debugPath):
# logging.writeToFile(result)
#
# command = f"docker-compose -f {self.data['ComposePath']} exec {self.ContainerID} chmod + wp-cli.phar"
# result = ProcessUtilities.outputExecutioner(command)
#
# if os.path.exists(ProcessUtilities.debugPath):
# logging.writeToFile(result)
#
# command = f"docker-compose -f {self.data['ComposePath']} exec {self.ContainerID} mv wp-cli.phar /bin/wp"
# result = ProcessUtilities.outputExecutioner(command)
#
# if os.path.exists(ProcessUtilities.debugPath):
# logging.writeToFile(result)
# command = f'docker-compose -f {self.data["ComposePath"]} exec {self.ContainerID} wp core install --url="http://{self.data["finalURL"]}" --title="{self.data["blogTitle"]}" --admin_user="{self.data["adminUser"]}" --admin_password="{self.data["adminPassword"]}" --admin_email="{self.data["adminEmail"]}" --path=. --allow-root'
# result = ProcessUtilities.outputExecutioner(command)
#
# if os.path.exists(ProcessUtilities.debugPath):
# logging.writeToFile(result)
2023-12-10 10:55:39 +05:00
except BaseException as msg:
2023-12-18 12:46:32 +05:00
logging.writeToFile(f'{str(msg)}. [DeployWPContainer]')
2023-12-21 12:56:49 +05:00
logging.statusWriter(self.JobID, f'Error {str(msg)} . [404]')
2023-12-10 10:55:39 +05:00
print(str(msg))
pass
2023-12-20 16:01:01 +05:00
def SubmitDockersiteCreation(self):
try:
from websiteFunctions.models import DockerSites, Websites
from websiteFunctions.website import WebsiteManager
tempStatusPath = self.data['JobID']
statusFile = open(tempStatusPath, 'w')
statusFile.writelines('Creating Website...,10')
statusFile.close()
Domain = self.data['Domain']
WPemal = self.data['WPemal']
Owner = self.data['Owner']
userID = self.data['userID']
MysqlCPU = self.data['MysqlCPU']
MYsqlRam = self.data['MYsqlRam']
SiteCPU = self.data['SiteCPU']
SiteRam = self.data['SiteRam']
sitename = self.data['sitename']
WPusername = self.data['WPusername']
WPpasswd = self.data['WPpasswd']
externalApp = self.data['externalApp']
currentTemp = tempStatusPath
DataToPass = {}
DataToPass['tempStatusPath'] = tempStatusPath
DataToPass['domainName'] = Domain
DataToPass['adminEmail'] = WPemal
DataToPass['phpSelection'] = "PHP 8.1"
DataToPass['websiteOwner'] = Owner
DataToPass['package'] = 'Default'
DataToPass['ssl'] = 1
DataToPass['dkimCheck'] = 0
DataToPass['openBasedir'] = 0
DataToPass['mailDomain'] = 0
DataToPass['apacheBackend'] = 0
UserID = userID
2023-12-21 12:56:49 +05:00
if Websites.objects.filter(domain=DataToPass['domainName']).count() == 0:
try:
website = Websites.objects.get(domain=DataToPass['domainName'])
2023-12-20 16:01:01 +05:00
2023-12-21 12:56:49 +05:00
if website.phpSelection == 'PHP 7.3':
website.phpSelection = 'PHP 8.0'
website.save()
if ACLManager.checkOwnership(website.domain, self.data['adminID'],
self.data['currentACL']) == 0:
statusFile = open(tempStatusPath, 'w')
statusFile.writelines('You dont own this site.[404]')
statusFile.close()
except:
ab = WebsiteManager()
coreResult = ab.submitWebsiteCreation(UserID, DataToPass)
coreResult1 = json.loads((coreResult).content)
logging.writeToFile("Creating website result....%s" % coreResult1)
reutrntempath = coreResult1['tempStatusPath']
while (1):
lastLine = open(reutrntempath, 'r').read()
logging.writeToFile("Error web creating lastline ....... %s" % lastLine)
if lastLine.find('[200]') > -1:
break
elif lastLine.find('[404]') > -1:
statusFile = open(currentTemp, 'w')
statusFile.writelines('Failed to Create Website: error: %s. [404]' % lastLine)
statusFile.close()
return 0
else:
statusFile = open(currentTemp, 'w')
statusFile.writelines('Creating Website....,20')
statusFile.close()
time.sleep(2)
2023-12-20 16:01:01 +05:00
statusFile = open(tempStatusPath, 'w')
2023-12-21 12:56:49 +05:00
statusFile.writelines('Creating DockerSite....,30')
2023-12-20 16:01:01 +05:00
statusFile.close()
2023-12-21 12:56:49 +05:00
webobj = Websites.objects.get(domain=Domain)
2023-12-20 16:01:01 +05:00
2023-12-21 12:56:49 +05:00
if webobj.dockersites_set.all().count() > 0:
logging.statusWriter(self.JobID, f'Docker container already exists on this domain. [404]')
return 0
2023-12-20 16:01:01 +05:00
dbname = randomPassword.generate_pass()
dbpasswd = randomPassword.generate_pass()
dbusername = randomPassword.generate_pass()
MySQLRootPass = randomPassword.generate_pass()
2023-12-21 12:56:49 +05:00
if DockerSites.objects.count() == 0:
port = '11000'
else:
port = str(int(DockerSites.objects.last().port) + 1)
2023-12-20 16:01:01 +05:00
f_data = {
"JobID": tempStatusPath,
"ComposePath": f"/home/docker/{Domain}/docker-compose.yml",
2023-12-20 16:01:01 +05:00
"MySQLPath": f'/home/{Domain}/public_html/sqldocker',
"MySQLRootPass": MySQLRootPass,
"MySQLDBName": dbname,
"MySQLDBNUser": dbusername,
"MySQLPassword": dbpasswd,
"CPUsMySQL": MysqlCPU,
"MemoryMySQL": MYsqlRam,
2023-12-21 12:56:49 +05:00
"port": port,
2023-12-20 16:01:01 +05:00
"SitePath": f'/home/{Domain}/public_html/wpdocker',
"CPUsSite": SiteCPU,
"MemorySite": SiteRam,
"SiteName": sitename,
"finalURL": Domain,
"blogTitle": sitename,
"adminUser": WPusername,
"adminPassword": WPpasswd,
"adminEmail": WPemal,
"htaccessPath": f'/home/{Domain}/public_html/.htaccess',
2023-12-21 12:56:49 +05:00
"externalApp": webobj.externalApp,
2023-12-20 16:01:01 +05:00
"docRoot": f"/home/{Domain}"
}
2023-12-21 12:56:49 +05:00
2023-12-20 16:01:01 +05:00
dockersiteobj = DockerSites(
2023-12-21 12:56:49 +05:00
admin=webobj, ComposePath=f"/home/{Domain}/docker-compose.yml",
SitePath=f'/home/{Domain}/public_html/wpdocker',
2023-12-20 16:01:01 +05:00
MySQLPath=f'/home/{Domain}/public_html/sqldocker', SiteType=Docker_Sites.Wordpress, MySQLDBName=dbname,
2023-12-21 12:56:49 +05:00
MySQLDBNUser=dbusername, CPUsMySQL=MysqlCPU, MemoryMySQL=MYsqlRam, port=port, CPUsSite=SiteCPU,
MemorySite=SiteRam,
SiteName=sitename, finalURL=Domain, blogTitle=sitename, adminUser=WPusername, adminEmail=WPemal
2023-12-20 16:01:01 +05:00
)
dockersiteobj.save()
2023-12-27 17:41:04 +05:00
if self.data['App'] == 'WordPress':
background = Docker_Sites('DeployWPContainer', f_data)
background.start()
elif self.data['App'] == 'n8n':
background = Docker_Sites('DeployN8NContainer', f_data)
background.start()
2023-12-10 10:55:39 +05:00
2023-12-20 16:01:01 +05:00
except BaseException as msg:
logging.writeToFile("Error Submit Docker site Creation ....... %s" % str(msg))
return 0
2023-12-18 12:46:32 +05:00
2023-12-21 12:56:49 +05:00
def DeleteDockerApp(self):
try:
command = f'docker-compose -f /home/docker/{self.data["domain"]}/docker-compose.yml down'
ProcessUtilities.executioner(command)
command = f'rm -rf /home/docker/{self.data["domain"]}'
ProcessUtilities.executioner(command)
command = f'rm -f /home/{self.data["domain"]}/public_html/.htaccess'
ProcessUtilities.executioner(command)
2023-12-22 07:41:48 +05:00
### forcefully delete containers
import docker
# Create a Docker client
client = docker.from_env()
2024-01-05 12:42:03 +05:00
FilerValue = self.DockerAppName
2023-12-25 11:26:41 +05:00
2023-12-22 07:41:48 +05:00
# Define the label to filter containers
2023-12-25 11:26:41 +05:00
label_filter = {'name': FilerValue}
2023-12-22 07:41:48 +05:00
# List containers matching the label filter
containers = client.containers.list(filters=label_filter)
logging.writeToFile(f'List of containers {str(containers)}')
for container in containers:
command = f'docker stop {container.short_id}'
ProcessUtilities.executioner(command)
command = f'docker rm {container.short_id}'
ProcessUtilities.executioner(command)
command = f"rm -rf /home/{self.data['domain']}/public_html/.htaccess'"
ProcessUtilities.executioner(command)
2023-12-21 12:56:49 +05:00
from plogical.installUtilities import installUtilities
installUtilities.reStartLiteSpeed()
except BaseException as msg:
logging.writeToFile("Error Delete Docker APP ....... %s" % str(msg))
return 0
## This function need site name which was passed while creating the app
def ListContainers(self):
try:
import docker
# Create a Docker client
client = docker.from_env()
2024-01-05 12:42:03 +05:00
FilerValue = self.DockerAppName
2023-12-25 11:26:41 +05:00
2023-12-21 12:56:49 +05:00
# Define the label to filter containers
2023-12-25 11:26:41 +05:00
label_filter = {'name': FilerValue}
2023-12-21 12:56:49 +05:00
# List containers matching the label filter
containers = client.containers.list(filters=label_filter)
json_data = "["
checker = 0
for container in containers:
dic = {
'id': container.short_id,
'name': container.name,
'status': container.status,
'volumes': container.attrs['HostConfig']['Binds'] if 'HostConfig' in container.attrs else [],
'logs_50': container.logs(tail=50).decode('utf-8'),
'ports': container.attrs['HostConfig']['PortBindings'] if 'HostConfig' in container.attrs else {}
}
if checker == 0:
json_data = json_data + json.dumps(dic)
checker = 1
else:
json_data = json_data + ',' + json.dumps(dic)
json_data = json_data + ']'
return 1, json_data
except BaseException as msg:
logging.writeToFile("List Container ....... %s" % str(msg))
return 0, str(msg)
### pass container id and number of lines to fetch from logs
def ContainerLogs(self):
try:
import docker
# Create a Docker client
client = docker.from_env()
# Get the container by ID
container = client.containers.get(self.data['containerID'])
# Fetch last 'tail' logs for the container
logs = container.logs(tail=self.data['numberOfLines']).decode('utf-8')
return 1, logs
except BaseException as msg:
logging.writeToFile("List Container ....... %s" % str(msg))
return 0, str(msg)
2023-12-21 18:08:24 +05:00
### pass container id and number of lines to fetch from logs
2023-12-22 07:15:19 +05:00
2023-12-21 18:08:24 +05:00
def ContainerInfo(self):
try:
import docker
# Create a Docker client
client = docker.from_env()
# Get the container by ID
container = client.containers.get(self.data['containerID'])
# Fetch container stats
stats = container.stats(stream=False)
dic = {
'id': container.short_id,
'name': container.name,
'status': container.status,
'volumes': container.attrs['HostConfig']['Binds'] if 'HostConfig' in container.attrs else [],
'logs_50': container.logs(tail=50).decode('utf-8'),
'ports': container.attrs['HostConfig']['PortBindings'] if 'HostConfig' in container.attrs else {},
'memory': stats['memory_stats']['usage'],
'cpu' : stats['cpu_stats']['cpu_usage']['total_usage']
}
return 1, dic
except BaseException as msg:
logging.writeToFile("List Container ....... %s" % str(msg))
return 0, str(msg)
2023-12-25 11:26:41 +05:00
def RebuildApp(self):
self.DeleteDockerApp()
self.SubmitDockersiteCreation()
2023-12-27 18:41:53 +05:00
def RestartContainer(self):
try:
import docker
# Create a Docker client
client = docker.from_env()
# Get the container by ID
container = client.containers.get(self.data['containerID'])
container.restart()
return 1, None
except BaseException as msg:
logging.writeToFile("List Container ....... %s" % str(msg))
return 0, str(msg)
def StopContainer(self):
try:
import docker
# Create a Docker client
client = docker.from_env()
# Get the container by ID
container = client.containers.get(self.data['containerID'])
container.stop()
return 1, None
except BaseException as msg:
logging.writeToFile("List Container ....... %s" % str(msg))
return 0, str(msg)
2023-12-26 12:46:54 +05:00
##### N8N Container
2025-04-09 16:01:42 +05:00
def setup_n8n_data_directory(self):
"""Helper method to create and set up n8n data directory with proper permissions"""
2025-04-09 16:57:30 +05:00
try:
# Create base n8n data directory
base_dir = f"/home/docker/{self.data['finalURL']}/n8n_data"
2025-04-10 00:52:28 +05:00
2025-04-10 02:57:36 +05:00
# Create each directory in the path with proper permissions
path_parts = base_dir.split('/')
current_path = ''
2025-04-10 00:52:28 +05:00
2025-04-10 02:57:36 +05:00
# Build path incrementally ensuring proper permissions at each level
for part in path_parts:
if part: # Skip empty parts
current_path += f'/{part}'
# Create directory if it doesn't exist
command = f"mkdir -p {current_path}"
ProcessUtilities.executioner(command)
# Set ownership and permissions
if current_path == '/home/docker':
# Root directory
command = f"chown root:root {current_path}"
ProcessUtilities.executioner(command)
command = f"chmod 755 {current_path}"
ProcessUtilities.executioner(command)
else:
# n8n directories
command = f"chown 1000:1000 {current_path}"
ProcessUtilities.executioner(command)
command = f"chmod 755 {current_path}"
ProcessUtilities.executioner(command)
2025-04-10 01:55:22 +05:00
2025-04-10 01:19:08 +05:00
# Generate encryption key - store in self.data for use in docker-compose
2025-04-10 02:21:43 +05:00
encryption_key = randomPassword.generate_pass(32)
2025-04-09 16:57:30 +05:00
self.data['N8N_ENCRYPTION_KEY'] = encryption_key
2025-04-10 02:57:36 +05:00
# Create n8n subdirectories
2025-04-10 00:52:28 +05:00
required_dirs = [
f"{base_dir}/.n8n",
f"{base_dir}/.n8n/.n8n",
f"{base_dir}/.n8n/database",
f"{base_dir}/.n8n/workflows",
f"{base_dir}/.n8n/credentials"
]
2025-04-10 02:57:36 +05:00
# Create each required directory with proper permissions
2025-04-10 00:52:28 +05:00
for directory in required_dirs:
command = f"mkdir -p {directory}"
ProcessUtilities.executioner(command)
2025-04-10 01:55:22 +05:00
command = f"chown 1000:1000 {directory}"
2025-04-10 00:52:28 +05:00
ProcessUtilities.executioner(command)
command = f"chmod 755 {directory}"
ProcessUtilities.executioner(command)
2025-04-10 02:57:36 +05:00
# Create n8n config with the encryption key
2025-04-09 16:57:30 +05:00
config_content = {
2025-04-10 02:50:04 +05:00
"database": {
"type": "postgresdb",
"postgresdb": {
"host": f"{self.data['ServiceName']}-db",
"port": 5432,
"database": self.data['MySQLDBName'],
"user": self.data['MySQLDBNUser'],
"password": self.data['MySQLPassword']
}
},
"credentials": {
"overwrite": {
"defaults": False,
"oauth2": False
}
},
"userManagement": {
"disabled": False,
"jwtAuth": {
"jwtExpiration": "7d",
"jwtRefreshExpiration": "30d"
}
},
"nodes": {
"exclude": [],
"include": []
},
2025-04-09 16:57:30 +05:00
"encryptionKey": encryption_key,
2025-04-10 02:50:04 +05:00
"onboardingCallPromptEnabled": False,
2025-04-09 16:42:51 +05:00
"instanceId": f"n8n_{randomPassword.generate_pass(12)}",
2025-04-10 02:50:04 +05:00
"deployment": {
"type": "default"
},
"generic": {
"timezone": "UTC"
},
"security": {
"basicAuth": {
"active": True,
"user": self.data['adminUser'],
"password": self.data['MySQLPassword']
}
},
"endpoints": {
"rest": "/"
2025-04-09 16:57:30 +05:00
},
2025-04-10 02:50:04 +05:00
"executions": {
"process": "main",
"mode": "regular",
"timeout": 3600,
"maxTimeout": 7200
},
"workflowTagsDisabled": False,
"logLevel": "info",
2025-04-09 16:57:30 +05:00
"versionNotifications": {
2025-04-10 02:50:04 +05:00
"enabled": False
2025-04-09 16:57:30 +05:00
}
2025-04-09 16:27:31 +05:00
}
2025-04-09 16:57:30 +05:00
2025-04-09 17:19:08 +05:00
# Write config to a temporary file first
2025-04-10 01:19:08 +05:00
temp_config = f'/home/cyberpanel/{str(randint(1000, 9999))}-config'
2025-04-09 17:19:08 +05:00
with open(temp_config, 'w') as f:
2025-04-09 16:57:30 +05:00
json.dump(config_content, f, indent=2)
2025-04-09 17:19:08 +05:00
# Set proper ownership and permissions on temp file
command = f"chown 1000:1000 {temp_config}"
ProcessUtilities.executioner(command)
command = f"chmod 644 {temp_config}"
2025-04-09 16:57:30 +05:00
ProcessUtilities.executioner(command)
2025-04-10 02:57:36 +05:00
# Create config directory if it doesn't exist
config_dir = f"{base_dir}/.n8n/.n8n"
command = f"mkdir -p {config_dir}"
2025-04-09 16:57:30 +05:00
ProcessUtilities.executioner(command)
2025-04-10 02:57:36 +05:00
command = f"chown 1000:1000 {config_dir}"
2025-04-09 16:57:30 +05:00
ProcessUtilities.executioner(command)
2025-04-10 02:57:36 +05:00
command = f"chmod 755 {config_dir}"
2025-04-10 00:52:28 +05:00
ProcessUtilities.executioner(command)
2025-04-09 17:13:18 +05:00
2025-04-10 02:57:36 +05:00
# Move config to final location
config_file = f"{config_dir}/config"
command = f"mv {temp_config} {config_file}"
2025-04-09 17:13:18 +05:00
ProcessUtilities.executioner(command)
2025-04-10 02:57:36 +05:00
command = f"chmod 600 {config_file}"
2025-04-10 00:52:28 +05:00
ProcessUtilities.executioner(command)
2025-04-10 02:57:36 +05:00
# Create empty .gitignore
gitignore_file = f"{base_dir}/.n8n/.gitignore"
command = f"touch {gitignore_file}"
2025-04-09 17:13:18 +05:00
ProcessUtilities.executioner(command)
2025-04-10 02:57:36 +05:00
command = f"chown 1000:1000 {gitignore_file}"
2025-04-10 00:52:28 +05:00
ProcessUtilities.executioner(command)
2025-04-10 02:57:36 +05:00
command = f"chmod 644 {gitignore_file}"
2025-04-10 00:52:28 +05:00
ProcessUtilities.executioner(command)
2025-04-10 02:50:04 +05:00
# Write debug file to verify encryption key
debug_file = f"{base_dir}/.n8n/.n8n/debug_encryption_key"
2025-04-10 02:57:36 +05:00
# Create debug file with proper permissions first
command = f"touch {debug_file}"
ProcessUtilities.executioner(command)
2025-04-10 02:50:04 +05:00
command = f"chown 1000:1000 {debug_file}"
ProcessUtilities.executioner(command)
2025-04-10 02:57:36 +05:00
2025-04-10 02:50:04 +05:00
command = f"chmod 600 {debug_file}"
ProcessUtilities.executioner(command)
2025-04-10 02:57:36 +05:00
# Now write the content
with open(debug_file, 'w') as f:
f.write(f"Config file key: {encryption_key}\nEnvironment variable: {self.data['N8N_ENCRYPTION_KEY']}")
2025-04-10 00:52:28 +05:00
return True
2025-04-09 16:57:30 +05:00
except BaseException as msg:
logging.writeToFile(f'Error in setup_n8n_data_directory: {str(msg)}')
raise
2025-04-09 16:19:51 +05:00
2023-12-26 12:46:54 +05:00
def DeployN8NContainer(self):
try:
2025-04-09 15:11:09 +05:00
# Initialize container state tracking
self.containerState = {
'docker_installed': False,
'directories_created': False,
'compose_written': False,
'containers_started': False,
'proxy_configured': False
}
2023-12-26 12:46:54 +05:00
2025-04-09 15:18:31 +05:00
# Setup service name first
self.data['ServiceName'] = self.data["SiteName"].replace(' ', '-')
2025-04-09 15:11:09 +05:00
# Validate environment variables
2025-04-10 01:03:51 +05:00
logging.statusWriter(self.JobID, 'Starting environment validation..,2')
2025-04-09 15:11:09 +05:00
self.validate_environment()
logging.statusWriter(self.JobID, 'Environment validation completed..,5')
2023-12-26 12:46:54 +05:00
2025-04-09 15:11:09 +05:00
# Check Docker installation
logging.statusWriter(self.JobID, 'Checking if Docker is installed..,10')
2023-12-26 12:46:54 +05:00
command = 'docker --help'
result = ProcessUtilities.outputExecutioner(command)
2023-12-26 12:46:54 +05:00
if result.find("not found") > -1:
2025-04-10 01:03:51 +05:00
logging.statusWriter(self.JobID, 'Docker not found, installing..,12')
execPath = "/usr/local/CyberCP/bin/python /usr/local/CyberCP/dockerManager/dockerInstall.py"
ProcessUtilities.executioner(execPath)
2023-12-26 12:46:54 +05:00
2025-04-09 15:11:09 +05:00
self.containerState['docker_installed'] = True
logging.statusWriter(self.JobID, 'Docker is ready to use..,15')
2023-12-26 12:46:54 +05:00
2025-04-10 01:10:31 +05:00
# Create and setup base docker directory first
logging.statusWriter(self.JobID, 'Setting up base docker directory..,18')
2025-04-10 01:03:51 +05:00
2025-04-10 01:10:31 +05:00
# Create /home/docker if it doesn't exist
command = "mkdir -p /home/docker"
result = ProcessUtilities.executioner(command)
if not result:
logging.statusWriter(self.JobID, 'Failed to create base docker directory. [404]')
return 0
# Set proper permissions on base docker directory
command = "chown root:root /home/docker"
ProcessUtilities.executioner(command)
command = "chmod 755 /home/docker"
ProcessUtilities.executioner(command)
# Create site-specific directory
2025-04-10 01:03:51 +05:00
parent_dir = f"/home/docker/{self.data['finalURL']}"
2025-04-10 01:10:31 +05:00
logging.statusWriter(self.JobID, f'Creating site directory {parent_dir}..,20')
2025-04-10 01:03:51 +05:00
command = f"mkdir -p {parent_dir}"
result = ProcessUtilities.executioner(command)
if not result:
2025-04-10 01:10:31 +05:00
logging.statusWriter(self.JobID, f'Failed to create site directory {parent_dir}. [404]')
2025-04-09 16:33:34 +05:00
return 0
2025-04-10 01:10:31 +05:00
# Set site directory permissions
2025-04-10 01:03:51 +05:00
command = f"chown root:root {parent_dir}"
ProcessUtilities.executioner(command)
command = f"chmod 755 {parent_dir}"
ProcessUtilities.executioner(command)
2023-12-26 12:46:54 +05:00
2025-04-10 01:03:51 +05:00
# Create backups directory
logging.statusWriter(self.JobID, 'Creating backups directory..,25')
command = f"mkdir -p {parent_dir}/backups"
result = ProcessUtilities.executioner(command)
if not result:
logging.statusWriter(self.JobID, f'Failed to create backups directory. [404]')
2023-12-26 12:46:54 +05:00
return 0
2025-04-10 01:10:31 +05:00
# Set backups directory permissions
command = f"chown root:root {parent_dir}/backups"
ProcessUtilities.executioner(command)
command = f"chmod 755 {parent_dir}/backups"
ProcessUtilities.executioner(command)
2025-04-10 01:03:51 +05:00
# Set up n8n data directory
logging.statusWriter(self.JobID, 'Setting up n8n data directory..,30')
try:
self.setup_n8n_data_directory()
except Exception as e:
logging.statusWriter(self.JobID, f'Failed to set up n8n data directory: {str(e)} [404]')
2023-12-26 12:46:54 +05:00
return 0
2025-04-10 01:03:51 +05:00
if 'N8N_ENCRYPTION_KEY' not in self.data:
logging.statusWriter(self.JobID, f'Error: N8N_ENCRYPTION_KEY not set after directory setup. [404]')
return 0
2025-04-10 01:03:51 +05:00
self.containerState['directories_created'] = True
logging.statusWriter(self.JobID, 'Directory setup completed successfully..,35')
2023-12-26 12:46:54 +05:00
2025-04-10 01:10:31 +05:00
# Generate Docker Compose configuration
compose_config = f'''
version: '3.8'
volumes:
{self.data['ServiceName']}_db:
driver: local
{self.data['ServiceName']}_data:
driver: local
networks:
n8n_net:
driver: bridge
services:
'{self.data['ServiceName']}-db':
image: docker.io/bitnami/postgresql:16
user: root
restart: always
healthcheck:
test: ["CMD-SHELL", "pg_isready -U {self.data['MySQLDBNUser']}"]
interval: 10s
timeout: 5s
retries: 5
environment:
- POSTGRESQL_USERNAME={self.data['MySQLDBNUser']}
- POSTGRESQL_DATABASE={self.data['MySQLDBName']}
- POSTGRESQL_PASSWORD={self.data['MySQLPassword']}
volumes:
- "{self.data['ServiceName']}_db:/bitnami/postgresql"
networks:
- n8n_net
deploy:
resources:
limits:
cpus: '{self.data["CPUsMySQL"]}'
memory: {self.data["MemoryMySQL"]}M
'{self.data['ServiceName']}':
image: docker.n8n.io/n8nio/n8n
user: "1000:1000"
restart: always
healthcheck:
test: ["CMD", "wget", "--spider", "http://localhost:5678"]
interval: 20s
timeout: 10s
retries: 3
environment:
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST={self.data['ServiceName']}-db
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE={self.data['MySQLDBName']}
- DB_POSTGRESDB_USER={self.data['MySQLDBNUser']}
- DB_POSTGRESDB_PASSWORD={self.data['MySQLPassword']}
- N8N_HOST={self.data['finalURL']}
- NODE_ENV=production
- WEBHOOK_URL=https://{self.data['finalURL']}
- N8N_PUSH_BACKEND=sse
- N8N_BASIC_AUTH_ACTIVE=true
- N8N_BASIC_AUTH_USER={self.data['adminUser']}
- N8N_BASIC_AUTH_PASSWORD={self.data['MySQLPassword']}
- N8N_ENCRYPTION_KEY={self.data['N8N_ENCRYPTION_KEY']}
- N8N_USER_FOLDER=/home/node/.n8n
- GENERIC_TIMEZONE=UTC
2025-04-10 02:21:43 +05:00
- N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=false
2025-04-10 01:10:31 +05:00
ports:
- "{self.data['port']}:5678"
volumes:
- "/home/docker/{self.data['finalURL']}/n8n_data:/home/node/.n8n"
depends_on:
{self.data['ServiceName']}-db:
condition: service_healthy
networks:
- n8n_net
deploy:
resources:
limits:
cpus: '{self.data["CPUsSite"]}'
memory: {self.data["MemorySite"]}M
'''
# Write Docker Compose file
logging.statusWriter(self.JobID, 'Writing Docker Compose configuration..,40')
TempCompose = f'/home/cyberpanel/{self.data["finalURL"]}-docker-compose.yml'
WriteToFile = open(TempCompose, 'w')
WriteToFile.write(compose_config)
WriteToFile.close()
command = f"mv {TempCompose} {self.data['ComposePath']}"
result = ProcessUtilities.executioner(command)
if not result:
logging.statusWriter(self.JobID, f'Failed to move compose file to final location. [404]')
return 0
command = f"chmod 600 {self.data['ComposePath']} && chown root:root {self.data['ComposePath']}"
ProcessUtilities.executioner(command)
self.containerState['compose_written'] = True
# Start containers
logging.statusWriter(self.JobID, 'Starting containers..,50')
if ProcessUtilities.decideDistro() == ProcessUtilities.cent8 or ProcessUtilities.decideDistro() == ProcessUtilities.centos:
dockerCommand = 'docker compose'
else:
dockerCommand = 'docker-compose'
command = f"{dockerCommand} -f {self.data['ComposePath']} -p '{self.data['SiteName']}' up -d"
result = ProcessUtilities.executioner(command)
if not result:
logging.statusWriter(self.JobID, f'Failed to start containers. [404]')
return 0
logging.statusWriter(self.JobID, 'Waiting for containers to be healthy..,60')
time.sleep(25)
# Check container health
passdata = {}
passdata["JobID"] = None
passdata['name'] = self.data['ServiceName']
da = Docker_Sites(None, passdata)
retdata, containers = da.ListContainers()
containers = json.loads(containers)
if len(containers) < 2:
logging.writeToFile(f'Unknown error, containers not running. [DeployN8NContainer]')
logging.statusWriter(self.JobID, f'Unknown error, containers not running. [DeployN8NContainer] . [404]')
return 0
self.containerState['containers_started'] = True
# Setup proxy
logging.statusWriter(self.JobID, 'Configuring proxy..,80')
execPath = "/usr/local/CyberCP/bin/python /usr/local/CyberCP/plogical/DockerSites.py"
execPath = execPath + f" SetupProxy --port {self.data['port']}"
ProcessUtilities.executioner(execPath)
execPath = "/usr/local/CyberCP/bin/python /usr/local/CyberCP/plogical/DockerSites.py"
execPath = execPath + f" SetupHTAccess --port {self.data['port']} --htaccess {self.data['htaccessPath']}"
ProcessUtilities.executioner(execPath, self.data['externalApp'])
self.containerState['proxy_configured'] = True
# Restart web server
from plogical.installUtilities import installUtilities
installUtilities.reStartLiteSpeedSocket()
logging.statusWriter(self.JobID, 'Deployment completed successfully. [200]')
2023-12-26 12:46:54 +05:00
except BaseException as msg:
2025-04-09 15:11:09 +05:00
self.cleanup()
2023-12-26 12:46:54 +05:00
logging.writeToFile(f'{str(msg)}. [DeployN8NContainer]')
2025-04-09 15:11:09 +05:00
logging.statusWriter(self.JobID, f'Error: {str(msg)} . [404]')
2023-12-26 12:46:54 +05:00
print(str(msg))
2025-04-09 15:11:09 +05:00
def validate_environment(self):
2025-04-09 15:18:31 +05:00
"""
Validate required environment variables
"""
2025-04-09 15:11:09 +05:00
required_vars = [
'MySQLDBName', 'MySQLDBNUser', 'MySQLPassword',
2025-04-09 15:18:31 +05:00
'finalURL', 'port', 'SiteName', 'adminUser',
2025-04-09 15:11:09 +05:00
'CPUsMySQL', 'MemoryMySQL', 'CPUsSite', 'MemorySite'
]
for var in required_vars:
if var not in self.data or not self.data[var]:
raise Exception(f"Missing required environment variable: {var}")
2025-04-09 15:18:31 +05:00
# Validate numeric values
numeric_vars = ['CPUsMySQL', 'MemoryMySQL', 'CPUsSite', 'MemorySite']
for var in numeric_vars:
try:
float(self.data[var])
except (ValueError, TypeError):
raise Exception(f"Invalid numeric value for {var}: {self.data[var]}")
# Validate minimum memory requirements
if int(self.data['MemoryMySQL']) < 256:
raise Exception("Minimum MySQL memory requirement is 256MB")
if int(self.data['MemorySite']) < 256:
raise Exception("Minimum site memory requirement is 256MB")
2025-04-09 15:11:09 +05:00
def cleanup(self):
try:
if not self.containerState.get('containers_started', False):
command = f"rm -rf /home/docker/{self.data['finalURL']}"
ProcessUtilities.executioner(command)
if self.containerState.get('proxy_configured', False):
# Cleanup proxy configurations if needed
pass
except:
2023-12-26 12:46:54 +05:00
pass
2023-12-21 12:56:49 +05:00
2023-12-10 10:55:39 +05:00
def Main():
try:
2023-12-18 12:46:32 +05:00
parser = argparse.ArgumentParser(description='CyberPanel Docker Sites')
parser.add_argument('function', help='Specify a function to call!')
parser.add_argument('--port', help='')
parser.add_argument('--htaccess', help='')
parser.add_argument('--externalApp', help='')
2023-12-21 12:56:49 +05:00
parser.add_argument('--domain', help='')
2023-12-18 12:46:32 +05:00
args = parser.parse_args()
if args.function == "SetupProxy":
Docker_Sites.SetupProxy(args.port)
2023-12-18 12:46:32 +05:00
elif args.function == 'SetupHTAccess':
Docker_Sites.SetupHTAccess(args.port, args.htaccess)
2023-12-18 12:46:32 +05:00
elif args.function == 'DeployWPDocker':
# Takes
# ComposePath, MySQLPath, MySQLRootPass, MySQLDBName, MySQLDBNUser, MySQLPassword, CPUsMySQL, MemoryMySQL,
# port, SitePath, CPUsSite, MemorySite, SiteName
# finalURL, blogTitle, adminUser, adminPassword, adminEmail, htaccessPath, externalApp
data = {
2023-12-27 17:41:04 +05:00
"JobID": '/home/cyberpanel/hey.txt',
2023-12-18 12:46:32 +05:00
"ComposePath": "/home/docker.cyberpanel.net/docker-compose.yml",
"MySQLPath": '/home/docker.cyberpanel.net/public_html/sqldocker',
"MySQLRootPass": 'testdbwp12345',
"MySQLDBName": 'testdbwp',
"MySQLDBNUser": 'testdbwp',
"MySQLPassword": 'testdbwp12345',
"CPUsMySQL": '2',
"MemoryMySQL": '512',
"port": '8000',
"SitePath": '/home/docker.cyberpanel.net/public_html/wpdocker',
"CPUsSite": '2',
"MemorySite": '512',
"SiteName": 'wp docker test',
"finalURL": 'docker.cyberpanel.net',
"blogTitle": 'docker site',
"adminUser": 'testdbwp',
"adminPassword": 'testdbwp',
"adminEmail": 'usman@cyberpersons.com',
"htaccessPath": '/home/docker.cyberpanel.net/public_html/.htaccess',
"externalApp": 'docke8463',
2023-12-18 12:46:32 +05:00
"docRoot": "/home/docker.cyberpanel.net"
}
ds = Docker_Sites('', data)
2023-12-27 12:55:22 +05:00
ds.DeployN8NContainer()
2023-12-18 12:46:32 +05:00
2023-12-21 12:56:49 +05:00
elif args.function == 'DeleteDockerApp':
data = {
"domain": args.domain}
ds = Docker_Sites('', data)
ds.DeleteDockerApp()
2023-12-18 12:46:32 +05:00
2023-12-10 10:55:39 +05:00
except BaseException as msg:
print(str(msg))
pass
2023-12-21 12:56:49 +05:00
2023-12-10 10:55:39 +05:00
if __name__ == "__main__":
2023-12-21 12:56:49 +05:00
Main()