Files
ServerKit/backend/app/models/site_import.py
T
Juan DenisandClaude Fable 5 d01d82036b feat(imports): panel-migration pipeline with cPanel, DirectAdmin and Hestia importers
Format-pluggable analyse->plan->run pipeline (migration 051) that maps
legacy panel backups to ServerKit apps, managed DBs (preserving MySQL
password hashes where portable) and cron entries, as resumable jobs
with per-step logs, SSRF/tar-traversal guards, and a 5-step Import a
Site wizard at /imports.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 14:24:29 -04:00

113 lines
4.0 KiB
Python

"""Site import runs — migrating a site from another control panel.
A ``SiteImport`` row tracks one archive-to-app migration: where the archive
came from, what the analyse step discovered, the step-by-step run log, and
the final result. The heavy lifting happens in
``app.services.site_import_service`` as ``import.analyze`` / ``import.run``
jobs; this row is the durable record the wizard UI polls.
"""
import json
from datetime import datetime
from app import db
# Formats the importer registry may grow to support. 'cpanel' ships first;
# the others plug into the same pipeline (see app/services/site_importers/).
VALID_SOURCE_TYPES = ('auto', 'cpanel', 'directadmin', 'hestia', 'wordpress_ssh')
VALID_STATUSES = (
'created', 'uploading', 'analyzing', 'analyzed',
'running', 'completed', 'failed',
)
class SiteImport(db.Model):
__tablename__ = 'site_imports'
id = db.Column(db.Integer, primary_key=True)
source_type = db.Column(db.String(30), nullable=False, default='cpanel')
status = db.Column(db.String(20), nullable=False, default='created')
# JSON: {'upload_path': '<relative token>'} or {'url': 'https://...'}
source = db.Column(db.Text, nullable=True)
# JSON: caller options (app name override, which steps to skip, ...)
options = db.Column(db.Text, nullable=True)
# JSON: the analyse report produced by the format importer.
analysis = db.Column(db.Text, nullable=True)
# JSON: run outcome ({'app_id': ..., 'databases': [...], ...})
result = db.Column(db.Text, nullable=True)
# Appended, newline-separated human-readable log lines.
log_text = db.Column(db.Text, nullable=True)
current_step = db.Column(db.String(60), nullable=True)
error = db.Column(db.Text, nullable=True)
created_by = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, default=datetime.utcnow,
onupdate=datetime.utcnow)
# ── JSON helpers ──
@staticmethod
def _loads(raw, default):
try:
value = json.loads(raw) if raw else default
except (ValueError, TypeError):
return default
return value if isinstance(value, type(default)) else default
def get_source(self):
return self._loads(self.source, {})
def set_source(self, value):
self.source = json.dumps(value or {})
def get_options(self):
return self._loads(self.options, {})
def set_options(self, value):
self.options = json.dumps(value or {})
def get_analysis(self):
return self._loads(self.analysis, {})
def set_analysis(self, value):
self.analysis = json.dumps(value or {})
def get_result(self):
return self._loads(self.result, {})
def set_result(self, value):
self.result = json.dumps(value or {})
def append_log(self, line):
stamp = datetime.utcnow().strftime('%H:%M:%S')
entry = f'[{stamp}] {line}'
self.log_text = (self.log_text + '\n' + entry) if self.log_text else entry
def log_tail(self, max_lines=500):
if not self.log_text:
return ''
lines = self.log_text.splitlines()
return '\n'.join(lines[-max_lines:])
def to_dict(self, log_lines=500):
return {
'id': self.id,
'source_type': self.source_type,
'status': self.status,
'source': self.get_source(),
'options': self.get_options(),
'analysis': self.get_analysis() or None,
'result': self.get_result() or None,
'log_text': self.log_tail(log_lines),
'current_step': self.current_step,
'error': self.error,
'created_by': self.created_by,
'created_at': self.created_at.isoformat() if self.created_at else None,
'updated_at': self.updated_at.isoformat() if self.updated_at else None,
}
def __repr__(self):
return f'<SiteImport {self.id} {self.source_type} {self.status}>'