docker sites

This commit is contained in:
usmannasir
2023-12-10 10:58:18 +05:00
8 changed files with 369 additions and 68 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

After

Width:  |  Height:  |  Size: 33 KiB

View File

@@ -5,36 +5,36 @@ from django.shortcuts import HttpResponse
from random import randint from random import randint
from .models import * from .models import *
from xml.etree import ElementTree from xml.etree import ElementTree
from plogical.CyberCPLogFileWriter import CyberCPLogFileWriter as logging
class PHPManager: class PHPManager:
@staticmethod @staticmethod
def findPHPVersions(): def findPHPVersions():
import re # distro = ProcessUtilities.decideDistro()
import os # if distro == ProcessUtilities.centos:
php_versions = [] # return ['PHP 5.3', 'PHP 5.4', 'PHP 5.5', 'PHP 5.6', 'PHP 7.0', 'PHP 7.1', 'PHP 7.2', 'PHP 7.3', 'PHP 7.4', 'PHP 8.0', 'PHP 8.1']
lsws_directory = "/usr/local/lsws" # elif distro == ProcessUtilities.cent8:
# return ['PHP 7.1','PHP 7.2', 'PHP 7.3', 'PHP 7.4', 'PHP 8.0', 'PHP 8.1']
# elif distro == ProcessUtilities.ubuntu20:
# return ['PHP 7.2', 'PHP 7.3', 'PHP 7.4', 'PHP 8.0', 'PHP 8.1']
# else:
# return ['PHP 7.0', 'PHP 7.1', 'PHP 7.2', 'PHP 7.3', 'PHP 7.4', 'PHP 8.0', 'PHP 8.1']
if os.path.exists(lsws_directory): try:
for dir_name in os.listdir(lsws_directory):
full_path = os.path.join(lsws_directory, dir_name)
if os.path.isdir(full_path) and dir_name.startswith("lsphp"):
php_version = dir_name.replace("lsphp", "PHP ").replace("", ".")
php_versions.append(php_version)
result_list = [] # Run the shell command and capture the output
for item in sorted(php_versions): result = ProcessUtilities.outputExecutioner('ls -la /usr/local/lsws')
# Use regular expression to find numbers in the string
numbers = re.findall(r'\d+', item)
# Join the numbers with dots and add 'PHP' back to the string # Get the lines containing 'lsphp' in the output
result = 'PHP ' + '.'.join(numbers) lsphp_lines = [line for line in result.split('\n') if 'lsphp' in line]
result_list.append(result) # Extract the version from the lines and format it as 'PHP x.y'
php_versions = ['PHP ' + line.split()[8][5] + '.' + line.split()[8][6:] for line in lsphp_lines]
return sorted(result_list) # Now php_versions contains the formatted PHP versions
return php_versions
except BaseException as msg:
return ['PHP 7.0', 'PHP 7.1', 'PHP 7.2', 'PHP 7.3', 'PHP 7.4', 'PHP 8.0', 'PHP 8.1']
@staticmethod @staticmethod
def getPHPString(phpVersion): def getPHPString(phpVersion):

155
plogical/DockerSites.py Normal file
View File

@@ -0,0 +1,155 @@
from plogical.processUtilities import ProcessUtilities
from plogical.CyberCPLogFileWriter import CyberCPLogFileWriter as logging
class DockerSites:
def __init__(self, data):
self.data = data
self.JobID = self.data['JobID'] ##JOBID will be file path where status is being written
pass
def InstallDocker(self):
command = 'apt install docker-compose -y'
ReturnCode = ProcessUtilities.executioner(command)
if ReturnCode:
return 1, None
else:
return 0, ReturnCode
# Takes
# ComposePath, MySQLPath, MySQLRootPass, MySQLDBName, MySQLDBNUser, MySQLPassword, CPUsMySQL, MemoryMySQL,
# port, SitePath, CPUsSite, MemorySite, ComposePath, SiteName
# finalURL, blogTitle, adminUser, adminPassword, adminEmail
def DeployWPContainer(self):
try:
logging.statusWriter(self.JobID, 'Checking if Docker is installed..,0')
command = 'docker --help'
ReturnCode = ProcessUtilities.executioner(command)
if ReturnCode == 0:
status, message = self.InstallDocker()
if status == 0:
logging.statusWriter(self.JobID, 'Failed to installed docker. [404]')
return 0, message
logging.statusWriter(self.JobID, 'Docker is ready to use..,10')
WPSite = f"""
version: "3.8"
services:
db:
image: mysql:5.7
restart: always
volumes:
- "{self.data['MySQLPath']}:/var/lib/mysql"
environment:
MYSQL_ROOT_PASSWORD: {self.data['MySQLRootPass']}
MYSQL_DATABASE: {self.data['MySQLDBName']}
MYSQL_USER: {self.data['MySQLDBNUser']}
MYSQL_PASSWORD: {self.data['MySQLPassword']}
deploy:
resources:
limits:
cpus: '{self.data['CPUsMySQL']}' # Use 50% of one CPU core
memory: {self.data['MemoryMySQL']}M # Limit memory to 512 megabytes
wordpress:
depends_on:
- db
image: wordpress:latest
restart: always
ports:
- "{self.data['port']}:80"
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: {self.data['MySQLDBNUser']}
WORDPRESS_DB_PASSWORD: {self.data['MySQLPassword']}
WORDPRESS_DB_NAME: {self.data['MySQLDBName']}
volumes:
- "{self.data['SitePath']}:/var/www/html"
deploy:
resources:
limits:
cpus: '{self.data['CPUsSite']}' # Use 50% of one CPU core
memory: {self.data['MemorySite']}M # Limit memory to 512 megabytes
volumes:
mysql: {{}}
"""
### WriteConfig to compose-file
WriteToFile = open(self.data['ComposePath'], 'w')
WriteToFile.write(WPSite)
WriteToFile.close()
####
command = f"docker-compose -f {self.data['ComposePath']} -p '{self.data['SiteName']}' up -d"
ReturnCode = ProcessUtilities.executioner(command)
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'
ReturnCode = ProcessUtilities.executioner(command)
command = f"docker-compose -f {self.data['ComposePath']} exec {self.ContainerID} chmod + wp-cli.phar"
ReturnCode = ProcessUtilities.executioner(command)
command = f"docker-compose -f {self.data['ComposePath']} exec {self.ContainerID} mv wp-cli.phar /bin/wp"
ReturnCode = ProcessUtilities.executioner(command)
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'
ReturnCode = ProcessUtilities.executioner(command)
except BaseException as msg:
print(str(msg))
pass
def Main():
try:
# Takes
# ComposePath, MySQLPath, MySQLRootPass, MySQLDBName, MySQLDBNUser, MySQLPassword, CPUsMySQL, MemoryMySQL,
# port, SitePath, CPUsSite, MemorySite, SiteName
# finalURL, blogTitle, adminUser, adminPassword, adminEmail
data = {
"JobID": 1122344566667778888,
"ComposePath": "/home/dockercloudpagescloud/docker-compose.yml",
"MySQLPath": '/home/dockercloudpagescloud/public_html/sqldocker',
"MySQLRootPass": 'testdbwp12345',
"MySQLDBName": 'testdbwp',
"MySQLDBNUser": 'testdbwp',
"MySQLPassword": 'testdbwp12345',
"CPUsMySQL": '2',
"MemoryMySQL": '512',
"port": '8000',
"SitePath": '/home/dockercloudpagescloud/public_html/wpdocker',
"CPUsSite": '2',
"MemorySite": '512',
"SiteName": 'wp docker test',
"finalURL": '95.217.125.218:8001',
"blogTitle": 'testdbwp',
"adminUser": 'testdbwp',
"adminPassword": 'testdbwp',
"adminEmail": 'testdbwp',
}
ds = DockerSites(data)
ds.DeployWPContainer()
except BaseException as msg:
print(str(msg))
pass
if __name__ == "__main__":
Main()

View File

@@ -1,6 +1,7 @@
#!/usr/local/CyberCP/bin/python #!/usr/local/CyberCP/bin/python
import os,sys import os,sys
from ApachController.ApacheVhosts import ApacheVhost
from manageServices.models import PDNSStatus from manageServices.models import PDNSStatus
from .processUtilities import ProcessUtilities from .processUtilities import ProcessUtilities
@@ -676,8 +677,6 @@ class ACLManager:
@staticmethod @staticmethod
def checkOwnership(domain, admin, currentACL): def checkOwnership(domain, admin, currentACL):
try: try:
childDomain = ChildDomains.objects.get(domain=domain) childDomain = ChildDomains.objects.get(domain=domain)
@@ -997,3 +996,70 @@ class ACLManager:
except BaseException as msg: except BaseException as msg:
return 0, str(msg), None return 0, str(msg), None
@staticmethod
def FindDocRootOfSite(vhostConf,domainName):
try:
if vhostConf == None:
vhostConf = f'/usr/local/lsws/conf/vhosts/{domainName}/vhost.conf'
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
command = "awk '/docRoot/ {print $2}' " + vhostConf
docRoot = ProcessUtilities.outputExecutioner(command, 'root', True).rstrip('\n')
#docRoot = docRoot.replace('$VH_ROOT', f'/home/{domainName}')
return docRoot
else:
command = "awk '/DocumentRoot/ {print $2; exit}' " + vhostConf
docRoot = ProcessUtilities.outputExecutioner(command, 'root', True).rstrip('\n')
return docRoot
except:
pass
@staticmethod
def ReplaceDocRoot(vhostConf, domainName, NewDocRoot):
try:
if vhostConf == None:
vhostConf = f'/usr/local/lsws/conf/vhosts/{domainName}/vhost.conf'
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
#command = f"sed -i 's/docRoot\s\s*.*/docRoot {NewDocRoot}/g " + vhostConf
command = f"sed -i 's#docRoot\s\s*.*#docRoot {NewDocRoot}#g' " + vhostConf
ProcessUtilities.executioner(command, 'root', True)
else:
command = f"sed -i 's#DocumentRoot\s\s*[^[:space:]]*#DocumentRoot {NewDocRoot}#g' " + vhostConf
ProcessUtilities.executioner(command, 'root', True)
except:
pass
@staticmethod
def FindDocRootOfSiteApache(vhostConf, domainName):
try:
finalConfPath = ApacheVhost.configBasePath + domainName + '.conf'
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
if os.path.exists(finalConfPath):
command = "awk '/DocumentRoot/ {print $2; exit}' " + finalConfPath
docRoot = ProcessUtilities.outputExecutioner(command, 'root', True).rstrip('\n')
return docRoot
else:
return None
else:
return None
except:
return None
@staticmethod
def ReplaceDocRootApache(vhostConf, domainName, NewDocRoot):
try:
finalConfPath = ApacheVhost.configBasePath + domainName + '.conf'
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
command = f"sed -i 's#DocumentRoot\s\s*[^[:space:]]*#DocumentRoot {NewDocRoot}#g' " + finalConfPath
ProcessUtilities.executioner(command, 'root', True)
except:
pass

View File

@@ -4,6 +4,7 @@ import os, sys
import shutil import shutil
import time import time
from ApachController.ApacheVhosts import ApacheVhost
from loginSystem.models import Administrator from loginSystem.models import Administrator
from managePHP.phpManager import PHPManager from managePHP.phpManager import PHPManager
from plogical.acl import ACLManager from plogical.acl import ACLManager
@@ -112,7 +113,7 @@ class ApplicationInstaller(multi.Thread):
vhFile = f'/usr/local/lsws/conf/vhosts/{domainName}/vhost.conf' vhFile = f'/usr/local/lsws/conf/vhosts/{domainName}/vhost.conf'
phpPath = phpUtilities.GetPHPVersionFromFile(vhFile) phpPath = phpUtilities.GetPHPVersionFromFile(vhFile, domainName)
### basically for now php 8.0 is being checked ### basically for now php 8.0 is being checked
@@ -188,6 +189,13 @@ class ApplicationInstaller(multi.Thread):
command = 'mkdir -p ' + finalPath command = 'mkdir -p ' + finalPath
ProcessUtilities.executioner(command, externalApp) ProcessUtilities.executioner(command, externalApp)
command = f'rm -rf {finalPath}*'
ProcessUtilities.executioner(command, externalApp)
command = f'rm -rf {finalPath}.*'
ProcessUtilities.executioner(command, externalApp)
## checking for directories/files ## checking for directories/files
if self.dataLossCheck(finalPath, tempStatusPath, externalApp) == 0: if self.dataLossCheck(finalPath, tempStatusPath, externalApp) == 0:
@@ -199,16 +207,20 @@ class ApplicationInstaller(multi.Thread):
statusFile.writelines('Downloading Mautic Core,30') statusFile.writelines('Downloading Mautic Core,30')
statusFile.close() statusFile.close()
command = "wget https://github.com/mautic/mautic/releases/download/%s/%s.zip" % ( #command = "wget https://github.com/mautic/mautic/releases/download/%s/%s.zip" % (
ApplicationInstaller.MauticVersion, ApplicationInstaller.MauticVersion) #ApplicationInstaller.MauticVersion, ApplicationInstaller.MauticVersion)
ProcessUtilities.outputExecutioner(command, externalApp, None, finalPath)
### replace command with composer install
command = f'{phpPath} /usr/bin/composer create-project mautic/recommended-project:^4 {finalPath}'
ProcessUtilities.outputExecutioner(command, externalApp, None)
statusFile = open(tempStatusPath, 'w') statusFile = open(tempStatusPath, 'w')
statusFile.writelines('Extracting Mautic Core,50') statusFile.writelines('Extracting Mautic Core,50')
statusFile.close() statusFile.close()
command = "unzip %s.zip" % (ApplicationInstaller.MauticVersion) ### replace command with composer install
ProcessUtilities.outputExecutioner(command, externalApp, None, finalPath) #command = "unzip %s.zip" % (ApplicationInstaller.MauticVersion)
#ProcessUtilities.outputExecutioner(command, externalApp, None, finalPath)
## ##
@@ -222,53 +234,99 @@ class ApplicationInstaller(multi.Thread):
else: else:
finalURL = domainName finalURL = domainName
ACLManager.CreateSecureDir()
localDB = '%s/%s' % ('/usr/local/CyberCP/tmp', str(randint(1000, 9999)))
localDBContent = """<?php # ACLManager.CreateSecureDir()
// Example local.php to test install (to adapt of course) # localDB = '%s/%s' % ('/usr/local/CyberCP/tmp', str(randint(1000, 9999)))
$parameters = array( #
// Do not set db_driver and mailer_from_name as they are used to assume Mautic is installed # localDBContent = """<?php
'db_host' => 'localhost', # // Example local.php to test install (to adapt of course)
'db_table_prefix' => null, # $parameters = array(
'db_port' => 3306, # // Do not set db_driver and mailer_from_name as they are used to assume Mautic is installed
'db_name' => '%s', # 'db_host' => 'localhost',
'db_user' => '%s', # 'db_table_prefix' => null,
'db_password' => '%s', # 'db_port' => 3306,
'db_backup_tables' => true, # 'db_name' => '%s',
'db_backup_prefix' => 'bak_', # 'db_user' => '%s',
'admin_email' => '%s', # 'db_password' => '%s',
'admin_password' => '%s', # 'db_backup_tables' => true,
'mailer_transport' => null, # 'db_backup_prefix' => 'bak_',
'mailer_host' => null, # 'admin_email' => '%s',
'mailer_port' => null, # 'admin_password' => '%s',
'mailer_user' => null, # 'mailer_transport' => null,
'mailer_password' => null, # 'mailer_host' => null,
'mailer_api_key' => null, # 'mailer_port' => null,
'mailer_encryption' => null, # 'mailer_user' => null,
'mailer_auth_mode' => null, # 'mailer_password' => null,
);""" % (dbName, dbUser, dbPassword, email, password) # 'mailer_api_key' => null,
# 'mailer_encryption' => null,
# 'mailer_auth_mode' => null,
# );""" % (dbName, dbUser, dbPassword, email, password)
#
# writeToFile = open(localDB, 'w')
# writeToFile.write(localDBContent)
# writeToFile.close()
writeToFile = open(localDB, 'w') #command = 'rm -rf %s/app/config/local.php' % (finalPath)
writeToFile.write(localDBContent) #ProcessUtilities.executioner(command, externalApp)
writeToFile.close()
command = 'rm -rf %s/app/config/local.php' % (finalPath) #command = 'chown %s:%s %s' % (externalApp, externalApp, localDB)
ProcessUtilities.executioner(command, externalApp) #ProcessUtilities.executioner(command)
command = 'chown %s:%s %s' % (externalApp, externalApp, localDB) #command = 'cp %s %s/app/config/local.php' % (localDB, finalPath)
ProcessUtilities.executioner(command) #ProcessUtilities.executioner(command, externalApp)
command = 'cp %s %s/app/config/local.php' % (localDB, finalPath) ### replace install command with comspoer soo
ProcessUtilities.executioner(command, externalApp) #command = f"{phpPath} bin/console mautic:install http://%s -f" % (finalURL)
command = f"{phpPath} bin/console mautic:install --db_host='localhost' --db_name='{dbName}' --db_user='{dbUser}' --db_password='{dbPassword}' --admin_username='{username}' --admin_email='{email}' --admin_password='{password}' --db_port='3306' http://{finalURL} -f"
command = f"{phpPath} bin/console mautic:install http://%s -f" % (finalURL)
result = ProcessUtilities.outputExecutioner(command, externalApp, None, finalPath) result = ProcessUtilities.outputExecutioner(command, externalApp, None, finalPath)
if result.find('Install complete') == -1: if result.find('Install complete') == -1:
raise BaseException(result) raise BaseException(result)
os.remove(localDB)
ExistingDocRoot = ACLManager.FindDocRootOfSite(None, domainName)
if ExistingDocRoot.find('docroot') > -1:
ExistingDocRoot = ExistingDocRoot.replace('docroot', '')
NewDocRoot = f'{ExistingDocRoot}/docroot'
ACLManager.ReplaceDocRoot(None, domainName, NewDocRoot)
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
try:
ExistingDocRootApache = ACLManager.FindDocRootOfSiteApache(None, domainName)
if ExistingDocRootApache.find('docroot') == -1:
NewDocRootApache = f'{ExistingDocRootApache}docroot'
else:
NewDocRootApache = ExistingDocRootApache
if ExistingDocRootApache != None:
ACLManager.ReplaceDocRootApache(None, domainName, NewDocRootApache)
except:
pass
### fix incorrect rules in .htaccess of mautic
if ProcessUtilities.decideServer() == ProcessUtilities.ent:
htAccessPath = f'{finalPath}docroot/.htaccess'
command = f"sed -i '/# Fallback for Apache < 2.4/,/<\/IfModule>/d' {htAccessPath}"
ProcessUtilities.executioner(command, externalApp, True)
command = f"sed -i '/# Apache 2.4+/,/<\/IfModule>/d' {htAccessPath}"
ProcessUtilities.executioner(command, externalApp, True)
#os.remove(localDB)
command = f"systemctl restart {ApacheVhost.serviceName}"
ProcessUtilities.normalExecutioner(command)
installUtilities.reStartLiteSpeedSocket() installUtilities.reStartLiteSpeedSocket()
statusFile = open(tempStatusPath, 'w') statusFile = open(tempStatusPath, 'w')
@@ -560,11 +618,17 @@ $parameters = array(
### lets first find php path ### lets first find php path
from plogical.phpUtilities import phpUtilities from plogical.phpUtilities import phpUtilities
vhFile = f'/usr/local/lsws/conf/vhosts/{domainName}/vhost.conf' vhFile = f'/usr/local/lsws/conf/vhosts/{domainName}/vhost.conf'
phpPath = phpUtilities.GetPHPVersionFromFile(vhFile) try:
phpPath = phpUtilities.GetPHPVersionFromFile(vhFile)
except:
phpPath = '/usr/local/lsws/lsphp80/bin/php'
### basically for now php 8.0 is being checked ### basically for now php 8.0 is being checked
@@ -574,6 +638,7 @@ $parameters = array(
statusFile.close() statusFile.close()
phpUtilities.InstallSaidPHP('80') phpUtilities.InstallSaidPHP('80')
finalPath = '' finalPath = ''
self.permPath = '' self.permPath = ''

View File

@@ -9,6 +9,7 @@ import argparse
import os import os
from plogical.mailUtilities import mailUtilities from plogical.mailUtilities import mailUtilities
from plogical.processUtilities import ProcessUtilities from plogical.processUtilities import ProcessUtilities
from ApachController.ApacheVhosts import ApacheVhost
import json import json
from django.urls import reverse from django.urls import reverse
@@ -217,7 +218,18 @@ class phpUtilities:
return msg return msg
@staticmethod @staticmethod
def GetPHPVersionFromFile(vhFile): def GetPHPVersionFromFile(vhFile, domainName=None):
finalConfPath = ApacheVhost.configBasePath + domainName + '.conf'
if os.path.exists(finalConfPath):
command = f'grep -Eo -m 1 "php[0-9]+" {finalConfPath} | sed -n "1p"'
result = ProcessUtilities.outputExecutioner(command, None, True).rstrip('\n')
result = f'/usr/local/lsws/ls{result}/bin/lsphp'
result = result.rsplit("lsphp", 1)[0] + "php"
return result
if os.path.exists('/usr/local/CyberCP/debug'):
logging.CyberCPLogFileWriter.writeToFile(f'VHFile in GetPHPVersion {vhFile}')
if ProcessUtilities.decideServer() == ProcessUtilities.OLS: if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
command = f'grep -Eo "/usr/local/lsws/lsphp[0-9]+/bin/lsphp" {vhFile}' command = f'grep -Eo "/usr/local/lsws/lsphp[0-9]+/bin/lsphp" {vhFile}'
result = ProcessUtilities.outputExecutioner(command, None, True).rstrip('\n') result = ProcessUtilities.outputExecutioner(command, None, True).rstrip('\n')

View File

@@ -10,6 +10,7 @@ try:
from websiteFunctions.models import ChildDomains, Websites from websiteFunctions.models import ChildDomains, Websites
except: except:
pass pass
from plogical.acl import ACLManager
class sslUtilities: class sslUtilities:
@@ -320,7 +321,8 @@ context /.well-known/acme-challenge {
except BaseException as msg: except BaseException as msg:
website = Websites.objects.get(domain=virtualHostName) website = Websites.objects.get(domain=virtualHostName)
externalApp = website.externalApp externalApp = website.externalApp
DocumentRoot = ' DocumentRoot /home/' + virtualHostName + '/public_html\n' docRoot = ACLManager.FindDocRootOfSite(None, virtualHostName)
DocumentRoot = f' DocumentRoot {docRoot}\n'
data = open(completePathToConfigFile, 'r').readlines() data = open(completePathToConfigFile, 'r').readlines()
phpHandler = '' phpHandler = ''

View File

@@ -270,7 +270,8 @@ class sslUtilities:
except BaseException as msg: except BaseException as msg:
website = Websites.objects.get(domain=virtualHostName) website = Websites.objects.get(domain=virtualHostName)
externalApp = website.externalApp externalApp = website.externalApp
DocumentRoot = ' DocumentRoot /home/' + virtualHostName + '/public_html\n' docRoot = ACLManager.FindDocRootOfSite(None, virtualHostName)
DocumentRoot = f' DocumentRoot {docRoot}\n'
data = open(completePathToConfigFile, 'r').readlines() data = open(completePathToConfigFile, 'r').readlines()
phpHandler = '' phpHandler = ''