Prepare isolated WordPress staging revisions #240
No files matched your search
@@ -23,7 +23,7 @@ DEFINER_OBJECTS = (
|
||||
)
|
||||
|
||||
|
||||
def backup_database(database, socket, archive):
|
||||
def backup_database(database, socket, archive, *, wordpress_prefix=None):
|
||||
if os.geteuid() != 0:
|
||||
raise ValueError("Run backup-database as root on the MariaDB node.")
|
||||
archive = archive.absolute()
|
||||
@@ -33,6 +33,13 @@ def backup_database(database, socket, archive):
|
||||
|
||||
with managed_database(database, socket) as (cursor, record, record_path):
|
||||
check_definers(cursor, database.name)
|
||||
if wordpress_prefix is not None:
|
||||
check_wordpress_schema(cursor, database.name, wordpress_prefix)
|
||||
objects = (
|
||||
("--routines", "--events", "--triggers")
|
||||
if wordpress_prefix is None
|
||||
else ("--skip-routines", "--skip-events", "--skip-triggers")
|
||||
)
|
||||
with (
|
||||
client_options(record, record_path.parent) as options,
|
||||
NamedTemporaryFile(dir=archive.parent) as staged,
|
||||
@@ -45,9 +52,7 @@ def backup_database(database, socket, archive):
|
||||
"--skip-add-drop-table",
|
||||
"--no-tablespaces",
|
||||
"--hex-blob",
|
||||
"--routines",
|
||||
"--events",
|
||||
"--triggers",
|
||||
*objects,
|
||||
database.name,
|
||||
)
|
||||
with gzip.GzipFile(fileobj=staged, mode="wb", filename="") as data:
|
||||
@@ -65,11 +70,7 @@ def backup_database(database, socket, archive):
|
||||
|
||||
|
||||
def restore_database(database, socket, archive):
|
||||
if os.geteuid() != 0:
|
||||
raise ValueError("Run restore-database as root on the MariaDB node.")
|
||||
archive = archive.absolute()
|
||||
require_root_directory(archive.parent)
|
||||
check_root_file(archive, mode=0o600)
|
||||
archive = private_source_archive(archive)
|
||||
with TemporaryFile(dir=archive.parent) as validated:
|
||||
read_dump(database, archive, validated)
|
||||
restore_dump(database, socket, validated)
|
||||
@@ -177,6 +178,49 @@ def check_definers(cursor, name):
|
||||
)
|
||||
|
||||
|
||||
def check_wordpress_schema(cursor, name, prefix):
|
||||
from .wordpress_staging import validate_table_prefix
|
||||
|
||||
validate_table_prefix(prefix)
|
||||
cursor.execute(
|
||||
"SELECT TABLE_NAME, TABLE_TYPE FROM information_schema.TABLES "
|
||||
"WHERE TABLE_SCHEMA = %s",
|
||||
(name,),
|
||||
)
|
||||
tables = cursor.fetchall()
|
||||
if (
|
||||
not tables
|
||||
or any(kind != "BASE TABLE" for _, kind in tables)
|
||||
or any(not table.startswith(prefix) for table, _ in tables)
|
||||
):
|
||||
raise ValueError(
|
||||
"Staging requires a dedicated WordPress database "
|
||||
"with ordinary tables."
|
||||
)
|
||||
table_names = {table for table, _ in tables}
|
||||
if (
|
||||
not {
|
||||
prefix + table
|
||||
for table in ("options", "posts", "users", "usermeta")
|
||||
}
|
||||
<= table_names
|
||||
or {prefix + table for table in ("blogs", "sitemeta", "site")}
|
||||
& table_names
|
||||
):
|
||||
raise ValueError("Staging requires a single WordPress website.")
|
||||
for table, schema in DEFINER_OBJECTS[1:]:
|
||||
cursor.execute(
|
||||
f"SELECT 1 FROM information_schema.{table} "
|
||||
f"WHERE {schema} = %s LIMIT 1",
|
||||
(name,),
|
||||
)
|
||||
if cursor.fetchone() is not None:
|
||||
raise ValueError(
|
||||
"Staging requires a database without triggers, "
|
||||
"routines or events."
|
||||
)
|
||||
|
||||
|
||||
def check_empty_database(cursor, name):
|
||||
for table, schema in (("TABLES", "TABLE_SCHEMA"), *DEFINER_OBJECTS[2:]):
|
||||
cursor.execute(
|
||||
@@ -265,3 +309,26 @@ def import_dump(command, data):
|
||||
"MariaDB could not complete the restore. "
|
||||
"Inspect the target before retrying."
|
||||
)
|
||||
|
||||
|
||||
def clone_database_from_archive(source, destination, socket, archive):
|
||||
if (
|
||||
source.account_id != destination.account_id
|
||||
or source.name == destination.name
|
||||
):
|
||||
raise ValueError(
|
||||
"Choose a separate database owned by the same account."
|
||||
)
|
||||
archive = private_source_archive(archive)
|
||||
with TemporaryFile(dir=archive.parent) as validated:
|
||||
read_dump(source, archive, validated)
|
||||
restore_dump(destination, socket, validated)
|
||||
|
||||
|
||||
def private_source_archive(archive):
|
||||
if os.geteuid() != 0:
|
||||
raise ValueError("Run database restore as root on the MariaDB node.")
|
||||
archive = archive.absolute()
|
||||
require_root_directory(archive.parent)
|
||||
check_root_file(archive, mode=0o600)
|
||||
return archive
|
||||
@@ -0,0 +1,80 @@
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from uuid import UUID
|
||||
|
||||
from .database_node import DatabaseSpec
|
||||
from .web_node import WebSite
|
||||
|
||||
TABLE_PREFIX = re.compile(r"[A-Za-z0-9_]{1,48}", re.ASCII)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WordPressStagingRevision:
|
||||
site: WebSite
|
||||
source_database: DatabaseSpec
|
||||
domain: str
|
||||
revision: UUID
|
||||
|
||||
def __post_init__(self):
|
||||
stage = WebSite(self.site.account_id, self.domain)
|
||||
if stage.domain == self.site.domain:
|
||||
raise ValueError("Choose a separate hostname for staging.")
|
||||
if self.source_database.account_id != self.site.account_id:
|
||||
raise ValueError(
|
||||
"The live database must belong to this website's account."
|
||||
)
|
||||
if not isinstance(self.revision, UUID) or self.revision.version != 4:
|
||||
raise ValueError("Expected a random staging revision ID.")
|
||||
if self.source_database.name == self.database.name:
|
||||
raise ValueError("Staging requires a separate database.")
|
||||
object.__setattr__(self, "domain", stage.domain)
|
||||
|
||||
@property
|
||||
def key(self):
|
||||
return "wps-" + self.revision.hex[:20]
|
||||
|
||||
@property
|
||||
def database(self):
|
||||
return DatabaseSpec(
|
||||
self.site.account_id, "wps_" + self.revision.hex[:20]
|
||||
)
|
||||
|
||||
def manifest(self):
|
||||
return {
|
||||
"site": self.site.manifest(),
|
||||
"source_database": self.source_database.manifest(),
|
||||
"domain": self.domain,
|
||||
"revision": str(self.revision),
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_manifest(cls, data):
|
||||
if not isinstance(data, dict) or data.keys() != {
|
||||
"site",
|
||||
"source_database",
|
||||
"domain",
|
||||
"revision",
|
||||
}:
|
||||
raise ValueError("Expected a staging revision manifest.")
|
||||
try:
|
||||
revision = UUID(data["revision"])
|
||||
except (ValueError, TypeError, AttributeError):
|
||||
raise ValueError(
|
||||
"Expected a random staging revision ID."
|
||||
) from None
|
||||
if data["revision"] != str(revision):
|
||||
raise ValueError("Expected a canonical staging revision ID.")
|
||||
return cls(
|
||||
WebSite.from_manifest(data["site"]),
|
||||
DatabaseSpec.from_manifest(data["source_database"]),
|
||||
data["domain"],
|
||||
revision,
|
||||
)
|
||||
|
||||
|
||||
def validate_table_prefix(value):
|
||||
if not isinstance(value, str) or not TABLE_PREFIX.fullmatch(value):
|
||||
raise ValueError(
|
||||
"WordPress must use a supported literal table prefix."
|
||||
)
|
||||
return value
|
||||
Reference in new issue
Block a user