feat: fix export/import, add backup download/upload, restore service checkboxes
- export_config: clean output (no internal _keys), identity exposed as 'identity' - import_config: handle 'identity' key, merge into existing config (not replace) - restore_config: accept optional services list for selective restore - backup_config: include 'identity' in manifest services list - new GET /api/config/backups/<id>/download → zip file download - new POST /api/config/backup/upload → zip file upload - webui: Download + Upload buttons, restore modal with per-service checkboxes Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+62
-17
@@ -247,10 +247,11 @@ class ConfigManager:
|
||||
for zone_file in dns_data.glob('*.zone'):
|
||||
shutil.copy2(zone_file, zones_dir / zone_file.name)
|
||||
|
||||
services = ['identity'] + list(self.service_schemas.keys())
|
||||
manifest = {
|
||||
"backup_id": backup_id,
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
"services": list(self.service_schemas.keys()),
|
||||
"services": services,
|
||||
"files": [f.name for f in backup_path.iterdir()],
|
||||
}
|
||||
with open(backup_path / 'manifest.json', 'w') as f:
|
||||
@@ -263,8 +264,8 @@ class ConfigManager:
|
||||
logger.error(f"Error creating backup: {e}")
|
||||
raise
|
||||
|
||||
def restore_config(self, backup_id: str) -> bool:
|
||||
"""Restore cell_config.json, secrets, Caddyfile, .env, Corefile, and DNS zones from backup."""
|
||||
def restore_config(self, backup_id: str, services: list = None) -> bool:
|
||||
"""Restore from backup. If services list given, only restore those service configs (selective)."""
|
||||
try:
|
||||
backup_path = self.backup_dir / backup_id
|
||||
if not backup_path.exists():
|
||||
@@ -273,7 +274,23 @@ class ConfigManager:
|
||||
if not manifest_file.exists():
|
||||
raise ValueError(f"Backup manifest not found")
|
||||
|
||||
# Restore primary config
|
||||
if services is not None:
|
||||
# Selective restore: only update specified services in running config
|
||||
backup_cfg_path = backup_path / 'cell_config.json'
|
||||
if backup_cfg_path.exists():
|
||||
with open(backup_cfg_path) as f:
|
||||
backup_cfg = json.load(f)
|
||||
for svc in services:
|
||||
if svc == 'identity':
|
||||
if '_identity' in backup_cfg:
|
||||
self.configs['_identity'] = backup_cfg['_identity']
|
||||
elif svc in backup_cfg:
|
||||
self.configs[svc] = backup_cfg[svc]
|
||||
self._save_all_configs()
|
||||
logger.info(f"Selectively restored {services} from backup: {backup_id}")
|
||||
return True
|
||||
|
||||
# Full restore: copy all files back
|
||||
config_backup = backup_path / 'cell_config.json'
|
||||
if config_backup.exists():
|
||||
shutil.copy2(config_backup, self.config_file)
|
||||
@@ -281,7 +298,6 @@ class ConfigManager:
|
||||
if secrets_backup.exists():
|
||||
shutil.copy2(secrets_backup, self.secrets_file)
|
||||
|
||||
# Restore runtime-generated files so they stay consistent with cell_config.json
|
||||
config_dir = Path(os.environ.get('CONFIG_DIR', '/app/config'))
|
||||
data_dir = Path(os.environ.get('DATA_DIR', '/app/data'))
|
||||
env_file = Path(os.environ.get('ENV_FILE', '/app/.env'))
|
||||
@@ -296,7 +312,6 @@ class ConfigManager:
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(src, dest)
|
||||
|
||||
# Restore DNS zone files
|
||||
zones_backup = backup_path / 'dns_zones'
|
||||
if zones_backup.is_dir():
|
||||
dns_data = data_dir / 'dns'
|
||||
@@ -353,21 +368,32 @@ class ConfigManager:
|
||||
current_hash = self.get_config_hash(service)
|
||||
return current_hash != previous_hash
|
||||
|
||||
def export_config(self, format: str = 'json') -> str:
|
||||
"""Export all configurations in specified format"""
|
||||
def export_config(self, format: str = 'json', services: list = None) -> str:
|
||||
"""Export service configurations (excludes internal state like pending_restart)."""
|
||||
try:
|
||||
export_data = {}
|
||||
# Include identity under a clean key
|
||||
if '_identity' in self.configs:
|
||||
export_data['identity'] = dict(self.configs['_identity'])
|
||||
# Include service configs, skip internal _ keys
|
||||
for key, val in self.configs.items():
|
||||
if key.startswith('_'):
|
||||
continue
|
||||
if services is not None and key not in services:
|
||||
continue
|
||||
export_data[key] = val
|
||||
if format == 'json':
|
||||
return json.dumps(self.configs, indent=2)
|
||||
return json.dumps(export_data, indent=2)
|
||||
elif format == 'yaml':
|
||||
return yaml.dump(self.configs, default_flow_style=False)
|
||||
return yaml.dump(export_data, default_flow_style=False)
|
||||
else:
|
||||
raise ValueError(f"Unsupported format: {format}")
|
||||
except Exception as e:
|
||||
logger.error(f"Error exporting config: {e}")
|
||||
raise
|
||||
|
||||
def import_config(self, config_data: str, format: str = 'json') -> bool:
|
||||
"""Import configurations from string"""
|
||||
|
||||
def import_config(self, config_data: str, format: str = 'json', services: list = None) -> bool:
|
||||
"""Import configurations from string. Merges into existing config."""
|
||||
try:
|
||||
if format == 'json':
|
||||
configs = json.loads(config_data)
|
||||
@@ -375,10 +401,29 @@ class ConfigManager:
|
||||
configs = yaml.safe_load(config_data)
|
||||
else:
|
||||
raise ValueError(f"Unsupported format: {format}")
|
||||
# Import only services present in the data — don't fabricate missing ones
|
||||
for service, config in configs.items():
|
||||
if service in self.service_schemas:
|
||||
self.update_service_config(service, config)
|
||||
|
||||
# Handle identity (exported as 'identity', stored as '_identity')
|
||||
if 'identity' in configs and (services is None or 'identity' in services):
|
||||
ident = configs['identity']
|
||||
cur = dict(self.configs.get('_identity', {}))
|
||||
for k in ('cell_name', 'domain', 'ip_range', 'wireguard_port'):
|
||||
if k in ident:
|
||||
cur[k] = ident[k]
|
||||
self.configs['_identity'] = cur
|
||||
|
||||
# Merge service configs (don't replace wholesale — keep existing fields not in import)
|
||||
for key, val in configs.items():
|
||||
if key == 'identity':
|
||||
continue
|
||||
if key not in self.service_schemas:
|
||||
continue
|
||||
if services is not None and key not in services:
|
||||
continue
|
||||
cur_svc = dict(self.configs.get(key, {}))
|
||||
cur_svc.update(val)
|
||||
self.configs[key] = cur_svc
|
||||
|
||||
self._save_all_configs()
|
||||
logger.info("Imported configurations successfully")
|
||||
return True
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user