bug fix: do not initiate site backup if space is full

This commit is contained in:
usmannasir
2024-01-02 13:58:47 +05:00
parent 7d554454f9
commit db3423f537
4 changed files with 57 additions and 26 deletions

View File

@@ -5,6 +5,7 @@ sys.path.append('/usr/local/CyberCP')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
import django
django.setup()
from plogical.getSystemInformation import SystemInformation
from IncBackups.IncBackupsControl import IncJobs
from IncBackups.models import BackupJob
from random import randint
@@ -68,7 +69,16 @@ class IncScheduler(multi.Thread):
logging.statusWriter(IncScheduler.logPath, 'Job Description:\n\n Destination: %s, Frequency: %s.\n ' % (
job.destination, job.frequency), 1)
if job.frequency == type:
### now run backups
for web in job.jobsites_set.all():
### Lets first update disk usage of all sites, to see if enough space for backups
IncScheduler.CalculateAndUpdateDiskUsage()
logging.statusWriter(IncScheduler.logPath, 'Backing up %s.' % (web.website), 1)
extraArgs = {}

View File

@@ -2048,11 +2048,29 @@ def submitBackupCreation(tempStoragePath, backupName, backupPath, backupDomain):
## /home/cyberpanel/1047.xml - metaPath
## /home/backup/<random_number> - CPHomeStorage
status = os.path.join(backupPath, 'status')
### Lets first see if enough space for backups is available
from plogical.IncScheduler import IncScheduler
import json
from plogical.getSystemInformation import SystemInformation
IncScheduler.CalculateAndUpdateDiskUsage()
website = Websites.objects.get(domain=backupDomain)
DiskUsageOfSite = json.loads(website.config)['DiskUsage']
used_disk, free_disk, percent_used = SystemInformation.GetRemainingDiskUsageInMBs()
if float(free_disk) <= float(DiskUsageOfSite):
command = f"echo 'Disk space exceeded the website size. [2065][5009]' > %s"
ProcessUtilities.executioner(command, website.externalApp)
return 0
###
status = os.path.join(backupPath, 'status')
website = Websites.objects.get(domain=backupDomain)
##

View File

@@ -99,6 +99,18 @@ class SystemInformation:
return SystemInfo
@staticmethod
def GetRemainingDiskUsageInMBs():
import psutil
total_disk = psutil.disk_usage('/').total / (1024 * 1024) # Total disk space in MB
used_disk = psutil.disk_usage('/').used / (1024 * 1024) # Used disk space in MB
free_disk = psutil.disk_usage('/').free / (1024 * 1024) # Free disk space in MB
percent_used = psutil.disk_usage('/').percent # Percentage of disk used
return used_disk, free_disk, percent_used
@staticmethod
def populateOLSReport():
SystemInformation.olsReport = open("/tmp/lshttpd/.rtreport", "r").readlines()

View File

@@ -1,28 +1,19 @@
import docker
import psutil
# Create a Docker client
client = docker.from_env()
def get_disk_usage():
total_disk = psutil.disk_usage('/').total / (1024 * 1024) # Total disk space in MB
used_disk = psutil.disk_usage('/').used / (1024 * 1024) # Used disk space in MB
free_disk = psutil.disk_usage('/').free / (1024 * 1024) # Free disk space in MB
percent_used = psutil.disk_usage('/').percent # Percentage of disk used
# Define the label to filter containers
label_filter = {'name': 'cyberplanner-new'}
return {
"current_disk_usage_mb": used_disk,
"current_disk_free_mb": free_disk,
"percentage_disk_used": percent_used
}
# List containers matching the label filter
containers = client.containers.list(filters=label_filter)
# Print container information
for container in containers:
print(f"Container ID: {container.id}, Name: {container.name}, Status: {container.status}")
# Get volume information for the container
volumes = container.attrs['HostConfig']['Binds'] if 'HostConfig' in container.attrs else []
for volume in volumes:
print(f"Volume: {volume}")
# # Fetch last 50 logs for the container
# logs = container.logs(tail=50).decode('utf-8')
# print(f"Last 50 Logs:\n{logs}")
# Get exposed ports for the container
ports = container.attrs['HostConfig']['PortBindings'] if 'HostConfig' in container.attrs else {}
for port in ports:
print(f"Exposed Port: {port}")
# Usage example:
disk_info = get_disk_usage()
print("Current disk usage (MB):", disk_info["current_disk_usage_mb"])
print("Current disk free (MB):", disk_info["current_disk_free_mb"])
print("Percentage of disk used:", disk_info["percentage_disk_used"], "%")