Skip to content

Commit

Permalink
bug fix: do not initiate site backup if space is full
Browse files Browse the repository at this point in the history
  • Loading branch information
usmannasir committed Jan 2, 2024
1 parent 7d55445 commit db3423f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 26 deletions.
10 changes: 10 additions & 0 deletions plogical/IncScheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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
Expand Down Expand Up @@ -68,7 +69,16 @@ def startBackup(type):
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 = {}
Expand Down
20 changes: 19 additions & 1 deletion plogical/backupUtilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

##
Expand Down
12 changes: 12 additions & 0 deletions plogical/getSystemInformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ def cpuRamDisk():

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()
Expand Down
41 changes: 16 additions & 25 deletions plogical/test.py
Original file line number Diff line number Diff line change
@@ -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"], "%")

0 comments on commit db3423f

Please sign in to comment.