Allow DirectAdmin restore over an untouched starter page
This commit is contained in:
1 parent
9caee44642
commit
880b62e3d8
5 files changed
+131
-25
No files matched your search
+5
-3
@@ -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
|
components or only what you need. Select a package with enough website
|
||||||
slots when restoring more than one site.
|
slots when restoring more than one site.
|
||||||
The destination can be a new hosting account or an existing empty account
|
The destination can be a new hosting account or an existing empty account
|
||||||
whose primary domain exactly matches. Remove any existing starter page
|
whose primary domain exactly matches. A document root containing only an
|
||||||
before restoring website files. Existing DNS can be left in place if you
|
unchanged Coulobre starter page counts as empty. A modified starter page
|
||||||
do not select DNS import. New accounts do not include a new customer login.
|
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.
|
Open the destination account after import to attach a customer or manager.
|
||||||
|
|
||||||
The selected component types apply to supported content across the selected
|
The selected component types apply to supported content across the selected
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ from unittest.mock import patch
|
|||||||
from django.test import SimpleTestCase
|
from django.test import SimpleTestCase
|
||||||
|
|
||||||
from coulobre.web_node import WebSite, main
|
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
|
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(list(self.public.iterdir()), [existing])
|
||||||
self.assertEqual(existing.read_bytes(), b"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<!-- edited -->", 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):
|
def test_atomic_publish_preserves_a_file_created_during_restore(self):
|
||||||
existing = self.public / "new.txt"
|
existing = self.public / "new.txt"
|
||||||
|
|
||||||
|
|||||||
@@ -81,6 +81,19 @@ def starter_page_matches(page):
|
|||||||
return False
|
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():
|
def starter_page_read_limit():
|
||||||
return (
|
return (
|
||||||
max(
|
max(
|
||||||
|
|||||||
@@ -2,10 +2,23 @@ import errno
|
|||||||
import fcntl
|
import fcntl
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from tempfile import TemporaryDirectory
|
from tempfile import TemporaryDirectory, mkdtemp
|
||||||
|
|
||||||
from . import web_node_apply
|
from . import web_node_apply
|
||||||
from .directadmin_website import copy_website
|
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):
|
def check_website_empty(site):
|
||||||
@@ -17,8 +30,7 @@ def check_website_empty(site):
|
|||||||
fcntl.flock(lock, fcntl.LOCK_SH)
|
fcntl.flock(lock, fcntl.LOCK_SH)
|
||||||
web_node_apply.check_reservation(site)
|
web_node_apply.check_reservation(site)
|
||||||
public, _, _ = web_node_apply.site_storage(site)
|
public, _, _ = web_node_apply.site_storage(site)
|
||||||
if any(public.iterdir()):
|
starter_to_replace(public)
|
||||||
raise ValueError("Restore requires an empty document root.")
|
|
||||||
|
|
||||||
|
|
||||||
def restore_website(site, backup, source_root):
|
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)
|
paths = web_node_apply.SitePaths.for_site(site)
|
||||||
uid, gid = web_node_apply.check_installation(site, paths)
|
uid, gid = web_node_apply.check_installation(site, paths)
|
||||||
public = paths.account / "public"
|
public = paths.account / "public"
|
||||||
if any(public.iterdir()):
|
starter_to_replace(public)
|
||||||
raise ValueError("Restore requires an empty document root.")
|
|
||||||
|
|
||||||
with TemporaryDirectory(
|
with TemporaryDirectory(
|
||||||
dir=paths.account, prefix=".directadmin-restore-"
|
dir=paths.account, prefix=".directadmin-restore-"
|
||||||
@@ -55,7 +66,7 @@ def restore_website(site, backup, source_root):
|
|||||||
on_copy=remember_private_entry,
|
on_copy=remember_private_entry,
|
||||||
)
|
)
|
||||||
prepare_web_permissions(staged, uid, gid, private)
|
prepare_web_permissions(staged, uid, gid, private)
|
||||||
publish_empty_webroot(staged, public)
|
publish_directadmin_webroot(staged, public)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
**site.manifest(),
|
**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):
|
def publish_empty_webroot(staged, public):
|
||||||
try:
|
try:
|
||||||
# A rename can replace the live root while it is empty.
|
# A rename can replace the live root while it is empty.
|
||||||
|
|||||||
@@ -140,24 +140,11 @@ def wordpress_version(cli, root):
|
|||||||
return version
|
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):
|
def check_install_target(public):
|
||||||
entries = os.listdir(public)
|
entries = os.listdir(public)
|
||||||
if entries and (
|
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(
|
raise ValueError(
|
||||||
"Use an empty website or the original Coulobre starter page."
|
"Use an empty website or the original Coulobre starter page."
|
||||||
@@ -174,7 +161,7 @@ def publish_wordpress(staged, public):
|
|||||||
saved = staged.parent / "previous-index"
|
saved = staged.parent / "previous-index"
|
||||||
os.replace(index, saved)
|
os.replace(index, saved)
|
||||||
try:
|
try:
|
||||||
if not is_starter_page(saved):
|
if not web.is_starter_page(saved):
|
||||||
raise ValueError("The starter page changed during setup.")
|
raise ValueError("The starter page changed during setup.")
|
||||||
publish_empty_webroot(staged, public)
|
publish_empty_webroot(staged, public)
|
||||||
except (OSError, ValueError, KeyboardInterrupt):
|
except (OSError, ValueError, KeyboardInterrupt):
|
||||||
|
|||||||
Reference in new issue
Block a user