From 880b62e3d8613bcdb7b09128cf86c6f17baefa5e Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Fri, 4 Dec 2026 16:31:35 +0100 Subject: [PATCH] Allow DirectAdmin restore over an untouched starter page --- docs/development.md | 8 ++- src/coulobre/hosting/test_web_node_restore.py | 54 ++++++++++++++++ src/coulobre/web_node_apply.py | 13 ++++ src/coulobre/web_node_restore.py | 62 +++++++++++++++++-- src/coulobre/wordpress_node.py | 19 +----- 5 files changed, 131 insertions(+), 25 deletions(-) diff --git a/docs/development.md b/docs/development.md index a5744e4..c35ee32 100644 --- a/docs/development.md +++ b/docs/development.md @@ -736,9 +736,11 @@ domain has only one web root, Coulobre uses it. You can restore all supported components or only what you need. Select a package with enough website slots when restoring more than one site. The destination can be a new hosting account or an existing empty account -whose primary domain exactly matches. Remove any existing starter page -before restoring website files. Existing DNS can be left in place if you -do not select DNS import. New accounts do not include a new customer login. +whose primary domain exactly matches. A document root containing only an +unchanged Coulobre starter page counts as empty. A modified starter page +or any other file blocks the website restore. Existing DNS can be left in +place if you do not select DNS import. New accounts do not include a new +customer login. Open the destination account after import to attach a customer or manager. The selected component types apply to supported content across the selected diff --git a/src/coulobre/hosting/test_web_node_restore.py b/src/coulobre/hosting/test_web_node_restore.py index 9caebf6..b460019 100644 --- a/src/coulobre/hosting/test_web_node_restore.py +++ b/src/coulobre/hosting/test_web_node_restore.py @@ -11,6 +11,7 @@ from unittest.mock import patch from django.test import SimpleTestCase from coulobre.web_node import WebSite, main +from coulobre.web_node_apply import LEGACY_STARTER_PAGE, render_starter_page from coulobre.web_node_restore import copy_website, restore_website @@ -146,6 +147,59 @@ class DirectAdminWebRestoreTests(SimpleTestCase): self.assertEqual(list(self.public.iterdir()), [existing]) self.assertEqual(existing.read_bytes(), b"existing") + def test_only_stock_starter_page_can_be_replaced(self): + index = self.public / "index.html" + starter = render_starter_page(self.site.domain) + index.write_text(starter + "\n", encoding="utf-8") + with self.assertRaisesRegex(ValueError, "empty document root"): + self.restore() + + index.unlink() + other_index = self.account / "other.html" + other_index.write_text(starter, encoding="utf-8") + index.symlink_to(other_index) + with self.assertRaisesRegex(ValueError, "empty document root"): + self.restore() + self.assertTrue(index.is_symlink()) + index.unlink() + + index.write_text(starter, encoding="utf-8") + self.assertEqual(self.restore()["files"], 6) + self.assertFalse(index.exists()) + self.assertEqual(other_index.read_text(encoding="utf-8"), starter) + self.assertEqual( + set(self.account.iterdir()), {self.public, other_index} + ) + + def test_legacy_starter_page_can_be_replaced(self): + (self.public / "index.html").write_text( + LEGACY_STARTER_PAGE, encoding="utf-8" + ) + self.assertEqual(self.restore()["files"], 6) + + def test_restore_keeps_both_versions_if_index_appears_during_publish(self): + index = self.public / "index.html" + starter = render_starter_page(self.site.domain) + index.write_text(starter, encoding="utf-8") + + def publish_with_concurrent_file(staged, public): + self.assertFalse(index.exists()) + index.write_bytes(b"new homepage") + raise ValueError("Restore requires an empty document root.") + + with ( + patch( + "coulobre.web_node_restore.publish_empty_webroot", + side_effect=publish_with_concurrent_file, + ), + self.assertRaisesRegex(ValueError, "private recovery directory"), + ): + self.restore() + + self.assertEqual(index.read_bytes(), b"new homepage") + (saved,) = self.account.glob(".directadmin-recovery-*/index.html") + self.assertEqual(saved.read_text(encoding="utf-8"), starter) + def test_atomic_publish_preserves_a_file_created_during_restore(self): existing = self.public / "new.txt" diff --git a/src/coulobre/web_node_apply.py b/src/coulobre/web_node_apply.py index 0f24f3d..8594046 100644 --- a/src/coulobre/web_node_apply.py +++ b/src/coulobre/web_node_apply.py @@ -81,6 +81,19 @@ def starter_page_matches(page): return False +def is_starter_page(index): + try: + descriptor = os.open( + index, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK + ) + except OSError: + return False + with os.fdopen(descriptor, "rb") as page: + if not stat.S_ISREG(os.fstat(page.fileno()).st_mode): + return False + return starter_page_matches(page.read(starter_page_read_limit())) + + def starter_page_read_limit(): return ( max( diff --git a/src/coulobre/web_node_restore.py b/src/coulobre/web_node_restore.py index 1acb872..3475030 100644 --- a/src/coulobre/web_node_restore.py +++ b/src/coulobre/web_node_restore.py @@ -2,10 +2,23 @@ import errno import fcntl import os from pathlib import Path -from tempfile import TemporaryDirectory +from tempfile import TemporaryDirectory, mkdtemp from . import web_node_apply from .directadmin_website import copy_website +from .web_node_files import rename_entry + + +def starter_to_replace(public): + entries = os.listdir(public) + if not entries: + return None + index = public / "index.html" + if entries == [index.name] and web_node_apply.is_starter_page(index): + return index + raise ValueError( + "Restore requires an empty document root or the original starter page." + ) def check_website_empty(site): @@ -17,8 +30,7 @@ def check_website_empty(site): fcntl.flock(lock, fcntl.LOCK_SH) web_node_apply.check_reservation(site) public, _, _ = web_node_apply.site_storage(site) - if any(public.iterdir()): - raise ValueError("Restore requires an empty document root.") + starter_to_replace(public) def restore_website(site, backup, source_root): @@ -32,8 +44,7 @@ def restore_website(site, backup, source_root): paths = web_node_apply.SitePaths.for_site(site) uid, gid = web_node_apply.check_installation(site, paths) public = paths.account / "public" - if any(public.iterdir()): - raise ValueError("Restore requires an empty document root.") + starter_to_replace(public) with TemporaryDirectory( dir=paths.account, prefix=".directadmin-restore-" @@ -55,7 +66,7 @@ def restore_website(site, backup, source_root): on_copy=remember_private_entry, ) prepare_web_permissions(staged, uid, gid, private) - publish_empty_webroot(staged, public) + publish_directadmin_webroot(staged, public) return { **site.manifest(), @@ -66,6 +77,45 @@ def restore_website(site, backup, source_root): } +def publish_directadmin_webroot(staged, public): + index = starter_to_replace(public) + if index is None: + publish_empty_webroot(staged, public) + return + + recovery = Path( + mkdtemp(dir=public.parent, prefix=".directadmin-recovery-") + ) + saved = recovery / index.name + try: + os.replace(index, saved) + if not web_node_apply.is_starter_page(saved): + raise ValueError("The starter page changed during import.") + publish_empty_webroot(staged, public) + except BaseException: + if os.path.lexists(saved) and staged.exists(): + folder = os.open(public.parent, os.O_RDONLY | os.O_DIRECTORY) + try: + rename_entry( + folder, + saved.relative_to(public.parent), + index.relative_to(public.parent), + ) + except (OSError, ValueError): + raise ValueError( + "The document root changed. The previous index was kept " + "in a private recovery directory on the web node." + ) from None + finally: + os.close(folder) + if not os.path.lexists(saved): + recovery.rmdir() + raise + else: + saved.unlink() + recovery.rmdir() + + def publish_empty_webroot(staged, public): try: # A rename can replace the live root while it is empty. diff --git a/src/coulobre/wordpress_node.py b/src/coulobre/wordpress_node.py index 28c7e98..27a36b6 100644 --- a/src/coulobre/wordpress_node.py +++ b/src/coulobre/wordpress_node.py @@ -140,24 +140,11 @@ def wordpress_version(cli, root): return version -def is_starter_page(index): - try: - descriptor = os.open( - index, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK - ) - except OSError: - return False - with os.fdopen(descriptor, "rb") as page: - if not stat.S_ISREG(os.fstat(page.fileno()).st_mode): - return False - contents = page.read(web.starter_page_read_limit()) - return web.starter_page_matches(contents) - - def check_install_target(public): entries = os.listdir(public) if entries and ( - entries != ["index.html"] or not is_starter_page(public / "index.html") + entries != ["index.html"] + or not web.is_starter_page(public / "index.html") ): raise ValueError( "Use an empty website or the original Coulobre starter page." @@ -174,7 +161,7 @@ def publish_wordpress(staged, public): saved = staged.parent / "previous-index" os.replace(index, saved) try: - if not is_starter_page(saved): + if not web.is_starter_page(saved): raise ValueError("The starter page changed during setup.") publish_empty_webroot(staged, public) except (OSError, ValueError, KeyboardInterrupt):