Skip to content

Commit 00885d2

Browse files
committed
bug fix: #1470
1 parent 651324b commit 00885d2

File tree

2 files changed

+105
-68
lines changed

2 files changed

+105
-68
lines changed

.idea/workspace.xml

Lines changed: 2 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plogical/upgrade.py

Lines changed: 103 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,14 +2311,76 @@ def enableServices():
23112311
except:
23122312
pass
23132313

2314+
@staticmethod
2315+
def backupCriticalFiles():
2316+
"""Backup all critical configuration files before upgrade"""
2317+
import tempfile
2318+
backup_dir = tempfile.mkdtemp(prefix='cyberpanel_backup_')
2319+
2320+
critical_files = [
2321+
'/usr/local/CyberCP/CyberCP/settings.py',
2322+
'/usr/local/CyberCP/CyberCP/SecurityLevel.py',
2323+
'/usr/local/CyberCP/plogical/acl.py', # Custom ACL settings
2324+
'/usr/local/CyberCP/.git/config', # Git configuration
2325+
]
2326+
2327+
# Also backup any custom configurations
2328+
custom_configs = [
2329+
'/usr/local/CyberCP/baseTemplate/static/baseTemplate/custom/',
2330+
'/usr/local/CyberCP/public/phpmyadmin/config.inc.php',
2331+
'/usr/local/CyberCP/rainloop/data/_data_/',
2332+
]
2333+
2334+
backed_up_files = {}
2335+
2336+
for file_path in critical_files:
2337+
if os.path.exists(file_path):
2338+
try:
2339+
backup_path = os.path.join(backup_dir, os.path.basename(file_path))
2340+
shutil.copy2(file_path, backup_path)
2341+
backed_up_files[file_path] = backup_path
2342+
Upgrade.stdOut(f"Backed up {file_path}")
2343+
except Exception as e:
2344+
Upgrade.stdOut(f"Failed to backup {file_path}: {str(e)}")
2345+
2346+
# Backup directories
2347+
for dir_path in custom_configs:
2348+
if os.path.exists(dir_path):
2349+
try:
2350+
backup_path = os.path.join(backup_dir, os.path.basename(dir_path))
2351+
shutil.copytree(dir_path, backup_path)
2352+
backed_up_files[dir_path] = backup_path
2353+
Upgrade.stdOut(f"Backed up directory {dir_path}")
2354+
except Exception as e:
2355+
Upgrade.stdOut(f"Failed to backup {dir_path}: {str(e)}")
2356+
2357+
return backup_dir, backed_up_files
2358+
2359+
@staticmethod
2360+
def restoreCriticalFiles(backup_dir, backed_up_files):
2361+
"""Restore critical configuration files after upgrade"""
2362+
for original_path, backup_path in backed_up_files.items():
2363+
try:
2364+
if os.path.isdir(backup_path):
2365+
if os.path.exists(original_path):
2366+
shutil.rmtree(original_path)
2367+
shutil.copytree(backup_path, original_path)
2368+
else:
2369+
# Create directory if it doesn't exist
2370+
os.makedirs(os.path.dirname(original_path), exist_ok=True)
2371+
shutil.copy2(backup_path, original_path)
2372+
Upgrade.stdOut(f"Restored {original_path}")
2373+
except Exception as e:
2374+
Upgrade.stdOut(f"Failed to restore {original_path}: {str(e)}")
2375+
23142376
@staticmethod
23152377
def downloadAndUpgrade(versionNumbring, branch):
23162378
try:
23172379
## Download latest version.
23182380

2319-
## Backup settings file.
2320-
2321-
Upgrade.stdOut("Backing up settings file.")
2381+
## Backup all critical files
2382+
Upgrade.stdOut("Backing up critical configuration files...")
2383+
backup_dir, backed_up_files = Upgrade.backupCriticalFiles()
23222384

23232385
## CyberPanel DB Creds
23242386
dbName = settings.DATABASES['default']['NAME']
@@ -2356,74 +2418,54 @@ def downloadAndUpgrade(versionNumbring, branch):
23562418

23572419
settingsFile = '/usr/local/CyberCP/CyberCP/settings.py'
23582420

2359-
Upgrade.stdOut("Settings file backed up.")
2360-
2361-
## Check git branch status
2362-
2363-
os.chdir('/usr/local/CyberCP')
2421+
Upgrade.stdOut("Critical files backed up to: " + backup_dir)
23642422

2423+
## Always do a fresh clone for clean upgrade
2424+
2425+
Upgrade.stdOut("Performing clean upgrade by removing and re-cloning CyberPanel...")
2426+
2427+
# Set git config first
23652428
command = 'git config --global user.email "[email protected]"'
2366-
23672429
if not Upgrade.executioner(command, command, 1):
23682430
return 0, 'Failed to execute %s' % (command)
23692431

23702432
command = 'git config --global user.name "CyberPanel"'
2371-
23722433
if not Upgrade.executioner(command, command, 1):
23732434
return 0, 'Failed to execute %s' % (command)
2435+
2436+
# Change to parent directory
2437+
os.chdir('/usr/local')
23742438

2375-
command = 'git status'
2376-
try:
2377-
currentBranch = subprocess.check_output(shlex.split(command)).decode()
2378-
except Exception as e:
2379-
Upgrade.stdOut(f"Error checking git status: {str(e)}")
2380-
currentBranch = ""
2381-
2382-
if currentBranch.find('On branch %s' % (branch)) > -1 and currentBranch.find(
2383-
'On branch %s-dev' % (branch)) == -1:
2384-
2385-
command = 'git stash'
2386-
if not Upgrade.executioner(command, command, 1):
2387-
return 0, 'Failed to execute %s' % (command)
2388-
2389-
command = 'git clean -f'
2390-
if not Upgrade.executioner(command, command, 1):
2391-
return 0, 'Failed to execute %s' % (command)
2392-
2393-
command = 'git pull'
2394-
if not Upgrade.executioner(command, command, 1):
2395-
return 0, 'Failed to execute %s' % (command)
2396-
2397-
elif currentBranch.find('not a git repository') > -1:
2398-
2399-
os.chdir('/usr/local')
2400-
2401-
command = 'git clone https://github.com/usmannasir/cyberpanel'
2402-
if not Upgrade.executioner(command, command, 1):
2403-
return 0, 'Failed to execute %s' % (command)
2404-
2405-
if os.path.exists('CyberCP'):
2439+
# Remove old CyberCP directory
2440+
if os.path.exists('CyberCP'):
2441+
Upgrade.stdOut("Removing old CyberCP directory...")
2442+
try:
24062443
shutil.rmtree('CyberCP')
2407-
2408-
shutil.move('cyberpanel', 'CyberCP')
2409-
2410-
else:
2411-
2412-
command = 'git fetch'
2413-
if not Upgrade.executioner(command, command, 1):
2414-
return 0, 'Failed to execute %s' % (command)
2415-
2416-
command = 'git stash'
2417-
if not Upgrade.executioner(command, command, 1):
2418-
return 0, 'Failed to execute %s' % (command)
2419-
2420-
command = 'git checkout %s' % (branch)
2421-
if not Upgrade.executioner(command, command, 1):
2422-
return 0, 'Failed to execute %s' % (command)
2423-
2424-
command = 'git pull'
2425-
if not Upgrade.executioner(command, command, 1):
2426-
return 0, 'Failed to execute %s' % (command)
2444+
Upgrade.stdOut("Old CyberCP directory removed successfully.")
2445+
except Exception as e:
2446+
Upgrade.stdOut(f"Error removing CyberCP directory: {str(e)}")
2447+
# Try to restore backup if removal fails
2448+
Upgrade.restoreCriticalFiles(backup_dir, backed_up_files)
2449+
return 0, 'Failed to remove old CyberCP directory'
2450+
2451+
# Clone the new repository directly to CyberCP
2452+
Upgrade.stdOut("Cloning fresh CyberPanel repository...")
2453+
command = 'git clone https://github.com/usmannasir/cyberpanel CyberCP'
2454+
if not Upgrade.executioner(command, command, 1):
2455+
# Try to restore backup if clone fails
2456+
Upgrade.stdOut("Clone failed, attempting to restore backup...")
2457+
Upgrade.restoreCriticalFiles(backup_dir, backed_up_files)
2458+
return 0, 'Failed to clone CyberPanel repository'
2459+
2460+
# Checkout the correct branch
2461+
os.chdir('/usr/local/CyberCP')
2462+
command = 'git checkout %s' % (branch)
2463+
if not Upgrade.executioner(command, command, 1):
2464+
Upgrade.stdOut(f"Warning: Failed to checkout branch {branch}, continuing with default branch")
2465+
2466+
# Restore all backed up configuration files
2467+
Upgrade.stdOut("Restoring configuration files...")
2468+
Upgrade.restoreCriticalFiles(backup_dir, backed_up_files)
24272469

24282470
## Copy settings file
24292471

0 commit comments

Comments
 (0)