Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

feat(imports): panel-migration pipeline with cPanel, DirectAdmin and Hestia importers

Juan Denisd01d82036bb8
diff --git a/backend/app/api/site_imports.py b/backend/app/api/site_imports.py
new file mode 100644
index 00000000..71228796
--- /dev/null
+++ b/backend/app/api/site_imports.py
@@ -0,0 +1,108 @@
+"""Site imports API — migrate a control-panel backup archive into ServerKit.
+
+Mounted at /api/v1/imports (see app/__init__.py blueprint registration).
+All routes are admin-only: an import writes files, creates apps and touches
+the database engine.
+"""
+from flask import Blueprint, jsonify, request
+
+from app.middleware.rbac import admin_required, get_current_user
+from app.services.site_import_service import SiteImportError, SiteImportService
+
+site_imports_bp = Blueprint('site_imports', __name__)
+
+
+@site_imports_bp.route('/upload', methods=['POST'])
+@admin_required
+def upload_archive():
+ """Accept a backup archive upload; returns the token to pass as
+ source.upload_path when creating the import."""
+ if 'file' not in request.files:
+ return jsonify({'error': 'No file provided'}), 400
+ uploaded = request.files['file']
+ if not uploaded or not uploaded.filename:
+ return jsonify({'error': 'No file selected'}), 400
+ try:
+ upload_path = SiteImportService.save_upload(uploaded)
+ except SiteImportError as exc:
+ return jsonify({'error': str(exc)}), 400
+ return jsonify({'upload_path': upload_path}), 201
+
+
+@site_imports_bp.route('', methods=['POST'])
+@admin_required
+def create_import():
+ data = request.get_json(silent=True) or {}
+ user = get_current_user()
+ try:
+ imp = SiteImportService.create(
+ source_type=data.get('source_type') or 'cpanel',
+ source=data.get('source') or {},
+ options=data.get('options') or {},
+ user_id=user.id if user else None,
+ )
+ except SiteImportError as exc:
+ return jsonify({'error': str(exc)}), 400
+ return jsonify({'import': imp.to_dict()}), 201
+
+
+@site_imports_bp.route('', methods=['GET'])
+@admin_required
+def list_imports():
+ return jsonify({'imports': [i.to_dict(log_lines=0) for i in
+ SiteImportService.list()]})
+
+
+@site_imports_bp.route('/<int:import_id>', methods=['GET'])
+@admin_required
+def get_import(import_id):
+ imp = SiteImportService.get(import_id)
+ if not imp:
+ return jsonify({'error': 'Import not found'}), 404
+ return jsonify({'import': imp.to_dict(log_lines=500)})
+
+
+@site_imports_bp.route('/<int:import_id>/analyze', methods=['POST'])
+@admin_required
+def analyze_import(import_id):
+ imp = SiteImportService.get(import_id)
+ if not imp:
+ return jsonify({'error': 'Import not found'}), 404
+ if imp.status in ('analyzing', 'running'):
+ return jsonify({'error': f'Import is currently {imp.status}'}), 409
+ job = SiteImportService.enqueue_analyze(imp)
+ return jsonify({'job_id': job.id}), 202
+
+
+@site_imports_bp.route('/<int:import_id>/run', methods=['POST'])
+@admin_required
+def run_import(import_id):
+ imp = SiteImportService.get(import_id)
+ if not imp:
+ return jsonify({'error': 'Import not found'}), 404
+ if imp.status not in ('analyzed', 'failed'):
+ return jsonify({'error': "Import must be analysed before it can run "
+ f"(status: {imp.status})"}), 409
+ data = request.get_json(silent=True) or {}
+ from_step = data.get('from_step') or None
+ # Merge wizard-provided run options into the stored ones (body wins),
+ # e.g. {'skip_db': True, 'skip_crontab': True}.
+ body_options = data.get('options') or {}
+ if body_options:
+ from app import db
+ imp.set_options({**imp.get_options(), **body_options})
+ db.session.commit()
+ job = SiteImportService.enqueue_run(imp, from_step=from_step)
+ return jsonify({'job_id': job.id}), 202
+
+
+@site_imports_bp.route('/<int:import_id>', methods=['DELETE'])
+@admin_required
+def delete_import(import_id):
+ imp = SiteImportService.get(import_id)
+ if not imp:
+ return jsonify({'error': 'Import not found'}), 404
+ if imp.status in ('analyzing', 'running'):
+ return jsonify({'error': f'Import is currently {imp.status}'}), 409
+ SiteImportService.delete(imp)
+ return jsonify({'message': 'Import deleted'})
diff --git a/backend/app/models/site_import.py b/backend/app/models/site_import.py
new file mode 100644
index 00000000..d245959a
--- /dev/null
+++ b/backend/app/models/site_import.py
@@ -0,0 +1,112 @@
+"""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}>'
diff --git a/backend/app/services/site_import_service.py b/backend/app/services/site_import_service.py
new file mode 100644
index 00000000..3057c98d
--- /dev/null
+++ b/backend/app/services/site_import_service.py
@@ -0,0 +1,736 @@
+"""Site import orchestration — migrate a panel backup archive into ServerKit.
+
+Pipeline: fetch archive → extract (safely) → analyse (format importer) →
+run the plan's steps (create app, copy docroot, import databases, recreate
+DB users preserving password hashes where the engine allows, install cron
+jobs, fix permissions, validate). Runs as jobs on the unified job bus:
+
+ * ``import.analyze`` — fetch + extract + analyse, status → 'analyzed'.
+ * ``import.run`` — execute the plan steps; supports ``from_step``
+ retry so a failed step can be re-run without
+ repeating completed ones.
+
+Format knowledge lives in ``app.services.site_importers``; this module owns
+every side effect so all formats share the same step implementations.
+"""
+import ipaddress
+import logging
+import os
+import re
+import secrets
+import shutil
+import socket
+import tarfile
+from urllib.parse import urlparse
+
+from app import db, paths
+from app.models.site_import import SiteImport, VALID_SOURCE_TYPES
+from app.services.site_importers import detect_format, get_importer
+
+logger = logging.getLogger(__name__)
+
+ANALYZE_JOB_KIND = 'import.analyze'
+RUN_JOB_KIND = 'import.run'
+
+# Hard cap on archive size (upload or URL fetch): 20 GB.
+MAX_ARCHIVE_BYTES = 20 * 1024 ** 3
+_DOWNLOAD_CHUNK = 1024 * 1024
+
+_SAFE_DB_NAME_RE = re.compile(r'^[A-Za-z0-9_-]{1,64}$')
+_SAFE_DB_USER_RE = re.compile(r'^[A-Za-z0-9_.-]{1,32}$')
+_NATIVE_HASH_RE = re.compile(r'^\*[0-9A-Fa-f]{40}$')
+
+
+class SiteImportError(ValueError):
+ """Invalid input or a rejected operation (maps to HTTP 400)."""
+
+
+class SiteImportService:
+
+ # Overridable in tests; falls back to env then /var/serverkit/imports.
+ imports_base = None
+
+ # ── paths ──
+ @classmethod
+ def base_dir(cls):
+ return (cls.imports_base
+ or os.environ.get('SERVERKIT_IMPORTS_DIR')
+ or os.path.join(paths.SERVERKIT_DIR, 'imports'))
+
+ @classmethod
+ def uploads_dir(cls):
+ return os.path.join(cls.base_dir(), 'uploads')
+
+ @classmethod
+ def workdir(cls, imp):
+ return os.path.join(cls.base_dir(), str(imp.id))
+
+ @classmethod
+ def extracted_dir(cls, imp):
+ return os.path.join(cls.workdir(imp), 'extracted')
+
+ # ── CRUD ──
+ @classmethod
+ def create(cls, source_type, source, options=None, user_id=None):
+ source_type = (source_type or 'cpanel').strip().lower()
+ if source_type not in VALID_SOURCE_TYPES:
+ raise SiteImportError(
+ f'source_type must be one of {VALID_SOURCE_TYPES}')
+ source = source or {}
+ if not source.get('upload_path') and not source.get('url'):
+ raise SiteImportError(
+ "source must contain 'upload_path' or 'url'")
+ if source.get('url'):
+ cls._validate_url(source['url']) # fail fast at creation time
+ imp = SiteImport(source_type=source_type, status='created',
+ created_by=user_id)
+ imp.set_source(source)
+ imp.set_options(options or {})
+ imp.append_log(f'Import created (source_type={source_type}).')
+ db.session.add(imp)
+ db.session.commit()
+ return imp
+
+ @classmethod
+ def get(cls, import_id):
+ return SiteImport.query.get(import_id)
+
+ @classmethod
+ def list(cls):
+ return SiteImport.query.order_by(SiteImport.created_at.desc(),
+ SiteImport.id.desc()).all()
+
+ @classmethod
+ def delete(cls, imp):
+ # Workdir + uploaded archive cleanup, then the row.
+ shutil.rmtree(cls.workdir(imp), ignore_errors=True)
+ upload_path = imp.get_source().get('upload_path')
+ if upload_path:
+ try:
+ resolved = cls._resolve_upload_path(upload_path)
+ if os.path.isfile(resolved):
+ os.remove(resolved)
+ except (SiteImportError, OSError):
+ pass
+ db.session.delete(imp)
+ db.session.commit()
+
+ # ── upload handling ──
+ @classmethod
+ def save_upload(cls, file_storage):
+ """Persist an uploaded archive under the uploads dir with a random
+ name; returns the relative token the client passes back as
+ ``source.upload_path``."""
+ original = getattr(file_storage, 'filename', '') or ''
+ suffix = '.tar.gz'
+ for known in ('.tar.gz', '.tgz', '.tar'):
+ if original.lower().endswith(known):
+ suffix = known
+ break
+ token = f'{secrets.token_hex(16)}{suffix}'
+ os.makedirs(cls.uploads_dir(), exist_ok=True)
+ dest = os.path.join(cls.uploads_dir(), token)
+ file_storage.save(dest)
+ if os.path.getsize(dest) > MAX_ARCHIVE_BYTES:
+ os.remove(dest)
+ raise SiteImportError('Archive exceeds the 20 GB size limit')
+ return os.path.join('uploads', token).replace('\\', '/')
+
+ @classmethod
+ def _resolve_upload_path(cls, upload_path):
+ """Resolve a client-provided upload token; rejects traversal."""
+ if not upload_path or os.path.isabs(upload_path):
+ raise SiteImportError('Invalid upload_path')
+ base = os.path.realpath(cls.uploads_dir())
+ resolved = os.path.realpath(os.path.join(cls.base_dir(), upload_path))
+ if resolved != base and not resolved.startswith(base + os.sep):
+ raise SiteImportError('Invalid upload_path')
+ return resolved
+
+ # ── URL fetch + SSRF guards ──
+ @staticmethod
+ def _validate_url(url):
+ parsed = urlparse(url or '')
+ if parsed.scheme not in ('http', 'https'):
+ raise SiteImportError('Only http:// and https:// URLs are allowed')
+ host = parsed.hostname
+ if not host:
+ raise SiteImportError('URL has no host')
+ try:
+ infos = socket.getaddrinfo(host, parsed.port or
+ (443 if parsed.scheme == 'https' else 80),
+ proto=socket.IPPROTO_TCP)
+ except socket.gaierror as exc:
+ raise SiteImportError(f'Could not resolve host {host!r}: {exc}')
+ for info in infos:
+ addr = ipaddress.ip_address(info[4][0])
+ if (addr.is_private or addr.is_loopback or addr.is_link_local
+ or addr.is_reserved or addr.is_multicast
+ or addr.is_unspecified):
+ raise SiteImportError(
+ f'URL resolves to a disallowed address ({addr})')
+ return parsed
+
+ @classmethod
+ def fetch_archive(cls, imp):
+ """Materialise the source archive into the workdir; returns its path."""
+ source = imp.get_source()
+ workdir = cls.workdir(imp)
+ os.makedirs(workdir, exist_ok=True)
+
+ if source.get('upload_path'):
+ archive = cls._resolve_upload_path(source['upload_path'])
+ if not os.path.isfile(archive):
+ raise SiteImportError('Uploaded archive not found')
+ cls._log(imp, f'Using uploaded archive ({os.path.getsize(archive)} bytes).')
+ return archive
+
+ url = source.get('url')
+ if not url:
+ raise SiteImportError('Import has no source archive')
+ cls._validate_url(url)
+ dest = os.path.join(workdir, 'archive.tar.gz')
+ cls._log(imp, f'Downloading archive from {url} ...')
+ import requests
+ received = 0
+ with requests.get(url, stream=True, timeout=60,
+ allow_redirects=False) as resp:
+ resp.raise_for_status()
+ length = resp.headers.get('Content-Length')
+ if length and int(length) > MAX_ARCHIVE_BYTES:
+ raise SiteImportError('Archive exceeds the 20 GB size limit')
+ with open(dest, 'wb') as fh:
+ for chunk in resp.iter_content(chunk_size=_DOWNLOAD_CHUNK):
+ received += len(chunk)
+ if received > MAX_ARCHIVE_BYTES:
+ raise SiteImportError(
+ 'Archive exceeds the 20 GB size limit')
+ fh.write(chunk)
+ cls._log(imp, f'Downloaded {received} bytes.')
+ return dest
+
+ # ── safe extraction ──
+ @staticmethod
+ def _safe_members(tar, warnings=None):
+ """Yield tar members that are safe to extract; raises on traversal."""
+ for member in tar.getmembers():
+ name = member.name.replace('\\', '/')
+ if name.startswith('/') or os.path.isabs(member.name):
+ raise SiteImportError(
+ f'Archive member has an absolute path: {member.name!r}')
+ parts = name.split('/')
+ if '..' in parts:
+ raise SiteImportError(
+ f'Archive member escapes the extraction dir: {member.name!r}')
+ if member.issym() or member.islnk():
+ if warnings is not None:
+ warnings.append(f'Skipped link member {member.name!r}')
+ continue
+ if member.isdev():
+ continue
+ yield member
+
+ @classmethod
+ def _extract_tar(cls, archive_path, dest, warnings=None):
+ os.makedirs(dest, exist_ok=True)
+ with tarfile.open(archive_path, 'r:*') as tar:
+ members = list(cls._safe_members(tar, warnings=warnings))
+ tar.extractall(dest, members=members)
+
+ @classmethod
+ def _extract_nested_homedir(cls, extracted, analysis_warnings):
+ """cPanel often ships the home as homedir.tar next to the metadata —
+ unpack it so the copy step sees a plain homedir/ directory."""
+ for base, _dirs, files in os.walk(extracted):
+ if 'homedir.tar' in files and not os.path.isdir(
+ os.path.join(base, 'homedir')):
+ cls._extract_tar(os.path.join(base, 'homedir.tar'),
+ os.path.join(base, 'homedir'),
+ warnings=analysis_warnings)
+ return
+
+ # ── analyse ──
+ @classmethod
+ def analyze(cls, imp):
+ """Fetch + extract + analyse; sets status analyzed/failed."""
+ imp.status = 'analyzing'
+ imp.error = None
+ imp.current_step = 'analyze'
+ db.session.commit()
+ try:
+ archive = cls.fetch_archive(imp)
+ extracted = cls.extracted_dir(imp)
+ if os.path.isdir(extracted):
+ shutil.rmtree(extracted, ignore_errors=True)
+ extraction_warnings = []
+ cls._log(imp, 'Extracting archive ...')
+ cls._extract_tar(archive, extracted, warnings=extraction_warnings)
+ cls._extract_nested_homedir(extracted, extraction_warnings)
+
+ if imp.source_type == 'auto':
+ fmt, importer = detect_format(extracted)
+ if not importer:
+ raise SiteImportError(
+ 'Could not detect the backup format — supported '
+ 'formats: cpanel')
+ imp.source_type = fmt
+ cls._log(imp, f'Detected backup format: {fmt}.')
+ else:
+ importer = get_importer(imp.source_type)
+ if not importer:
+ raise SiteImportError(
+ f'No importer registered for {imp.source_type!r}')
+ if not importer.detect(extracted):
+ cls._log(imp, f'Warning: archive does not look like a '
+ f'{imp.source_type} backup — analysing anyway.')
+
+ cls._log(imp, f'Analysing {imp.source_type} backup ...')
+ analysis = importer.analyze(extracted)
+ analysis.setdefault('warnings', []).extend(extraction_warnings)
+ imp.set_analysis(analysis)
+ imp.status = 'analyzed'
+ imp.current_step = None
+ cls._log(imp, 'Analysis complete: '
+ f"{len(analysis.get('domains', []))} domain(s), "
+ f"{len(analysis.get('databases', []))} database(s), "
+ f"{len(analysis.get('db_users', []))} DB user(s), "
+ f"{len(analysis.get('crontab', []))} cron line(s).")
+ for warning in analysis.get('warnings', []):
+ cls._log(imp, f'Warning: {warning}')
+ db.session.commit()
+ return analysis
+ except Exception as exc:
+ db.session.rollback()
+ imp.status = 'failed'
+ imp.error = str(exc)
+ cls._log(imp, f'Analysis failed: {exc}')
+ db.session.commit()
+ raise
+
+ # ── run ──
+ @classmethod
+ def run(cls, imp, from_step=None):
+ """Execute the migration plan. ``from_step`` re-runs from a specific
+ step key, skipping the ones before it (retry support)."""
+ analysis = imp.get_analysis()
+ if not analysis:
+ raise SiteImportError('Import has not been analysed yet')
+ importer = get_importer(imp.source_type)
+ if not importer:
+ raise SiteImportError(
+ f'No importer registered for {imp.source_type!r}')
+ options = imp.get_options()
+ steps = importer.plan(analysis, options)
+ step_keys = [s['key'] for s in steps]
+ if from_step and from_step not in step_keys:
+ raise SiteImportError(
+ f'Unknown step {from_step!r}; plan steps: {step_keys}')
+
+ imp.status = 'running'
+ imp.error = None
+ db.session.commit()
+
+ ctx = {
+ 'analysis': analysis,
+ 'options': options,
+ 'extracted': cls.extracted_dir(imp),
+ 'result': imp.get_result() or {'warnings': []},
+ }
+ ctx['result'].setdefault('warnings', [])
+
+ skipping = bool(from_step)
+ for step in steps:
+ key = step['key']
+ if skipping:
+ if key == from_step:
+ skipping = False
+ else:
+ cls._log(imp, f"Skipping step '{key}' (already completed).")
+ continue
+ # Wizard options can opt out of whole pipeline areas.
+ if options.get('skip_db') and key in ('create_databases',
+ 'create_db_users'):
+ cls._log(imp, f"Skipping step '{key}' (skip_db option).")
+ continue
+ if options.get('skip_crontab') and key == 'install_crontab':
+ cls._log(imp, f"Skipping step '{key}' (skip_crontab option).")
+ continue
+ imp.current_step = key
+ db.session.commit()
+ cls._log(imp, f"Step '{key}': {step['title']} ...")
+ handler = getattr(cls, f'_step_{key}', None)
+ if handler is None:
+ cls._log(imp, f"Warning: no handler for step '{key}' — skipped.")
+ ctx['result']['warnings'].append(f'Step {key} has no handler')
+ continue
+ try:
+ handler(imp, ctx)
+ except Exception as exc:
+ db.session.rollback()
+ imp.status = 'failed'
+ imp.error = f"Step '{key}' failed: {exc}"
+ imp.set_result(ctx['result'])
+ cls._log(imp, f"Step '{key}' failed: {exc}")
+ cls._log(imp, f"Fix the cause and retry with from_step='{key}'.")
+ db.session.commit()
+ return imp
+ cls._log(imp, f"Step '{key}' done.")
+ imp.set_result(ctx['result'])
+ db.session.commit()
+
+ imp.status = 'completed'
+ imp.current_step = None
+ imp.set_result(ctx['result'])
+ cls._log(imp, 'Import completed.')
+ db.session.commit()
+ return imp
+
+ # ── steps ──
+ @classmethod
+ def _step_create_app(cls, imp, ctx):
+ """Create the Application row + on-disk root for the imported site.
+
+ There is no standalone programmatic app-provisioning service (the
+ panel's create paths live inside the apps API), so we create the
+ Application row directly — same fields as POST /api/v1/apps — and
+ flag that the operator finishes container provisioning (deploy) from
+ the panel."""
+ from app.models import Application, User
+
+ analysis = ctx['analysis']
+ options = ctx['options']
+ from app.utils.slug import slugify
+ primary = (analysis.get('domains') or [{}])[0]
+ raw_name = (options.get('app_name') or primary.get('domain')
+ or analysis.get('account_user') or f'imported-{imp.id}')
+ name = slugify(raw_name)[:100] or f'imported-{imp.id}'
+ if Application.query.filter_by(name=name).first():
+ name = f'{name}-{imp.id}'
+
+ app_type = options.get('app_type') or ('php' if analysis.get('php_version')
+ else 'php')
+ root_path = os.path.join(paths.APPS_DIR, name, 'current')
+ try:
+ os.makedirs(root_path, exist_ok=True)
+ except OSError as exc:
+ raise SiteImportError(f'Could not create app directory: {exc}')
+
+ user_id = imp.created_by
+ if user_id is None:
+ admin = User.query.filter_by(role='admin').first()
+ if not admin:
+ raise SiteImportError('No admin user to own the imported app')
+ user_id = admin.id
+
+ application = Application(
+ name=name,
+ app_type=app_type,
+ status='stopped',
+ source='manual',
+ php_version=analysis.get('php_version'),
+ root_path=root_path,
+ user_id=user_id,
+ )
+ db.session.add(application)
+ db.session.commit()
+ ctx['app'] = application
+ ctx['result']['app_id'] = application.id
+ ctx['result']['app_name'] = name
+ ctx['result']['warnings'].append(
+ 'Application row created; complete container provisioning '
+ '(deploy) from the Services page.')
+ cls._log(imp, f"Created application '{name}' (id={application.id}, "
+ f'type={app_type}).')
+
+ @classmethod
+ def _get_app(cls, imp, ctx):
+ if ctx.get('app') is not None:
+ return ctx['app']
+ from app.models import Application
+ app_id = ctx['result'].get('app_id') or imp.get_result().get('app_id')
+ application = Application.query.get(app_id) if app_id else None
+ if not application:
+ raise SiteImportError('Imported application row not found — '
+ "retry from step 'create_app'")
+ ctx['app'] = application
+ ctx['result'].setdefault('app_id', application.id)
+ return application
+
+ @classmethod
+ def _step_copy_files(cls, imp, ctx):
+ application = cls._get_app(imp, ctx)
+ analysis = ctx['analysis']
+ if not analysis.get('homedir_present'):
+ ctx['result']['warnings'].append(
+ 'Backup has no homedir — no site files copied.')
+ cls._log(imp, 'Warning: no homedir in backup; nothing to copy.')
+ return
+ root = os.path.join(ctx['extracted'],
+ analysis.get('source_root') or '.')
+ homedir = os.path.join(root, 'homedir')
+ primary = (analysis.get('domains') or [{}])[0]
+ docroot = primary.get('docroot') or ''
+ # docroots are absolute inside the old server ('/home/user/public_html');
+ # keep only the path under the home directory.
+ rel = re.sub(r'^/home[^/]*/[^/]+/?', '', docroot).strip('/')
+ src = os.path.join(homedir, rel) if rel else homedir
+ if not os.path.isdir(src):
+ fallback = os.path.join(homedir, 'public_html')
+ if os.path.isdir(fallback):
+ src = fallback
+ else:
+ src = homedir
+ if not os.path.isdir(src):
+ raise SiteImportError(f'Docroot source not found in backup: {src}')
+ shutil.copytree(src, application.root_path, dirs_exist_ok=True)
+ copied = sum(len(files) for _b, _d, files in os.walk(application.root_path))
+ cls._log(imp, f'Copied site files from {os.path.relpath(src, ctx["extracted"])} '
+ f'({copied} file(s) now in docroot).')
+
+ @classmethod
+ def _step_create_databases(cls, imp, ctx):
+ from app.services.database_service import DatabaseService
+ from app.services.managed_database_service import ManagedDatabaseService
+
+ application = cls._get_app(imp, ctx)
+ imported = ctx['result'].setdefault('databases', [])
+ for entry in ctx['analysis'].get('databases', []):
+ name = entry.get('name') or ''
+ if not _SAFE_DB_NAME_RE.match(name):
+ ctx['result']['warnings'].append(
+ f'Skipped database with unsafe name {name!r}')
+ cls._log(imp, f'Warning: skipped database with unsafe name {name!r}.')
+ continue
+ if entry.get('engine') != 'mysql':
+ ctx['result']['warnings'].append(
+ f"Database {name}: engine {entry.get('engine')!r} not "
+ 'supported yet — skipped.')
+ continue
+ created = DatabaseService.mysql_create_database(name)
+ if not created.get('success'):
+ raise SiteImportError(
+ f"Could not create database {name}: {created.get('error')}")
+ cls._log(imp, f'Created MySQL database {name}.')
+ dump = os.path.join(ctx['extracted'], entry.get('dump_path') or '')
+ if entry.get('dump_path') and os.path.isfile(dump):
+ restored = DatabaseService.mysql_restore(name, dump)
+ if not restored.get('success'):
+ raise SiteImportError(
+ f"Import of dump for {name} failed: {restored.get('error')}")
+ cls._log(imp, f'Imported dump into {name} '
+ f"({entry.get('size', 0)} bytes).")
+ else:
+ ctx['result']['warnings'].append(
+ f'Dump file for database {name} not found — created empty.')
+ cls._log(imp, f'Warning: dump for {name} missing; created empty.')
+ try:
+ ManagedDatabaseService.record_provisioned(
+ 'mysql', name, owner_application_id=application.id)
+ except Exception as exc: # tracking is additive, never fatal
+ logger.warning('Could not record managed DB %s: %s', name, exc)
+ if name not in imported:
+ imported.append(name)
+
+ # SQL used to recreate a user with its original password hash.
+ @staticmethod
+ def _preserve_user_sql(username, hash_value, host='localhost'):
+ return (f"CREATE USER IF NOT EXISTS '{username}'@'{host}' "
+ f"IDENTIFIED WITH mysql_native_password AS '{hash_value}'")
+
+ @classmethod
+ def _step_create_db_users(cls, imp, ctx):
+ from app.services.database_service import DatabaseService
+
+ imported_dbs = set(ctx['result'].get('databases') or
+ [d['name'] for d in ctx['analysis'].get('databases', [])])
+ created_users = ctx['result'].setdefault('db_users', [])
+ for entry in ctx['analysis'].get('db_users', []):
+ user = entry.get('user') or ''
+ if not _SAFE_DB_USER_RE.match(user):
+ ctx['result']['warnings'].append(
+ f'Skipped DB user with unsafe name {user!r}')
+ cls._log(imp, f'Warning: skipped DB user with unsafe name {user!r}.')
+ continue
+ hash_value = entry.get('hash') or ''
+ preserved = (entry.get('hash_format') == 'mysql_native_password'
+ and _NATIVE_HASH_RE.match(hash_value))
+ if preserved:
+ result = DatabaseService.mysql_execute(
+ cls._preserve_user_sql(user, hash_value))
+ if not result.get('success'):
+ raise SiteImportError(
+ f"Could not create DB user {user}: {result.get('error')}")
+ cls._log(imp, f'Recreated DB user {user} with its original '
+ 'password hash (mysql_native_password).')
+ else:
+ password = DatabaseService.generate_password()
+ result = DatabaseService.mysql_create_user(user, password)
+ if not result.get('success'):
+ raise SiteImportError(
+ f"Could not create DB user {user}: {result.get('error')}")
+ ctx['result']['warnings'].append(
+ f'DB user {user}: original password hash could not be '
+ 'preserved — a new password was generated; update the '
+ "site's DB credentials.")
+ cls._log(imp, f'Created DB user {user} with a NEW password '
+ '(hash format not portable).')
+ # Re-grant on the imported databases the user had grants on.
+ granted = set()
+ for grant in entry.get('grants', []):
+ for name in imported_dbs:
+ if name in granted:
+ continue
+ if re.search(r'ON\s+[`\'"]?' + re.escape(name), grant,
+ re.IGNORECASE):
+ granted.add(name)
+ if not granted and imported_dbs and not entry.get('grants'):
+ granted = set(imported_dbs) # no grant info — grant on all imported
+ for name in sorted(granted):
+ result = DatabaseService.mysql_grant_privileges(user, name)
+ if result.get('success'):
+ cls._log(imp, f'Granted ALL on {name} to {user}.')
+ else:
+ ctx['result']['warnings'].append(
+ f"Could not grant {user} access to {name}: "
+ f"{result.get('error')}")
+ created_users.append({'user': user, 'preserved_hash': bool(preserved)})
+
+ @classmethod
+ def _step_install_crontab(cls, imp, ctx):
+ try:
+ from app.services.cron_service import CronService
+ except ImportError:
+ ctx['result']['warnings'].append(
+ 'Cron service unavailable — crontab lines not installed.')
+ return
+ installed = 0
+ for line in ctx['analysis'].get('crontab', []):
+ parts = line.split(None, 5)
+ if len(parts) < 6:
+ ctx['result']['warnings'].append(
+ f'Unparseable cron line skipped: {line!r}')
+ continue
+ schedule = ' '.join(parts[:5])
+ command = parts[5]
+ result = CronService.add_job(
+ schedule, command, name=f'import-{imp.id}',
+ description=f'Imported from {imp.source_type} backup')
+ if result.get('success'):
+ installed += 1
+ cls._log(imp, f'Installed cron job: {schedule} {command}')
+ else:
+ ctx['result']['warnings'].append(
+ f"Cron line not installed ({result.get('error')}): {line!r}")
+ cls._log(imp, f'Warning: cron line rejected: {line!r} '
+ f"({result.get('error')}).")
+ ctx['result']['cron_installed'] = installed
+
+ @classmethod
+ def _step_fix_permissions(cls, imp, ctx):
+ application = cls._get_app(imp, ctx)
+ if os.name == 'nt':
+ cls._log(imp, 'Skipping permission fix (not a Linux host).')
+ return
+ root = application.root_path
+ fixed = 0
+ for base, dirs, files in os.walk(root):
+ for d in dirs:
+ try:
+ os.chmod(os.path.join(base, d), 0o755)
+ fixed += 1
+ except OSError:
+ pass
+ for f in files:
+ try:
+ os.chmod(os.path.join(base, f), 0o644)
+ fixed += 1
+ except OSError:
+ pass
+ try:
+ shutil.chown(root, user='www-data', group='www-data')
+ except (LookupError, PermissionError, OSError):
+ ctx['result']['warnings'].append(
+ 'Could not chown the docroot to www-data (best-effort).')
+ cls._log(imp, f'Permissions normalised on {fixed} path(s).')
+
+ @classmethod
+ def _step_validate(cls, imp, ctx):
+ from app.services.database_service import DatabaseService
+
+ application = cls._get_app(imp, ctx)
+ problems = []
+ if ctx['analysis'].get('homedir_present'):
+ try:
+ non_empty = bool(os.listdir(application.root_path))
+ except OSError:
+ non_empty = False
+ if non_empty:
+ cls._log(imp, 'Validate: docroot is non-empty.')
+ else:
+ problems.append('Docroot is empty after copy')
+ for name in ctx['result'].get('databases', []):
+ result = DatabaseService.mysql_execute(
+ 'SELECT COUNT(*) FROM information_schema.tables '
+ f"WHERE table_schema='{name}'")
+ if result.get('success'):
+ cls._log(imp, f'Validate: database {name} reachable '
+ f"(tables: {(result.get('output') or '').strip() or '?'}).")
+ else:
+ ctx['result']['warnings'].append(
+ f'Could not verify database {name}: '
+ f"{result.get('error')} (best-effort check)")
+ if problems:
+ raise SiteImportError('; '.join(problems))
+ ctx['result']['validated'] = True
+
+ # ── logging ──
+ @classmethod
+ def _log(cls, imp, msg):
+ imp.append_log(msg)
+ db.session.commit()
+ logger.info('[site-import %s] %s', imp.id, msg)
+
+ # ── jobs ──
+ @classmethod
+ def enqueue_analyze(cls, imp):
+ from app.jobs.service import JobService
+ return JobService.enqueue(ANALYZE_JOB_KIND,
+ payload={'import_id': imp.id},
+ max_attempts=1,
+ owner_type='site_import', owner_id=imp.id)
+
+ @classmethod
+ def enqueue_run(cls, imp, from_step=None):
+ from app.jobs.service import JobService
+ payload = {'import_id': imp.id}
+ if from_step:
+ payload['from_step'] = from_step
+ return JobService.enqueue(RUN_JOB_KIND, payload=payload,
+ max_attempts=1,
+ owner_type='site_import', owner_id=imp.id)
+
+ @classmethod
+ def _job_analyze(cls, job):
+ payload = job.get_payload() or {}
+ imp = SiteImport.query.get(payload.get('import_id'))
+ if not imp:
+ raise ValueError(f"site import {payload.get('import_id')!r} not found")
+ cls.analyze(imp)
+ return {'import_id': imp.id, 'status': imp.status}
+
+ @classmethod
+ def _job_run(cls, job):
+ payload = job.get_payload() or {}
+ imp = SiteImport.query.get(payload.get('import_id'))
+ if not imp:
+ raise ValueError(f"site import {payload.get('import_id')!r} not found")
+ cls.run(imp, from_step=payload.get('from_step'))
+ return {'import_id': imp.id, 'status': imp.status}
+
+ @classmethod
+ def register_jobs(cls):
+ """Register the import job kinds on the unified job bus. Called once
+ at boot (see app/__init__.py job wiring)."""
+ from app.jobs import registry
+ registry.register(ANALYZE_JOB_KIND, cls._job_analyze, replace=True)
+ registry.register(RUN_JOB_KIND, cls._job_run, replace=True)
diff --git a/backend/app/services/site_importers/__init__.py b/backend/app/services/site_importers/__init__.py
new file mode 100644
index 00000000..b37609f4
--- /dev/null
+++ b/backend/app/services/site_importers/__init__.py
@@ -0,0 +1,54 @@
+"""Registry of control-panel archive importers.
+
+Each supported source panel format (cPanel, DirectAdmin, Hestia)
+contributes a :class:`~.base.BaseSiteImporter` subclass and registers it here.
+The orchestrator (``SiteImportService``) resolves importers exclusively
+through :func:`get_importer` / :func:`detect_format`, so adding a format is:
+write the subclass, decorate it with ``@register_importer``, import the
+module below.
+"""
+from .base import BaseSiteImporter # noqa: F401 (re-exported for subclasses)
+
+_IMPORTERS = {}
+
+
+def register_importer(cls):
+ """Register an importer class (usable as a decorator). Keyed by
+ ``cls.format``; re-registering a format replaces it (plugin reloads)."""
+ if not getattr(cls, 'format', None):
+ raise ValueError('importer class must define a non-empty `format`')
+ _IMPORTERS[cls.format] = cls
+ return cls
+
+
+def get_importer(source_type):
+ """Return an importer INSTANCE for ``source_type``, or None if unknown."""
+ cls = _IMPORTERS.get(source_type)
+ return cls() if cls else None
+
+
+def available_formats():
+ return sorted(_IMPORTERS.keys())
+
+
+def detect_format(extracted_dir):
+ """Run every registered importer's detect() and return the first
+ (format, importer instance) match, or (None, None)."""
+ for fmt in available_formats():
+ importer = _IMPORTERS[fmt]()
+ try:
+ if importer.detect(extracted_dir):
+ return fmt, importer
+ except Exception: # a broken detect() must not mask other formats
+ continue
+ return None, None
+
+
+# ── built-in formats ──
+from .cpanel import CpanelImporter # noqa: E402
+from .directadmin import DirectadminImporter # noqa: E402
+from .hestia import HestiaImporter # noqa: E402
+
+register_importer(CpanelImporter)
+register_importer(DirectadminImporter)
+register_importer(HestiaImporter)
diff --git a/backend/app/services/site_importers/base.py b/backend/app/services/site_importers/base.py
new file mode 100644
index 00000000..0fadb309
--- /dev/null
+++ b/backend/app/services/site_importers/base.py
@@ -0,0 +1,105 @@
+"""Base class for control-panel archive importers.
+
+A format importer knows how to recognise one panel's backup layout, turn it
+into a neutral analyse report, and propose the ordered migration steps the
+orchestrator (``SiteImportService``) executes. Importers are pure inspectors:
+they never touch Docker, MySQL or the filesystem outside ``extracted_dir`` —
+all side effects live in the orchestration service so every format shares the
+same battle-tested step implementations.
+
+Contract for new formats (DirectAdmin, Hestia, ...):
+
+* subclass ``BaseSiteImporter``, set ``format`` to the source_type slug;
+* implement ``detect(extracted_dir) -> bool`` — cheap layout sniff, must not
+ raise on foreign layouts;
+* implement ``analyze(extracted_dir) -> dict`` returning the REPORT SHAPE
+ below — missing pieces become entries in ``warnings``/``unsupported``,
+ never exceptions;
+* optionally override ``plan(analysis, options)`` if the default step list
+ does not fit; step ``key`` values must map to ``_step_<key>`` methods on
+ ``SiteImportService``;
+* register with ``@register_importer`` (see package ``__init__``).
+
+REPORT SHAPE (all keys present, defaulted):
+ {
+ 'format': '<format slug>',
+ 'homedir_present': bool,
+ 'domains': [{'domain', 'docroot', 'type'}],
+ 'databases': [{'name', 'engine', 'dump_path', 'size'}],
+ 'db_users': [{'user', 'hash', 'hash_format', 'grants': [...]}],
+ 'crontab': ['<raw cron lines>'],
+ 'mail_accounts_count': int, # informational only — mail not migrated
+ 'php_version': str | None,
+ 'warnings': [str],
+ 'unsupported': [str],
+ }
+"""
+
+
+class BaseSiteImporter:
+ """Interface every format importer implements."""
+
+ #: source_type slug this importer handles ('cpanel', 'directadmin', ...).
+ format = None
+
+ # ── required interface ──
+ def detect(self, extracted_dir):
+ """Return True if ``extracted_dir`` looks like this panel's backup."""
+ raise NotImplementedError
+
+ def analyze(self, extracted_dir):
+ """Return the analyse report dict (see module docstring)."""
+ raise NotImplementedError
+
+ def plan(self, analysis, options):
+ """Return the ordered list of step dicts for ``analysis``.
+
+ Each step: {'key': str, 'title': str}. ``key`` must correspond to a
+ ``_step_<key>`` method on SiteImportService. The default plan covers
+ the common docroot+MySQL+cron migration; skip steps that have no
+ inputs so retries and progress bars stay honest.
+ """
+ options = options or {}
+ steps = [
+ {'key': 'create_app', 'title': 'Create the application'},
+ {'key': 'copy_files', 'title': 'Copy site files'},
+ ]
+ if analysis.get('databases'):
+ steps.append({'key': 'create_databases',
+ 'title': 'Create and import databases'})
+ if analysis.get('db_users'):
+ steps.append({'key': 'create_db_users',
+ 'title': 'Recreate database users'})
+ if analysis.get('crontab'):
+ steps.append({'key': 'install_crontab',
+ 'title': 'Install cron jobs'})
+ steps.append({'key': 'fix_permissions', 'title': 'Fix permissions'})
+ steps.append({'key': 'validate', 'title': 'Validate the migration'})
+ return steps
+
+ # ── shared helpers ──
+ @staticmethod
+ def _empty_report(fmt):
+ return {
+ 'format': fmt,
+ 'homedir_present': False,
+ 'domains': [],
+ 'databases': [],
+ 'db_users': [],
+ 'crontab': [],
+ 'mail_accounts_count': 0,
+ 'php_version': None,
+ 'warnings': [],
+ 'unsupported': [],
+ }
+
+ @staticmethod
+ def _read_text(path, warnings=None, label=None):
+ """Best-effort text read; returns '' and records a warning on failure."""
+ try:
+ with open(path, 'r', encoding='utf-8', errors='replace') as fh:
+ return fh.read()
+ except OSError as exc:
+ if warnings is not None:
+ warnings.append(f'Could not read {label or path}: {exc}')
+ return ''
diff --git a/backend/app/services/site_importers/cpanel.py b/backend/app/services/site_importers/cpanel.py
new file mode 100644
index 00000000..3e2a5314
--- /dev/null
+++ b/backend/app/services/site_importers/cpanel.py
@@ -0,0 +1,313 @@
+"""cPanel/WHM full-backup importer.
+
+Understands the ``backup-M.D.YYYY_HH-MM-SS_user.tar.gz`` layout produced by
+cPanel's "Full Account Backup":
+
+ backup-.../ (single top-level dir)
+ homedir/ or homedir.tar — the account home (docroots live here)
+ mysql/<db>.sql — one dump per database
+ mysql.sql — CREATE USER / GRANT statements
+ cp/<user> — account metadata (main domain, plan)
+ userdata/<domain> — per-domain config (documentroot, php)
+ userdata/main — index of main/addon/sub domains
+ cron/<user> — the account crontab
+ dnszones/<zone>.db — BIND zone files (not migrated)
+ va/ — vacation autoresponders (not migrated)
+
+Everything is parsed defensively: a missing or malformed piece becomes a
+``warnings`` entry, never an exception — partial backups still analyse.
+"""
+import os
+import re
+
+from .base import BaseSiteImporter
+
+# Old-style mysql_native_password hash: '*' + 40 hex chars.
+_NATIVE_HASH_RE = re.compile(r'^\*[0-9A-Fa-f]{40}$')
+
+# GRANT ... TO 'user'@'host' [IDENTIFIED BY PASSWORD '*hash']
+_GRANT_RE = re.compile(
+ r"GRANT\s+(?P<privs>.+?)\s+ON\s+(?P<scope>\S+)\s+TO\s+"
+ r"['`\"](?P<user>[^'`\"]+)['`\"]@['`\"](?P<host>[^'`\"]+)['`\"]"
+ r"(?:\s+IDENTIFIED\s+BY\s+PASSWORD\s+'(?P<hash>[^']*)')?",
+ re.IGNORECASE)
+
+# CREATE USER 'user'@'host' IDENTIFIED WITH mysql_native_password AS '*hash'
+# (also matches the legacy IDENTIFIED BY PASSWORD form)
+_CREATE_USER_RE = re.compile(
+ r"CREATE\s+USER\s+(?:IF\s+NOT\s+EXISTS\s+)?"
+ r"['`\"](?P<user>[^'`\"]+)['`\"]@['`\"](?P<host>[^'`\"]+)['`\"]\s+"
+ r"IDENTIFIED\s+(?:WITH\s+(?P<plugin>\S+)\s+AS|BY\s+PASSWORD)\s+"
+ r"'(?P<hash>[^']*)'",
+ re.IGNORECASE)
+
+_EA_PHP_RE = re.compile(r'ea-php(\d)(\d+)', re.IGNORECASE)
+
+# userdata files that are not domain configs.
+_USERDATA_SKIP = {'main', 'cache', 'cache.json'}
+
+
+class CpanelImporter(BaseSiteImporter):
+ format = 'cpanel'
+
+ # ── layout ──
+ @staticmethod
+ def _root(extracted_dir):
+ """The backup content root: the extraction dir itself, or its single
+ ``backup-*``-style top-level directory."""
+ markers = ('cp', 'userdata', 'homedir', 'homedir.tar', 'mysql')
+ if any(os.path.exists(os.path.join(extracted_dir, m)) for m in markers):
+ return extracted_dir
+ try:
+ entries = [e for e in os.listdir(extracted_dir)
+ if os.path.isdir(os.path.join(extracted_dir, e))]
+ except OSError:
+ return extracted_dir
+ for entry in entries:
+ candidate = os.path.join(extracted_dir, entry)
+ if any(os.path.exists(os.path.join(candidate, m)) for m in markers):
+ return candidate
+ return extracted_dir
+
+ def detect(self, extracted_dir):
+ root = self._root(extracted_dir)
+ has_cp = os.path.isdir(os.path.join(root, 'cp'))
+ has_userdata = os.path.isdir(os.path.join(root, 'userdata'))
+ has_homedir = (os.path.isdir(os.path.join(root, 'homedir'))
+ or os.path.isfile(os.path.join(root, 'homedir.tar')))
+ # cp/ alone is a strong signal; otherwise require two markers.
+ return has_cp or (has_userdata and has_homedir)
+
+ # ── analyse ──
+ def analyze(self, extracted_dir):
+ report = self._empty_report(self.format)
+ warnings = report['warnings']
+ root = self._root(extracted_dir)
+ report['source_root'] = os.path.relpath(root, extracted_dir)
+
+ # homedir
+ homedir = os.path.join(root, 'homedir')
+ homedir_tar = os.path.join(root, 'homedir.tar')
+ report['homedir_present'] = (os.path.isdir(homedir)
+ or os.path.isfile(homedir_tar))
+ if not report['homedir_present']:
+ warnings.append('No homedir/ or homedir.tar found — '
+ 'site files cannot be copied.')
+
+ # account username (cp/<user>)
+ username = self._account_username(root)
+ if username:
+ report['account_user'] = username
+
+ # domains + php version (userdata/)
+ self._parse_userdata(root, report)
+ if not report['domains']:
+ warnings.append('No domains found in userdata/ — the app will be '
+ 'created without a docroot mapping.')
+
+ # databases (mysql/*.sql)
+ self._parse_databases(root, extracted_dir, report)
+
+ # db users + grants (mysql.sql)
+ self._parse_db_users(root, report)
+
+ # crontab (cron/<user>)
+ self._parse_crontab(root, username, report)
+
+ # mail accounts — informational only, never migrated.
+ report['mail_accounts_count'] = self._count_mail_accounts(root)
+ if report['mail_accounts_count']:
+ report['unsupported'].append(
+ f"{report['mail_accounts_count']} mail account(s) found — "
+ 'mailbox migration is not supported; recreate them manually.')
+
+ if os.path.isdir(os.path.join(root, 'dnszones')):
+ report['unsupported'].append(
+ 'DNS zone files found (dnszones/) — zones are not imported; '
+ 'manage DNS from the Domains page instead.')
+
+ return report
+
+ @staticmethod
+ def _account_username(root):
+ cp_dir = os.path.join(root, 'cp')
+ if not os.path.isdir(cp_dir):
+ return None
+ try:
+ entries = [e for e in os.listdir(cp_dir)
+ if os.path.isfile(os.path.join(cp_dir, e))]
+ except OSError:
+ return None
+ return entries[0] if entries else None
+
+ @classmethod
+ def _parse_kv_file(cls, path):
+ """Parse a cPanel ``key: value`` userdata file into a dict."""
+ data = {}
+ text = cls._read_text(path)
+ for line in text.splitlines():
+ if ':' not in line or line.lstrip().startswith('#'):
+ continue
+ key, _, value = line.partition(':')
+ data[key.strip().lower()] = value.strip().strip("'\"")
+ return data
+
+ def _parse_userdata(self, root, report):
+ userdata = os.path.join(root, 'userdata')
+ if not os.path.isdir(userdata):
+ report['warnings'].append('No userdata/ directory found — '
+ 'domain list unavailable.')
+ return
+ main_meta = self._parse_kv_file(os.path.join(userdata, 'main'))
+ main_domain = main_meta.get('main_domain')
+ try:
+ entries = sorted(os.listdir(userdata))
+ except OSError as exc:
+ report['warnings'].append(f'Could not list userdata/: {exc}')
+ return
+ for entry in entries:
+ path = os.path.join(userdata, entry)
+ if (not os.path.isfile(path) or entry in _USERDATA_SKIP
+ or entry.endswith('.json') or entry.endswith('_SSL')):
+ continue
+ meta = self._parse_kv_file(path)
+ docroot = meta.get('documentroot')
+ if not docroot and '.' not in entry:
+ continue # not a domain config
+ report['domains'].append({
+ 'domain': entry,
+ 'docroot': docroot,
+ 'type': 'primary' if entry == main_domain else 'secondary',
+ })
+ php = meta.get('phpversion')
+ if php and (entry == main_domain or not report['php_version']):
+ report['php_version'] = self._normalize_php(php)
+ # Primary first so downstream steps can just take domains[0].
+ report['domains'].sort(key=lambda d: d['type'] != 'primary')
+
+ @staticmethod
+ def _normalize_php(raw):
+ match = _EA_PHP_RE.search(raw or '')
+ if match:
+ return f'{match.group(1)}.{match.group(2)}'
+ return raw or None
+
+ @staticmethod
+ def _parse_databases(root, extracted_dir, report):
+ mysql_dir = os.path.join(root, 'mysql')
+ if not os.path.isdir(mysql_dir):
+ report['warnings'].append('No mysql/ directory found — '
+ 'no databases to import.')
+ return
+ try:
+ entries = sorted(os.listdir(mysql_dir))
+ except OSError as exc:
+ report['warnings'].append(f'Could not list mysql/: {exc}')
+ return
+ for entry in entries:
+ if not entry.endswith('.sql'):
+ continue
+ path = os.path.join(mysql_dir, entry)
+ if not os.path.isfile(path):
+ continue
+ try:
+ size = os.path.getsize(path)
+ except OSError:
+ size = 0
+ report['databases'].append({
+ 'name': entry[:-4],
+ 'engine': 'mysql',
+ 'dump_path': os.path.relpath(path, extracted_dir).replace('\\', '/'),
+ 'size': size,
+ })
+
+ @classmethod
+ def _parse_db_users(cls, root, report):
+ grants_file = os.path.join(root, 'mysql.sql')
+ if not os.path.isfile(grants_file):
+ report['warnings'].append('No mysql.sql grants file found — '
+ 'database users cannot be preserved.')
+ return
+ text = cls._read_text(grants_file, report['warnings'], 'mysql.sql')
+ users = {}
+
+ def _user(name):
+ return users.setdefault(name, {
+ 'user': name, 'hash': None,
+ 'hash_format': 'unknown', 'grants': [],
+ })
+
+ for match in _CREATE_USER_RE.finditer(text):
+ entry = _user(match.group('user'))
+ cls._record_hash(entry, match.group('hash'),
+ plugin=match.group('plugin'))
+ for match in _GRANT_RE.finditer(text):
+ entry = _user(match.group('user'))
+ statement = match.group(0).strip()
+ # Never carry password material inside the grant list.
+ statement = re.sub(r"\s+IDENTIFIED\s+BY\s+PASSWORD\s+'[^']*'", '',
+ statement, flags=re.IGNORECASE)
+ entry['grants'].append(statement)
+ if match.group('hash'):
+ cls._record_hash(entry, match.group('hash'))
+ report['db_users'] = list(users.values())
+ for entry in report['db_users']:
+ if entry['hash_format'] != 'mysql_native_password':
+ report['warnings'].append(
+ f"Password hash for DB user '{entry['user']}' is not a "
+ 'mysql_native_password hash — a new password will be '
+ 'generated on import.')
+
+ @staticmethod
+ def _record_hash(entry, hash_value, plugin=None):
+ if not hash_value:
+ return
+ entry['hash'] = hash_value
+ plugin = (plugin or '').lower()
+ if _NATIVE_HASH_RE.match(hash_value) and plugin in ('', 'mysql_native_password'):
+ entry['hash_format'] = 'mysql_native_password'
+ elif plugin:
+ entry['hash_format'] = plugin
+
+ @classmethod
+ def _parse_crontab(cls, root, username, report):
+ cron_dir = os.path.join(root, 'cron')
+ if not os.path.isdir(cron_dir):
+ return
+ candidates = []
+ if username:
+ candidates.append(os.path.join(cron_dir, username))
+ try:
+ candidates.extend(os.path.join(cron_dir, e)
+ for e in sorted(os.listdir(cron_dir)))
+ except OSError:
+ pass
+ for path in candidates:
+ if not os.path.isfile(path):
+ continue
+ for line in cls._read_text(path).splitlines():
+ stripped = line.strip()
+ if (not stripped or stripped.startswith('#')
+ or re.match(r'^[A-Z_]+=', stripped)):
+ continue # comments and SHELL=/PATH= env lines
+ if stripped not in report['crontab']:
+ report['crontab'].append(stripped)
+ break # first crontab file wins
+
+ @staticmethod
+ def _count_mail_accounts(root):
+ """Count mailbox entries in homedir/etc/<domain>/passwd (informational)."""
+ etc_dir = os.path.join(root, 'homedir', 'etc')
+ if not os.path.isdir(etc_dir):
+ return 0
+ count = 0
+ try:
+ for domain in os.listdir(etc_dir):
+ passwd = os.path.join(etc_dir, domain, 'passwd')
+ if os.path.isfile(passwd):
+ with open(passwd, 'r', encoding='utf-8',
+ errors='replace') as fh:
+ count += sum(1 for line in fh if line.strip())
+ except OSError:
+ pass
+ return count
diff --git a/backend/app/services/site_importers/directadmin.py b/backend/app/services/site_importers/directadmin.py
new file mode 100644
index 00000000..c8530158
--- /dev/null
+++ b/backend/app/services/site_importers/directadmin.py
@@ -0,0 +1,372 @@
+"""DirectAdmin full-backup importer.
+
+Understands the account backup layout produced by DirectAdmin's backup
+system (``user.admin.<user>.tar.gz`` / ``backup/<user>.tar.gz``). The tar
+root is the account's home content plus metadata:
+
+ backup/ — account metadata
+ user.conf — account config (username, primary domain)
+ <domain>/domain.conf — per-domain config
+ <domain>/email/passwd — mailbox credential file (not migrated)
+ mysql.conf — the account's MySQL login
+ <db>.sql or mysql/<db>.sql — one dump per database
+ <db>.conf — per-database users/hosts
+ crontab.conf — the account crontab ("<id>=<cron line>")
+ domains/<domain>/public_html — docroots (plain files, not nested tars)
+ imap/<domain>/<box>/ — mail data (not migrated)
+
+The orchestrator's copy step expects site files under
+``<source_root>/homedir/``, so ``analyze`` stages a copy of ``domains/``
+at ``homedir/domains/`` inside the work directory (mirroring how cPanel's
+nested ``homedir.tar`` gets unpacked before the copy step).
+
+Everything is parsed defensively: a missing or malformed piece becomes a
+``warnings`` entry, never an exception — partial backups still analyse.
+"""
+import os
+import re
+import shutil
+
+from .base import BaseSiteImporter
+
+# Old-style mysql_native_password hash: '*' + 40 hex chars.
+_NATIVE_HASH_RE = re.compile(r'^\*[0-9A-Fa-f]{40}$')
+
+# '8.1', 'php81', '8_1' → ('8', '1')
+_PHP_VER_RE = re.compile(r'(\d)[._-]?(\d{1,2})')
+
+# "<id>=<cron line>" prefix used by crontab.conf.
+_CRON_ID_RE = re.compile(r'^\d+=')
+
+# conf keys naming per-database users: user, user0, user1, ...
+_DB_USER_KEY_RE = re.compile(r'^user(\d*)$')
+
+# backup/ entries that are metadata files, never domain dirs.
+_NON_DOMAIN_DIRS = {'mysql', 'email', 'imap'}
+
+
+class DirectadminImporter(BaseSiteImporter):
+ format = 'directadmin'
+
+ # ── layout ──
+ @staticmethod
+ def _root(extracted_dir):
+ """The backup content root: the extraction dir itself, or its single
+ top-level directory (archives usually extract to one wrapper dir)."""
+ markers = ('backup', 'domains', 'imap')
+ if any(os.path.isdir(os.path.join(extracted_dir, m)) for m in markers):
+ return extracted_dir
+ try:
+ entries = [e for e in os.listdir(extracted_dir)
+ if os.path.isdir(os.path.join(extracted_dir, e))]
+ except OSError:
+ return extracted_dir
+ for entry in entries:
+ candidate = os.path.join(extracted_dir, entry)
+ if any(os.path.isdir(os.path.join(candidate, m)) for m in markers):
+ return candidate
+ return extracted_dir
+
+ def detect(self, extracted_dir):
+ root = self._root(extracted_dir)
+ backup_dir = os.path.join(root, 'backup')
+ if not os.path.isdir(backup_dir):
+ return False
+ return (os.path.isfile(os.path.join(backup_dir, 'user.conf'))
+ or os.path.isfile(os.path.join(backup_dir, 'mysql.conf'))
+ or os.path.isfile(os.path.join(backup_dir, 'crontab.conf'))
+ or os.path.isdir(os.path.join(root, 'domains')))
+
+ # ── analyse ──
+ def analyze(self, extracted_dir):
+ report = self._empty_report(self.format)
+ warnings = report['warnings']
+ root = self._root(extracted_dir)
+ report['source_root'] = os.path.relpath(root, extracted_dir)
+ backup_dir = os.path.join(root, 'backup')
+ if not os.path.isdir(backup_dir):
+ warnings.append('No backup/ metadata directory found — '
+ 'account metadata unavailable.')
+
+ # account config (backup/user.conf)
+ user_conf = {}
+ user_conf_path = os.path.join(backup_dir, 'user.conf')
+ if os.path.isfile(user_conf_path):
+ user_conf = self._parse_conf_pairs(self._read_text(
+ user_conf_path, warnings, 'backup/user.conf'))
+ else:
+ warnings.append('No backup/user.conf found — account username '
+ 'and primary domain unavailable.')
+ username = user_conf.get('username') or user_conf.get('name')
+ if username:
+ report['account_user'] = username
+
+ # domains + php version
+ self._parse_domains(root, backup_dir, user_conf, report)
+ if not report['domains']:
+ warnings.append('No domains found in the backup — the app will '
+ 'be created without a docroot mapping.')
+
+ # stage domains/ as homedir/domains/ for the shared copy step
+ self._stage_homedir(root, report)
+
+ # databases (backup/mysql/*.sql or backup/*.sql)
+ self._parse_databases(backup_dir, extracted_dir, report)
+
+ # db users (backup/mysql.conf + backup/<db>.conf)
+ self._parse_db_users(backup_dir, report)
+
+ # crontab (backup/crontab.conf or crontab/)
+ self._parse_crontab(root, backup_dir, report)
+
+ # mail accounts — informational only, never migrated.
+ report['mail_accounts_count'] = self._count_mail_accounts(
+ root, backup_dir)
+ if report['mail_accounts_count']:
+ report['unsupported'].append(
+ f"{report['mail_accounts_count']} mail account(s) found — "
+ 'mailbox migration is not supported; recreate them manually.')
+
+ return report
+
+ # ── conf parsing ──
+ @staticmethod
+ def _parse_conf_pairs(text):
+ """Parse DirectAdmin conf content: ``key=value`` per line, or
+ ``&``-joined single-line query style. Values may be quoted."""
+ pairs = {}
+ for chunk in re.split(r'[&\r\n]+', text or ''):
+ chunk = chunk.strip()
+ if not chunk or chunk.startswith('#') or '=' not in chunk:
+ continue
+ key, _, value = chunk.partition('=')
+ pairs[key.strip().lower()] = value.strip().strip('\'"')
+ return pairs
+
+ # ── domains ──
+ def _parse_domains(self, root, backup_dir, user_conf, report):
+ primary = (user_conf.get('domain') or '').lower() or None
+ username = report.get('account_user') or 'user'
+ found = {}
+
+ # per-domain metadata dirs under backup/
+ if os.path.isdir(backup_dir):
+ try:
+ entries = sorted(os.listdir(backup_dir))
+ except OSError as exc:
+ report['warnings'].append(f'Could not list backup/: {exc}')
+ entries = []
+ for entry in entries:
+ path = os.path.join(backup_dir, entry)
+ if (os.path.isdir(path) and '.' in entry
+ and entry.lower() not in _NON_DOMAIN_DIRS):
+ found[entry] = path
+
+ # docroot dirs under domains/
+ domains_dir = os.path.join(root, 'domains')
+ if os.path.isdir(domains_dir):
+ try:
+ for entry in sorted(os.listdir(domains_dir)):
+ if (os.path.isdir(os.path.join(domains_dir, entry))
+ and '.' in entry):
+ found.setdefault(entry, None)
+ except OSError as exc:
+ report['warnings'].append(f'Could not list domains/: {exc}')
+
+ for domain in sorted(found):
+ report['domains'].append({
+ 'domain': domain,
+ 'docroot': f'/home/{username}/domains/{domain}/public_html',
+ 'type': 'primary' if domain == primary else 'secondary',
+ })
+ if not os.path.isdir(os.path.join(domains_dir, domain,
+ 'public_html')):
+ report['warnings'].append(
+ f'No domains/{domain}/public_html directory in the '
+ 'backup — site files for this domain are missing.')
+ if primary and primary not in found and report['domains']:
+ report['warnings'].append(
+ f'Primary domain {primary!r} from user.conf has no data in '
+ 'the backup.')
+ # Primary first so downstream steps can just take domains[0].
+ report['domains'].sort(key=lambda d: d['type'] != 'primary')
+
+ # php version: user.conf then per-domain domain.conf
+ report['php_version'] = self._find_php_version(
+ [user_conf] + [self._parse_conf_pairs(self._read_text(
+ os.path.join(meta, 'domain.conf')))
+ for meta in found.values() if meta])
+
+ @staticmethod
+ def _find_php_version(conf_dicts):
+ for conf in conf_dicts:
+ for key, value in conf.items():
+ if 'php' not in key:
+ continue
+ if 'release' not in key and 'ver' not in key:
+ continue
+ match = _PHP_VER_RE.search(value or '')
+ if match:
+ return f'{match.group(1)}.{match.group(2)}'
+ return None
+
+ # ── homedir staging ──
+ @staticmethod
+ def _stage_homedir(root, report):
+ """The shared copy step reads ``<source_root>/homedir/…`` — stage a
+ copy of domains/ there (the DirectAdmin analogue of unpacking
+ cPanel's nested homedir.tar)."""
+ domains_dir = os.path.join(root, 'domains')
+ if not os.path.isdir(domains_dir):
+ report['warnings'].append('No domains/ directory found — '
+ 'site files cannot be copied.')
+ return
+ staged = os.path.join(root, 'homedir', 'domains')
+ try:
+ if not os.path.isdir(staged):
+ shutil.copytree(domains_dir, staged)
+ report['warnings'].append(
+ 'Staged domains/ under homedir/ in the work directory '
+ 'so the shared file-copy step can run (extra copy).')
+ report['homedir_present'] = True
+ except OSError as exc:
+ report['warnings'].append(
+ f'Could not stage domains/ for the copy step: {exc}')
+
+ # ── databases ──
+ @staticmethod
+ def _parse_databases(backup_dir, extracted_dir, report):
+ candidates = []
+ mysql_dir = os.path.join(backup_dir, 'mysql')
+ for directory in (mysql_dir, backup_dir):
+ if not os.path.isdir(directory):
+ continue
+ try:
+ entries = sorted(os.listdir(directory))
+ except OSError as exc:
+ report['warnings'].append(
+ f'Could not list {os.path.basename(directory)}/: {exc}')
+ continue
+ candidates = [os.path.join(directory, e) for e in entries
+ if e.endswith('.sql')
+ and os.path.isfile(os.path.join(directory, e))]
+ if candidates:
+ break
+ if not candidates:
+ report['warnings'].append('No database dumps (*.sql) found in '
+ 'backup/ — no databases to import.')
+ return
+ for path in candidates:
+ try:
+ size = os.path.getsize(path)
+ except OSError:
+ size = 0
+ report['databases'].append({
+ 'name': os.path.basename(path)[:-4],
+ 'engine': 'mysql',
+ 'dump_path': os.path.relpath(
+ path, extracted_dir).replace('\\', '/'),
+ 'size': size,
+ })
+
+ # ── db users ──
+ def _parse_db_users(self, backup_dir, report):
+ users = {}
+
+ def _record(name, password):
+ if not name:
+ return
+ entry = users.setdefault(name, {
+ 'user': name, 'hash': None,
+ 'hash_format': 'unknown', 'grants': [],
+ })
+ if password and _NATIVE_HASH_RE.match(password):
+ entry['hash'] = password
+ entry['hash_format'] = 'mysql_native_password'
+ elif password and not entry['hash']:
+ entry['hash'] = password # opaque — new password on import
+
+ # the account's own MySQL login (backup/mysql.conf)
+ mysql_conf = os.path.join(backup_dir, 'mysql.conf')
+ if os.path.isfile(mysql_conf):
+ pairs = self._parse_conf_pairs(self._read_text(
+ mysql_conf, report['warnings'], 'backup/mysql.conf'))
+ _record(pairs.get('user'), pairs.get('passwd'))
+ else:
+ report['warnings'].append('No backup/mysql.conf found — the '
+ "account's MySQL login is unavailable.")
+
+ # per-database user lists (backup/<db>.conf)
+ for database in report['databases']:
+ conf_path = os.path.join(backup_dir, f"{database['name']}.conf")
+ if not os.path.isfile(conf_path):
+ continue
+ pairs = self._parse_conf_pairs(self._read_text(conf_path))
+ for key, value in pairs.items():
+ match = _DB_USER_KEY_RE.match(key)
+ if match:
+ _record(value, pairs.get(f'passwd{match.group(1)}'))
+
+ report['db_users'] = list(users.values())
+ for entry in report['db_users']:
+ if entry['hash_format'] != 'mysql_native_password':
+ report['warnings'].append(
+ f"Password for DB user '{entry['user']}' is not a "
+ 'portable mysql_native_password hash — a new password '
+ 'will be generated on import.')
+
+ # ── crontab ──
+ def _parse_crontab(self, root, backup_dir, report):
+ candidates = [os.path.join(backup_dir, 'crontab.conf')]
+ crontab_dir = os.path.join(root, 'crontab')
+ if os.path.isdir(crontab_dir):
+ try:
+ candidates.extend(os.path.join(crontab_dir, e)
+ for e in sorted(os.listdir(crontab_dir)))
+ except OSError:
+ pass
+ for path in candidates:
+ if not os.path.isfile(path):
+ continue
+ for line in self._read_text(path).splitlines():
+ stripped = _CRON_ID_RE.sub('', line.strip())
+ if not stripped or stripped.startswith('#'):
+ continue
+ parts = stripped.split(None, 5)
+ if len(parts) < 6 or stripped[0] not in '*0123456789/@-':
+ continue # env lines (MAILTO=...) and malformed rows
+ if stripped not in report['crontab']:
+ report['crontab'].append(stripped)
+ break # first crontab source wins
+
+ # ── mail ──
+ @staticmethod
+ def _count_mail_accounts(root, backup_dir):
+ count = 0
+ # mailbox credential files: backup/<domain>/email/passwd
+ if os.path.isdir(backup_dir):
+ try:
+ for entry in os.listdir(backup_dir):
+ passwd = os.path.join(backup_dir, entry, 'email', 'passwd')
+ if '.' in entry and os.path.isfile(passwd):
+ with open(passwd, 'r', encoding='utf-8',
+ errors='replace') as fh:
+ count += sum(1 for line in fh if line.strip())
+ except OSError:
+ pass
+ if count:
+ return count
+ # fall back to mailbox dirs: imap/<domain>/<box>/
+ imap_dir = os.path.join(root, 'imap')
+ if os.path.isdir(imap_dir):
+ try:
+ for domain in os.listdir(imap_dir):
+ domain_dir = os.path.join(imap_dir, domain)
+ if not os.path.isdir(domain_dir):
+ continue
+ count += sum(
+ 1 for box in os.listdir(domain_dir)
+ if os.path.isdir(os.path.join(domain_dir, box)))
+ except OSError:
+ pass
+ return count
diff --git a/backend/app/services/site_importers/hestia.py b/backend/app/services/site_importers/hestia.py
new file mode 100644
index 00000000..87e4adba
--- /dev/null
+++ b/backend/app/services/site_importers/hestia.py
@@ -0,0 +1,464 @@
+"""Hestia / VestaCP backup importer.
+
+Understands the user backup layout produced by ``v-backup-user``
+(``<user>.YYYY-MM-DD.tar``):
+
+ pam/passwd, pam/group — account identity
+ user.conf — account config (also hestia/ or vesta/)
+ hestia/ or vesta/ — panel object indexes (web.conf, db.conf,
+ cron.conf — KEY='VALUE' lines)
+ web/<domain>/ — per-domain data; the docroot ships as a
+ nested public_html.tar.gz or
+ domain_data.tar.gz
+ db/<name>/<name>.sql[.gz] — one dump (dir) per database
+ cron/cron.conf — cron jobs in panel config format
+ (MIN='..' HOUR='..' ... CMD='..')
+ mail/<domain>/ — mailboxes (not migrated)
+ dns/ — zone data (not imported)
+
+The orchestrator's copy step expects site files under
+``<source_root>/homedir/``, so ``analyze`` safely extracts each domain's
+nested docroot archive into ``homedir/web/<domain>/`` inside the work
+directory (mirroring how cPanel's nested homedir.tar gets unpacked).
+
+Everything is parsed defensively: a missing or malformed piece becomes a
+``warnings`` entry, never an exception — partial backups still analyse.
+"""
+import gzip
+import os
+import re
+import shutil
+import tarfile
+
+from .base import BaseSiteImporter
+
+# Old-style mysql_native_password hash: '*' + 40 hex chars.
+_NATIVE_HASH_RE = re.compile(r'^\*[0-9A-Fa-f]{40}$')
+
+# KEY='VALUE' pairs used throughout Hestia/Vesta config lines.
+_KV_RE = re.compile(r"(\w+)='([^']*)'")
+
+# PHP-FPM-81 / php-fpm-8.1 / php81 → ('8', '1')
+_PHP_RE = re.compile(r'php[-_ ]?(?:fpm[-_]?)?(\d)\.?(\d{1,2})', re.IGNORECASE)
+
+# Cap for decompressing a gzipped SQL dump (matches the archive cap).
+_MAX_DUMP_BYTES = 20 * 1024 ** 3
+
+# Directories that may hold the panel's object indexes.
+_INDEX_DIRS = ('hestia', 'vesta')
+
+
+class HestiaImporter(BaseSiteImporter):
+ format = 'hestia'
+
+ # ── layout ──
+ @staticmethod
+ def _root(extracted_dir):
+ """The backup content root: the extraction dir itself, or its single
+ top-level directory (archives usually extract to one wrapper dir)."""
+ markers = ('pam', 'user.conf', 'hestia', 'vesta', 'web', 'db', 'cron')
+ if any(os.path.exists(os.path.join(extracted_dir, m)) for m in markers):
+ return extracted_dir
+ try:
+ entries = [e for e in os.listdir(extracted_dir)
+ if os.path.isdir(os.path.join(extracted_dir, e))]
+ except OSError:
+ return extracted_dir
+ for entry in entries:
+ candidate = os.path.join(extracted_dir, entry)
+ if any(os.path.exists(os.path.join(candidate, m)) for m in markers):
+ return candidate
+ return extracted_dir
+
+ def detect(self, extracted_dir):
+ root = self._root(extracted_dir)
+ identity = (os.path.isdir(os.path.join(root, 'pam'))
+ or os.path.isfile(os.path.join(root, 'user.conf'))
+ or any(os.path.isfile(os.path.join(root, d, 'user.conf'))
+ for d in _INDEX_DIRS))
+ content = (os.path.isdir(os.path.join(root, 'web'))
+ or os.path.isdir(os.path.join(root, 'db'))
+ or os.path.isdir(os.path.join(root, 'mail'))
+ or os.path.isfile(os.path.join(root, 'cron', 'cron.conf')))
+ return identity and content
+
+ # ── analyse ──
+ def analyze(self, extracted_dir):
+ report = self._empty_report(self.format)
+ warnings = report['warnings']
+ root = self._root(extracted_dir)
+ report['source_root'] = os.path.relpath(root, extracted_dir)
+
+ # account username (pam/passwd)
+ username = self._account_username(root)
+ if username:
+ report['account_user'] = username
+ else:
+ warnings.append('No pam/passwd found — account username '
+ 'unavailable.')
+
+ # web domains: stage nested docroot archives under homedir/
+ self._parse_web(root, report)
+ if not report['domains']:
+ warnings.append('No domains found under web/ — the app will be '
+ 'created without a docroot mapping.')
+ homedir = os.path.join(root, 'homedir')
+ report['homedir_present'] = os.path.isdir(homedir)
+ if not report['homedir_present']:
+ warnings.append('No site files could be staged — '
+ 'nothing to copy.')
+
+ # php version (per-domain web.conf, index web.conf, user.conf)
+ report['php_version'] = self._find_php_version(root)
+
+ # databases (db/<name>/) + users (db.conf index)
+ self._parse_databases(root, extracted_dir, report)
+ self._parse_db_users(root, report)
+
+ # cron (panel config format → standard crontab lines)
+ self._parse_crontab(root, report)
+
+ # mail accounts — informational only, never migrated.
+ report['mail_accounts_count'] = self._count_mail_accounts(root)
+ if report['mail_accounts_count']:
+ report['unsupported'].append(
+ f"{report['mail_accounts_count']} mail account(s) found — "
+ 'mailbox migration is not supported; recreate them manually.')
+
+ if os.path.isdir(os.path.join(root, 'dns')):
+ report['unsupported'].append(
+ 'DNS zone data found (dns/) — zones are not imported; '
+ 'manage DNS from the Domains page instead.')
+
+ return report
+
+ @staticmethod
+ def _account_username(root):
+ passwd = os.path.join(root, 'pam', 'passwd')
+ if not os.path.isfile(passwd):
+ return None
+ try:
+ with open(passwd, 'r', encoding='utf-8', errors='replace') as fh:
+ for line in fh:
+ line = line.strip()
+ if line and not line.startswith('#') and ':' in line:
+ return line.split(':', 1)[0]
+ except OSError:
+ pass
+ return None
+
+ # ── web domains + docroot staging ──
+ def _parse_web(self, root, report):
+ web_dir = os.path.join(root, 'web')
+ if not os.path.isdir(web_dir):
+ report['warnings'].append('No web/ directory found — '
+ 'domain list unavailable.')
+ return
+ try:
+ domains = sorted(e for e in os.listdir(web_dir)
+ if os.path.isdir(os.path.join(web_dir, e)))
+ except OSError as exc:
+ report['warnings'].append(f'Could not list web/: {exc}')
+ return
+ username = report.get('account_user') or 'user'
+ for index, domain in enumerate(domains):
+ docroot_rel = self._stage_docroot(root, domain,
+ report['warnings'])
+ report['domains'].append({
+ 'domain': domain,
+ 'docroot': (f'/home/{username}/{docroot_rel}'
+ if docroot_rel else None),
+ # Hestia has no primary-domain marker; first domain leads.
+ 'type': 'primary' if index == 0 else 'secondary',
+ })
+
+ def _stage_docroot(self, root, domain, warnings):
+ """Materialise the domain's docroot under homedir/web/<domain>/ and
+ return its path relative to homedir (or None)."""
+ domain_dir = os.path.join(root, 'web', domain)
+ stage = os.path.join(root, 'homedir', 'web', domain)
+ rel = f'web/{domain}'
+
+ archive = None
+ for name in ('public_html.tar.gz', 'domain_data.tar.gz'):
+ candidate = os.path.join(domain_dir, name)
+ if os.path.isfile(candidate):
+ archive = candidate
+ break
+ if archive:
+ if not os.path.isdir(stage): # idempotent on re-analyse
+ os.makedirs(stage, exist_ok=True)
+ if not self._extract_nested(archive, stage, warnings):
+ shutil.rmtree(stage, ignore_errors=True)
+ return None
+ warnings.append(
+ f'Extracted nested {os.path.basename(archive)} for '
+ f'{domain} into the work directory (extra step).')
+ elif os.path.isdir(os.path.join(domain_dir, 'public_html')):
+ # rare: plain (already unpacked) docroot
+ try:
+ if not os.path.isdir(os.path.join(stage, 'public_html')):
+ shutil.copytree(os.path.join(domain_dir, 'public_html'),
+ os.path.join(stage, 'public_html'))
+ except OSError as exc:
+ warnings.append(f'Could not stage docroot for {domain}: {exc}')
+ return None
+ else:
+ warnings.append(f'No docroot archive found for {domain} '
+ '(public_html.tar.gz / domain_data.tar.gz).')
+ return None
+ if os.path.isdir(os.path.join(stage, 'public_html')):
+ return f'{rel}/public_html'
+ return rel
+
+ @classmethod
+ def _extract_nested(cls, archive_path, dest, warnings):
+ """Safely extract a nested docroot archive; True on success."""
+ try:
+ try:
+ from app.services.site_import_service import SiteImportService
+ SiteImportService._extract_tar(archive_path, dest,
+ warnings=warnings)
+ except ImportError:
+ cls._fallback_extract(archive_path, dest, warnings)
+ return True
+ except Exception as exc:
+ warnings.append('Could not extract nested archive '
+ f'{os.path.basename(archive_path)}: {exc}')
+ return False
+
+ @staticmethod
+ def _fallback_extract(archive_path, dest, warnings):
+ """Traversal-guarded extraction for use outside the app context."""
+ os.makedirs(dest, exist_ok=True)
+ with tarfile.open(archive_path, 'r:*') as tar:
+ members = []
+ for member in tar.getmembers():
+ name = member.name.replace('\\', '/')
+ if name.startswith('/') or os.path.isabs(member.name):
+ raise ValueError(
+ f'archive member has an absolute path: {member.name!r}')
+ if '..' in name.split('/'):
+ raise ValueError(
+ f'archive member escapes the extraction dir: '
+ f'{member.name!r}')
+ if member.issym() or member.islnk():
+ warnings.append(f'Skipped link member {member.name!r}')
+ continue
+ if member.isdev():
+ continue
+ members.append(member)
+ tar.extractall(dest, members=members)
+
+ # ── php ──
+ @classmethod
+ def _find_php_version(cls, root):
+ candidates = []
+ # per-domain web.conf files anywhere under web/<domain>/
+ web_dir = os.path.join(root, 'web')
+ if os.path.isdir(web_dir):
+ for base, _dirs, files in os.walk(web_dir):
+ candidates.extend(os.path.join(base, f) for f in files
+ if f == 'web.conf')
+ # object indexes + user config
+ for index_dir in _INDEX_DIRS:
+ candidates.append(os.path.join(root, index_dir, 'web.conf'))
+ candidates.append(os.path.join(root, index_dir, 'user.conf'))
+ candidates.append(os.path.join(root, 'user.conf'))
+ for path in candidates:
+ if not os.path.isfile(path):
+ continue
+ match = _PHP_RE.search(cls._read_text(path))
+ if match:
+ return f'{match.group(1)}.{match.group(2)}'
+ return None
+
+ # ── databases ──
+ def _parse_databases(self, root, extracted_dir, report):
+ db_dir = os.path.join(root, 'db')
+ if not os.path.isdir(db_dir):
+ report['warnings'].append('No db/ directory found — '
+ 'no databases to import.')
+ return
+ try:
+ names = sorted(e for e in os.listdir(db_dir)
+ if os.path.isdir(os.path.join(db_dir, e)))
+ except OSError as exc:
+ report['warnings'].append(f'Could not list db/: {exc}')
+ return
+ engines = self._db_engines(root)
+ for name in names:
+ dump = self._find_dump(os.path.join(db_dir, name), name,
+ report['warnings'])
+ if not dump:
+ report['warnings'].append(
+ f'No SQL dump found for database {name} — '
+ 'it will be created empty.')
+ try:
+ size = os.path.getsize(dump) if dump else 0
+ except OSError:
+ size = 0
+ report['databases'].append({
+ 'name': name,
+ 'engine': engines.get(name, 'mysql'),
+ 'dump_path': (os.path.relpath(dump, extracted_dir)
+ .replace('\\', '/') if dump else None),
+ 'size': size,
+ })
+
+ def _find_dump(self, db_path, name, warnings):
+ """Locate (and if needed gunzip) the dump inside db/<name>/."""
+ try:
+ entries = sorted(os.listdir(db_path))
+ except OSError:
+ return None
+ preferred = f'{name}.sql'
+ sql = [e for e in entries if e.endswith('.sql')]
+ if preferred in sql:
+ return os.path.join(db_path, preferred)
+ if sql:
+ return os.path.join(db_path, sql[0])
+ gzipped = [e for e in entries if e.endswith('.sql.gz')]
+ if gzipped:
+ src = os.path.join(db_path, gzipped[0])
+ dest = src[:-3]
+ try:
+ self._gunzip(src, dest)
+ warnings.append(
+ f'Decompressed {gzipped[0]} for database {name} '
+ '(extra step in the work directory).')
+ return dest
+ except (OSError, ValueError) as exc:
+ warnings.append(
+ f'Could not decompress dump {gzipped[0]}: {exc}')
+ return None
+
+ @staticmethod
+ def _gunzip(src, dest, max_bytes=_MAX_DUMP_BYTES):
+ total = 0
+ with gzip.open(src, 'rb') as fin, open(dest, 'wb') as fout:
+ while True:
+ chunk = fin.read(1024 * 1024)
+ if not chunk:
+ break
+ total += len(chunk)
+ if total > max_bytes:
+ raise ValueError('decompressed dump exceeds size limit')
+ fout.write(chunk)
+
+ @classmethod
+ def _db_conf_records(cls, root):
+ """Parse the db.conf object index (one KEY='VALUE' line per DB)."""
+ for candidate in ([os.path.join(root, d, 'db.conf')
+ for d in _INDEX_DIRS]
+ + [os.path.join(root, 'db', 'db.conf'),
+ os.path.join(root, 'db.conf')]):
+ if not os.path.isfile(candidate):
+ continue
+ records = []
+ for line in cls._read_text(candidate).splitlines():
+ pairs = dict(_KV_RE.findall(line))
+ if pairs.get('DB'):
+ records.append(pairs)
+ return records
+ return None
+
+ def _db_engines(self, root):
+ records = self._db_conf_records(root) or []
+ return {r['DB']: (r.get('TYPE') or 'mysql').lower() for r in records}
+
+ def _parse_db_users(self, root, report):
+ records = self._db_conf_records(root)
+ if records is None:
+ if report['databases']:
+ report['warnings'].append(
+ 'No db.conf index found — database users cannot be '
+ 'preserved.')
+ return
+ users = {}
+ for record in records:
+ name = record.get('DBUSER')
+ if not name:
+ continue
+ entry = users.setdefault(name, {
+ 'user': name, 'hash': None,
+ 'hash_format': 'unknown', 'grants': [],
+ })
+ digest = record.get('MD5') or record.get('PASSWORD')
+ if digest and _NATIVE_HASH_RE.match(digest):
+ entry['hash'] = digest
+ entry['hash_format'] = 'mysql_native_password'
+ elif digest and not entry['hash']:
+ entry['hash'] = digest
+ report['db_users'] = list(users.values())
+ for entry in report['db_users']:
+ if entry['hash_format'] != 'mysql_native_password':
+ report['warnings'].append(
+ f"Password hash for DB user '{entry['user']}' is not a "
+ 'portable mysql_native_password hash — a new password '
+ 'will be generated on import.')
+
+ # ── cron ──
+ @classmethod
+ def _parse_crontab(cls, root, report):
+ candidates = [os.path.join(root, 'cron', 'cron.conf')]
+ candidates.extend(os.path.join(root, d, 'cron.conf')
+ for d in _INDEX_DIRS)
+ path = next((p for p in candidates if os.path.isfile(p)), None)
+ if not path:
+ return
+ for line in cls._read_text(path).splitlines():
+ line = line.strip()
+ if not line or line.startswith('#'):
+ continue
+ pairs = dict(_KV_RE.findall(line))
+ if not pairs:
+ continue
+ if pairs.get('SUSPENDED', 'no').lower() == 'yes':
+ report['warnings'].append(
+ 'Skipped a suspended cron job: '
+ f"{pairs.get('CMD', '?')!r}")
+ continue
+ fields = [pairs.get(k) for k in
+ ('MIN', 'HOUR', 'DAY', 'MONTH', 'WDAY')]
+ command = pairs.get('CMD')
+ if not command or any(f is None for f in fields):
+ report['warnings'].append(
+ f'Unparseable cron entry skipped: {line!r}')
+ continue
+ converted = ' '.join(fields + [command])
+ if converted not in report['crontab']:
+ report['crontab'].append(converted)
+
+ # ── mail ──
+ @staticmethod
+ def _count_mail_accounts(root):
+ mail_dir = os.path.join(root, 'mail')
+ if not os.path.isdir(mail_dir):
+ return 0
+ count = 0
+ try:
+ for domain in os.listdir(mail_dir):
+ domain_dir = os.path.join(mail_dir, domain)
+ if not os.path.isdir(domain_dir):
+ continue
+ # account list files: mail/<domain>/*.conf (ACCOUNT='..')
+ accounts = 0
+ for entry in os.listdir(domain_dir):
+ if entry.endswith('.conf'):
+ path = os.path.join(domain_dir, entry)
+ with open(path, 'r', encoding='utf-8',
+ errors='replace') as fh:
+ accounts += sum(1 for line in fh
+ if "ACCOUNT='" in line)
+ if not accounts:
+ # fall back to per-mailbox directories
+ boxes = os.path.join(domain_dir, 'accounts')
+ scan = boxes if os.path.isdir(boxes) else domain_dir
+ accounts = sum(
+ 1 for e in os.listdir(scan)
+ if os.path.isdir(os.path.join(scan, e)))
+ count += accounts
+ except OSError:
+ pass
+ return count
diff --git a/backend/migrations/versions/051_site_imports.py b/backend/migrations/versions/051_site_imports.py
new file mode 100644
index 00000000..b461513d
--- /dev/null
+++ b/backend/migrations/versions/051_site_imports.py
@@ -0,0 +1,55 @@
+"""Site imports.
+
+Adds the site_imports table: one row per archive-to-app migration run
+(cPanel first; DirectAdmin/Hestia later via the same importer registry).
+
+Idempotent: MigrationService runs _fix_missing_columns() + db.create_all() on
+boot before Alembic, so guard on the live schema.
+
+Revision ID: 051_site_imports
+Revises: 050_managed_database_users
+Create Date: 2026-07-03
+"""
+from alembic import op
+import sqlalchemy as sa
+
+revision = '051_site_imports'
+down_revision = '050_managed_database_users'
+branch_labels = None
+depends_on = None
+
+TABLE = 'site_imports'
+
+
+def upgrade():
+ conn = op.get_bind()
+ inspector = sa.inspect(conn)
+ if TABLE in set(inspector.get_table_names()):
+ return
+ op.create_table(
+ TABLE,
+ sa.Column('id', sa.Integer(), primary_key=True),
+ sa.Column('source_type', sa.String(30), nullable=False,
+ server_default='cpanel'),
+ sa.Column('status', sa.String(20), nullable=False,
+ server_default='created'),
+ sa.Column('source', sa.Text(), nullable=True),
+ sa.Column('options', sa.Text(), nullable=True),
+ sa.Column('analysis', sa.Text(), nullable=True),
+ sa.Column('result', sa.Text(), nullable=True),
+ sa.Column('log_text', sa.Text(), nullable=True),
+ sa.Column('current_step', sa.String(60), nullable=True),
+ sa.Column('error', sa.Text(), nullable=True),
+ sa.Column('created_by', sa.Integer(), sa.ForeignKey('users.id'),
+ nullable=True),
+ sa.Column('created_at', sa.DateTime(), nullable=True),
+ sa.Column('updated_at', sa.DateTime(), nullable=True),
+ )
+
+
+def downgrade():
+ conn = op.get_bind()
+ inspector = sa.inspect(conn)
+ if TABLE not in set(inspector.get_table_names()):
+ return
+ op.drop_table(TABLE)
diff --git a/backend/tests/test_site_import.py b/backend/tests/test_site_import.py
new file mode 100644
index 00000000..b4d7542c
--- /dev/null
+++ b/backend/tests/test_site_import.py
@@ -0,0 +1,778 @@
+"""Tests for the site import pipeline (cPanel archive importer).
+
+Covers: the analyse report produced from a synthetic cPanel full backup,
+tar path-traversal guards, SSRF guards on fetch-by-URL, upload-path
+traversal, the plan step list, step execution with retry-from-step, MySQL
+password-hash preservation, and the /api/v1/imports HTTP contract.
+"""
+import io
+import json
+import os
+import tarfile
+from types import SimpleNamespace
+
+import pytest
+
+from app.models.site_import import SiteImport
+from app.services.site_import_service import (
+ SiteImportError,
+ SiteImportService,
+)
+from app.services.site_importers import (
+ available_formats,
+ detect_format,
+ get_importer,
+)
+from app.services.site_importers.cpanel import CpanelImporter
+
+NATIVE_HASH = '*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29'
+BACKUP_NAME = 'backup-7.3.2026_12-00-00_alice'
+
+
+# --------------------------------------------------------------------------
+# helpers / fixtures
+# --------------------------------------------------------------------------
+
+def _build_backup_tree(root, homedir_as_tar=False):
+ """Create a synthetic cPanel full-backup content tree under ``root``."""
+ def write(rel, content):
+ path = os.path.join(root, rel)
+ os.makedirs(os.path.dirname(path), exist_ok=True)
+ with open(path, 'w', encoding='utf-8') as fh:
+ fh.write(content)
+
+ write('homedir/public_html/index.php', "<?php echo 'imported'; ?>")
+ write('homedir/public_html/wp-config.php', "<?php // config")
+ write('homedir/etc/example.com/passwd', 'info:x:100\nsales:x:101\n')
+ write('mysql/alice_wp.sql', 'CREATE TABLE wp_posts (id INT);\n')
+ write('mysql.sql',
+ "GRANT USAGE ON *.* TO 'alice_wp'@'localhost' "
+ f"IDENTIFIED BY PASSWORD '{NATIVE_HASH}';\n"
+ "GRANT ALL PRIVILEGES ON `alice_wp`.* TO 'alice_wp'@'localhost';\n")
+ write('cp/alice', 'DNS=example.com\nPLAN=default\n')
+ write('userdata/main', 'main_domain: example.com\n')
+ write('userdata/example.com',
+ 'documentroot: /home/alice/public_html\nphpversion: ea-php81\n')
+ write('cron/alice',
+ 'SHELL=/bin/bash\n'
+ '# a comment\n'
+ '*/5 * * * * /usr/bin/php /home/alice/cron.php\n')
+ write('dnszones/example.com.db', '; zone data\n')
+
+ if homedir_as_tar:
+ homedir = os.path.join(root, 'homedir')
+ with tarfile.open(os.path.join(root, 'homedir.tar'), 'w') as tar:
+ for entry in os.listdir(homedir):
+ tar.add(os.path.join(homedir, entry), arcname=entry)
+ import shutil
+ shutil.rmtree(homedir)
+
+
+def make_backup_archive(tmp_path, homedir_as_tar=False):
+ src = os.path.join(str(tmp_path), 'src', BACKUP_NAME)
+ os.makedirs(src, exist_ok=True)
+ _build_backup_tree(src, homedir_as_tar=homedir_as_tar)
+ archive = os.path.join(str(tmp_path), 'backup.tar.gz')
+ with tarfile.open(archive, 'w:gz') as tar:
+ tar.add(src, arcname=BACKUP_NAME)
+ return archive
+
+
[email protected](autouse=True)
+def imports_base(tmp_path):
+ """Point the import service at an isolated temp base dir."""
+ SiteImportService.imports_base = os.path.join(str(tmp_path), 'imports')
+ os.makedirs(SiteImportService.uploads_dir(), exist_ok=True)
+ yield SiteImportService.imports_base
+ SiteImportService.imports_base = None
+
+
+def _stage_upload(tmp_path, archive=None, token='staged.tar.gz'):
+ """Copy an archive into the uploads dir; returns the upload_path token."""
+ import shutil
+ archive = archive or make_backup_archive(tmp_path)
+ dest = os.path.join(SiteImportService.uploads_dir(), token)
+ shutil.copyfile(archive, dest)
+ return f'uploads/{token}'
+
+
+def _make_import(app, tmp_path, **kwargs):
+ upload_path = kwargs.pop('upload_path', None) or _stage_upload(tmp_path)
+ return SiteImportService.create(
+ kwargs.pop('source_type', 'cpanel'),
+ {'upload_path': upload_path},
+ options=kwargs.pop('options', {}),
+ user_id=kwargs.pop('user_id', None),
+ )
+
+
+class FakeDb:
+ """Records DatabaseService calls; every call succeeds."""
+
+ def __init__(self):
+ self.calls = []
+ self.fail_restore = False
+
+ def install(self, monkeypatch):
+ from app.services.database_service import DatabaseService
+
+ def record(name):
+ def _fn(*args, **kwargs):
+ self.calls.append((name, args, kwargs))
+ if name == 'mysql_restore' and self.fail_restore:
+ return {'success': False, 'error': 'boom'}
+ return {'success': True, 'output': '3'}
+ return _fn
+
+ for name in ('mysql_create_database', 'mysql_restore',
+ 'mysql_execute', 'mysql_create_user',
+ 'mysql_grant_privileges'):
+ monkeypatch.setattr(DatabaseService, name, record(name))
+ monkeypatch.setattr(DatabaseService, 'generate_password',
+ lambda *a, **k: 'newpass123')
+ return self
+
+ def named(self, name):
+ return [c for c in self.calls if c[0] == name]
+
+
+def fake_db(monkeypatch):
+ return FakeDb().install(monkeypatch)
+
+
+def fake_cron(monkeypatch):
+ calls = []
+ from app.services.cron_service import CronService
+
+ def fake_add_job(schedule, command, name=None, description=None):
+ calls.append({'schedule': schedule, 'command': command, 'name': name})
+ return {'success': True, 'job_id': 'job_x'}
+
+ monkeypatch.setattr(CronService, 'add_job', fake_add_job)
+ return calls
+
+
+def apps_dir(tmp_path, monkeypatch):
+ apps = os.path.join(str(tmp_path), 'apps')
+ monkeypatch.setattr('app.paths.APPS_DIR', apps)
+ return apps
+
+
+# --------------------------------------------------------------------------
+# importer registry + detection
+# --------------------------------------------------------------------------
+
+def test_registry_has_cpanel_and_returns_instances():
+ assert 'cpanel' in available_formats()
+ importer = get_importer('cpanel')
+ assert isinstance(importer, CpanelImporter)
+ assert get_importer('nope') is None
+
+
+def test_detect_format_on_extracted_backup(app, tmp_path):
+ extracted = os.path.join(str(tmp_path), 'extracted', BACKUP_NAME)
+ os.makedirs(extracted)
+ _build_backup_tree(extracted)
+ fmt, importer = detect_format(os.path.dirname(extracted))
+ assert fmt == 'cpanel'
+ assert importer.detect(os.path.dirname(extracted)) is True
+ # A foreign layout does not detect.
+ other = os.path.join(str(tmp_path), 'other')
+ os.makedirs(os.path.join(other, 'random-stuff'))
+ assert detect_format(other) == (None, None)
+
+
+# --------------------------------------------------------------------------
+# analyse
+# --------------------------------------------------------------------------
+
+def test_analyze_report_shape_and_contents(app, tmp_path):
+ imp = _make_import(app, tmp_path)
+ report = SiteImportService.analyze(imp)
+
+ assert imp.status == 'analyzed'
+ assert imp.error is None
+
+ # Shape: every contract key present.
+ for key in ('format', 'homedir_present', 'domains', 'databases',
+ 'db_users', 'crontab', 'mail_accounts_count', 'php_version',
+ 'warnings', 'unsupported'):
+ assert key in report, f'missing report key {key}'
+
+ assert report['format'] == 'cpanel'
+ assert report['homedir_present'] is True
+ assert report['php_version'] == '8.1'
+
+ assert report['domains'] == [{
+ 'domain': 'example.com',
+ 'docroot': '/home/alice/public_html',
+ 'type': 'primary',
+ }]
+
+ assert len(report['databases']) == 1
+ database = report['databases'][0]
+ assert database['name'] == 'alice_wp'
+ assert database['engine'] == 'mysql'
+ assert database['dump_path'].endswith('mysql/alice_wp.sql')
+ assert database['size'] > 0
+
+ assert len(report['db_users']) == 1
+ db_user = report['db_users'][0]
+ assert db_user['user'] == 'alice_wp'
+ assert db_user['hash'] == NATIVE_HASH
+ assert db_user['hash_format'] == 'mysql_native_password'
+ assert any('ALL PRIVILEGES' in g for g in db_user['grants'])
+ # Password material never leaks into the grants list.
+ assert all(NATIVE_HASH not in g for g in db_user['grants'])
+
+ # Env lines and comments filtered out of the crontab.
+ assert report['crontab'] == ['*/5 * * * * /usr/bin/php /home/alice/cron.php']
+
+ assert report['mail_accounts_count'] == 2
+ unsupported = ' '.join(report['unsupported'])
+ assert 'mail account' in unsupported
+ assert 'DNS zone' in unsupported
+
+ # Analysis persisted on the row + logged.
+ assert imp.get_analysis()['format'] == 'cpanel'
+ assert 'Analysis complete' in imp.log_text
+
+
+def test_analyze_handles_nested_homedir_tar(app, tmp_path):
+ archive = make_backup_archive(tmp_path, homedir_as_tar=True)
+ token = _stage_upload(tmp_path, archive=archive, token='nested.tar.gz')
+ imp = _make_import(app, tmp_path, upload_path=token)
+ report = SiteImportService.analyze(imp)
+ assert report['homedir_present'] is True
+ assert os.path.isfile(os.path.join(
+ SiteImportService.extracted_dir(imp), BACKUP_NAME,
+ 'homedir', 'public_html', 'index.php'))
+
+
+def test_analyze_partial_backup_warns_not_crashes(app, tmp_path):
+ """A backup missing mysql/, cron/ and userdata/ still analyses."""
+ src = os.path.join(str(tmp_path), 'partial', BACKUP_NAME)
+ os.makedirs(os.path.join(src, 'cp'))
+ with open(os.path.join(src, 'cp', 'alice'), 'w') as fh:
+ fh.write('DNS=example.com\n')
+ archive = os.path.join(str(tmp_path), 'partial.tar.gz')
+ with tarfile.open(archive, 'w:gz') as tar:
+ tar.add(src, arcname=BACKUP_NAME)
+ token = _stage_upload(tmp_path, archive=archive, token='partial.tar.gz')
+ imp = _make_import(app, tmp_path, upload_path=token)
+ report = SiteImportService.analyze(imp)
+ assert imp.status == 'analyzed'
+ assert report['homedir_present'] is False
+ assert report['databases'] == []
+ assert report['warnings'] # missing pieces surfaced as warnings
+
+
+def test_auto_source_type_detects_cpanel(app, tmp_path):
+ imp = _make_import(app, tmp_path, source_type='auto')
+ SiteImportService.analyze(imp)
+ assert imp.source_type == 'cpanel'
+ assert imp.status == 'analyzed'
+
+
+# --------------------------------------------------------------------------
+# extraction safety
+# --------------------------------------------------------------------------
+
+def _evil_archive(tmp_path, member_name):
+ archive = os.path.join(str(tmp_path), 'evil.tar.gz')
+ payload = b'owned'
+ with tarfile.open(archive, 'w:gz') as tar:
+ info = tarfile.TarInfo(member_name)
+ info.size = len(payload)
+ tar.addfile(info, io.BytesIO(payload))
+ return archive
+
+
+def test_tar_traversal_member_rejected(app, tmp_path):
+ archive = _evil_archive(tmp_path, '../../evil.txt')
+ token = _stage_upload(tmp_path, archive=archive, token='evil.tar.gz')
+ imp = _make_import(app, tmp_path, upload_path=token)
+ with pytest.raises(SiteImportError, match='escapes'):
+ SiteImportService.analyze(imp)
+ assert imp.status == 'failed'
+ assert 'escapes' in imp.error
+ assert not os.path.exists(os.path.join(str(tmp_path), 'evil.txt'))
+
+
+def test_tar_absolute_member_rejected(tmp_path):
+ archive = _evil_archive(tmp_path, '/etc/evil.txt')
+ with pytest.raises(SiteImportError, match='absolute'):
+ SiteImportService._extract_tar(archive,
+ os.path.join(str(tmp_path), 'out'))
+
+
+def test_tar_symlink_member_skipped_with_warning(tmp_path):
+ archive = os.path.join(str(tmp_path), 'link.tar.gz')
+ with tarfile.open(archive, 'w:gz') as tar:
+ info = tarfile.TarInfo('innocent.txt')
+ info.size = 2
+ tar.addfile(info, io.BytesIO(b'ok'))
+ link = tarfile.TarInfo('escape')
+ link.type = tarfile.SYMTYPE
+ link.linkname = '/etc/passwd'
+ tar.addfile(link)
+ warnings = []
+ dest = os.path.join(str(tmp_path), 'out')
+ SiteImportService._extract_tar(archive, dest, warnings=warnings)
+ assert os.path.isfile(os.path.join(dest, 'innocent.txt'))
+ assert not os.path.lexists(os.path.join(dest, 'escape'))
+ assert any('escape' in w for w in warnings)
+
+
+# --------------------------------------------------------------------------
+# SSRF + upload-path guards
+# --------------------------------------------------------------------------
+
+ 'http://169.254.169.254/latest/meta-data/', # cloud metadata
+ 'http://10.0.0.5/backup.tar.gz', # private
+ 'http://192.168.1.10/backup.tar.gz', # private
+ 'http://127.0.0.1/backup.tar.gz', # loopback
+ 'http://0.0.0.0/backup.tar.gz', # unspecified
+ 'file:///etc/passwd', # bad scheme
+ 'ftp://example.com/backup.tar.gz', # bad scheme
+ 'http:///backup.tar.gz', # no host
+])
+def test_ssrf_guard_rejects(url):
+ with pytest.raises(SiteImportError):
+ SiteImportService._validate_url(url)
+
+
+def test_ssrf_guard_allows_public_address():
+ # Literal public IP: resolvable without network access.
+ assert SiteImportService._validate_url('https://8.8.8.8/backup.tar.gz')
+
+
+def test_create_rejects_ssrf_url(app):
+ with pytest.raises(SiteImportError):
+ SiteImportService.create('cpanel',
+ {'url': 'http://169.254.169.254/x.tar.gz'})
+
+
+ '../outside.tar.gz',
+ 'uploads/../../outside.tar.gz',
+ '',
+])
+def test_upload_path_traversal_rejected(token):
+ with pytest.raises(SiteImportError):
+ SiteImportService._resolve_upload_path(token)
+
+
+def test_upload_path_absolute_rejected(tmp_path):
+ outside = os.path.join(str(tmp_path), 'outside.tar.gz')
+ with open(outside, 'wb') as fh:
+ fh.write(b'x')
+ with pytest.raises(SiteImportError):
+ SiteImportService._resolve_upload_path(outside)
+
+
+def test_fetch_archive_rejects_traversal_source(app, tmp_path):
+ imp = SiteImport(source_type='cpanel', status='created')
+ imp.set_source({'upload_path': '../../etc/passwd'})
+ from app import db
+ db.session.add(imp)
+ db.session.commit()
+ with pytest.raises(SiteImportError):
+ SiteImportService.fetch_archive(imp)
+
+
+# --------------------------------------------------------------------------
+# plan
+# --------------------------------------------------------------------------
+
+def test_plan_full_step_list(app, tmp_path):
+ imp = _make_import(app, tmp_path)
+ analysis = SiteImportService.analyze(imp)
+ steps = get_importer('cpanel').plan(analysis, {})
+ assert [s['key'] for s in steps] == [
+ 'create_app', 'copy_files', 'create_databases', 'create_db_users',
+ 'install_crontab', 'fix_permissions', 'validate',
+ ]
+ assert all(s.get('title') for s in steps)
+
+
+def test_plan_skips_stepless_inputs():
+ importer = get_importer('cpanel')
+ empty = CpanelImporter._empty_report('cpanel')
+ keys = [s['key'] for s in importer.plan(empty, {})]
+ assert 'create_databases' not in keys
+ assert 'create_db_users' not in keys
+ assert 'install_crontab' not in keys
+ assert keys[0] == 'create_app' and keys[-1] == 'validate'
+
+
+# --------------------------------------------------------------------------
+# run
+# --------------------------------------------------------------------------
+
+def _admin_user(app):
+ from app import db
+ from app.models import User
+ from werkzeug.security import generate_password_hash
+ user = User(email='[email protected]', username='impadmin',
+ password_hash=generate_password_hash('x'),
+ role=User.ROLE_ADMIN, is_active=True)
+ db.session.add(user)
+ db.session.commit()
+ return user
+
+
+def test_run_executes_all_steps(app, tmp_path, fake_db, fake_cron, apps_dir):
+ from app.models import Application
+ user = _admin_user(app)
+ imp = _make_import(app, tmp_path, user_id=user.id)
+ SiteImportService.analyze(imp)
+ SiteImportService.run(imp)
+
+ assert imp.status == 'completed'
+ assert imp.current_step is None
+ assert imp.error is None
+
+ result = imp.get_result()
+ application = Application.query.get(result['app_id'])
+ assert application is not None
+ assert application.app_type == 'php'
+ assert application.php_version == '8.1'
+ assert application.name == 'example-com'
+ assert os.path.isfile(os.path.join(application.root_path, 'index.php'))
+
+ # Databases created + dump imported + tracked.
+ assert [c[1][0] for c in fake_db.named('mysql_create_database')] == ['alice_wp']
+ assert fake_db.named('mysql_restore')[0][1][0] == 'alice_wp'
+ from app.models.managed_database import ManagedDatabase
+ managed = ManagedDatabase.query.filter_by(name='alice_wp').first()
+ assert managed is not None and managed.owner_application_id == application.id
+
+ # Hash-preserving CREATE USER went through mysql_execute.
+ executed = ' ;; '.join(c[1][0] for c in fake_db.named('mysql_execute'))
+ assert ("IDENTIFIED WITH mysql_native_password AS "
+ f"'{NATIVE_HASH}'") in executed
+ assert 'alice_wp' in executed
+ # Grants re-applied on the imported database.
+ assert fake_db.named('mysql_grant_privileges')[0][1][:2] == ('alice_wp', 'alice_wp')
+
+ # Crontab installed through the cron service.
+ assert fake_cron == [{'schedule': '*/5 * * * *',
+ 'command': '/usr/bin/php /home/alice/cron.php',
+ 'name': f'import-{imp.id}'}]
+ assert result['cron_installed'] == 1
+ assert result['validated'] is True
+ assert result['db_users'] == [{'user': 'alice_wp', 'preserved_hash': True}]
+
+ # Per-step log lines.
+ for key in ('create_app', 'copy_files', 'create_databases',
+ 'create_db_users', 'install_crontab', 'fix_permissions',
+ 'validate'):
+ assert f"Step '{key}'" in imp.log_text
+ assert f"Step '{key}' done." in imp.log_text
+ assert 'Import completed.' in imp.log_text
+
+
+def test_run_failure_marks_failed_and_retry_skips_earlier_steps(
+ app, tmp_path, fake_db, fake_cron, apps_dir):
+ from app.models import Application
+ user = _admin_user(app)
+ imp = _make_import(app, tmp_path, user_id=user.id)
+ SiteImportService.analyze(imp)
+
+ fake_db.fail_restore = True
+ SiteImportService.run(imp)
+ assert imp.status == 'failed'
+ assert imp.current_step == 'create_databases'
+ assert "Step 'create_databases' failed" in imp.error
+ assert "retry with from_step='create_databases'" in imp.log_text
+ # Earlier steps completed: app row exists, files copied.
+ assert Application.query.count() == 1
+ app_id = imp.get_result()['app_id']
+
+ # Retry from the failed step: earlier steps are skipped, run completes.
+ fake_db.fail_restore = False
+ SiteImportService.run(imp, from_step='create_databases')
+ assert imp.status == 'completed'
+ assert "Skipping step 'create_app' (already completed)." in imp.log_text
+ assert "Skipping step 'copy_files' (already completed)." in imp.log_text
+ assert Application.query.count() == 1 # no duplicate app
+ assert imp.get_result()['app_id'] == app_id
+
+
+def test_run_honors_skip_db_and_skip_crontab(app, tmp_path, fake_db,
+ fake_cron, apps_dir):
+ imp = _make_import(app, tmp_path, user_id=_admin_user(app).id,
+ options={'skip_db': True, 'skip_crontab': True})
+ SiteImportService.analyze(imp)
+ SiteImportService.run(imp)
+
+ assert imp.status == 'completed'
+ # DB + cron paths never touched.
+ assert fake_db.named('mysql_create_database') == []
+ assert fake_db.named('mysql_create_user') == []
+ assert fake_cron == []
+ assert "Skipping step 'create_databases' (skip_db option)." in imp.log_text
+ assert "Skipping step 'create_db_users' (skip_db option)." in imp.log_text
+ assert ("Skipping step 'install_crontab' (skip_crontab option)."
+ in imp.log_text)
+ # The other steps still ran.
+ assert "Step 'copy_files' done." in imp.log_text
+ assert 'databases' not in imp.get_result() or \
+ imp.get_result().get('databases') in (None, [])
+
+
+def test_run_requires_analysis(app, tmp_path):
+ imp = _make_import(app, tmp_path)
+ with pytest.raises(SiteImportError, match='analysed'):
+ SiteImportService.run(imp)
+
+
+def test_run_rejects_unknown_from_step(app, tmp_path, fake_db, fake_cron,
+ apps_dir):
+ imp = _make_import(app, tmp_path, user_id=_admin_user(app).id)
+ SiteImportService.analyze(imp)
+ with pytest.raises(SiteImportError, match='Unknown step'):
+ SiteImportService.run(imp, from_step='does_not_exist')
+
+
+def test_preserve_user_sql_shape():
+ sql = SiteImportService._preserve_user_sql('alice_wp', NATIVE_HASH)
+ assert sql == ("CREATE USER IF NOT EXISTS 'alice_wp'@'localhost' "
+ "IDENTIFIED WITH mysql_native_password AS "
+ f"'{NATIVE_HASH}'")
+
+
+def test_non_native_hash_falls_back_to_new_password(app, tmp_path, fake_db,
+ fake_cron, apps_dir):
+ imp = _make_import(app, tmp_path, user_id=_admin_user(app).id)
+ SiteImportService.analyze(imp)
+ analysis = imp.get_analysis()
+ analysis['db_users'] = [{'user': 'alice_wp', 'hash': '$A$005$opaque',
+ 'hash_format': 'caching_sha2_password',
+ 'grants': []}]
+ imp.set_analysis(analysis)
+ from app import db
+ db.session.commit()
+
+ SiteImportService.run(imp)
+ assert imp.status == 'completed'
+ assert fake_db.named('mysql_create_user')[0][1][0] == 'alice_wp'
+ warnings = ' '.join(imp.get_result()['warnings'])
+ assert 'new password was generated' in warnings
+ assert imp.get_result()['db_users'] == [
+ {'user': 'alice_wp', 'preserved_hash': False}]
+
+
+# --------------------------------------------------------------------------
+# jobs
+# --------------------------------------------------------------------------
+
+def test_register_jobs_registers_both_kinds(app):
+ from app.jobs import registry
+ SiteImportService.register_jobs()
+ assert registry.is_registered('import.analyze')
+ assert registry.is_registered('import.run')
+
+
+def test_job_handlers_resolve_import_row(app, tmp_path, fake_db, fake_cron,
+ apps_dir):
+ imp = _make_import(app, tmp_path, user_id=_admin_user(app).id)
+ job = SimpleNamespace(get_payload=lambda: {'import_id': imp.id})
+ result = SiteImportService._job_analyze(job)
+ assert result == {'import_id': imp.id, 'status': 'analyzed'}
+ result = SiteImportService._job_run(job)
+ assert result == {'import_id': imp.id, 'status': 'completed'}
+ missing = SimpleNamespace(get_payload=lambda: {'import_id': 999999})
+ with pytest.raises(ValueError):
+ SiteImportService._job_analyze(missing)
+
+
+# --------------------------------------------------------------------------
+# API
+# --------------------------------------------------------------------------
+
+def api_app(app):
+ from app.api.site_imports import site_imports_bp
+ if 'site_imports' not in app.blueprints:
+ app.register_blueprint(site_imports_bp, url_prefix='/api/v1/imports')
+ return app
+
+
+def api_client(api_app):
+ return api_app.test_client()
+
+
+def viewer_headers(api_app):
+ from app import db
+ from app.models import User
+ from flask_jwt_extended import create_access_token
+ from werkzeug.security import generate_password_hash
+ user = User(email='[email protected]', username='viewer',
+ password_hash=generate_password_hash('x'),
+ role='viewer', is_active=True)
+ db.session.add(user)
+ db.session.commit()
+ return {'Authorization': f'Bearer {create_access_token(identity=user.id)}'}
+
+
+def stub_enqueue(monkeypatch):
+ calls = []
+
+ def fake_enqueue(cls, kind, payload=None, **kwargs):
+ calls.append({'kind': kind, 'payload': payload})
+ return SimpleNamespace(id=4242)
+
+ from app.jobs.service import JobService
+ monkeypatch.setattr(JobService, 'enqueue', classmethod(fake_enqueue))
+ return calls
+
+
+def test_api_requires_auth(api_client):
+ assert api_client.get('/api/v1/imports').status_code == 401
+ assert api_client.post('/api/v1/imports', json={}).status_code == 401
+ assert api_client.post('/api/v1/imports/upload').status_code == 401
+
+
+def test_api_requires_admin(api_client, viewer_headers):
+ resp = api_client.get('/api/v1/imports', headers=viewer_headers)
+ assert resp.status_code == 403
+ assert 'error' in resp.get_json()
+ resp = api_client.post('/api/v1/imports', headers=viewer_headers, json={})
+ assert resp.status_code == 403
+
+
+def test_api_upload_create_get_list_delete(api_client, auth_headers,
+ tmp_path, stub_enqueue):
+ # Upload
+ archive = make_backup_archive(tmp_path)
+ with open(archive, 'rb') as fh:
+ resp = api_client.post(
+ '/api/v1/imports/upload', headers=auth_headers,
+ data={'file': (io.BytesIO(fh.read()), 'backup.tar.gz')},
+ content_type='multipart/form-data')
+ assert resp.status_code == 201
+ upload_path = resp.get_json()['upload_path']
+ assert upload_path.startswith('uploads/')
+ assert not os.path.isabs(upload_path)
+
+ # Create
+ resp = api_client.post('/api/v1/imports', headers=auth_headers, json={
+ 'source_type': 'cpanel',
+ 'source': {'upload_path': upload_path},
+ 'options': {'app_name': 'my-imported-site'},
+ })
+ assert resp.status_code == 201
+ body = resp.get_json()['import']
+ assert body['status'] == 'created'
+ assert body['source_type'] == 'cpanel'
+ assert body['options'] == {'app_name': 'my-imported-site'}
+ import_id = body['id']
+
+ # List (newest first)
+ resp = api_client.get('/api/v1/imports', headers=auth_headers)
+ assert resp.status_code == 200
+ imports = resp.get_json()['imports']
+ assert imports[0]['id'] == import_id
+
+ # Detail
+ resp = api_client.get(f'/api/v1/imports/{import_id}', headers=auth_headers)
+ assert resp.status_code == 200
+ assert resp.get_json()['import']['id'] == import_id
+ assert 'log_text' in resp.get_json()['import']
+
+ # Analyze → 202 with job id
+ resp = api_client.post(f'/api/v1/imports/{import_id}/analyze',
+ headers=auth_headers)
+ assert resp.status_code == 202
+ assert resp.get_json() == {'job_id': 4242}
+ assert stub_enqueue[-1]['kind'] == 'import.analyze'
+ assert stub_enqueue[-1]['payload'] == {'import_id': import_id}
+
+ # Run refused before analysis
+ resp = api_client.post(f'/api/v1/imports/{import_id}/run',
+ headers=auth_headers, json={})
+ assert resp.status_code == 409
+ assert 'error' in resp.get_json()
+
+ # Mark analyzed → run accepted, from_step forwarded
+ imp = SiteImport.query.get(import_id)
+ imp.status = 'analyzed'
+ from app import db
+ db.session.commit()
+ resp = api_client.post(f'/api/v1/imports/{import_id}/run',
+ headers=auth_headers,
+ json={'from_step': 'create_databases'})
+ assert resp.status_code == 202
+ assert resp.get_json() == {'job_id': 4242}
+ assert stub_enqueue[-1]['kind'] == 'import.run'
+ assert stub_enqueue[-1]['payload'] == {'import_id': import_id,
+ 'from_step': 'create_databases'}
+
+ # Body options merge into the stored options (body wins).
+ resp = api_client.post(f'/api/v1/imports/{import_id}/run',
+ headers=auth_headers,
+ json={'options': {'skip_db': True,
+ 'app_name': 'renamed-site'}})
+ assert resp.status_code == 202
+ imp = SiteImport.query.get(import_id)
+ assert imp.get_options() == {'app_name': 'renamed-site', 'skip_db': True}
+
+ # Delete cleans up
+ resp = api_client.delete(f'/api/v1/imports/{import_id}',
+ headers=auth_headers)
+ assert resp.status_code == 200
+ assert SiteImport.query.get(import_id) is None
+ resp = api_client.get(f'/api/v1/imports/{import_id}', headers=auth_headers)
+ assert resp.status_code == 404
+
+
+def test_api_create_validation_errors(api_client, auth_headers):
+ resp = api_client.post('/api/v1/imports', headers=auth_headers, json={
+ 'source_type': 'cpanel', 'source': {},
+ })
+ assert resp.status_code == 400
+ assert 'error' in resp.get_json()
+
+ resp = api_client.post('/api/v1/imports', headers=auth_headers, json={
+ 'source_type': 'unknown-panel', 'source': {'upload_path': 'uploads/x'},
+ })
+ assert resp.status_code == 400
+
+ resp = api_client.post('/api/v1/imports', headers=auth_headers, json={
+ 'source_type': 'cpanel',
+ 'source': {'url': 'http://169.254.169.254/backup.tar.gz'},
+ })
+ assert resp.status_code == 400
+
+
+def test_api_upload_requires_file(api_client, auth_headers):
+ resp = api_client.post('/api/v1/imports/upload', headers=auth_headers,
+ data={}, content_type='multipart/form-data')
+ assert resp.status_code == 400
+ assert resp.get_json() == {'error': 'No file provided'}
+
+
+def test_model_to_dict_and_log_tail(app):
+ imp = SiteImport(source_type='cpanel', status='created')
+ imp.set_source({'upload_path': 'uploads/x.tar.gz'})
+ for i in range(600):
+ imp.append_log(f'line {i}')
+ from app import db
+ db.session.add(imp)
+ db.session.commit()
+ data = imp.to_dict(log_lines=500)
+ lines = data['log_text'].splitlines()
+ assert len(lines) == 500
+ assert 'line 599' in lines[-1]
+ assert data['source'] == {'upload_path': 'uploads/x.tar.gz'}
+ assert data['status'] == 'created'
+ assert json.dumps(data) # JSON-serializable throughout
diff --git a/backend/tests/test_site_importers_extra.py b/backend/tests/test_site_importers_extra.py
new file mode 100644
index 00000000..80ce2093
--- /dev/null
+++ b/backend/tests/test_site_importers_extra.py
@@ -0,0 +1,540 @@
+"""Tests for the DirectAdmin and Hestia archive importers.
+
+Covers: detect() truth matrix across all three built-in formats, the
+analyse reports built from synthetic DirectAdmin/Hestia backups (domains,
+databases, users + hashes, crontab incl. the Hestia config-format
+conversion, php version, warnings on missing sections), format
+auto-detection, nested-archive docroot staging, and an end-to-end
+SiteImportService.run() for each format with exec paths monkeypatched.
+"""
+import os
+import shutil
+import tarfile
+
+import pytest
+
+from app.services.site_import_service import SiteImportService
+from app.services.site_importers import (
+ available_formats,
+ detect_format,
+ get_importer,
+)
+from app.services.site_importers.directadmin import DirectadminImporter
+from app.services.site_importers.hestia import HestiaImporter
+
+NATIVE_HASH = '*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29'
+DA_WRAPPER = 'user.admin.alice'
+HESTIA_WRAPPER = 'alice.2026-07-01'
+
+
+# --------------------------------------------------------------------------
+# synthetic backup builders
+# --------------------------------------------------------------------------
+
+def _write(root, rel, content):
+ path = os.path.join(root, rel)
+ os.makedirs(os.path.dirname(path), exist_ok=True)
+ with open(path, 'w', encoding='utf-8') as fh:
+ fh.write(content)
+
+
+def _build_da_tree(root):
+ """Synthetic DirectAdmin account-backup content tree."""
+ _write(root, 'backup/user.conf',
+ 'username=alice\ndomain=example.com\nphp1_release=8.1\n')
+ _write(root, 'backup/example.com/domain.conf', 'domain=example.com\n')
+ _write(root, 'backup/example.com/email/passwd',
+ 'info:$1$x:100\nsales:$1$y:101\n')
+ _write(root, 'backup/blog.example.net/domain.conf',
+ 'domain=blog.example.net\n')
+ _write(root, 'backup/alice_wp.sql', 'CREATE TABLE wp_posts (id INT);\n')
+ _write(root, 'backup/mysql.conf', f'user=alice\npasswd={NATIVE_HASH}\n')
+ _write(root, 'backup/alice_wp.conf',
+ f'user0=alice_wp&passwd0={NATIVE_HASH}&accesshost0=localhost')
+ _write(root, 'backup/crontab.conf',
+ '1=*/5 * * * * /usr/bin/php /home/alice/cron.php\n')
+ _write(root, 'domains/example.com/public_html/index.php',
+ "<?php echo 'imported'; ?>")
+ _write(root, 'domains/blog.example.net/public_html/index.html',
+ '<html>blog</html>')
+ os.makedirs(os.path.join(root, 'imap', 'example.com', 'info'),
+ exist_ok=True)
+
+
+def _nested_targz(dest, files, inner_prefix=''):
+ """Create a nested .tar.gz whose members are ``files`` (rel → content),
+ optionally nested under ``inner_prefix``."""
+ import tempfile
+ stage = tempfile.mkdtemp()
+ try:
+ for rel, content in files.items():
+ _write(stage, os.path.join(inner_prefix, rel), content)
+ os.makedirs(os.path.dirname(dest), exist_ok=True)
+ with tarfile.open(dest, 'w:gz') as tar:
+ for entry in sorted(os.listdir(stage)):
+ tar.add(os.path.join(stage, entry), arcname=entry)
+ finally:
+ shutil.rmtree(stage, ignore_errors=True)
+
+
+def _build_hestia_tree(root):
+ """Synthetic Hestia user-backup content tree (nested docroot tars)."""
+ _write(root, 'pam/passwd', 'alice:x:1001:1001::/home/alice:/bin/bash\n')
+ _write(root, 'user.conf', "NAME='alice' PACKAGE='default'\n")
+ # domain_data.tar.gz wraps a public_html/ dir; public_html.tar.gz
+ # ships the files directly.
+ _nested_targz(os.path.join(root, 'web', 'example.com',
+ 'domain_data.tar.gz'),
+ {'index.php': "<?php echo 'imported'; ?>"},
+ inner_prefix='public_html')
+ _nested_targz(os.path.join(root, 'web', 'static.example.com',
+ 'public_html.tar.gz'),
+ {'index.html': '<html>static</html>'})
+ _write(root, 'web/example.com/hestia/web.conf',
+ "DOMAIN='example.com' BACKEND_TEMPLATE='PHP-FPM-81'\n")
+ _write(root, 'db/alice_db/alice_db.sql',
+ 'CREATE TABLE posts (id INT);\n')
+ _write(root, 'hestia/db.conf',
+ f"DB='alice_db' DBUSER='alice_db' MD5='{NATIVE_HASH}' "
+ "HOST='localhost' TYPE='mysql'\n")
+ _write(root, 'cron/cron.conf',
+ "JOB='1' MIN='*/10' HOUR='*' DAY='*' MONTH='*' WDAY='*' "
+ "CMD='php /home/alice/cron.php' SUSPENDED='no'\n"
+ "JOB='2' MIN='0' HOUR='3' DAY='*' MONTH='*' WDAY='*' "
+ "CMD='echo paused' SUSPENDED='yes'\n")
+ _write(root, 'mail/example.com/example.com.conf',
+ "ACCOUNT='info' MD5='$1$x'\nACCOUNT='sales' MD5='$1$y'\n")
+ _write(root, 'dns/example.com.conf', "RECORD='@ A 1.2.3.4'\n")
+
+
+def _build_cpanel_tree(root):
+ """Minimal cPanel layout (enough for its detect())."""
+ _write(root, 'cp/alice', 'DNS=example.com\n')
+ _write(root, 'userdata/main', 'main_domain: example.com\n')
+ _write(root, 'homedir/public_html/index.php', '<?php ?>')
+
+
+def _make_archive(tmp_path, builder, wrapper, name):
+ src = os.path.join(str(tmp_path), 'src', wrapper)
+ os.makedirs(src, exist_ok=True)
+ builder(src)
+ archive = os.path.join(str(tmp_path), name)
+ with tarfile.open(archive, 'w:gz') as tar:
+ tar.add(src, arcname=wrapper)
+ return archive
+
+
+def _extracted_tree(tmp_path, builder, wrapper, label):
+ """Build a tree under <tmp>/<label>/<wrapper>/ and return the parent
+ (mirrors what SiteImportService's extraction produces)."""
+ extracted = os.path.join(str(tmp_path), label, wrapper)
+ os.makedirs(extracted, exist_ok=True)
+ builder(extracted)
+ return os.path.dirname(extracted)
+
+
+# --------------------------------------------------------------------------
+# shared fixtures (mirroring test_site_import.py)
+# --------------------------------------------------------------------------
+
[email protected](autouse=True)
+def imports_base(tmp_path):
+ SiteImportService.imports_base = os.path.join(str(tmp_path), 'imports')
+ os.makedirs(SiteImportService.uploads_dir(), exist_ok=True)
+ yield SiteImportService.imports_base
+ SiteImportService.imports_base = None
+
+
+def _stage_upload(archive, token):
+ dest = os.path.join(SiteImportService.uploads_dir(), token)
+ shutil.copyfile(archive, dest)
+ return f'uploads/{token}'
+
+
+def _make_import(tmp_path, source_type, builder, wrapper, user_id=None):
+ archive = _make_archive(tmp_path, builder, wrapper, f'{wrapper}.tar.gz')
+ upload_path = _stage_upload(archive, f'{wrapper}.tar.gz')
+ return SiteImportService.create(source_type, {'upload_path': upload_path},
+ options={}, user_id=user_id)
+
+
+class FakeDb:
+ """Records DatabaseService calls; every call succeeds."""
+
+ def __init__(self):
+ self.calls = []
+
+ def install(self, monkeypatch):
+ from app.services.database_service import DatabaseService
+
+ def record(name):
+ def _fn(*args, **kwargs):
+ self.calls.append((name, args, kwargs))
+ return {'success': True, 'output': '3'}
+ return _fn
+
+ for name in ('mysql_create_database', 'mysql_restore',
+ 'mysql_execute', 'mysql_create_user',
+ 'mysql_grant_privileges'):
+ monkeypatch.setattr(DatabaseService, name, record(name))
+ monkeypatch.setattr(DatabaseService, 'generate_password',
+ lambda *a, **k: 'newpass123')
+ return self
+
+ def named(self, name):
+ return [c for c in self.calls if c[0] == name]
+
+
+def fake_db(monkeypatch):
+ return FakeDb().install(monkeypatch)
+
+
+def fake_cron(monkeypatch):
+ calls = []
+ from app.services.cron_service import CronService
+
+ def fake_add_job(schedule, command, name=None, description=None):
+ calls.append({'schedule': schedule, 'command': command, 'name': name})
+ return {'success': True, 'job_id': 'job_x'}
+
+ monkeypatch.setattr(CronService, 'add_job', fake_add_job)
+ return calls
+
+
+def apps_dir(tmp_path, monkeypatch):
+ apps = os.path.join(str(tmp_path), 'apps')
+ monkeypatch.setattr('app.paths.APPS_DIR', apps)
+ return apps
+
+
+def _admin_user(app):
+ from app import db
+ from app.models import User
+ from werkzeug.security import generate_password_hash
+ user = User(email='[email protected]', username='imp2admin',
+ password_hash=generate_password_hash('x'),
+ role=User.ROLE_ADMIN, is_active=True)
+ db.session.add(user)
+ db.session.commit()
+ return user
+
+
+# --------------------------------------------------------------------------
+# registry + detection matrix
+# --------------------------------------------------------------------------
+
+def test_registry_has_new_formats():
+ formats = available_formats()
+ assert 'directadmin' in formats
+ assert 'hestia' in formats
+ assert isinstance(get_importer('directadmin'), DirectadminImporter)
+ assert isinstance(get_importer('hestia'), HestiaImporter)
+
+
+def test_detect_matrix_across_formats(tmp_path):
+ da = _extracted_tree(tmp_path, _build_da_tree, DA_WRAPPER, 'da')
+ hestia = _extracted_tree(tmp_path, _build_hestia_tree, HESTIA_WRAPPER,
+ 'hestia')
+ cpanel = _extracted_tree(tmp_path, _build_cpanel_tree,
+ 'backup-7.3.2026_12-00-00_alice', 'cpanel')
+ empty = os.path.join(str(tmp_path), 'empty')
+ os.makedirs(os.path.join(empty, 'random-stuff'))
+
+ da_imp = get_importer('directadmin')
+ hestia_imp = get_importer('hestia')
+ cpanel_imp = get_importer('cpanel')
+
+ assert da_imp.detect(da) is True
+ assert da_imp.detect(hestia) is False
+ assert da_imp.detect(cpanel) is False
+ assert da_imp.detect(empty) is False
+
+ assert hestia_imp.detect(hestia) is True
+ assert hestia_imp.detect(da) is False
+ assert hestia_imp.detect(cpanel) is False
+ assert hestia_imp.detect(empty) is False
+
+ assert cpanel_imp.detect(da) is False
+ assert cpanel_imp.detect(hestia) is False
+
+ assert detect_format(da)[0] == 'directadmin'
+ assert detect_format(hestia)[0] == 'hestia'
+ assert detect_format(cpanel)[0] == 'cpanel'
+ assert detect_format(empty) == (None, None)
+
+
+# --------------------------------------------------------------------------
+# DirectAdmin analyse
+# --------------------------------------------------------------------------
+
+def test_directadmin_analyze_report(tmp_path):
+ extracted = _extracted_tree(tmp_path, _build_da_tree, DA_WRAPPER, 'da')
+ report = get_importer('directadmin').analyze(extracted)
+
+ for key in ('format', 'homedir_present', 'domains', 'databases',
+ 'db_users', 'crontab', 'mail_accounts_count', 'php_version',
+ 'warnings', 'unsupported'):
+ assert key in report, f'missing report key {key}'
+
+ assert report['format'] == 'directadmin'
+ assert report['source_root'] == DA_WRAPPER
+ assert report['account_user'] == 'alice'
+ assert report['php_version'] == '8.1'
+
+ # Primary domain (from user.conf) sorts first.
+ assert [d['domain'] for d in report['domains']] == [
+ 'example.com', 'blog.example.net']
+ assert report['domains'][0] == {
+ 'domain': 'example.com',
+ 'docroot': '/home/alice/domains/example.com/public_html',
+ 'type': 'primary',
+ }
+ assert report['domains'][1]['type'] == 'secondary'
+
+ # Docroots staged under homedir/ for the shared copy step.
+ assert report['homedir_present'] is True
+ assert os.path.isfile(os.path.join(
+ extracted, DA_WRAPPER, 'homedir', 'domains', 'example.com',
+ 'public_html', 'index.php'))
+ assert any('Staged domains/' in w for w in report['warnings'])
+
+ assert len(report['databases']) == 1
+ database = report['databases'][0]
+ assert database['name'] == 'alice_wp'
+ assert database['engine'] == 'mysql'
+ assert database['dump_path'] == f'{DA_WRAPPER}/backup/alice_wp.sql'
+ assert database['size'] > 0
+
+ users = {u['user']: u for u in report['db_users']}
+ assert set(users) == {'alice', 'alice_wp'}
+ for entry in users.values():
+ assert entry['hash'] == NATIVE_HASH
+ assert entry['hash_format'] == 'mysql_native_password'
+ assert entry['grants'] == []
+
+ # MAILTO env line dropped, "<id>=" prefix stripped.
+ assert report['crontab'] == [
+ '*/5 * * * * /usr/bin/php /home/alice/cron.php']
+
+ assert report['mail_accounts_count'] == 2
+ assert any('mail account' in u for u in report['unsupported'])
+
+
+def test_directadmin_partial_backup_warns_not_crashes(tmp_path):
+ root = os.path.join(str(tmp_path), 'partial', DA_WRAPPER)
+ _write(root, 'backup/user.conf', 'username=alice\n')
+ report = get_importer('directadmin').analyze(os.path.dirname(root))
+ assert report['format'] == 'directadmin'
+ assert report['homedir_present'] is False
+ assert report['domains'] == []
+ assert report['databases'] == []
+ assert report['db_users'] == []
+ assert report['crontab'] == []
+ assert report['warnings'] # every missing section surfaced
+
+
+def test_directadmin_non_native_hash_warns(tmp_path):
+ root = os.path.join(str(tmp_path), 'plain', DA_WRAPPER)
+ _write(root, 'backup/user.conf', 'username=alice\n')
+ _write(root, 'backup/mysql.conf', 'user=alice\npasswd=plaintextpw\n')
+ report = get_importer('directadmin').analyze(os.path.dirname(root))
+ (entry,) = report['db_users']
+ assert entry['hash_format'] == 'unknown'
+ assert any('new password will be generated' in w
+ for w in report['warnings'])
+
+
+# --------------------------------------------------------------------------
+# Hestia analyse
+# --------------------------------------------------------------------------
+
+def test_hestia_analyze_report(tmp_path):
+ extracted = _extracted_tree(tmp_path, _build_hestia_tree, HESTIA_WRAPPER,
+ 'hestia')
+ report = get_importer('hestia').analyze(extracted)
+
+ for key in ('format', 'homedir_present', 'domains', 'databases',
+ 'db_users', 'crontab', 'mail_accounts_count', 'php_version',
+ 'warnings', 'unsupported'):
+ assert key in report, f'missing report key {key}'
+
+ assert report['format'] == 'hestia'
+ assert report['source_root'] == HESTIA_WRAPPER
+ assert report['account_user'] == 'alice'
+ assert report['php_version'] == '8.1' # from BACKEND_TEMPLATE='PHP-FPM-81'
+
+ # First (sorted) domain is treated as primary.
+ assert [d['domain'] for d in report['domains']] == [
+ 'example.com', 'static.example.com']
+ assert report['domains'][0] == {
+ 'domain': 'example.com',
+ 'docroot': '/home/alice/web/example.com/public_html',
+ 'type': 'primary',
+ }
+ # public_html.tar.gz shipped files directly → docroot is the stage dir.
+ assert report['domains'][1]['docroot'] == \
+ '/home/alice/web/static.example.com'
+
+ # Nested archives extracted into homedir/ with traversal-safe helper.
+ root = os.path.join(extracted, HESTIA_WRAPPER)
+ assert report['homedir_present'] is True
+ assert os.path.isfile(os.path.join(
+ root, 'homedir', 'web', 'example.com', 'public_html', 'index.php'))
+ assert os.path.isfile(os.path.join(
+ root, 'homedir', 'web', 'static.example.com', 'index.html'))
+ assert any('Extracted nested' in w for w in report['warnings'])
+
+ (database,) = report['databases']
+ assert database['name'] == 'alice_db'
+ assert database['engine'] == 'mysql'
+ assert database['dump_path'] == \
+ f'{HESTIA_WRAPPER}/db/alice_db/alice_db.sql'
+ assert database['size'] > 0
+
+ (db_user,) = report['db_users']
+ assert db_user['user'] == 'alice_db'
+ assert db_user['hash'] == NATIVE_HASH
+ assert db_user['hash_format'] == 'mysql_native_password'
+
+ # Config-format cron converted; suspended job skipped with a warning.
+ assert report['crontab'] == ['*/10 * * * * php /home/alice/cron.php']
+ assert any('suspended cron job' in w for w in report['warnings'])
+
+ assert report['mail_accounts_count'] == 2
+ unsupported = ' '.join(report['unsupported'])
+ assert 'mail account' in unsupported
+ assert 'DNS zone' in unsupported
+
+
+def test_hestia_partial_backup_warns_not_crashes(tmp_path):
+ root = os.path.join(str(tmp_path), 'partial', HESTIA_WRAPPER)
+ _write(root, 'pam/passwd', 'alice:x:1001:1001::/home/alice:/bin/bash\n')
+ _write(root, 'user.conf', "NAME='alice'\n")
+ os.makedirs(os.path.join(root, 'web')) # empty — no domains
+ report = get_importer('hestia').analyze(os.path.dirname(root))
+ assert report['format'] == 'hestia'
+ assert report['homedir_present'] is False
+ assert report['domains'] == []
+ assert report['databases'] == []
+ assert report['crontab'] == []
+ assert report['warnings']
+
+
+def test_hestia_gzipped_dump_is_decompressed(tmp_path):
+ import gzip
+ root = os.path.join(str(tmp_path), 'gz', HESTIA_WRAPPER)
+ _write(root, 'pam/passwd', 'alice:x:1001:1001::/home/alice:/bin/bash\n')
+ os.makedirs(os.path.join(root, 'db', 'alice_db'), exist_ok=True)
+ with gzip.open(os.path.join(root, 'db', 'alice_db',
+ 'alice_db.sql.gz'), 'wb') as fh:
+ fh.write(b'CREATE TABLE posts (id INT);\n')
+ report = get_importer('hestia').analyze(os.path.dirname(root))
+ (database,) = report['databases']
+ assert database['dump_path'].endswith('db/alice_db/alice_db.sql')
+ assert os.path.isfile(os.path.join(
+ root, 'db', 'alice_db', 'alice_db.sql'))
+ assert any('Decompressed' in w for w in report['warnings'])
+
+
+# --------------------------------------------------------------------------
+# auto-detection through the service
+# --------------------------------------------------------------------------
+
[email protected]('builder,wrapper,expected', [
+ (_build_da_tree, DA_WRAPPER, 'directadmin'),
+ (_build_hestia_tree, HESTIA_WRAPPER, 'hestia'),
+])
+def test_auto_source_type_detects_format(app, tmp_path, builder, wrapper,
+ expected):
+ imp = _make_import(tmp_path, 'auto', builder, wrapper)
+ SiteImportService.analyze(imp)
+ assert imp.source_type == expected
+ assert imp.status == 'analyzed'
+
+
+# --------------------------------------------------------------------------
+# end-to-end run() with the default plan
+# --------------------------------------------------------------------------
+
+def test_directadmin_end_to_end_run(app, tmp_path, fake_db, fake_cron,
+ apps_dir):
+ from app.models import Application
+ user = _admin_user(app)
+ imp = _make_import(tmp_path, 'directadmin', _build_da_tree, DA_WRAPPER,
+ user_id=user.id)
+ SiteImportService.analyze(imp)
+ SiteImportService.run(imp)
+
+ assert imp.status == 'completed'
+ assert imp.error is None
+ result = imp.get_result()
+ application = Application.query.get(result['app_id'])
+ assert application is not None
+ assert application.name == 'example-com'
+ assert application.php_version == '8.1'
+ # Primary docroot copied out of the staged homedir/.
+ assert os.path.isfile(os.path.join(application.root_path, 'index.php'))
+
+ assert [c[1][0] for c in fake_db.named('mysql_create_database')] == \
+ ['alice_wp']
+ assert fake_db.named('mysql_restore')[0][1][0] == 'alice_wp'
+ # Both users recreated with their preserved native hash.
+ executed = ' ;; '.join(c[1][0] for c in fake_db.named('mysql_execute'))
+ assert f"AS '{NATIVE_HASH}'" in executed
+ assert {u['user'] for u in result['db_users']} == {'alice', 'alice_wp'}
+ assert all(u['preserved_hash'] for u in result['db_users'])
+
+ assert fake_cron == [{'schedule': '*/5 * * * *',
+ 'command': '/usr/bin/php /home/alice/cron.php',
+ 'name': f'import-{imp.id}'}]
+ assert result['validated'] is True
+
+
+def test_hestia_end_to_end_run(app, tmp_path, fake_db, fake_cron, apps_dir):
+ from app.models import Application
+ user = _admin_user(app)
+ imp = _make_import(tmp_path, 'hestia', _build_hestia_tree,
+ HESTIA_WRAPPER, user_id=user.id)
+ SiteImportService.analyze(imp)
+ SiteImportService.run(imp)
+
+ assert imp.status == 'completed'
+ assert imp.error is None
+ result = imp.get_result()
+ application = Application.query.get(result['app_id'])
+ assert application is not None
+ assert application.name == 'example-com'
+ assert application.php_version == '8.1'
+ # Docroot came from the nested domain_data.tar.gz staging.
+ assert os.path.isfile(os.path.join(application.root_path, 'index.php'))
+
+ assert [c[1][0] for c in fake_db.named('mysql_create_database')] == \
+ ['alice_db']
+ assert fake_db.named('mysql_restore')[0][1][0] == 'alice_db'
+ executed = ' ;; '.join(c[1][0] for c in fake_db.named('mysql_execute'))
+ assert f"AS '{NATIVE_HASH}'" in executed
+ assert result['db_users'] == [{'user': 'alice_db',
+ 'preserved_hash': True}]
+
+ assert fake_cron == [{'schedule': '*/10 * * * *',
+ 'command': 'php /home/alice/cron.php',
+ 'name': f'import-{imp.id}'}]
+ assert result['validated'] is True
+
+
+def test_default_plan_step_keys_map_to_handlers(tmp_path):
+ """Every step the default plan emits for these formats has a
+ _step_<key> handler on the orchestrator."""
+ da = _extracted_tree(tmp_path, _build_da_tree, DA_WRAPPER, 'da')
+ hestia = _extracted_tree(tmp_path, _build_hestia_tree, HESTIA_WRAPPER,
+ 'hestia')
+ for fmt, tree in (('directadmin', da), ('hestia', hestia)):
+ importer = get_importer(fmt)
+ analysis = importer.analyze(tree)
+ for step in importer.plan(analysis, {}):
+ assert hasattr(SiteImportService, f"_step_{step['key']}"), \
+ f"{fmt}: no handler for step {step['key']}"
diff --git a/frontend/src/pages/ImportWizard.jsx b/frontend/src/pages/ImportWizard.jsx
new file mode 100644
index 00000000..4552866e
--- /dev/null
+++ b/frontend/src/pages/ImportWizard.jsx
@@ -0,0 +1,680 @@
+import { useState, useEffect, useCallback, useRef } from 'react';
+import { Link } from 'react-router-dom';
+import {
+ ArrowDownToLine, ArrowLeft, ArrowRight, CheckCircle2, ChevronRight, Clock,
+ Database, Globe, Link2, Package, RotateCcw, Server, Trash2, AlertTriangle,
+ Upload, Users,
+} from 'lucide-react';
+import api from '../services/api';
+import { useToast } from '../contexts/ToastContext';
+import { useConfirm } from '../hooks/useConfirm';
+import { PageTopbar, Pill } from '@/components/ds';
+import { Button } from '@/components/ui/button';
+import { Input } from '@/components/ui/input';
+import { Label } from '@/components/ui/label';
+import Spinner from '../components/Spinner';
+
+const POLL_MS = 2000;
+
+const SOURCE_TYPES = [
+ {
+ id: 'cpanel',
+ name: 'cPanel full backup',
+ description: 'A cpmove / backup-*.tar.gz archive from cPanel\'s Full Account Backup.',
+ },
+ {
+ id: 'directadmin',
+ name: 'DirectAdmin',
+ description: 'A user backup archive created by DirectAdmin\'s Create/Restore Backups.',
+ },
+ {
+ id: 'hestia',
+ name: 'Hestia',
+ description: 'A v-backup-user archive from HestiaCP (also fits VestaCP layouts).',
+ },
+];
+
+const STEPS = ['Source', 'Backup', 'Analyse', 'Review', 'Run'];
+
+// Import status -> Pill colour, shared by the run pane and the history list.
+const PILL_KIND = {
+ created: 'gray',
+ analyzing: 'cyan',
+ analyzed: 'violet',
+ running: 'cyan',
+ completed: 'green',
+ failed: 'red',
+};
+
+function StatusPill({ status }) {
+ return <Pill kind={PILL_KIND[status] || 'gray'}>{status}</Pill>;
+}
+
+function formatSize(bytes) {
+ if (bytes == null || bytes === '') return '—';
+ const n = Number(bytes);
+ if (Number.isNaN(n)) return String(bytes);
+ if (n < 1024) return `${n} B`;
+ if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} KB`;
+ if (n < 1024 * 1024 * 1024) return `${(n / (1024 * 1024)).toFixed(1)} MB`;
+ return `${(n / (1024 * 1024 * 1024)).toFixed(2)} GB`;
+}
+
+// Analysis report (step 3): domains/databases tables, db users, crontab,
+// warnings + unsupported callouts. Pure render off the contract's shape.
+function AnalysisReport({ analysis }) {
+ const domains = analysis.domains || [];
+ const databases = analysis.databases || [];
+ const dbUsers = analysis.db_users || [];
+ const crontab = analysis.crontab || [];
+ const warnings = analysis.warnings || [];
+ const unsupported = analysis.unsupported || [];
+
+ return (
+ <div className="import-wizard__report">
+ <div className="import-wizard__report-meta">
+ <span>Format <code>{analysis.format || '—'}</code></span>
+ <span>PHP <code>{analysis.php_version || '—'}</code></span>
+ <span>Mail accounts <code>{analysis.mail_accounts_count ?? 0}</code></span>
+ </div>
+
+ {unsupported.length > 0 && (
+ <div className="import-wizard__callout import-wizard__callout--danger">
+ <AlertTriangle size={16} aria-hidden="true" />
+ <div>
+ <strong>Not supported — these items will be skipped:</strong>
+ <ul>{unsupported.map((u, i) => <li key={i}>{u}</li>)}</ul>
+ </div>
+ </div>
+ )}
+ {warnings.length > 0 && (
+ <div className="import-wizard__callout import-wizard__callout--warning">
+ <AlertTriangle size={16} aria-hidden="true" />
+ <div>
+ <strong>Warnings:</strong>
+ <ul>{warnings.map((w, i) => <li key={i}>{w}</li>)}</ul>
+ </div>
+ </div>
+ )}
+
+ <section className="import-wizard__report-section">
+ <h3><Globe size={15} aria-hidden="true" /> Domains ({domains.length})</h3>
+ {domains.length === 0 ? (
+ <p className="import-wizard__muted">No domains found in the archive.</p>
+ ) : (
+ <div className="import-wizard__table-wrap">
+ <table className="import-wizard__table">
+ <thead>
+ <tr><th>Domain</th><th>Type</th><th>Docroot</th></tr>
+ </thead>
+ <tbody>
+ {domains.map((d) => (
+ <tr key={d.domain}>
+ <td>{d.domain}</td>
+ <td>{d.type || '—'}</td>
+ <td><code>{d.docroot || '—'}</code></td>
+ </tr>
+ ))}
+ </tbody>
+ </table>
+ </div>
+ )}
+ </section>
+
+ <section className="import-wizard__report-section">
+ <h3><Database size={15} aria-hidden="true" /> Databases ({databases.length})</h3>
+ {databases.length === 0 ? (
+ <p className="import-wizard__muted">No database dumps found.</p>
+ ) : (
+ <div className="import-wizard__table-wrap">
+ <table className="import-wizard__table">
+ <thead>
+ <tr><th>Name</th><th>Engine</th><th>Size</th><th>Dump</th></tr>
+ </thead>
+ <tbody>
+ {databases.map((db) => (
+ <tr key={db.name}>
+ <td>{db.name}</td>
+ <td>{db.engine || '—'}</td>
+ <td>{formatSize(db.size)}</td>
+ <td><code>{db.dump_path || '—'}</code></td>
+ </tr>
+ ))}
+ </tbody>
+ </table>
+ </div>
+ )}
+ </section>
+
+ {dbUsers.length > 0 && (
+ <section className="import-wizard__report-section">
+ <h3><Users size={15} aria-hidden="true" /> Database users ({dbUsers.length})</h3>
+ <div className="import-wizard__chips">
+ {dbUsers.map((u) => (
+ <span key={u.user} className="import-wizard__chip">
+ {u.user}
+ {u.hash_format && <em>{u.hash_format}</em>}
+ </span>
+ ))}
+ </div>
+ </section>
+ )}
+
+ {crontab.length > 0 && (
+ <section className="import-wizard__report-section">
+ <h3><Clock size={15} aria-hidden="true" /> Crontab ({crontab.length})</h3>
+ <pre className="import-wizard__cron">{crontab.join('\n')}</pre>
+ </section>
+ )}
+ </div>
+ );
+}
+
+function ImportWizard() {
+ const toast = useToast();
+ const { confirm } = useConfirm();
+
+ // Wizard position: 1 source · 2 backup · 3 analyse · 4 review · 5 run
+ const [step, setStep] = useState(1);
+ const [sourceType, setSourceType] = useState(null);
+
+ // Step 2 — the backup archive (file upload or fetch-by-URL)
+ const [inputMode, setInputMode] = useState('upload');
+ const [file, setFile] = useState(null);
+ const [sourceUrl, setSourceUrl] = useState('');
+ const [uploadProgress, setUploadProgress] = useState(null);
+ const [dragActive, setDragActive] = useState(false);
+ const [busy, setBusy] = useState(false);
+ const dragCounter = useRef(0);
+ const fileInputRef = useRef(null);
+
+ // The server-side import record, polled while analyzing/running
+ const [imp, setImp] = useState(null);
+
+ // Step 4 — dry-run options
+ const [skipDb, setSkipDb] = useState(false);
+ const [skipCrontab, setSkipCrontab] = useState(false);
+
+ // Previous imports (history list)
+ const [history, setHistory] = useState([]);
+ const [historyLoading, setHistoryLoading] = useState(true);
+
+ const logRef = useRef(null);
+
+ const loadHistory = useCallback(async () => {
+ try {
+ const data = await api.getImports();
+ setHistory(data.imports || []);
+ } catch {
+ setHistory([]);
+ } finally {
+ setHistoryLoading(false);
+ }
+ }, []);
+
+ useEffect(() => { loadHistory(); }, [loadHistory]);
+
+ // Poll the active import every 2s while the backend is working on it.
+ const status = imp?.status;
+ useEffect(() => {
+ if (!imp?.id || (status !== 'analyzing' && status !== 'running' && status !== 'created')) return undefined;
+ // 'created' is only polled right after we fire analyze, so a slow
+ // status flip to 'analyzing' doesn't strand the wizard.
+ const timer = setInterval(async () => {
+ try {
+ const data = await api.getImport(imp.id);
+ if (data.import) setImp(data.import);
+ } catch {
+ // transient poll failure — keep polling
+ }
+ }, POLL_MS);
+ return () => clearInterval(timer);
+ }, [imp?.id, status]);
+
+ // Autoscroll the live log pane as new lines arrive.
+ const logText = imp?.log_text;
+ useEffect(() => {
+ if (logRef.current) logRef.current.scrollTop = logRef.current.scrollHeight;
+ }, [logText]);
+
+ // Refresh history when a run settles so the list's pills stay honest.
+ useEffect(() => {
+ if (status === 'completed' || status === 'failed' || status === 'analyzed') loadHistory();
+ }, [status, loadHistory]);
+
+ const resetWizard = () => {
+ setStep(1);
+ setSourceType(null);
+ setFile(null);
+ setSourceUrl('');
+ setUploadProgress(null);
+ setImp(null);
+ setSkipDb(false);
+ setSkipCrontab(false);
+ };
+
+ const pickSource = (typeId) => {
+ setSourceType(typeId);
+ setStep(2);
+ };
+
+ // Step 2 → 3: upload (or hand over the URL), create the import record,
+ // kick off analysis, then let the poller carry the status forward.
+ const startAnalyse = async () => {
+ setBusy(true);
+ try {
+ let source;
+ if (inputMode === 'upload') {
+ setUploadProgress(0);
+ const uploaded = await api.uploadImportArchive(file, (pct) => setUploadProgress(pct));
+ source = { upload_path: uploaded.upload_path };
+ } else {
+ source = { url: sourceUrl.trim() };
+ }
+ const created = await api.createImport(sourceType, source);
+ const record = created.import;
+ setImp(record);
+ setStep(3);
+ await api.analyzeImport(record.id);
+ // Reflect the analyze kick-off immediately; the poller takes over.
+ setImp((prev) => (prev ? { ...prev, status: 'analyzing' } : prev));
+ } catch (error) {
+ toast.error(`Import failed to start: ${error.message}`);
+ } finally {
+ setBusy(false);
+ setUploadProgress(null);
+ }
+ };
+
+ const startRun = async (fromStep = null) => {
+ if (!imp?.id) return;
+ setBusy(true);
+ try {
+ await api.runImport(imp.id, fromStep, { skip_db: skipDb, skip_crontab: skipCrontab });
+ setImp((prev) => (prev ? { ...prev, status: 'running', error: null } : prev));
+ setStep(5);
+ } catch (error) {
+ toast.error(`Failed to start the import run: ${error.message}`);
+ } finally {
+ setBusy(false);
+ }
+ };
+
+ // Re-open a previous import at the right step for its status.
+ const resumeImport = async (record) => {
+ try {
+ const data = await api.getImport(record.id);
+ const full = data.import || record;
+ setImp(full);
+ setSourceType(full.source_type);
+ if (full.status === 'running' || full.status === 'completed' || full.status === 'failed') {
+ setStep(5);
+ } else {
+ setStep(3);
+ }
+ } catch (error) {
+ toast.error(`Failed to load import: ${error.message}`);
+ }
+ };
+
+ const removeImport = async (record) => {
+ const ok = await confirm({
+ title: 'Delete import?',
+ message: `This removes the import record${record.status === 'running' ? ' (currently running)' : ''} and its uploaded archive.`,
+ confirmText: 'Delete',
+ variant: 'danger',
+ });
+ if (!ok) return;
+ try {
+ await api.deleteImport(record.id);
+ if (imp?.id === record.id) resetWizard();
+ await loadHistory();
+ toast.success('Import deleted');
+ } catch (error) {
+ toast.error(`Failed to delete import: ${error.message}`);
+ }
+ };
+
+ // ── drag & drop (FileManager idiom: counter-based enter/leave) ──
+ const onDragEnter = (e) => {
+ e.preventDefault(); e.stopPropagation();
+ dragCounter.current += 1;
+ if (e.dataTransfer.items?.length > 0) setDragActive(true);
+ };
+ const onDragLeave = (e) => {
+ e.preventDefault(); e.stopPropagation();
+ dragCounter.current -= 1;
+ if (dragCounter.current === 0) setDragActive(false);
+ };
+ const onDragOver = (e) => { e.preventDefault(); e.stopPropagation(); };
+ const onDrop = (e) => {
+ e.preventDefault(); e.stopPropagation();
+ dragCounter.current = 0;
+ setDragActive(false);
+ if (e.dataTransfer.files?.length > 0) setFile(e.dataTransfer.files[0]);
+ };
+
+ const analysis = imp?.analysis || null;
+ const canContinueFromBackup = inputMode === 'upload' ? !!file : sourceUrl.trim() !== '';
+
+ // Dry-run figures for step 4, derived from the analysis + toggles.
+ const domainCount = analysis?.domains?.length || 0;
+ const dbCount = skipDb ? 0 : (analysis?.databases?.length || 0);
+ const cronCount = skipCrontab ? 0 : (analysis?.crontab?.length || 0);
+
+ const stepperIndex = step - 1;
+
+ return (
+ <>
+ <PageTopbar
+ icon={<ArrowDownToLine size={18} />}
+ title="Import a site"
+ meta="Bring a site over from another control panel"
+ actions={step > 1 && (
+ <Button variant="outline" size="sm" onClick={resetWizard}>
+ Start over
+ </Button>
+ )}
+ />
+
+ <div className="import-wizard">
+ {/* Stepper */}
+ <ol className="import-wizard__stepper">
+ {STEPS.map((label, i) => (
+ <li
+ key={label}
+ className={`import-wizard__step${i === stepperIndex ? ' is-active' : ''}${i < stepperIndex ? ' is-done' : ''}`}
+ >
+ <span className="import-wizard__step-dot">
+ {i < stepperIndex ? <CheckCircle2 size={14} aria-hidden="true" /> : i + 1}
+ </span>
+ {label}
+ </li>
+ ))}
+ </ol>
+
+ {/* Step 1 — source type */}
+ {step === 1 && (
+ <div className="import-wizard__panel">
+ <h2>Where is the site coming from?</h2>
+ <div className="import-wizard__sources">
+ {SOURCE_TYPES.map((s) => (
+ <button
+ key={s.id}
+ type="button"
+ className="import-wizard__source-card"
+ onClick={() => pickSource(s.id)}
+ >
+ <span className="import-wizard__source-ico"><Package size={18} aria-hidden="true" /></span>
+ <strong>{s.name}</strong>
+ <p>{s.description}</p>
+ <ChevronRight size={16} className="import-wizard__source-chev" aria-hidden="true" />
+ </button>
+ ))}
+ <Link to="/wordpress/ssh-import" className="import-wizard__source-card import-wizard__source-card--link">
+ <span className="import-wizard__source-ico"><Server size={18} aria-hidden="true" /></span>
+ <strong>WordPress over SSH</strong>
+ <p>Pull a live WordPress site straight from its current server. Opens the WordPress import surface.</p>
+ <ChevronRight size={16} className="import-wizard__source-chev" aria-hidden="true" />
+ </Link>
+ </div>
+ </div>
+ )}
+
+ {/* Step 2 — backup archive */}
+ {step === 2 && (
+ <div className="import-wizard__panel">
+ <h2>Provide the backup archive</h2>
+ <div className="import-wizard__mode">
+ <button
+ type="button"
+ className={`import-wizard__mode-btn${inputMode === 'upload' ? ' is-active' : ''}`}
+ onClick={() => setInputMode('upload')}
+ >
+ <Upload size={14} aria-hidden="true" /> Upload a file
+ </button>
+ <button
+ type="button"
+ className={`import-wizard__mode-btn${inputMode === 'url' ? ' is-active' : ''}`}
+ onClick={() => setInputMode('url')}
+ >
+ <Link2 size={14} aria-hidden="true" /> Fetch from URL
+ </button>
+ </div>
+
+ {inputMode === 'upload' ? (
+ <div
+ className={`import-wizard__dropzone${dragActive ? ' is-drag' : ''}`}
+ onDragEnter={onDragEnter}
+ onDragOver={onDragOver}
+ onDragLeave={onDragLeave}
+ onDrop={onDrop}
+ onClick={() => fileInputRef.current?.click()}
+ role="button"
+ tabIndex={0}
+ onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') fileInputRef.current?.click(); }}
+ >
+ <input
+ type="file"
+ ref={fileInputRef}
+ accept=".tar,.gz,.tgz,.tar.gz,.zip"
+ className="import-wizard__file-input"
+ onChange={(e) => setFile(e.target.files?.[0] || null)}
+ />
+ <Upload size={22} aria-hidden="true" />
+ {file ? (
+ <p><strong>{file.name}</strong> · {formatSize(file.size)}</p>
+ ) : (
+ <p>Drag the backup archive here, or click to browse</p>
+ )}
+ <span className="import-wizard__muted">.tar.gz / .tgz / .zip — the full panel backup, not a partial export</span>
+ {uploadProgress != null && (
+ <div className="import-wizard__progress">
+ {/* Inline width is the dynamic-progress pattern used by uploads elsewhere */}
+ <div className="import-wizard__progress-fill" style={{ width: `${uploadProgress}%` }} />
+ </div>
+ )}
+ </div>
+ ) : (
+ <div className="import-wizard__url">
+ <Label htmlFor="import-url">Archive URL</Label>
+ <Input
+ id="import-url"
+ type="url"
+ value={sourceUrl}
+ onChange={(e) => setSourceUrl(e.target.value)}
+ placeholder="https://old-server.example.com/backup-user.tar.gz"
+ disabled={busy}
+ />
+ <span className="import-wizard__muted">The panel downloads the archive server-side — handy when the backup is too large to route through your browser.</span>
+ </div>
+ )}
+
+ <div className="import-wizard__actions">
+ <Button variant="outline" onClick={() => setStep(1)} disabled={busy}>
+ <ArrowLeft size={14} /> Back
+ </Button>
+ <Button onClick={startAnalyse} disabled={busy || !canContinueFromBackup}>
+ {busy ? <><Spinner size="sm" /> {inputMode === 'upload' ? 'Uploading…' : 'Starting…'}</> : <>Analyse backup <ArrowRight size={14} /></>}
+ </Button>
+ </div>
+ </div>
+ )}
+
+ {/* Step 3 — analysis */}
+ {step === 3 && (
+ <div className="import-wizard__panel">
+ <h2>Analysis {imp && <StatusPill status={imp.status} />}</h2>
+ {(status === 'analyzing' || status === 'created') && (
+ <div className="import-wizard__waiting">
+ <Spinner size="sm" />
+ <span>Unpacking and inspecting the archive… this can take a few minutes for large backups.</span>
+ </div>
+ )}
+ {status === 'failed' && (
+ <div className="import-wizard__callout import-wizard__callout--danger">
+ <AlertTriangle size={16} aria-hidden="true" />
+ <div>
+ <strong>Analysis failed.</strong>
+ <p>{imp.error || 'The archive could not be analysed.'}</p>
+ </div>
+ </div>
+ )}
+ {analysis && <AnalysisReport analysis={analysis} />}
+ <div className="import-wizard__actions">
+ <Button variant="outline" onClick={resetWizard}>
+ <ArrowLeft size={14} /> Start over
+ </Button>
+ {status === 'failed' && (
+ <Button variant="outline" onClick={async () => {
+ try {
+ await api.analyzeImport(imp.id);
+ setImp((prev) => (prev ? { ...prev, status: 'analyzing', error: null } : prev));
+ } catch (error) {
+ toast.error(`Failed to re-analyse: ${error.message}`);
+ }
+ }}>
+ <RotateCcw size={14} /> Re-analyse
+ </Button>
+ )}
+ <Button onClick={() => setStep(4)} disabled={status !== 'analyzed'}>
+ Review import <ArrowRight size={14} />
+ </Button>
+ </div>
+ </div>
+ )}
+
+ {/* Step 4 — dry-run summary */}
+ {step === 4 && (
+ <div className="import-wizard__panel">
+ <h2>What will be created</h2>
+ <ul className="import-wizard__plan">
+ <li>
+ <Globe size={15} aria-hidden="true" />
+ <span><strong>{domainCount}</strong> app container{domainCount === 1 ? '' : 's'} — one per domain, docroot copied in and served behind Nginx</span>
+ </li>
+ <li className={skipDb ? 'is-skipped' : ''}>
+ <Database size={15} aria-hidden="true" />
+ <span><strong>{dbCount}</strong> managed database{dbCount === 1 ? '' : 's'} restored from the archive&apos;s dumps{skipDb && ' (skipped)'}</span>
+ </li>
+ <li className={skipCrontab ? 'is-skipped' : ''}>
+ <Clock size={15} aria-hidden="true" />
+ <span><strong>{cronCount}</strong> cron entr{cronCount === 1 ? 'y' : 'ies'} recreated as scheduled jobs{skipCrontab && ' (skipped)'}</span>
+ </li>
+ </ul>
+ {(analysis?.mail_accounts_count || 0) > 0 && (
+ <div className="import-wizard__callout import-wizard__callout--warning">
+ <AlertTriangle size={16} aria-hidden="true" />
+ <div>
+ <p>{analysis.mail_accounts_count} mail account{analysis.mail_accounts_count === 1 ? '' : 's'} found in the backup will not be imported — mail is handled by the mail extension.</p>
+ </div>
+ </div>
+ )}
+
+ <div className="import-wizard__options">
+ <label className="checkbox-label">
+ <input type="checkbox" checked={skipDb} onChange={(e) => setSkipDb(e.target.checked)} />
+ Skip database import
+ </label>
+ <label className="checkbox-label">
+ <input type="checkbox" checked={skipCrontab} onChange={(e) => setSkipCrontab(e.target.checked)} />
+ Skip crontab entries
+ </label>
+ </div>
+
+ <div className="import-wizard__actions">
+ <Button variant="outline" onClick={() => setStep(3)} disabled={busy}>
+ <ArrowLeft size={14} /> Back to report
+ </Button>
+ <Button onClick={() => startRun()} disabled={busy}>
+ {busy ? <><Spinner size="sm" /> Starting…</> : <>Run import <ArrowRight size={14} /></>}
+ </Button>
+ </div>
+ </div>
+ )}
+
+ {/* Step 5 — run with live log */}
+ {step === 5 && imp && (
+ <div className="import-wizard__panel">
+ <h2>
+ Import run <StatusPill status={imp.status} />
+ {imp.current_step && imp.status === 'running' && (
+ <span className="import-wizard__current-step">step: <code>{imp.current_step}</code></span>
+ )}
+ </h2>
+
+ {imp.status === 'failed' && (
+ <div className="import-wizard__callout import-wizard__callout--danger">
+ <AlertTriangle size={16} aria-hidden="true" />
+ <div>
+ <strong>Failed at step <code>{imp.current_step || 'unknown'}</code>.</strong>
+ <p>{imp.error || 'The import run failed.'}</p>
+ </div>
+ </div>
+ )}
+ {imp.status === 'completed' && (
+ <div className="import-wizard__callout import-wizard__callout--success">
+ <CheckCircle2 size={16} aria-hidden="true" />
+ <div>
+ <p>Import completed. The new services are on the <Link to="/services">Services</Link> page.</p>
+ </div>
+ </div>
+ )}
+
+ <pre ref={logRef} className="import-wizard__log">
+ {imp.log_text || 'Waiting for log output…'}
+ </pre>
+
+ <div className="import-wizard__actions">
+ <Button variant="outline" onClick={resetWizard}>
+ <ArrowLeft size={14} /> New import
+ </Button>
+ {imp.status === 'failed' && (
+ <Button onClick={() => startRun(imp.current_step)} disabled={busy}>
+ {busy ? <><Spinner size="sm" /> Starting…</> : <><RotateCcw size={14} /> Retry from failed step</>}
+ </Button>
+ )}
+ {imp.status === 'completed' && (
+ <Button asChild>
+ <Link to="/services">Go to Services</Link>
+ </Button>
+ )}
+ </div>
+ </div>
+ )}
+
+ {/* Previous imports */}
+ <section className="import-wizard__history">
+ <h2>Previous imports</h2>
+ {historyLoading ? (
+ <p className="import-wizard__muted">Loading…</p>
+ ) : history.length === 0 ? (
+ <p className="import-wizard__muted">No imports yet.</p>
+ ) : (
+ <ul className="import-wizard__history-list">
+ {history.map((rec) => (
+ <li key={rec.id} className="import-wizard__history-item">
+ <StatusPill status={rec.status} />
+ <span className="import-wizard__history-src">{rec.source_type}</span>
+ <span className="import-wizard__history-when">
+ {rec.created_at ? new Date(rec.created_at).toLocaleString() : '—'}
+ </span>
+ <span className="import-wizard__history-actions">
+ <Button variant="ghost" size="sm" onClick={() => resumeImport(rec)}>
+ {rec.status === 'running' || rec.status === 'analyzing' ? 'Resume' : 'View'}
+ </Button>
+ <Button variant="ghost" size="sm" onClick={() => removeImport(rec)} aria-label="Delete import">
+ <Trash2 size={14} />
+ </Button>
+ </span>
+ </li>
+ ))}
+ </ul>
+ )}
+ </section>
+ </div>
+ </>
+ );
+}
+
+export default ImportWizard;
diff --git a/frontend/src/pages/Services.jsx b/frontend/src/pages/Services.jsx
index 573a46b9..10bce687 100644
--- a/frontend/src/pages/Services.jsx
+++ b/frontend/src/pages/Services.jsx
@@ -1,6 +1,6 @@
import { useState, useEffect, useMemo } from 'react';
import { useNavigate, Link } from 'react-router-dom';
-import { Layers, Plus, Square, Play, RotateCw, GitBranch, Github, FolderOpen, FileArchive, FolderKanban } from 'lucide-react';
+import { Layers, Plus, Square, Play, RotateCw, GitBranch, Github, FolderOpen, FileArchive, FolderKanban, ArrowDownToLine } from 'lucide-react';
import api from '../services/api';
import { useToast } from '../contexts/ToastContext';
import { getServiceType, getStatusConfig, formatRelativeTime } from '../utils/serviceTypes';
@@ -112,12 +112,20 @@ const Services = () => {
const runningCount = useMemo(() => apps.filter(a => a.status === 'running').length, [apps]);
useTopbarActions(() =>
- <Button size="sm" asChild>
- <Link to="/services/new">
- <Plus size={16} />
- New Service
- </Link>
- </Button>,
+ <>
+ <Button variant="outline" size="sm" asChild>
+ <Link to="/imports">
+ <ArrowDownToLine size={16} />
+ Import a site
+ </Link>
+ </Button>
+ <Button size="sm" asChild>
+ <Link to="/services/new">
+ <Plus size={16} />
+ New Service
+ </Link>
+ </Button>
+ </>,
[]
);
diff --git a/frontend/src/pages/WordPress.jsx b/frontend/src/pages/WordPress.jsx
index 1b1b4d80..c2cd15b2 100644
--- a/frontend/src/pages/WordPress.jsx
+++ b/frontend/src/pages/WordPress.jsx
@@ -179,6 +179,9 @@ function WordPress() {
useTopbarActions(() =>
<>
+ <Button variant="outline" size="sm" onClick={() => navigate('/imports')}>
+ Import a site
+ </Button>
<Button variant="outline" size="sm" onClick={() => setShowImportModal(true)}>
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></svg>
Import Site
diff --git a/frontend/src/services/api/imports.js b/frontend/src/services/api/imports.js
new file mode 100644
index 00000000..e149cb26
--- /dev/null
+++ b/frontend/src/services/api/imports.js
@@ -0,0 +1,68 @@
+// Site migration imports (/api/v1/imports) — backs the Import wizard.
+// Flow: upload (or URL) → create import → analyze → run, with 2s polling
+// on GET /imports/<id> while the backend is analyzing/running.
+
+export async function getImports() {
+ return this.request('/imports');
+}
+
+export async function getImport(importId) {
+ return this.request(`/imports/${importId}`);
+}
+
+export async function createImport(sourceType, source, options = {}) {
+ return this.request('/imports', {
+ method: 'POST',
+ body: { source_type: sourceType, source, options },
+ });
+}
+
+export async function analyzeImport(importId) {
+ return this.request(`/imports/${importId}/analyze`, { method: 'POST' });
+}
+
+export async function runImport(importId, fromStep = null, options = null) {
+ const body = {};
+ if (fromStep) body.from_step = fromStep;
+ if (options) body.options = options;
+ return this.request(`/imports/${importId}/run`, { method: 'POST', body });
+}
+
+export async function deleteImport(importId) {
+ return this.request(`/imports/${importId}`, { method: 'DELETE' });
+}
+
+// Multipart backup-archive upload with progress, XHR-based like files.js
+// uploadFile (fetch has no upload progress). Resolves to {upload_path}.
+export async function uploadImportArchive(file, onProgress = null) {
+ const token = this.getToken();
+ const formData = new FormData();
+ formData.append('file', file);
+
+ return new Promise((resolve, reject) => {
+ const xhr = new XMLHttpRequest();
+ xhr.open('POST', `${this.baseUrl}/imports/upload`);
+ xhr.setRequestHeader('Authorization', `Bearer ${token}`);
+
+ if (onProgress) {
+ xhr.upload.onprogress = (e) => {
+ if (e.lengthComputable) onProgress((e.loaded / e.total) * 100);
+ };
+ }
+
+ xhr.onload = () => {
+ if (xhr.status >= 200 && xhr.status < 300) {
+ resolve(JSON.parse(xhr.responseText));
+ } else {
+ let message = 'Upload failed';
+ try {
+ message = JSON.parse(xhr.responseText).error || message;
+ } catch { /* non-JSON error body */ }
+ reject(new Error(message));
+ }
+ };
+
+ xhr.onerror = () => reject(new Error('Upload failed'));
+ xhr.send(formData);
+ });
+}
diff --git a/frontend/src/styles/pages/_import-wizard.scss b/frontend/src/styles/pages/_import-wizard.scss
new file mode 100644
index 00000000..9b2813c1
--- /dev/null
+++ b/frontend/src/styles/pages/_import-wizard.scss
@@ -0,0 +1,571 @@
+// ============================================
+// IMPORT WIZARD (migrate a site from another panel)
+// ============================================
+
+.import-wizard {
+ max-width: 960px;
+ margin: 0 auto;
+ padding: $space-6;
+ display: flex;
+ flex-direction: column;
+ gap: $space-6;
+
+ h2 {
+ display: flex;
+ align-items: center;
+ gap: $space-3;
+ margin: 0 0 $space-4;
+ font-size: $font-size-lg;
+ font-weight: $font-weight-semibold;
+ color: $text-primary;
+ }
+
+ // --------------------------------------------
+ // Stepper
+ // --------------------------------------------
+ &__stepper {
+ display: flex;
+ align-items: center;
+ gap: $space-2;
+ margin: 0;
+ padding: 0;
+ list-style: none;
+ overflow-x: auto;
+ }
+
+ &__step {
+ display: flex;
+ align-items: center;
+ gap: $space-2;
+ padding: $space-2 $space-3;
+ border-radius: $radius-full;
+ font-size: $font-size-sm;
+ color: $text-tertiary;
+ white-space: nowrap;
+
+ &.is-active {
+ color: $text-primary;
+ background: $accent-bg;
+
+ .import-wizard__step-dot {
+ background: $accent-primary;
+ border-color: $accent-primary;
+ color: #fff;
+ }
+ }
+
+ &.is-done {
+ color: $text-secondary;
+
+ .import-wizard__step-dot {
+ color: $success;
+ border-color: $success-border;
+ }
+ }
+
+ // Connector between steps
+ & + &::before {
+ content: '';
+ width: 16px;
+ height: 1px;
+ margin-right: $space-2;
+ background: $border-default;
+ }
+ }
+
+ &__step-dot {
+ display: inline-flex;
+ align-items: center;
+ justify-content: center;
+ width: 22px;
+ height: 22px;
+ border: 1px solid $border-default;
+ border-radius: $radius-full;
+ font-size: $font-size-xs;
+ font-weight: $font-weight-semibold;
+ flex-shrink: 0;
+ }
+
+ // --------------------------------------------
+ // Step panel (shared card shell)
+ // --------------------------------------------
+ &__panel {
+ background: $bg-card;
+ border: 1px solid $border-subtle;
+ border-radius: $radius-xl;
+ padding: $space-6;
+ }
+
+ &__actions {
+ display: flex;
+ justify-content: space-between;
+ gap: $space-3;
+ margin-top: $space-6;
+
+ > :last-child {
+ margin-left: auto;
+ }
+ }
+
+ &__muted {
+ color: $text-tertiary;
+ font-size: $font-size-sm;
+ }
+
+ // --------------------------------------------
+ // Step 1 — source cards
+ // --------------------------------------------
+ &__sources {
+ display: grid;
+ grid-template-columns: repeat(2, minmax(0, 1fr));
+ gap: $space-4;
+
+ @media (max-width: 720px) {
+ grid-template-columns: 1fr;
+ }
+ }
+
+ &__source-card {
+ position: relative;
+ display: flex;
+ flex-direction: column;
+ align-items: flex-start;
+ gap: $space-2;
+ padding: $space-5;
+ background: $bg-elevated;
+ border: 1px solid $border-default;
+ border-radius: $radius-lg;
+ text-align: left;
+ text-decoration: none;
+ cursor: pointer;
+ transition: border-color 0.15s ease, background 0.15s ease;
+
+ strong {
+ color: $text-primary;
+ font-size: $font-size-base;
+ }
+
+ p {
+ margin: 0;
+ color: $text-secondary;
+ font-size: $font-size-sm;
+ line-height: 1.5;
+ }
+
+ &:hover {
+ border-color: $accent-primary;
+ background: $bg-hover;
+ }
+
+ &--link p::after {
+ content: ' ↗';
+ color: $text-tertiary;
+ }
+ }
+
+ &__source-ico {
+ display: inline-flex;
+ align-items: center;
+ justify-content: center;
+ width: 36px;
+ height: 36px;
+ border-radius: $radius-md;
+ background: $accent-bg;
+ color: $accent-primary;
+ }
+
+ &__source-chev {
+ position: absolute;
+ top: $space-5;
+ right: $space-4;
+ color: $text-tertiary;
+ }
+
+ // --------------------------------------------
+ // Step 2 — upload / URL
+ // --------------------------------------------
+ &__mode {
+ display: inline-flex;
+ gap: $space-1;
+ padding: $space-1;
+ margin-bottom: $space-4;
+ background: $bg-tertiary;
+ border-radius: $radius-md;
+ }
+
+ &__mode-btn {
+ display: inline-flex;
+ align-items: center;
+ gap: $space-2;
+ padding: $space-2 $space-3;
+ border: none;
+ border-radius: $radius-sm;
+ background: transparent;
+ color: $text-secondary;
+ font-size: $font-size-sm;
+ cursor: pointer;
+
+ &.is-active {
+ background: $bg-card;
+ color: $text-primary;
+ }
+ }
+
+ &__dropzone {
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ gap: $space-2;
+ padding: $space-10 $space-6;
+ border: 1px dashed $border-active;
+ border-radius: $radius-lg;
+ color: $text-secondary;
+ text-align: center;
+ cursor: pointer;
+ transition: border-color 0.15s ease, background 0.15s ease;
+
+ p {
+ margin: 0;
+ color: $text-primary;
+ }
+
+ &:hover,
+ &.is-drag {
+ border-color: $accent-primary;
+ background: $accent-bg-soft;
+ }
+ }
+
+ &__file-input {
+ display: none;
+ }
+
+ &__progress {
+ width: 100%;
+ max-width: 360px;
+ height: 6px;
+ margin-top: $space-3;
+ background: $bg-tertiary;
+ border-radius: $radius-full;
+ overflow: hidden;
+ }
+
+ &__progress-fill {
+ height: 100%;
+ background: $accent-primary;
+ border-radius: $radius-full;
+ transition: width 0.2s ease;
+ }
+
+ &__url {
+ display: flex;
+ flex-direction: column;
+ gap: $space-2;
+ max-width: 560px;
+ }
+
+ // --------------------------------------------
+ // Step 3 — analysis report
+ // --------------------------------------------
+ &__waiting {
+ display: flex;
+ align-items: center;
+ gap: $space-3;
+ padding: $space-4;
+ color: $text-secondary;
+ font-size: $font-size-sm;
+ }
+
+ &__report {
+ display: flex;
+ flex-direction: column;
+ gap: $space-5;
+ }
+
+ &__report-meta {
+ display: flex;
+ flex-wrap: wrap;
+ gap: $space-4;
+ color: $text-secondary;
+ font-size: $font-size-sm;
+
+ code {
+ color: $text-primary;
+ }
+ }
+
+ &__report-section {
+ h3 {
+ display: flex;
+ align-items: center;
+ gap: $space-2;
+ margin: 0 0 $space-3;
+ font-size: $font-size-base;
+ font-weight: $font-weight-semibold;
+ color: $text-primary;
+ }
+ }
+
+ &__table-wrap {
+ overflow-x: auto;
+ border: 1px solid $border-subtle;
+ border-radius: $radius-md;
+ }
+
+ &__table {
+ width: 100%;
+ border-collapse: collapse;
+ font-size: $font-size-sm;
+
+ th,
+ td {
+ padding: $space-2 $space-3;
+ text-align: left;
+ border-bottom: 1px solid $border-subtle;
+ white-space: nowrap;
+ }
+
+ th {
+ color: $text-tertiary;
+ font-weight: $font-weight-medium;
+ background: $bg-elevated;
+ }
+
+ td {
+ color: $text-primary;
+ }
+
+ tbody tr:last-child td {
+ border-bottom: none;
+ }
+
+ code {
+ font-family: $font-mono;
+ font-size: $font-size-xs;
+ color: $text-secondary;
+ }
+ }
+
+ &__chips {
+ display: flex;
+ flex-wrap: wrap;
+ gap: $space-2;
+ }
+
+ &__chip {
+ display: inline-flex;
+ align-items: center;
+ gap: $space-2;
+ padding: $space-1 $space-3;
+ background: $bg-tertiary;
+ border-radius: $radius-full;
+ font-size: $font-size-sm;
+ color: $text-primary;
+
+ em {
+ font-style: normal;
+ color: $text-tertiary;
+ font-size: $font-size-xs;
+ }
+ }
+
+ &__cron {
+ margin: 0;
+ padding: $space-3;
+ background: $bg-code;
+ border: 1px solid $border-subtle;
+ border-radius: $radius-md;
+ font-family: $font-mono;
+ font-size: $font-size-sm;
+ color: $text-secondary;
+ overflow-x: auto;
+ }
+
+ // --------------------------------------------
+ // Callouts (warnings / unsupported / success)
+ // --------------------------------------------
+ &__callout {
+ display: flex;
+ gap: $space-3;
+ padding: $space-4;
+ margin-bottom: $space-4;
+ border-radius: $radius-md;
+ font-size: $font-size-sm;
+
+ svg {
+ flex-shrink: 0;
+ margin-top: 2px;
+ }
+
+ p {
+ margin: 0;
+ }
+
+ ul {
+ margin: $space-2 0 0;
+ padding-left: $space-5;
+ }
+
+ &--warning {
+ background: $warning-bg;
+ border: 1px solid $warning-border;
+ color: $warning;
+
+ ul,
+ p {
+ color: $text-secondary;
+ }
+ }
+
+ &--danger {
+ background: $danger-bg;
+ border: 1px solid $danger-border;
+ color: $danger;
+
+ ul,
+ p {
+ color: $text-secondary;
+ }
+ }
+
+ &--success {
+ background: $success-bg;
+ border: 1px solid $success-border;
+ color: $success;
+
+ p {
+ color: $text-secondary;
+ }
+
+ a {
+ color: $accent-primary;
+ }
+ }
+ }
+
+ // --------------------------------------------
+ // Step 4 — dry-run plan
+ // --------------------------------------------
+ &__plan {
+ display: flex;
+ flex-direction: column;
+ gap: $space-3;
+ margin: 0 0 $space-4;
+ padding: 0;
+ list-style: none;
+
+ li {
+ display: flex;
+ align-items: flex-start;
+ gap: $space-3;
+ padding: $space-3 $space-4;
+ background: $bg-elevated;
+ border: 1px solid $border-subtle;
+ border-radius: $radius-md;
+ color: $text-secondary;
+ font-size: $font-size-sm;
+
+ svg {
+ flex-shrink: 0;
+ margin-top: 1px;
+ color: $accent-primary;
+ }
+
+ strong {
+ color: $text-primary;
+ }
+
+ &.is-skipped {
+ opacity: 0.55;
+
+ svg {
+ color: $text-tertiary;
+ }
+ }
+ }
+ }
+
+ &__options {
+ display: flex;
+ flex-direction: column;
+ gap: $space-2;
+ }
+
+ // --------------------------------------------
+ // Step 5 — run log
+ // --------------------------------------------
+ &__current-step {
+ font-size: $font-size-sm;
+ font-weight: $font-weight-normal;
+ color: $text-tertiary;
+
+ code {
+ color: $text-secondary;
+ }
+ }
+
+ &__log {
+ height: 320px;
+ margin: 0;
+ padding: $space-4;
+ background: $bg-code;
+ border: 1px solid $border-subtle;
+ border-radius: $radius-md;
+ font-family: $font-mono;
+ font-size: $font-size-sm;
+ line-height: 1.6;
+ color: $text-secondary;
+ overflow-y: auto;
+ white-space: pre-wrap;
+ word-break: break-word;
+ }
+
+ // --------------------------------------------
+ // Previous imports
+ // --------------------------------------------
+ &__history {
+ h2 {
+ font-size: $font-size-base;
+ }
+ }
+
+ &__history-list {
+ display: flex;
+ flex-direction: column;
+ margin: 0;
+ padding: 0;
+ list-style: none;
+ border: 1px solid $border-subtle;
+ border-radius: $radius-lg;
+ background: $bg-card;
+ overflow: hidden;
+ }
+
+ &__history-item {
+ display: flex;
+ align-items: center;
+ gap: $space-4;
+ padding: $space-3 $space-4;
+ border-bottom: 1px solid $border-subtle;
+ font-size: $font-size-sm;
+
+ &:last-child {
+ border-bottom: none;
+ }
+ }
+
+ &__history-src {
+ color: $text-primary;
+ font-family: $font-mono;
+ }
+
+ &__history-when {
+ color: $text-tertiary;
+ }
+
+ &__history-actions {
+ display: inline-flex;
+ gap: $space-1;
+ margin-left: auto;
+ }
+}