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
|
|
|
|
|
|
2023-12-11 15:28:55 +05:00
|
|
|
sys.path.append('/usr/local/CyberCP')
|
|
|
|
|
import django
|
2023-12-21 12:56:49 +05:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from plogical import randomPassword
|
|
|
|
|
from plogical.acl import ACLManager
|
|
|
|
|
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
|
2023-12-10 10:55:39 +05:00
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except BaseException as msg:
|
|
|
|
|
logging.writeToFile(str(msg) + ' [Docker_Sites.run]')
|
2023-12-20 21:21:51 +05:00
|
|
|
|
2023-12-10 10:55:39 +05:00
|
|
|
def InstallDocker(self):
|
|
|
|
|
|
|
|
|
|
command = 'apt install docker-compose -y'
|
|
|
|
|
ReturnCode = ProcessUtilities.executioner(command)
|
|
|
|
|
|
|
|
|
|
if ReturnCode:
|
|
|
|
|
return 1, None
|
|
|
|
|
else:
|
|
|
|
|
return 0, ReturnCode
|
|
|
|
|
|
2023-12-18 12:46:32 +05:00
|
|
|
@staticmethod
|
|
|
|
|
def SetupProxy(port):
|
|
|
|
|
ConfPath = '/usr/local/lsws/conf/httpd_config.conf'
|
|
|
|
|
data = open(ConfPath, 'r').read()
|
|
|
|
|
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
print(f'return code of docker install {result}')
|
|
|
|
|
if result.find("not found") > -1:
|
2023-12-10 10:55:39 +05:00
|
|
|
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')
|
|
|
|
|
|
2023-12-21 12:56:49 +05:00
|
|
|
# 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: {{}}
|
|
|
|
|
# """
|
|
|
|
|
#
|
|
|
|
|
# WPSite = f"""
|
|
|
|
|
# # Copyright VMware, Inc.
|
|
|
|
|
# # SPDX-License-Identifier: APACHE-2.0
|
|
|
|
|
#
|
|
|
|
|
# version: '2'
|
|
|
|
|
# services:
|
|
|
|
|
# mariadb:
|
|
|
|
|
# image: mariadb:10.5.9
|
|
|
|
|
# user: root
|
|
|
|
|
# command: --max_allowed_packet=256M
|
|
|
|
|
# volumes:
|
|
|
|
|
# - "{self.data['MySQLPath']}:/var/lib/mysql:delegated"
|
|
|
|
|
# environment:
|
|
|
|
|
# - ALLOW_EMPTY_PASSWORD=no
|
|
|
|
|
# - MYSQL_USER={self.data['MySQLDBNUser']}
|
|
|
|
|
# - MYSQL_PASSWORD={self.data['MySQLPassword']}
|
|
|
|
|
# - MYSQL_DATABASE={self.data['MySQLDBName']}
|
|
|
|
|
# - MYSQL_ROOT_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:
|
|
|
|
|
# image: litespeedtech/openlitespeed:latest
|
|
|
|
|
# user: root
|
|
|
|
|
# ports:
|
|
|
|
|
# - "{self.data['port']}:80"
|
|
|
|
|
# # - '443:8443'
|
|
|
|
|
# volumes:
|
|
|
|
|
# - {self.data['docRoot']}/lsws/conf:/usr/local/lsws/conf
|
|
|
|
|
# - {self.data['docRoot']}/lsws/admin-conf:/usr/local/lsws/admin/conf
|
|
|
|
|
# - {self.data['docRoot']}/bin:/usr/local/bin
|
|
|
|
|
# - {self.data['SitePath']}:/var/www/vhosts/
|
|
|
|
|
# - {self.data['docRoot']}/acme:/root/.acme.sh/
|
|
|
|
|
# - {self.data['docRoot']}/logs:/usr/local/lsws/logs/
|
|
|
|
|
# depends_on:
|
|
|
|
|
# - mariadb
|
|
|
|
|
# environment:
|
|
|
|
|
# - TZ=America/New_York
|
|
|
|
|
# - PHP_VERSION=lsphp82
|
|
|
|
|
# - MYSQL_ROOT_PASSWORD={self.data['MySQLPassword']}
|
|
|
|
|
# - DOMAIN={self.data['finalURL']}
|
|
|
|
|
# - MYSQL_USER={self.data['MySQLDBNUser']}
|
|
|
|
|
# - MYSQL_DATABASE={self.data['MySQLDBName']}
|
|
|
|
|
# - MYSQL_PASSWORD={self.data['MySQLPassword']}
|
|
|
|
|
# # - ALLOW_EMPTY_PASSWORD=no
|
|
|
|
|
# # - WORDPRESS_DATABASE_HOST=mariadb
|
|
|
|
|
# # - WORDPRESS_DATABASE_PORT_NUMBER=3306
|
|
|
|
|
# # - WORDPRESS_USERNAME={self.data['adminUser']}
|
|
|
|
|
# # - WORDPRESS_PASSWORD={self.data["adminPassword"]}
|
|
|
|
|
# # - WORDPRESS_EMAIL={self.data["adminEmail"]}
|
|
|
|
|
# # - WORDPRESS_BLOG_NAME={self.data["blogTitle"]}
|
|
|
|
|
# # - WORDPRESS_ENABLE_REVERSE_PROXY=yes
|
|
|
|
|
# deploy:
|
|
|
|
|
# resources:
|
|
|
|
|
# limits:
|
|
|
|
|
# cpus: '{self.data['CPUsSite']}' # Use 50% of one CPU core
|
|
|
|
|
# memory: {self.data['MemorySite']}M # Limit memory to 512 megabytes
|
|
|
|
|
# # phpmyadmin:
|
|
|
|
|
# # image: bitnami/phpmyadmin:latest
|
|
|
|
|
# # ports:
|
|
|
|
|
# # # - 8080:8080
|
|
|
|
|
# # # - 8443:8443
|
|
|
|
|
# # environment:
|
|
|
|
|
# # DATABASE_HOST: mysql
|
|
|
|
|
# # restart: always
|
|
|
|
|
# # networks:
|
|
|
|
|
# # - default
|
|
|
|
|
#
|
|
|
|
|
# volumes:
|
|
|
|
|
# mariadb_data:
|
|
|
|
|
# driver: local
|
|
|
|
|
# wordpress_data:
|
|
|
|
|
# driver: local
|
|
|
|
|
# """
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2023-12-20 21:21:51 +05:00
|
|
|
command = f"mkdir -p /home/docker/{self.data['finalURL']}"
|
|
|
|
|
ProcessUtilities.executioner(command)
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
2023-12-20 21:21:51 +05:00
|
|
|
command = f"mv {TempCompose} {self.data['ComposePath']}"
|
|
|
|
|
ProcessUtilities.executioner(command)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
####
|
|
|
|
|
|
|
|
|
|
command = f"docker-compose -f {self.data['ComposePath']} -p '{self.data['SiteName']}' up -d"
|
2023-12-18 12:46:32 +05:00
|
|
|
result = ProcessUtilities.outputExecutioner(command)
|
|
|
|
|
|
|
|
|
|
if os.path.exists(ProcessUtilities.debugPath):
|
|
|
|
|
logging.writeToFile(result)
|
2023-12-10 10:55:39 +05:00
|
|
|
|
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
|
|
|
|
|
installUtilities.reStartLiteSpeed()
|
|
|
|
|
|
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,
|
2023-12-20 21:21:51 +05:00
|
|
|
"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()
|
|
|
|
|
|
|
|
|
|
background = Docker_Sites('DeployWPContainer', 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()
|
|
|
|
|
|
|
|
|
|
# Define the label to filter containers
|
|
|
|
|
label_filter = {'name': self.data['name'].replace(' ', '-')}
|
|
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
# Define the label to filter containers
|
2023-12-22 07:41:48 +05:00
|
|
|
label_filter = {'name': self.data['name'].replace(' ', '-')}
|
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-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":
|
2023-12-20 21:21:51 +05:00
|
|
|
Docker_Sites.SetupProxy(args.port)
|
2023-12-18 12:46:32 +05:00
|
|
|
elif args.function == 'SetupHTAccess':
|
2023-12-20 21:21:51 +05:00
|
|
|
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 = {
|
|
|
|
|
"JobID": '/home/cyberpanel/error-logs.txt',
|
|
|
|
|
"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',
|
2023-12-20 11:09:15 +05:00
|
|
|
"externalApp": 'docke8463',
|
2023-12-18 12:46:32 +05:00
|
|
|
"docRoot": "/home/docker.cyberpanel.net"
|
|
|
|
|
}
|
2023-12-20 21:21:51 +05:00
|
|
|
ds = Docker_Sites('', data)
|
2023-12-18 12:46:32 +05:00
|
|
|
ds.DeployWPContainer()
|
|
|
|
|
|
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()
|