quantum-ai-v2/agents/management/commands/restore_working_state.py
Claude 2d670d7b8e 🏠 Create restoration point system with agent freeze
- Add is_locked field to Agent model with migration
- Create stable-working-agents branch and v1.0-working tag
- Export working agents and user data to backups/
- Implement freeze_agents command to lock/unlock agents
- Create restore_working_state command for one-click restoration
- Lock current 3 working agents (Social Ads, Job Posting, PDF Summarizer)
- Add lock status to admin interface

Safe house established! All working agents are now protected.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-01 09:19:23 +05:30

73 lines
2.8 KiB
Python

from django.core.management.base import BaseCommand
from django.core.management import call_command
import subprocess
import os
class Command(BaseCommand):
help = 'Restore system to working state from backup'
def add_arguments(self, parser):
parser.add_argument(
'--confirm',
action='store_true',
help='Confirm restoration (required to prevent accidental use)',
)
def handle(self, *args, **options):
if not options['confirm']:
self.stdout.write(
self.style.ERROR('❌ Restoration requires --confirm flag to prevent accidents')
)
self.stdout.write('Usage: python manage.py restore_working_state --confirm')
return
self.stdout.write(
self.style.WARNING('🔄 Starting restoration to working state...')
)
try:
# Step 1: Switch to stable branch
self.stdout.write('📦 Switching to stable branch...')
result = subprocess.run(['git', 'checkout', 'stable-working-agents'],
capture_output=True, text=True)
if result.returncode != 0:
self.stdout.write(
self.style.ERROR(f'❌ Git checkout failed: {result.stderr}')
)
return
# Step 2: Run migrations to ensure database is up to date
self.stdout.write('🗄️ Running migrations...')
call_command('migrate')
# Step 3: Restore agents from backup
backup_file = 'backups/restore_agents.json'
if os.path.exists(backup_file):
self.stdout.write('🔧 Restoring agents from backup...')
call_command('loaddata', backup_file)
else:
self.stdout.write(
self.style.WARNING('⚠️ No agents backup file found')
)
# Step 4: Restore users and wallet if needed
user_backup = 'backups/restore_users_wallet.json'
if os.path.exists(user_backup):
self.stdout.write('👥 Restoring users and wallet data...')
call_command('loaddata', user_backup)
# Step 5: Collect static files
self.stdout.write('📁 Collecting static files...')
call_command('collectstatic', '--noinput')
self.stdout.write(
self.style.SUCCESS('✅ Restoration completed successfully!')
)
self.stdout.write('🔒 All agents should be locked and working')
self.stdout.write('🌐 Test the system at http://localhost:8000')
except Exception as e:
self.stdout.write(
self.style.ERROR(f'❌ Restoration failed: {str(e)}')
)
self.stdout.write('💡 Check logs and try manual restoration')